
    0                     B   S r SSKJr  SSKJr  SSKJr  SSK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  \R8                  " 5       S 5       r\R<                   " S S\R>                  5      5       r g)z3Implementation of create command for notifications.    )absolute_import)division)unicode_literalsN)
exceptions)api_factory)	cloud_api)errors)
error_util)apis)arg_parsers)base)#notification_configuration_iterator)storage_url)log)
propertiesc           	         [         R                  " SS5      n[         R                  " SS5      n UR                  R	                  UR                  U S95        [        R                  " SU -   5        SnUR                  R                  UR!                  U S95      nUR#                  SSU-   /S9nXR$                  ;  aw  UR$                  R'                  U5        UR                  R)                  UR+                  U UR-                  US9S95      n	[        R                  " SR                  U	5      5        g	[        R                  " SR                  X5      5        U$ ! [        R                   ak  nUR                  S:w  a  e UR                  R                  UR                  U S95      n[        R                  " SR                  U5      5        S	n S
nAGNcS
nAff = f)a  Ensures that topic with SA permissions exists, creating it if needed.

Args:
  topic_name (str): Name of the Cloud Pub/Sub topic to use or create.
  service_account_email (str): The project service account for Google Cloud
    Storage. This SA needs publish permission on the PubSub topic.

Returns:
  True if topic was created or had its IAM permissions modified.
  Otherwise, False.
pubsubv1)topiczTopic already exists: Fi  )namezCreated topic:
{}TN)resourcezroles/pubsub.publisherzserviceAccount:)rolemembers)policy)r   setIamPolicyRequestzUpdated topic IAM policy:
{}zFProject service account {} already has publish permission for topic {})r   GetClientInstanceGetMessagesModuleprojects_topicsGetPubsubProjectsTopicsGetRequestr   warningapitools_exceptions	HttpErrorstatus_codeCreateTopicinfoformatGetIamPolicy'PubsubProjectsTopicsGetIamPolicyRequestBindingbindingsappendSetIamPolicy'PubsubProjectsTopicsSetIamPolicyRequestSetIamPolicyRequest)

topic_nameservice_account_emailpubsub_clientpubsub_messagescreated_new_topice	new_topictopic_iam_policyexpected_bindingupdated_topic_iam_policys
             3lib/surface/storage/buckets/notifications/create.py_maybe_create_or_modify_topicr<   '   s    ((48-**8T:/!!%%66Z6HJKK(:56 #22??== >   %,,# #889 - ; 666$$%56,<<II?? / C C' !D !) 	@ 	* +
 HH,334LMNKKP	%	24 
G 
	&	& }}--44:.0IHH!((34s   AE G0A GGc                   8    \ rS rSrSrSSS.r\S 5       rS rSr	g	)
r%   b   z0Create a notification configuration on a bucket.a  
      *{command}* creates a notification configuration on a bucket,
      establishing a flow of event notifications from Cloud Storage to a
      Cloud Pub/Sub topic. As part of creating this flow, it also verifies
      that the destination Cloud Pub/Sub topic exists, creating it if necessary,
      and verifies that the Cloud Storage bucket has permission to publish
      events to that topic, granting the permission if necessary.

      If a destination Cloud Pub/Sub topic is not specified with the `-t` flag,
      Cloud Storage chooses a topic name in the default project whose ID is
      the same as the bucket name. For example, if the default project ID
      specified is `default-project` and the bucket being configured is
      `gs://example-bucket`, the create command uses the Cloud Pub/Sub topic
      `projects/default-project/topics/example-bucket`.

      In order to enable notifications, your project's
      [Cloud Storage service agent](https://cloud.google.com/storage/docs/projects#service-accounts)
      must have the IAM permission "pubsub.topics.publish".
      This command checks to see if the destination Cloud Pub/Sub topic grants
      the service agent this permission. If not, the create command attempts to
      grant it.

      A bucket can have up to 100 total notification configurations and up to
      10 notification configurations set to trigger for a specific event.
      a  
      Send notifications of all changes to the bucket
      `example-bucket` to the Cloud Pub/Sub topic
      `projects/default-project/topics/example-bucket`:

        $ {command} gs://example-bucket

      The same as the above but sends no notification payload:

        $ {command} --payload-format=none gs://example-bucket

      Include custom metadata in notification payloads:

        $ {command} --custom-attributes=key1:value1,key2:value2 gs://example-bucket

      Create a notification configuration that only sends an event when a new
      object has been created or an object is deleted:

        $ {command} --event-types=OBJECT_FINALIZE,OBJECT_DELETE gs://example-bucket

      Create a topic and notification configuration that sends events only when
      they affect objects with the prefix `photos/`:

        $ {command} --object-prefix=photos/ gs://example-bucket

      Specifies the destination topic ID `files-to-process` in the default
      project:

        $ {command} --topic=files-to-process gs://example-bucket

      The same as above but specifies a Cloud Pub/Sub topic belonging
      to the specific cloud project `example-project`:

        $ {command} --topic=projects/example-project/topics/files-to-process gs://example-bucket

      Skip creating a topic when creating the notification configuraiton:

        $ {command} --skip-topic-setup gs://example-bucket
      )DESCRIPTIONEXAMPLESc                 d   U R                  SSS9  U R                  SSS[        R                  " 5       SS9  U R                  S	S
S[        R                  " [	        [
        R                   Vs/ s H  oR                  PM     sn5      S9SS9  U R                  SSSS9  U R                  SS[	        [
        R                   Vs/ s H  oR                  PM     sn5      [
        R                  R                  R                  SS9  U R                  SSSSS9  U R                  SSSS9  g s  snf s  snf )Nurlz>URL of the bucket to create the notification configuration on.)helpz-mz--custom-attributesz	KEY=VALUEzSpecifies key:value attributes that are appended to the set of attributes sent to Cloud Pub/Sub for all events associated with this notification configuration.)metavartyperC   z-ez--event-typesNOTIFICATION_EVENT_TYPE)choicesa  Specify event type filters for this notification configuration. Cloud Storage will send notifications of only these types. By default, Cloud Storage sends notifications for all event types. * OBJECT_FINALIZE: An object has been created. * OBJECT_METADATA_UPDATE: The metadata of an object has changed. * OBJECT_DELETE: An object has been permanently deleted. * OBJECT_ARCHIVE: A live version of an object has become a noncurrent version.z-pz--object-prefixzSpecifies a prefix path for this notification configuration. Cloud Storage will send notifications for only objects in the bucket whose names begin with the prefix.z-fz--payload-formatzSpecifies the payload format of notification messages. Notification details are available in the message attributes. 'none' sends no payload.)rG   defaultrC   z-sz--skip-topic-setup
store_truezSkips creation and permission assignment of the Cloud Pub/Sub topic. This is useful if the caller does not have permission to access the topic in question, or if the topic already exists and has the appropriate publish permission assigned.)actionrC   z-tz--topiczSpecifies the Cloud Pub/Sub topic to send notifications to. If not specified, this command chooses a topic whose project is your default project and whose ID is the same as the Cloud Storage bucket name.)
add_argumentr   ArgDictArgListsortedr   NotificationEventTypevalueNotificationPayloadFormatJSON)parserstatuss     r;   ArgsCreate.Args   sm   
     ",  - )  ,5,K,KL,K&,KLNO#  %  5  6 (1(K(KL(Kf\\(KLN3388>>$  % 8	  9 &  'C M( Ms   ,D(D-c                 <   [         R                  R                  R                  R	                  5       n[
        R                  " UR                  5      n[        R                  " U5        UR                  (       d  SR                  X#R                  5      nO[UR                  R                  S5      (       d/  SR                  UUR                  R                  S5      S   5      nOUR                  n[        R                   " [
        R"                  R$                  5      nUR&                  (       dp  UR)                  UR                  5      R*                  R,                  nUR/                  US9n[0        R2                  " SR                  XBU5      5        [5        XG5      nOSnUR6                  (       a1  UR6                   V	s/ s H  n	[8        R:                  " U	5      PM     n
n	OS n
[<        R>                  " UR@                  UUURB                  U
URD                  [8        RF                  " URH                  5      S9n U" 5       $ s  sn	f ! [J        RL                   a>    U(       d  e [0        RN                  " S	5        [P        RR                  " S
5        U" 5       s $ f = f)Nzprojects/{}/topics/{}z	projects//)project_numberzDChecking for topic {} with access for project {} service account {}.F)custom_attributesevent_typesobject_name_prefixpayload_formatz`Retrying create notification request because topic changes may take up to 10 seconds to process.
   )*r   VALUEScoreproject	GetOrFailr   storage_url_from_stringrB   r   *raise_error_if_not_gcs_bucket_matching_urlr   r(   bucket_name
startswith
rpartitionr   get_apiProviderPrefixGCSskip_topic_setup
get_bucketmetadataprojectNumberget_service_agentr   r'   r<   r\   r   rO   	functoolspartial!create_notification_configurationr[   object_prefixrQ   r^   
api_errorsCloudApiErrorr!   timesleep)selfargs
project_idrB   r1   
gcs_clientbucket_project_numberr2   (created_new_topic_or_set_new_permissions
event_typer\   rs   s               r;   Run
Create.Run   s/   ""''//99;J

-
-dhh
7C'RR::*11*ooNjZZ"";//*11

**


$R
(*j ::j $$[%?%?%C%CDJ   )33
//#8MM  )::. ; 0	hh
P6**?@B 2O
2-. 27. !,,,j 
)
)*
5,  k
 k(1(9(94400-- ::4;N;NO)Q%	1.00  ## 15	kk/0 jjn.001s    I=I	 	AJJ N)
__name__
__module____qualname____firstlineno____doc__detailed_helpstaticmethodrU   r   __static_attributes__r       r;   r%   r%   b   s2    8
4&
9C-J :' :'x:1r   r%   )!r   
__future__r   r   r   rq   rw   apitools.base.pyr   r"   googlecloudsdk.api_lib.storager   r   r	   ru   'googlecloudsdk.api_lib.storage.gcs_jsonr
   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.storager   r   googlecloudsdk.corer   r   $catch_http_error_raise_gcs_api_errorr<   UniverseCompatibleCommandr%   r   r   r;   <module>r      s    : &  '   > 6 4 ? > , / ( R : # * 0027 37t 1T\\ 1 1r   