
                             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
ZdZddZddZ G d dej"                        Z G d de      Zy)z"Utilities for the clouddeploy API.    )absolute_import)division)unicode_literals)apis)waiter)
exceptions)log)	resourcesclouddeployv1Nc                 6    | xs
 t               } | j                  S )zReturns the messages module for Cloud Deploy.

  Args:
    client: base_api.BaseApiClient, the client class for Cloud Deploy.

  Returns:
    Module containing the definitions of messages for Cloud Deploy.
  )GetClientInstanceMESSAGES_MODULE)clients    5lib/googlecloudsdk/api_lib/clouddeploy/client_util.pyGetMessagesModuler      s     (&(&			    c                 D    t        j                  t        t        |        S )zReturns an instance of the Cloud Deploy client.

  Args:
    use_http: bool, True to create an http object for this client.

  Returns:
    base_api.BaseApiClient, An instance of the Cloud Deploy client.
  )no_http)r   r   	_API_NAME_API_VERSION)use_https    r   r   r   ,   s     
			<h,	PPr   c                   .     e Zd ZdZ fdZd Zd Z xZS )DeployOperationPollerzPoller for Cloud Deploy operations API.

  This is necessary because the core operations library doesn't directly support
  simple_uri.
  c                     || _         t        t        |   | j                   j                   j                  | j                   j                   j                         y)zInitiates a DeployOperationPoller.

    Args:
      client: base_api.BaseApiClient, An instance of the Cloud Deploy client.
    N)r   superr   __init__projects_locations_operations)selfr   	__class__s     r   r   zDeployOperationPoller.__init__?   sF     DK	

++II++IIKr   c                 8    | j                   j                  |      S N)r   Getr   operation_refs     r   PollzDeployOperationPoller.PollJ   s    ;;??=))r   c                     |S r"    )r   	operations     r   	GetResultzDeployOperationPoller.GetResultM   s    r   )__name__
__module____qualname____doc__r   r&   r*   __classcell__)r    s   @r   r   r   8   s    	K*r   r   c                   ,    e Zd ZdZddZd ZddZd Zy)	OperationsClientz:High-level client for the cloud deploy operations surface.Nc                 h    |xs
 t               | _        |xs | j                  j                  | _        y)zInitiates an OperationsClient.

    Args:
      client:  base_api.BaseApiClient, An instance of the Cloud Deploy client.
      messages: messages module for Cloud Deploy.
    N)r   r   r   messages)r   r   r3   s      r   r   zOperationsClient.__init__T   s*     /-/DK; ; ;DMr   c                     | j                   j                  j                  | j                  j	                  |j                                     S )N)name)r   r   r#   r3   0ClouddeployProjectsLocationsOperationsGetRequestRelativeNamer$   s     r   r#   zOperationsClient.Get^   sC    ;;4488FF++- 	G 	/0 0r   c                     t        |       }|j                  |      r|S |dj                  |j                               }t	        j
                  |||      S )a  Wait until the operation is complete or times out.

    Args:
      operation: The operation resource to wait on
      operation_ref: The operation reference to the operation resource. It's the
        result by calling resources.REGISTRY.Parse
      message: str, the message to print while waiting.

    Returns:
      The operation resource when it has completed

    Raises:
      OperationTimeoutError: when the operation polling times out
      OperationError: when the operation completed with an error
    zWaiting for operation [{}])r   IsDoneformatNamer   WaitFor)r   r)   r%   messagepollers        r   WaitForOperationz!OperationsClient.WaitForOperationc   sQ      #4(F}}Y,33M4F4F4HIg>>&-99r   c                    |j                         D ]  \  }}|r|j                  s	 t        j                  j	                  |j                  d      }| j                  ||dj                  |            j                  }t        j                  j                  |j                  |              y# t        j                  $ r8}t        j                  j                  dj                  |             Y d}~d}~ww xY w)a^  Checks operations status.

    Only logs the errors instead of re-throwing them.

    Args:
     operation_dict: dict[str, oOptional[clouddeploy_messages.Operation],
       dictionary of resource name and clouddeploy_messages.Operation. The
       operation can be None if the operation isn't executed.
     msg_template: output string template.
    z)clouddeploy.projects.locations.operations)
collectionz(Waiting for the operation on resource {}zOperation failed: {}N)itemsr5   r
   REGISTRYParseRelativeNamer?   r:   responser	   statusPrintcore_exceptionsError)r   operation_dictmsg_templateresource_namer)   r%   _es           r   CheckOperationStatusz%OperationsClient.CheckOperationStatus{   s     %3$8$8$: yinn;!**<<NNB = D !!}6==  !) 	

 	

,,];< %; "" ;

/66q9::;s   BB//C:.C55C:)NNr"   )r+   r,   r-   r.   r   r#   r?   rO   r(   r   r   r1   r1   Q   s    B<0
:0;r   r1   r"   )T)r.   
__future__r   r   r   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.corer   rH   r	   r
   r   r   r   r   CloudOperationPollerr   objectr1   r(   r   r   <module>rU      sW    ) &  ' , . = # )	
 	QF77 2D;v D;r   