
    3                        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dlmZ ddlmZ ddlmZ  G d de      Zd Z G d de      Z G d de      ZdZy)z"Utilities for Cloud Workflows API.    )absolute_import)division)unicode_literals)
exceptions)apis)waiter)cache)poller_utils)arg_parsers)base)	arg_utils)labels_util)flags)	resourcesc                       e Zd ZdZy)UnsupportedReleaseTrackErrorzDRaised when requesting API version for an unsupported release track.N)__name__
__module____qualname____doc__     1lib/googlecloudsdk/api_lib/workflows/workflows.pyr   r   #   s    Lr   r   c                     | t         j                  j                  k(  ry| t         j                  j                  k(  ry| t         j                  j                  k(  ryt        |       )Nv1alpha1v1betav1)r   ReleaseTrackALPHABETAGAr   )release_tracks    r   ReleaseTrackToApiVersionr#   '   sR    d''---))...)),,,
&}
55r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	WorkflowsClientz8Client for Workflows service in the Cloud Workflows API.c                     t        j                  d|      | _        | j                  j                  | _        | j                  j
                  | _        y )N	workflows)r   GetClientInstanceclientMESSAGES_MODULEmessagesprojects_locations_workflows_serviceselfapi_versions     r   __init__zWorkflowsClient.__init__5   s8    ((kBDKKK//DMKK<<DMr   c                     | j                   j                  |j                               }	 | j                  j	                  |      S # t
        j                  $ r Y yw xY w)zGets a Workflow.

    Args:
      workflow_ref: Resource reference to the Workflow to get.

    Returns:
      Workflow: The workflow if it exists, None otherwise.
    nameN)r+   -WorkflowsProjectsLocationsWorkflowsGetRequestRelativeNamer-   Getapi_exceptionsHttpNotFoundError)r/   workflow_refget_reqs      r   r7   zWorkflowsClient.Get:   s\     mmII&&( J G]]w''++ s   A AAc                     | j                   j                  |j                         j                         ||j	                               }| j
                  j                  |      S )zCreates a Workflow.

    Args:
      workflow_ref: Resource reference to the Workflow to create.
      workflow: Workflow resource message to create.

    Returns:
      Long-running operation for create.
    )parentworkflow
workflowId)r+   0WorkflowsProjectsLocationsWorkflowsCreateRequestParentr6   Namer-   Create)r/   r:   r>   
create_reqs       r   rC   zWorkflowsClient.CreateK   sY     OO""$113$$& P J
 ==
++r   c                     dj                  t        |            }| j                  j                  |j	                         ||      }| j
                  j                  |      S )aY  Updates a Workflow.

    If updated fields are specified it uses patch semantics.

    Args:
      workflow_ref: Resource reference to the Workflow to update.
      workflow: Workflow resource message to update.
      updated_fields: List of the updated fields used in a patch request.

    Returns:
      Long-running operation for update.
    ,)r4   
updateMaskr>   )joinsortedr+   /WorkflowsProjectsLocationsWorkflowsPatchRequestr6   r-   Patch)r/   r:   r>   updated_fieldsupdate_mask	patch_reqs         r   rK   zWorkflowsClient.Patch\   sZ     ((6.12KMM&&( N I
 ==y))r   c                    | j                   j                         }g }t        j                  |||       t        j                  |||       t        j
                  |||       t        j                  || j                   j                  j                        }t        j                  |||       |t        j                  j                  k(  rt        j                  |||       d}|j                  d      r5t        j                  || j                   j                  j                  d      }|j                  d      rt!        |j"                        t        j$                  kD  r3t'        j(                  dj+                  t        j$                              t        j                  || j                   j                  j                  d      }|j                  d      rt        j,                  }t        j.                  |||       d}|j                  d      rS|j0                  j2                  D ci c]  }|j4                  |j6                   }}|j9                  |j:                         |j                  d      r|j0                  j2                  D ci c]  }|j4                  |j6                   }}|j<                  D ]:  }	|	|v r||	= t&        j>                  jA                  dd	j+                  |	
             t        jB                  |||       |j                  d      r[| j                   j                  jD                  }
tG        jH                  |jJ                  |
g d      }t        jL                  |||       |j                  d      r[| j                   j                  jN                  }tG        jH                  |jP                  |g d      }t        jR                  |||       |j                  d      r#|!t&        j>                  jA                  dd      t        jT                  ||| j                   j                  jV                         ||fS c c}w c c}w )a  Creates a workflow from command-line arguments.

    Args:
      args: The arguments of the gcloud command.
      old_workflow: The workflow from previous revision.
      release_track: The gcloud release track used in the command.

    Returns:
      workflow: The consturcted Workflow message from the passed in arguments.
      updated_fields: The workflow fields that are updated.
    Nset_env_varsenv_vars_filez3too many environment variables, limit is {max_len}.)max_lenclear_env_varsupdate_env_varsremove_env_varszkey {k} is not found.)k)argumentmessagecall_log_levelnonezlog-all-callszlog-errors-onlyzlog-nonevalid_choicesexecution_history_levelr[   zexecution-history-basiczexecution-history-detailedtagsz/tags cannot be updated for an existing workflow),r+   Workflowr   	SetSourceSetDescriptionSetServiceAccountr   ParseCreateArgsLabelsValue	SetLabelsr   r   r!   	SetKmsKeyIsSpecifiedUserEnvVarsValuelenrQ   USER_ENV_VARS_LIMITr   ArgumentTypeErrorformatCLEAR_ENVIRONMENTSetUserEnvVarsuserEnvVarsadditionalPropertieskeyvalueupdaterT   rU   argparseArgumentErrorUpdateUserEnvVarsCallLogLevelValueValuesEnumr   ChoiceToEnumrY   SetWorkflowLoggingArg$ExecutionHistoryLevelValueValuesEnumr^   #SetWorkflowExecutionHistoryLevelArgSetWorkflowsTagsArg	TagsValue)r/   argsold_workflowr"   r>   rL   labelsenv_varspvcall_log_level_enum	log_levelexecution_history_level_enumhistory_levels                 r   BuildWorkflowFromArgsz%WorkflowsClient.BuildWorkflowFromArgsq   s    }}%%'HN	OOD(N3	x8	D(N;((dmm$$00F 
OOFHn5)),,,oodHn5h			.	)..$--((99>
 
		/	*t!!"U%>%>>--CJJ33 K  
 ..$--((99?
 
		*	+**8X~>h			+	, "--BB
B EE177NB 	 
 	,,-			+	, "--BB
B EE177NB 	 
 %%A(]&&44/666; 5  	 & h.A			*	+"mm44PP**	
	 	##IxH			3	4MM""GG 	% "..(((
 	118^	5			&	!l&>""00E 1 
 	
 
$--00:: ^##q

s   ,QQc                 H   t         j                  j                  |j                  d      }t	        j
                  | j                  | j                        }t	        j                  | ||      }dj                  |j                               }t        j                  |||      S )z9Waits until the given long-running operation is complete.z'workflows.projects.locations.operations)
collection)r'   
operationsr:   z&Waiting for operation [{}] to complete)r   REGISTRYParser4   r
   OperationsClientr)   r+   WorkflowsOperationPollerrn   rB   r   WaitFor)r/   	operationr:   operation_refr   pollerprogress_strings          r   WaitForOperationz WorkflowsClient.WaitForOperation   s    &&,,< - M ..t{{DMMJJ22!F
 ?EEO >>&-AAr   N)
r   r   r   r   r1   r7   rC   rK   r   r   r   r   r   r%   r%   2   s&    @=
","**g$RBr   r%   c                   2    e Zd ZdZd Z	 	 	 	 ddZd Zd Zy)WorkflowExecutionClientzLClient for Workflows Execution service in the Cloud Workflows Execution API.c                     t        j                  d|      | _        | j                  j                  | _        | j                  j
                  | _        y )Nworkflowexecutions)r   r(   r)   r*   r+   'projects_locations_workflows_executionsr-   r.   s     r   r1   z WorkflowExecutionClient.__init__   s9    (()={KDKKK//DMKKGGDMr   Nc                 r   | j                   j                         }||_        |rd|_        |||_        |D|dk7  r?| j                   j                  j
                  }t        j                  ||g d      |_        |D|dk7  r?| j                   j                  j                  }	t        j                  ||	g d      |_
        | j                   j                  |j                         |      }
	 | j                  j                  |
      S # t        j                   $ r}t#        j$                  |d	      d}~ww xY w)
a  Creates a Workflow execution.

    Args:
      workflow_ref: Resource reference to the Workflow to execute.
      data: Argments to use for executing the workflow.
      call_log_level: Level of call logging to apply during execution.
      execution_history_level: Level of execution history to apply for the
        execution.
      labels: Labels associated to the execution.
      overflow_buffering_disabled: If set to true, the execution will not be
        backlogged when the concurrency quota is exhausted. Backlogged
        executions start when the concurrency quota becomes available.

    Returns:
      Execution: The workflow execution.
    TNr[   rZ   r\   r_   )r=   	execution	{message}error_format)r+   	ExecutionrW   (disableConcurrencyQuotaOverflowBufferingr   ry   r   rz   callLogLevelr|   executionHistoryLevelCWorkflowexecutionsProjectsLocationsWorkflowsExecutionsCreateRequestr6   r-   rC   r8   	HttpErrorr   HttpException)r/   r:   datarY   r^   r   overflow_buffering_disabledr   r   r   rD   es               r   rC   zWorkflowExecutionClient.Create   s2   2 '')II";?i8i!n&> MM33OO(55

	 i 	 +#v- --
!
!
F
F # )2(>(>
!
&)i% bb((*i c JB]]!!*--## B$$Q[AABs   ,D D6D11D6c                    |t        j                         }| j                  j                  |j	                               }	 | j
                  j                  |      S # t        j                  $ r}t        j                  |d      d}~ww xY w)zGets a workflow execution.

    Args:
      execution_ref: Resource reference to the Workflow execution to get.

    Returns:
      Workflow: The workflow execution if it exists, an error exception
      otherwise.
    Nr3   r   r   )r	   get_cached_execution_idr+   @WorkflowexecutionsProjectsLocationsWorkflowsExecutionsGetRequestr6   r-   r7   r8   r   r   r   )r/   execution_refr;   r   s       r   r7   zWorkflowExecutionClient.Get7  s     335mmm\\'') ] GB]]w''## B$$Q[AABs   A B0BBc           	         |t        j                         }t        j                  |       }dj	                  |j                               }	 t        j                  |||dddd      S # t        j                  $ r* t        j                  dj	                  |t                    t        j                  $ r% t        j                  d	j	                  |            w xY w)
zPWaits until the given execution is complete or the maximum wait time is reached.)workflow_executionz&Waiting for execution [{}] to completed   i \&g      ?i`  )pre_start_sleep_msmax_wait_msexponential_sleep_multiplierwait_ceiling_msz/Execution {0} has not finished in 24 hours. {1}z Aborting wait for execution {0}.)r	   r   r
   ExecutionsPollerrn   rB   r   r   TimeoutError_TIMEOUT_MESSAGEAbortWaitError)r/   r   r   r   s       r   WaitForExecutionz(WorkflowExecutionClient.WaitForExecutionL  s     335m**dCF>EEO^^


 '+! !  
;
B
B- 
    !!
,
3
3M
B s   A) )A3C)NNNF)r   r   r   r   r1   rC   r7   r   r   r   r   r   r      s,    TH ""'ABFB*r   r   zThe execution may still be underway remotely and may still succeed; use gcloud list and describe commands or https://console.developers.google.com/ to check resource state.N) r   
__future__r   r   r   apitools.base.pyr   r8   googlecloudsdk.api_lib.utilr   r    googlecloudsdk.api_lib.workflowsr	   r
   googlecloudsdk.callioper   r   $googlecloudsdk.command_lib.util.apisr   $googlecloudsdk.command_lib.util.argsr   $googlecloudsdk.command_lib.workflowsr   googlecloudsdk.corer   	Exceptionr   r#   objectr%   r   r   r   r   r   <module>r      su    ) &  ' 9 , 2 . 2 9 / ( : < 6 )M9 M6wBf wBt|f |BF r   