
    3                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	J
r
  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSK!J"r"  Sr#Sr$\#\$RK                  S5      -   S-   r&Sr'Sr(S\'-   \(-   S-   r)\" \&\)5      r*\" \$\(5      r+\" \#\'5      r, " S S\5      r-g)z<Implementation of logging configuration command for buckets.    )absolute_import)print_function)division)unicode_literalsN)encoding)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)StorageUrlFromString)UrlsAreForSingleProvider)storage_v1_messages)NO_MAX)GcloudStorageFlag)GcloudStorageMap)	text_utilz
  gsutil logging set on -b <logging_bucket_name> [-o <log_object_prefix>] gs://<bucket_name>...
  gsutil logging set off gs://<bucket_name>...
z)
  gsutil logging get gs://<bucket_name>

a  
<B>SET</B>
  The ``set`` sub-command has two sub-commands:

<B>ON</B>
  The ``gsutil logging set on`` command enables usage and storage logging
  for the buckets named by the specified URLs, outputting log files to the
  bucket specified with the ``-b`` flag. Cloud Storage doesn't validate the
  existence of the output bucket, so users should ensure it already exists,
  and all URLs must name Cloud Storage buckets (e.g., ``gs://bucket``). The
  optional ``-o`` flag specifies the prefix for log object names. The
  default prefix is the bucket name. For example, the command:

    gsutil logging set on -b gs://my_logging_bucket -o UsageLog \
        gs://my_bucket1 gs://my_bucket2

  causes all read and write activity to objects in ``gs://mybucket1`` and
  ``gs://mybucket2`` to be logged to objects prefixed with the name
  ``UsageLog``, with those log objects written to the bucket
  ``gs://my_logging_bucket``.

  In addition to enabling logging on your bucket(s), you also need to grant
  cloud-storage-analytics@google.com write access to the log bucket, using this
  command:

    gsutil acl ch -g cloud-storage-analytics@google.com:W gs://my_logging_bucket

  Note that log data may contain sensitive information, so you should make
  sure to set an appropriate default bucket ACL to protect that data. (See
  "gsutil help defacl".)

<B>OFF</B>
  This command disables usage and storage logging for the buckets named by the
  specified URLs. All URLs must name Cloud Storage buckets (e.g.,
  ``gs://bucket``).

  No logging data is removed from the log buckets when you disable logging,
  but Google Cloud Storage stops delivering new logs once you have run this
  command.

a6  
<B>GET</B>
  If logging is enabled for the specified bucket url, the server responds
  with a JSON document that looks something like this:

    {
      "logBucket": "my_logging_bucket",
      "logObjectPrefix": "UsageLog"
    }

  You can download log data from your log bucket using the gsutil cp command.

aM  
  Google Cloud Storage offers `usage logs and storage logs
  <https://cloud.google.com/storage/docs/access-logs>`_ in the form of CSV
  files that you can download and view. Usage logs provide information for all
  of the requests made on a specified bucket and are created hourly. Storage
  logs provide information about the storage consumption of that bucket for
  the last day and are created daily.

  Once set up, usage logs and storage logs are automatically created as new
  objects in a bucket that you specify. Usage logs and storage logs are
  subject to the same pricing as other objects stored in Cloud Storage.

  For a complete list of usage log fields and storage data fields, see
  `Usage and storage log format
  <https://cloud.google.com/storage/docs/access-logs#format>`_.
  
  The ``logging`` command has two sub-commands:
a  

<B>OPTIONS</B>
  -b bucket_name Specifies the bucket that stores the generated logs. This
                 flag is only available for the ``set on`` command and is
                 required for that command.

  -o log_prefix  Specifies a common prefix for the names of generated
                 logs. This flag is only available for the ``set on``
                 command and is optional for that command.
c                   z   \ rS rSrSr\R                  " S/ SQ\S\SSSS\	R                  \	R                  /\	R                  \" S	S
S/S9\R                  " 5       /S9r\R                  " S/ SQSS\\\S.S9r\" \" / SQ0 S9\" \" / SQ\" S5      \" S5      S.S9\" / SQ0 S9S.0 S9S.0 S9rS rS rS rS rSrg )!LoggingCommand   z)Implementation of gsutil logging command.logging)disableloggingenablelogging
getlogging   zb:o:Fr   modeonoff)choices)command_name_aliasesusage_synopsismin_argsmax_argssupported_sub_argsfile_url_okprovider_url_okurls_start_arggs_api_supportgs_default_apiargparse_arguments)loggingconfiglogslogr   r   r   command_helpz(Configure or retrieve logging on buckets)getset)	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_text)storagebucketsdescribezk--format=gsutiljson[key=logging_config,empty=' has no logging configuration.',empty_prefix_key=storage_url])gcloud_commandflag_map)r:   r;   updatez--log-bucketz--log-object-prefix)-b-o)r:   r;   r?   z--clear-log-bucketz--clear-log-object-prefix)r    r!   c                    U R                  U R                  S   S/S9u  pUR                  S:X  a7  [        R                  " U R
                  R                  XR                  S9SS9  gUR                  (       at  UR                  R                  (       aY  UR                  R                  (       a>  [        R                  " [        [        R                  " UR                  5      5      5        g[        R                  " SU-  5        g)	z(Gets logging configuration for a bucket.r   r   bucket_fieldss3)provider )endz %s has no logging configuration.)GetSingleBucketUrlFromArgargsschemer   print_to_fd
gsutil_apiXmlPassThroughGetLoggingr   	logBucketlogObjectPrefixstrr   MessageToJson)self
bucket_urlbucket_metadatas      )platform/gsutil/gslib/commands/logging.py_GetLoggingCommand._Get   s    "&"@"@		!YK #A #1J D DOODD
00 E 2 "$  
!
!o&=&=&G&G

!
!
1
1&&'>'>?@	B  	@:MN    c           	      $   [        U R                  5      (       d  [        S5      eSnSnU R                   H   u  p4US:X  a  [	        U5      nUS:X  d  M  UnM"     U(       d  [        S5      eUR                  5       (       d  [        S5      eSnU R                   H  nU R                  US/S	9nU H  nUR                  n	S
nU R                  R                  SU5        [        R                  R                  UR                  U=(       d    U	R                  S9n
[        R                  " U
S9nU R                  R                  U	R                  UU	R                   S/S9  M     M     U(       d%  [        ["        [%        U R                  5      -  5      eg)z+Enables logging configuration for a bucket.z8"logging set on" command spanning providers not allowed.Nr@   rA   z2"logging set on" requires '-b <log_bucket>' optionz$-b option must specify a bucket URL.FidrC   TzEnabling logging on %s...)rO   rP   r   rF   fieldsr   )r   rJ   r   sub_optsr   IsBucketGetBucketUrlIterFromArgstorage_urlloggerinfoapitools_messagesBucketLoggingValuebucket_namerM   PatchBucketrK   r   list)rS   target_bucket_urltarget_prefixoptopt_argsome_matchedurl_strbucket_iterblrurlr   rU   s               rV   _EnableLoggingCommand._Enable   sx    $DII.. ( ) )M	09		 &  & ' '%%''CDD L99000Ok#oo4c:#**77'33)<S__ 8 > ,227C##COO$3-0ZZ,06 	$ 	3   3d499oEFFrY   c           	         SnU R                    H  nU R                  US/S9nU H  nUR                  nSnU R                  R	                  SU5        [
        R                  R                  5       n[
        R                  " US9nU R                  R                  UR                  UUR                  S/S9  M     M     U(       d%  [        [        [        U R                   5      -  5      eg)	z,Disables logging configuration for a bucket.Fr[   rC   TzDisabling logging on %s...r\   r]   r   )rJ   ra   rb   rc   rd   re   rf   rg   rM   ri   rh   rK   r   r   rj   )rS   ro   rp   rq   rr   rs   r   rU   s           rV   _DisableLoggingCommand._Disable#  s     L99000Ok#oo5s;#**779+227C##COO$3-0ZZ,06 	$ 	3   3d499oEFFrY   c           	         U R                   R                  S5      nUS:X  a"  U R                  n[        R                  " U/S9  OUS:X  a  U R                   R                  S5      nU R                   (       d  U R                  5         US:X  a"  U R                  n[        R                  " X/S9  OhUS:X  a"  U R                  n[        R                  " X/S9  O@[        SU< SU R                  < S	U< S
35      e[        SU< SU R                  < S35      eU R                  SS9  [        R                  " U R                  S9  U" 5         g)z,Command entry point for the logging command.r   r2   )subcommandsr3   r    r!   zInvalid subcommand "z" for the " z%" command.
See "gsutil help logging".z
" for the z$ command.
See "gsutil help logging".T)
check_args)r_   )rJ   poprW   r   LogCommandParams$RaiseWrongNumberOfArgumentsExceptionrt   rw   r   command_nameParseSubOptsr_   )rS   action_subcommandfuncstate_subcommands       rV   
RunCommandLoggingCommand.RunCommand8  s*    		a(E!YYd,=+>?	e	#q)YY113	T	!||  *=	?u$}}  *=	?  t002CEF 	F
 /1B1BD E E 	& dmm4FrY    N)__name__
__module____qualname____firstlineno____doc__r	   CreateCommandSpec	_SYNOPSISr   r   XMLJSONr
   %MakeZeroOrMoreCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   r   gcloud_storage_maprW   rt   rw   r   __static_attributes__r   rY   rV   r   r      s.   1 **L!oo{'7'78 %%
&4-
8

?
?
A,$  F#)& ( "   +.  '8&G&78M&N	(  +.  (*	!"6 9*V Y-^$(T*!rY   r   ).r   
__future__r   r   r   r   sysapitools.base.pyr   gslibr   gslib.commandr	   gslib.command_argumentr
   gslib.cs_api_mapr   gslib.exceptionr   r   gslib.help_providerr   gslib.storage_urlr   r   "gslib.third_party.storage_apitoolsr   re   gslib.utils.constantsr   gslib.utils.shim_utilr   r   gslib.utilsr   _SET_SYNOPSIS_GET_SYNOPSISlstripr   _SET_DESCRIPTION_GET_DESCRIPTION_DESCRIPTIONr   r   r   r   r   rY   rV   <module>r      s    C & %  ' 
 %  ! 2 ( , 2 . 2 6 W ( 3 2 !
 M0066=	( T " #" *#*"
-#: %Y= /?@/?@JW JrY   