
    %                        d Z ddlmZ ddlm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jJ                  jL                  Z&d ejN                         z   dz   dz   dz   Z( G d de      Z)y)z/This module provides the pap command to gsutil.    )absolute_import)print_functionN)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)storage_v1_messages)NO_MAX)GcloudStorageMap)	shim_utilz=
  gsutil pap set (enforced|inherited) gs://<bucket_name>...
z(
  gsutil pap get gs://<bucket_name>...

a  
<B>SET</B>
  The ``pap set`` command configures public access prevention
  for Cloud Storage buckets. If you set a bucket to be
  ``inherited``, it uses public access prevention only if
  the bucket is subject to the `public access prevention
  <https://cloud.google.com/storage/docs/org-policy-constraints#public-access-prevention>`_
  organization policy constraint.

<B>SET EXAMPLES</B>
  Configure ``redbucket`` and ``bluebucket`` to use public
  access prevention:

    gsutil pap set enforced gs://redbucket gs://bluebucket
a  
<B>GET</B>
  The ``pap get`` command returns public access prevention
  values for the specified Cloud Storage buckets.

<B>GET EXAMPLES</B>
  Check if ``redbucket`` and ``bluebucket`` are using public
  access prevention:

    gsutil pap get gs://redbucket gs://bluebucket
z
  The ``pap`` command is used to retrieve or configure the
  `public access prevention
  <https://cloud.google.com/storage/docs/public-access-prevention>`_ setting of
  Cloud Storage buckets. This command has two sub-commands: ``get`` and ``set``.
z)--format=value[separator=": "](name.sub("z","gs://"),z.iamConfiguration.publicAccessPrevention.yesno(zno="inherited"))c                      e Zd ZdZ ej
                  ddg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dgddeeed      Z e edddedgi d       e eg di        eg di       di       di       Zd Zd Zd Zd Zd Zy )!
PapCommandz"Implements the gsutil pap command.pappublicaccessprevention    F   modeenforced	inherited)choicesgetset)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command_helpz"Configure public access prevention)	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textstoragebucketslistz--rawT)gcloud_commandflag_mapsupports_output_translation)r1   r2   updatez--public-access-prevention)r4   r5   )r1   r2   r7   z--no-public-access-prevention)r   r   c                 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     %platform/gsutil/gslib/commands/pap.py,_ValidateBucketListingRefAndReturnBucketNamez7PapCommand._ValidateBucketListingRefAndReturnBucketName   s7    
%
C


  &    c                    |j                   }| j                  j                  |j                  dg|j                        }|j
                  }|j                  xs d}t        |      j                  d      }t        |d|       y)z7Gets the public access prevention setting for a bucket.iamConfigurationfieldsproviderr   /z: N)
r:   
gsutil_api	GetBucketbucket_namer;   rC   publicAccessPreventionstrrstripprint)r=   r>   
bucket_urlbucket_metadata
iam_configpublic_access_preventionbuckets          r?   _GetPublicAccessPreventionz%PapCommand._GetPublicAccessPrevention   s|    Joo//
0F0F8J7K9C9J9J 0 LO !11J)@@OK_##C(F	f6
78rA   c                 $   |j                   }t               }||_        t        j                  |      }t        d|dt        |      j                  d             | j                  j                  |j                  |dg|j                         y)zMSets the Public Access Prevention setting for a bucket enforced or inherited.)rC   z!Setting Public Access Prevention z for rG   rC   rD   r   )r:   IamConfigurationValuerK   apitools_messagesBucketrN   rL   rM   rH   PatchBucketrJ   r;   )r=   r>   setting_argrO   rQ   rP   s         r?   _SetPublicAccessPreventionz%PapCommand._SetPublicAccessPrevention   s    J&(J(3J%'..
KO	J..s35 6 	OO
 6 6 /(:';)3):):   < rA   c           
         | j                   j                  d      }|dvrt        d      d}g }d}|dk(  r| j                  }n=|dk(  r8| j                  }| j                   j                  d      }|j                  |       | j                  j                  d      t        j                  k7  r:t        dj                  t        j                  d	| j                  z                    d
}| j                   }|s| j                          |D ]  }| j                  |      }|D ]  }	| j                  j                  |	j                   j"                        t        j                  k7  r:t        dj                  t        j                  d| j                  z                    d} ||	g|    |st        t$        t'        |      z        y)z-Handles pap command on Cloud Storage buckets.r   r   zpap only supports get|setNr   r   r9   r   z=The "%s" command can only be with the Cloud Storage JSON API.Fz2The "%s" command can only be used for GCS Buckets.T)argspopr	   rT   r[   appendrH   GetApiSelectorr   JSONjointextwrapwrapr<   $RaiseWrongNumberOfArgumentsExceptionGetBucketUrlIterFromArgr:   r;   r
   r3   )
r=   
subcommandsubcommand_funcsubcommand_argsrZ   some_matchedurl_argsurl_strbucket_iterbucket_listing_refs
             r?   _PapzPapCommand._Pap   s   q!J'899OOKU77o	u	77oIIMM!$k[)%%d+{/?/??TYY
-- %(,(9(9: ;< = =
 LyyH
//1009k +
??))**1136A6F6FG mm (+/+<+<= >"? @ @ *=_= !,  3d8nDEErA   c                    | j                   d   }| j                  d       |dk(  s|dk(  rHt        j                  | j                         t        j                  |g       | j                          y	t        d|z        )
z(Command entry point for the pap command.r   T)
check_argsr   r   )sub_opts)subcommandsz-Invalid subcommand "%s", use get|set instead.N)r]   ParseSubOptsr   LogCommandParamsrr   ro   r	   )r=   action_subcommands     r?   
RunCommandzPapCommand.RunCommand   sv    		!&E!%6%%?6,=+>?
iikL./ 0 0rA   N)__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r   ra   r   MakeNCloudURLsArgument%MakeZeroOrMoreCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   _GCLOUD_LIST_FORMATgcloud_storage_mapr@   rT   r[   ro   rw    rA   r?   r   r   \   sH   ****45!&&' %%8/88;=fz;.GHCoCCE	,( g12@#
) ( F4G" .2  +.  (* +.  (*", /#H K&P
9$+Z0rA   r   )*r{   
__future__r   r   rc   gslibr   gslib.commandr   gslib.command_argumentr   gslib.cs_api_mapr   gslib.exceptionr	   r
   gslib.help_providerr   "gslib.third_party.storage_apitoolsr   rW   gslib.utils.constantsr   gslib.utils.shim_utilr   gslib.utilsr   _SET_SYNOPSIS_GET_SYNOPSISlstripr}   _SET_DESCRIPTION_GET_DESCRIPTION_DESCRIPTIONr   r   r   rX   rV   get_format_flag_caretr   r   r   rA   r?   <module>r      s   6 & %   ! 2 ( , 2 . W ( 2 ! M0066	  
 
 
 ** %Y= /?@/?@ *00FF G6y6689;HIGH ** g0 g0rA   