
    "                        d Z ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dZdZeej=                  d      z   ZdZ dZ!de!z   e z   Z" eee"      Z# eee       Z$ eee!      Z%ejL                  jN                  Z'e'jP                  Z( G d de	      Z)y)z+This module provides the command to gsutil.    )absolute_import)print_functionN)metrics)Command)CommandArgument)ubla)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)storage_v1_messages)NO_MAX)InsistOnOrOffz>
  gsutil bucketpolicyonly set (on|off) gs://<bucket_name>...
z5
  gsutil bucketpolicyonly get gs://<bucket_name>...

a  
<B>SET</B>
  The ``bucketpolicyonly set`` command enables or disables the uniform bucket-level
  access feature on Google Cloud Storage buckets.

<B>SET EXAMPLES</B>
  Configure your buckets to use uniform bucket-level access:

    gsutil bucketpolicyonly set on gs://redbucket gs://bluebucket

  Configure your buckets to NOT use uniform bucket-level access:

    gsutil bucketpolicyonly set off gs://redbucket gs://bluebucket
a&  
<B>GET</B>
  The ``bucketpolicyonly get`` command shows whether uniform bucket-level
  access is enabled for the specified Cloud Storage bucket.

<B>GET EXAMPLES</B>
  Check if your buckets are using uniform bucket-level access:

    gsutil bucketpolicyonly get gs://redbucket gs://bluebucket
a  
  The Bucket Policy Only feature is now known as `uniform bucket-level access
  <https://cloud.google.com/storage/docs/uniform-bucket-level-access>`_.
  The ``bucketpolicyonly`` command is still supported, but we recommend using
  the equivalent ``ubla`` `command
  <https://cloud.google.com/storage/docs/gsutil/commands/ubla>`_.

  The ``bucketpolicyonly`` command is used to retrieve or configure the
  uniform bucket-level access setting of Cloud Storage buckets. This command has
  two sub-commands, ``get`` and ``set``.
c                      e Zd ZdZ ej
                  dededdddej                  gej                   e
j                  d      g e
ddd	g
       e
j                         gd      Z ej                  dg ddeeed      Zej(                  j*                  Zej,                  d   j,                  d   Zej1                  dd      ej,                  d   j,                  d<   d Zd Zd Zd Zd Zy)BucketPolicyOnlyCommandz/Implements the gsutil bucketpolicyonly command.bucketpolicyonly    F   modeonoff)choicesgetset)
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command_helpz%Configure uniform bucket-level access)	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textr      zUniform bucket-level accesszBucket Policy Onlyc                 f    |j                   j                  dk7  rt        d| j                  z        y )Ngsz7The %s command can only be used with gs:// bucket URLs.)storage_urlschemer
   command_name)selfblrs     2platform/gsutil/gslib/commands/bucketpolicyonly.py,_ValidateBucketListingRefAndReturnBucketNamezDBucketPolicyOnlyCommand._ValidateBucketListingRefAndReturnBucketName   s7    
%
C


  &    c                    | j                  |       |j                  }| j                  j                  |j                  dg|j
                        }|j                  }|j                  }t        |      j                  d      |j                  d}d}|j                  r|j                  |d<   d}|rt         d|z   j                  d
i |       y	y	)z1Gets the Bucket Policy Only setting for a bucket.iamConfigurationfieldsprovider/)bucketenabledr   locked_timez  LockedTime: {locked_time}
z>Bucket Policy Only setting for {bucket}:
  Enabled: {enabled}
N )r8   r2   
gsutil_api	GetBucketbucket_namer3   r;   bucketPolicyOnlystrrstriprA   
lockedTimeprintformat)r5   r6   
bucket_urlbucket_metadata
iam_configbucket_policy_onlyr=   locked_time_lines           r7   _GetBucketPolicyOnlyz,BucketPolicyOnlyCommand._GetBucketPolicyOnly   s    55c:Joo//
0F0F8J7K9C9J9J 0 LO !11J#44 j/((-%--F
 $$0;;f]8 A &(89:@&KCIK L r9   c                    | j                  |       |j                  }t               }t               |_        |dk(  |j                  _        t        j                  |      }|dk(  rdnd}t        |dt        |      j                  d      d       | j                  j                  |j                  |dg|j                  	       y
)z;Sets the Bucket Policy Only setting for a bucket on or off.r   )r;   Enabling	Disablingz Bucket Policy Only for r?   z...r;   r<   r   )r8   r2   IamConfigurationValueBucketPolicyOnlyValuerG   rA   apitools_messagesBucketrK   rH   rI   rD   PatchBucketrF   r3   )r5   r6   setting_argrM   rO   rN   setting_verbs          r7   _SetBucketPolicyOnlyz,BucketPolicyOnlyCommand._SetBucketPolicyOnly   s    55c:J&(J"7"9J+6$+>J''..
KO!,!4:+L	Z//46 7 	OO
 6 6 /(:';)3):):   < r9   c                    | j                   j                  d      }|dvrt        d      d}g }d}|dk(  r| j                  }nI|dk(  rD| j                  }| j                   j                  d      }t        |d       |j                  |       d}| j                   }|s| j                          |D ]%  }| j                  |      }|D ]  }	d	} ||	g|   ' |st        t        t        |      z        y)
z;Handles bucketpolicyonly command on a Cloud Storage bucket.r   r   z&bucketpolicyonly only supports get|setNr   r   z-Only on and off values allowed for set optionFT)argspopr
   rR   r]   r   append$RaiseWrongNumberOfArgumentsExceptionGetBucketUrlIterFromArgr   list)
r5   
subcommandsubcommand_funcsubcommand_argsr[   some_matchedurl_argsurl_strbucket_iterbucket_listing_refs
             r7   _BucketPolicyOnlyz)BucketPolicyOnlyCommand._BucketPolicyOnly   s    q!J'EFFOOKU11o	u	11oIIMM!$kKCE[) LyyH
//1009k +
*=_= !,  3d8nDEEr9   c                    | j                   j                  d      t        j                  k7  r:t	        dj                  t        j                  d| j                  z                    | j                  d   }| j                  d       |dk(  s|d	k(  rHt        j                  | j                  
       t        j                  |g       | j                          yt	        d|z        )z5Command entry point for the bucketpolicyonly command.r1   )r>   r   zBThe "%s" command can only be used with the Cloud Storage JSON API.r   T)
check_argsr   r   )sub_opts)subcommandsz-Invalid subcommand "%s", use get|set instead.N)rD   GetApiSelectorr	   JSONr
   jointextwrapwrapr4   r_   ParseSubOptsr   LogCommandParamsrp   rm   )r5   action_subcommands     r7   
RunCommandz"BucketPolicyOnlyCommand.RunCommand   s    %%t%48H8HHTYY
--R!!"#$ % %
 		!&E!%6%%?6,=+>?
L./ 0 0r9   N)__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r	   rs   r   MakeNCloudURLsArgument%MakeZeroOrMoreCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   UblaCommandgcloud_storage_mapgcloud_commandformat_flagreplacer8   rR   r]   rm   rz   rC   r9   r7   r   r   \   s'   7***!&&' %%8/88;=ftUm<CoCCE	,& g"C#
) ''::"11%8GGJ+<35 ##E*99	L2*"H0r9   r   )*r~   
__future__r   r   getoptru   gslibr   gslib.commandr   gslib.command_argumentr   gslib.commandsr   gslib.cs_api_mapr	   gslib.exceptionr
   r   gslib.help_providerr   "gslib.third_party.storage_apitoolsr   rX   gslib.utils.constantsr   gslib.utils.text_utilr   _SET_SYNOPSIS_GET_SYNOPSISlstripr   _SET_DESCRIPTION_GET_DESCRIPTION_DESCRIPTIONr   r   r   rY   rV   rW   r   rC   r9   r7   <module>r      s    2 & %    ! 2  ( , 2 . W ( / M0066	 	 
 
 *
* %Y= /?@/?@ *00FF -CC S0g S0r9   