
                             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
  G d d	e      Z G d
 dej                        Z G d dej                        Zd Zy)z%Utilities for Cloud Workflows poller.    )absolute_import)division)unicode_literals)
exceptions)waiter)codesc                       e Zd ZdZd Zd Zy)OperationsClientz9Client for Operations service in the Cloud Workflows API.c                 V    || _         || _        | j                   j                  | _        y )N)clientmessagesprojects_locations_operations_service)selfr   r   s      4lib/googlecloudsdk/api_lib/workflows/poller_utils.py__init__zOperationsClient.__init__   s!    DKDMKK==DM    c                     | j                   j                  |j                               }	 | j                  j	                  |      S # t
        j                  $ r Y yw xY w)zGets an Operation.

    Args:
      operation_ref: Resource reference to the Operation to get.

    Returns:
      Operation: The operation if it exists, None otherwise.
    )nameN)r   .WorkflowsProjectsLocationsOperationsGetRequestRelativeNamer   Getr   HttpNotFoundError)r   operation_refget_reqs      r   r   zOperationsClient.Get"   s\     mmJJ'') K +G]]w'''' s   A AAN)__name__
__module____qualname____doc__r   r    r   r   r
   r
      s    A>
r   r
   c                   (    e Zd ZdZd Zd Zd Zd Zy)WorkflowsOperationPollerz;Implementation of OperationPoller for Workflows Operations.c                 .    || _         || _        || _        y)a4  Creates the poller.

    Args:
      workflows: the Workflows API client used to get the resource after
        operation is complete.
      operations: the Operations API client used to poll for the operation.
      workflow_ref: a reference to a workflow that is the subject of this
        operation.
    N)	workflows
operationsworkflow_ref)r   r$   r%   r&   s       r   r   z!WorkflowsOperationPoller.__init__6   s     DN DO$Dr   c                     |j                   r5|j                  r(t        j                  t	        |j                              yy)
Overrides.TF)doneerrorr   OperationError_ExtractErrorMessager   	operations     r   IsDonezWorkflowsOperationPoller.IsDoneD   s1    ~~	##$8$IJJr   c                 8    | j                   j                  |      S r(   )r%   r   )r   r   s     r   PollzWorkflowsOperationPoller.PollL   s    ??}--r   c                 L    | j                   j                  | j                        S r1   )r$   r   r&   r-   s     r   	GetResultz"WorkflowsOperationPoller.GetResultP   s    >>d//00r   Nr   r   r   r   r   r/   r2   r4   r    r   r   r"   r"   3   s    C%.1r   r"   c                   (    e Zd ZdZd Zd Zd Zd Zy)ExecutionsPollerz;Implementation of OperationPoller for Workflows Executions.c                     || _         y)zCreates the execution poller.

    Args:
      workflow_execution: the Workflows Executions API client used to get the
        execution resource.
    N)workflow_execution)r   r9   s     r   r   zExecutionsPoller.__init__X   s     1Dr   c                 j    |j                   j                  dk7  xr |j                   j                  dk7  S )r(   ACTIVEQUEUED)stater   r   	executions     r   r/   zExecutionsPoller.IsDonea   s+    ??8+P	0D0D0PPr   c                 8    | j                   j                  |      S r1   )r9   r   )r   execution_refs     r   r2   zExecutionsPoller.Polle   s    ""&&}55r   c                     |S r1   r    r>   s     r   r4   zExecutionsPoller.GetResulti   s    r   Nr5   r    r   r   r7   r7   U   s    C1Q6r   r7   c                     t        | d      r*t        j                  | j                        j                  }nd}t        | d      r| j
                  }n| }dj                  ||      S )z-Extracts the error message for better format.codeUNKNOWNmessagez[{code}] {message})rD   rF   )hasattrr   CoderD   r   rF   format)r*   	code_nameerror_messages      r   r,   r,   n   s[     UF

5::&++IIUIMMM M		$	$)]	$	KKr   N)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr    googlecloudsdk.api_lib.workflowsr   objectr
   OperationPollerr"   r7   r,   r    r   r   <module>rR      sS    , &  ' ' . 2v 21v55 1Dv-- 2Lr   