
    N>                         d 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dlmZ dZ G d dej,                        Zd Zd Z G d de
      Z G d de      Zy)z$Utilities for Eventarc Triggers API.    )absolute_import)division)unicode_literals)
list_pager)common)EventarcClientBase)apis)types)
exceptions)	resources)iso_duration)times   c                       e Zd ZdZy)NoFieldsSpecifiedErrorz:Error when no fields were specified for a Patch operation.N)__name__
__module____qualname____doc__     /lib/googlecloudsdk/api_lib/eventarc/triggers.pyr   r   "   s    Br   r   c                 x    t         j                  j                  | j                  d      }|j	                         S )Nz$eventarc.projects.locations.triggers)
collection)r   REGISTRYParseRelativeNamenameSelfLink)resourcetriggers     r   GetTriggerURIr!   &   s7    00mm F 1 H'				r   c                 4   t        j                  |       syt        j                  |      }t	        j
                  t              }t        j                  ||      }t        j                         |k\  ryt        j                  |dt        j                        S )a  Computes the time by which the trigger will become active.

  Args:
    event_type: str, the trigger's event type.
    update_time: str, the time when the trigger was last modified.

  Returns:
    The active time as a string, or None if the trigger is already active.
  N)minutesz%H:%M:%S)fmttzinfo)r
   IsAuditLogTyper   ParseDateTimer   DurationMAX_ACTIVE_DELAY_MINUTESGetDateTimePlusDurationNowFormatDateTimeLOCAL)
event_typeupdate_time	update_dtdelay	active_dts        r   TriggerActiveTimer3   ,   sr     
		j	)!!+.)


(@
A%++Iu=)
YY[I			iZ	LLr   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )_BaseTriggersClientzBase Triggers Client.c                     t         t        |   t        j                  dd       t        j                  t        j                  d      }|j                  | _        |j                  | _
        |j                  | _        y )Nv1r    )superr5   __init__r   API_NAMEr	   GetClientInstanceMESSAGES_MODULE	_messagesprojects_locations_triggers_serviceprojects_locations_operations_operation_service)selfclient	__class__s     r   r9   z_BaseTriggersClient.__init__D   sY    	
t-footYO##FOOT:F++DN66DM$BBDr   c                     | j                   j                  |j                         j                         ||j	                               }| j
                  j                  |      S )a!  Creates a new Trigger.

    Args:
      trigger_ref: Resource, the Trigger to create.
      trigger_message: Trigger, the trigger message that holds trigger's
        event_filters, service account, destination, transport, etc.

    Returns:
      A long-running operation for create.
    )parentr    	triggerId)r=   .EventarcProjectsLocationsTriggersCreateRequestParentRelativeNameNamer?   Create)rB   trigger_reftrigger_message
create_reqs       r   rL   z_BaseTriggersClient.CreateK   sY     NN!!#002""$ O &J ==
++r   c                     | j                   j                  |j                               }| j                  j	                  |      S )zDeletes a Trigger.

    Args:
      trigger_ref: Resource, the Trigger to delete.

    Returns:
      A long-running operation for delete.
    r   )r=   .EventarcProjectsLocationsTriggersDeleteRequestrJ   r?   Delete)rB   rM   
delete_reqs      r   rS   z_BaseTriggersClient.Delete\   s?     NN%%' O )J==
++r   c                     | j                   j                  |j                               }| j                  j	                  |      S )zyGets a Trigger.

    Args:
      trigger_ref: Resource, the Trigger to get.

    Returns:
      The Trigger message.
    rQ   )r=   +EventarcProjectsLocationsTriggersGetRequestrJ   r?   Get)rB   rM   get_reqs      r   rW   z_BaseTriggersClient.Geti   s?     nnHH%%' I )G==W%%r   c                     | j                   j                  |j                         |      }t        j                  | j
                  |d||d      S )ai  Lists Triggers in a given location.

    Args:
      location_ref: Resource, the location to list Triggers in.
      limit: int or None, the total number of results to return.
      page_size: int, the number of entries in each batch (affects requests
        made, but not the yielded results).

    Returns:
      A generator of Triggers in the location.
    )rF   pageSizetriggersrZ   )field
batch_sizelimitbatch_size_attribute)r=   ,EventarcProjectsLocationsTriggersListRequestrJ   r   YieldFromListr?   )rB   location_refr^   	page_sizelist_reqs        r   Listz_BaseTriggersClient.Listv   sV     ~~JJ((*Y K @H##') )r   c                     | j                   j                  |j                         ||      }| j                  j	                  |      S )ai  Updates a Trigger.

    Args:
      trigger_ref: Resource, the Trigger to update.
      trigger_message: Trigger, the trigger message that holds trigger's
        event_filters, service account, destination, transport, etc.
      update_mask: str, a comma-separated list of Trigger fields to update.

    Returns:
      A long-running operation for update.
    )r   r    
updateMask)r=   -EventarcProjectsLocationsTriggersPatchRequestrJ   r?   Patch)rB   rM   rN   update_mask	patch_reqs        r   ri   z_BaseTriggersClient.Patch   sG     LL%%' M  I ==y))r   )r   r   r   r   r9   rL   rS   rW   re   ri   __classcell__)rD   s   @r   r5   r5   A   s%    C,",&),*r   r5   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)TriggersClientV1z3Client for Triggers service in the Eventarc GA API.c
           
      B   |g n=|j                         D 
cg c]"  \  }
}| j                  j                  |
|      $ c}}
}|E|j                         D ]2  \  }
}|j                  | j                  j                  |
|d             4 |r|j	                         nd}| j                  j                  |      }| j                  j                  |      }|r|j	                         nd}| j                  j                  |j	                         |||||||	      S c c}}
w )a   Builds a Trigger message with the given data.

    Args:
      trigger_ref: Resource, the Trigger to create.
      event_filters: dict or None, the Trigger's event filters.
      event_filters_path_pattern: dict or None, the Trigger's event filters in
        path pattern format.
      event_data_content_type: str or None, the data content type of the
        Trigger's event.
      service_account: str or None, the Trigger's service account.
      destination_message: Destination message or None, the Trigger's
        destination.
      transport_topic_ref: Resource or None, the user-provided transport topic.
      channel_ref: Resource or None, the channel for 3p events
      labels: dict or None, the Trigger's labels.

    Returns:
      A Trigger message with a destination service.
    N)	attributevaluezmatch-path-pattern)rp   rq   operator)topic)pubsub)r   eventFilterseventDataContentTypeserviceAccountdestination	transportchannellabels)itemsr=   EventFilterappendrJ   Pubsub	TransportTrigger)rB   rM   event_filtersevent_filters_path_patternevent_data_content_typeservice_accountdestination_messagetransport_topic_refchannel_refr{   keyrq   filter_messagestransport_topic_namert   ry   rz   s                    r   BuildTriggerMessagez$TriggersClientV1.BuildTriggerMessage   s:   > *1b'--/8/JC 	""S">/8O "-288:*#uNN&&U5I ' K	L ; 
 /;; "&  ^^"")=">F(((7I,7k&&(TG>>!!%%'$4&' " 	 	!8s   'Dc                 x    | j                   j                  ||||      }| j                   j                  |      S )a  Builds a Destination message for a destination Cloud Run service.

    Args:
      destination_run_service: str or None, the destination Cloud Run service.
      destination_run_job: str or None, the destination Cloud Run job.
      destination_run_path: str or None, the path on the destination Cloud Run
        service.
      destination_run_region: str or None, the destination Cloud Run service's
        region.

    Returns:
      A Destination message for a destination Cloud Run service.
    )servicejobpathregion)cloudRun)r=   CloudRunDestination)rB   destination_run_servicedestination_run_jobdestination_run_pathdestination_run_regionrun_messages         r   BuildCloudRunDestinationMessagez0TriggersClientV1.BuildCloudRunDestinationMessage   sC    $ ..))'!%	 * 'K
 >>%%{%;;r   c                 z    | j                   j                  |||||      }| j                   j                  |      S )a]  Builds a Destination message for a destination GKE service.

    Args:
      destination_gke_cluster: str or None, the destination GKE service's
        cluster.
      destination_gke_location: str or None, the location of the destination GKE
        service's cluster.
      destination_gke_namespace: str or None, the destination GKE service's
        namespace.
      destination_gke_service: str or None, the destination GKE service.
      destination_gke_path: str or None, the path on the destination GKE
        service.

    Returns:
      A Destination message for a destination GKE service.
    )clusterlocation	namespacer   r   )gke)r=   GKEr   )rB   destination_gke_clusterdestination_gke_locationdestination_gke_namespacedestination_gke_servicedestination_gke_pathgke_messages          r   BuildGKEDestinationMessagez+TriggersClientV1.BuildGKEDestinationMessage   sF    ( ..$$')+'! % #K >>%%+%66r   c                 `    dj                  |||      }| j                  j                  |      S )a  Builds a Workflow Destination message with the given data.

    Args:
      project_id: the ID of the project.
      destination_workflow: str or None, the Trigger's destination Workflow ID.
      destination_workflow_location: str or None, the location of the Trigger's
        destination Workflow. It defaults to the Trigger's location.

    Returns:
      A Destination message with a Workflow destination.
    z%projects/{}/locations/{}/workflows/{})workflowformatr=   r   )rB   
project_iddestination_workflowdestination_workflow_locationworkflow_messages        r   BuildWorkflowDestinationMessagez0TriggersClientV1.BuildWorkflowDestinationMessage  s8     ?EE13GI>>%%/?%@@r   c                 `    dj                  |||      }| j                  j                  |      S )a  Builds a Function Destination message with the given data.

    Args:
      project_id: the ID of the project.
      destination_function: str or None, the Trigger's destination Function ID.
      destination_function_location: str or None, the location of the Trigger's
        destination Function. It defaults to the Trigger's location.

    Returns:
      A Destination message with a Function destination.
    z%projects/{}/locations/{}/functions/{})cloudFunctionr   )rB   r   destination_functiondestination_function_locationfunction_messages        r   BuildFunctionDestinationMessagez0TriggersClientV1.BuildFunctionDestinationMessage#  s8     ?EE13GI>>%%4D%EEr   c                     | j                   j                  |      }| j                   j                  |      }| j                   j                  ||      S )aI  Builds a HTTP Endpoint Destination message with the given data.

    Args:
      destination_http_endpoint_uri: str or None, the Trigger's destination uri.
      network_attachment: str or None, the Trigger's destination
        network attachment.

    Returns:
      A Destination message with a HTTP Endpoint destination.
    )uri)networkAttachment)httpEndpointnetworkConfig)r=   HttpEndpointNetworkConfigr   )rB   destination_http_endpoint_urinetwork_attachmenthttp_endpoint_messagenetwork_config_messages        r   #BuildHTTPEndpointDestinationMessagez4TriggersClientV1.BuildHTTPEndpointDestinationMessage5  sd     !NN77) 8  "^^99, :  >>%%*, &  r   c                 <   g }|r|j                  d       |r|j                  d       |r|j                  d       |r|j                  d       |	r|j                  d       |
r|j                  d       |r|j                  d       |s|r|j                  d       |s|r|j                  d	       |s|r|j                  d
       |r|j                  d       |r|j                  d       |r|j                  d       |st        d      dj                  |      S )a;  Builds an update mask for updating a Cloud Run trigger.

    Args:
      event_filters: bool, whether to update the event filters.
      event_filters_path_pattern: bool, whether to update the event filters with
        path pattern syntax.
      event_data_content_type: bool, whether to update the event data content
        type.
      service_account: bool, whether to update the service account.
      destination_run_service: bool, whether to update the destination Cloud Run
        service.
      destination_run_job: bool, whether to update the desintation Cloud Run
        job.
      destination_run_path: bool, whether to update the destination Cloud Run
        path.
      destination_run_region: bool, whether to update the destination Cloud Run
        region.
      destination_gke_namespace: bool, whether to update the destination GKE
        service namespace.
      destination_gke_service: bool, whether to update the destination GKE
        service name.
      destination_gke_path: bool, whether to update the destination GKE path.
      destination_workflow: bool, whether to update the destination workflow.
      destination_workflow_location: bool, whether to update the destination
        workflow location.
      destination_function: bool, whether to update the destination function.
      destination_function_location: bool, whether to update the destination
        function location.
      labels: bool, whether to update the labels.

    Returns:
      The update mask as a string.

    Raises:
      NoFieldsSpecifiedError: No fields are being updated.
    zdestination.cloudRun.pathzdestination.cloudRun.regionzdestination.cloudRun.servicezdestination.cloudRun.jobzdestination.gke.namespacezdestination.gke.servicezdestination.gke.pathzdestination.workflowzdestination.cloudFunctionru   rw   rv   r{   z*Must specify at least one field to update.,)r~   r   join)rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r{   rj   s                     r   BuildUpdateMaskz TriggersClientV1.BuildUpdateMaskO  s   n K45677834 4523/0</0<452()*/0""#OPP88K  r   c                 @    t        j                  |j                        S )zGets the Trigger's event type.)r
   EventFiltersMessageToTyperu   )rB   rN   s     r   GetEventTypezTriggersClientV1.GetEventType  s    **?+G+GHHr   c                 B    | j                   j                  j                  S )zReturns the labels value class.)r=   r   LabelsValue)rB   s    r   LabelsValueClassz!TriggersClientV1.LabelsValueClass  s    >>!!---r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rn   rn      s8    ;8t<278A$F$4T!lI.r   rn   N)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.eventarcr   $googlecloudsdk.api_lib.eventarc.baser   googlecloudsdk.api_lib.utilr	   #googlecloudsdk.command_lib.eventarcr
   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r   r)   Errorr   r!   r3   r5   rn   r   r   r   <module>r      sr    + &  ' ' 2 C , 5 * ) 1 * CZ-- CM*[*, [*|L.* L.r   