
                         f   S 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rS
rSr " S S\R(                  5      r\4S jr\4S jr\4S jr\4S jrS rS rS rS rS rS rS r  " S S\!5      r" " S S\!5      r# " S S\R(                  5      r$S r%S  r&g)!zCRM API Operations utilities.    N)encoding)
exceptions)apis)	resources)yaml)progress_tracker)retryv1v3c                       \ rS rSrSrg)OperationError#    N)__name__
__module____qualname____firstlineno____static_attributes__r       9lib/googlecloudsdk/api_lib/resource_manager/operations.pyr   r   #   s    r   r   c                 0    [         R                  " SU 5      $ Ncloudresourcemanager)r   GetClientInstanceversions    r   OperationsClientr   '       			 6	@@r   c                 f    [         R                  R                  5       nUR                  SU 5        U$ r   )r   REGISTRYCloneRegisterApiByName)r   registrys     r   OperationsRegistryr$   +   s,    %%'(
3W=	/r   c                 ,    [        U 5      R                  $ N)r   
operationsr   s    r   OperationsServicer(   1   s    	'	"	-	--r   c                 0    [         R                  " SU 5      $ r   )r   GetMessagesModuler   s    r   OperationsMessagesr+   5   r   r   c                     U [        S5      S  $ )Noperations/)len)operation_names    r   OperationNameToIdr0   9   s    	M*+	,,r   c                 $    SR                  U 5      $ )Nzoperations/{0})formatoperation_ids    r   OperationIdToNamer5   =   s    		 	 	..r   c                 ^    [        5       R                  [        5       R                  U S95      $ )N)operationsId)r(   Getr+   (CloudresourcemanagerOperationsGetRequestr3   s    r   GetOperationr:   A   s2    			 	 CC# D %
& &r   c                     [        [        5      R                  [        [        5      R	                  [        U 5      S95      $ )N)name)r(   OPERATIONS_API_V3r8   r+   r9   r5   r3   s    r   GetOperationV3r>   G   s?    	,	-	1	1
EE$\2 F 4
5 5r   c                     SR                  U R                  5      n[        R                  " USS9 n[	        5       n[        U5      nUR                  X@5      sS S S 5        $ ! , (       d  f       g = f)NzWaiting for [{0}] to finishF)autotick)r2   r<   trackerProgressTrackerOperationRetryerOperationPollerRetryPollOperation)	operationwait_messageptretryerpollers        r   WaitForOperationrK   N   sT    .55innE,|e< GR F%%f8 =<<s   &A!!
A/c                 n    [         R                  " U R                  5      n[         R                  " X!5      $ r&   )r   MessageToDictresponseDictToMessage)rF   response_message_typeraw_dicts      r   ExtractOperationResponserR   V   s)    ##I$6$67(				@@r   c                     [         R                  " U 5      n[         R                  " U[        5       R                  R
                  5      $ r&   )r   rM   rO   r+   	OperationResponseValue)messagerQ   s     r   ToOperationResponserW   [   s;    ##G,(			 2 4 > > L L
N Nr   c                   L    \ rS rSrSrS SSSS4S jrS rS	 rS
 rS r	S r
Srg)rC   a   zA wrapper around a Retryer that works with CRM operations.

Uses predefined constants for retry timing, so all CRM operation commands can
share their retry timing settings.
c                  .    [         R                  " S5      $ )N   )timesleepr   r   r   <lambda>OperationRetryer.<lambda>i   s    tzz!}r   i  i i N  c                 @    Xl         X l        X0l        X@l        XPl        g r&   )_pre_start_sleep_max_retry_ms_max_wait_ms_wait_ceiling_ms_first_retry_sleep_ms)selfpre_start_sleepmax_retry_msmax_wait_mswait_ceiling_msfirst_retry_sleep_mss         r   __init__OperationRetryer.__init__h   s"     ,%#+!5r   c                    ^^ U R                  5         U R                  5       R                  UU4S jU R                  U R                  S9$ )Nc                  &   > TR                  T 5      $ r&   )Poll)rF   operation_pollers   r   r^   5OperationRetryer.RetryPollOperation.<locals>.<lambda>w   s     %%i0r   )should_retry_ifsleep_ms)ra   _RetryerRetryOnResult_ShouldRetryre   )rf   rq   rF   s    ``r   rE   #OperationRetryer.RetryPollOperationt   sB    ==?((0))++ ) - -r   c                 V    [         R                  " SU R                  U R                  S9$ )N   )exponential_sleep_multiplierri   rj   )r	   Retryerrc   rd   )rf   s    r   ru   OperationRetryer._Retryer{   s(    ==%&%%--/ /r   c                     [        U[        R                  5      (       a  U R                  X5      $ U R	                  U5      $ r&   )
isinstancer   	HttpError_CheckTimePassedBelowMax_CheckResultNotExceptionrf   resultstates      r   rw   OperationRetryer._ShouldRetry   s7    &*..//**699((00r   c                 <    UR                   U R                  :  a  Ueg)NT)time_passed_msrb   r   s      r   r   )OperationRetryer._CheckTimePassedBelowMax   s    d000lr   c                 R    [        U[        5      (       a  UeUR                  (       + $ r&   )r   	Exceptiondone)rf   r   s     r   r   )OperationRetryer._CheckResultNotException   s     &)$$l{{?r   )re   rb   rc   ra   rd   N)r   r   r   r   __doc__rl   rE   ru   rw   r   r   r   r   r   r   rC   rC   a   s6      5 !$$(
6-/1

r   rC   c                   $    \ rS rSrSS jrS rSrg)rD      Nc                     Xl         g r&   _progress_tracker)rf   r   s     r   rl   OperationPoller.__init__   s    -r   c                     U R                   (       a  U R                   R                  5         [        [        UR                  5      5      nUR
                  (       a  UR                  (       a  [        U5      eU$ r&   )r   Tickr:   r0   r<   r   errorOperationFailedException)rf   rF   latests      r   rp   OperationPoller.Poll   sN    
!!#+INN;<F{{v||$V,,Mr   r   r&   )r   r   r   r   rl   rp   r   r   r   r   rD   rD      s    .r   rD   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )r      z Exception for failed operations.c                   > [        UR                  5      nUR                  R                  nUR                  R                  nSR                  X#U5      n [        R                  " UR                  5      nUR                  S/ 5      nU(       a  US[        R                  " U5      -   -  n[        [         U ]G  U5        g ! [        [        [        R                  R                  4 a  nUSU S3-  n S nANKS nAff = f)Nz Operation [{0}] failed: {1}: {2}details
z+
(Failed to parse or format error details: ))r0   r<   r   coderV   r2   r   MessageToPyValuegetr   dump	TypeErrorAttributeErrorruamel	YAMLErrorsuperr   rl   )
rf   operation_with_errorop_id
error_codeerror_messagerV   error_py_valuer   e	__class__s
            r   rl   !OperationFailedException.__init__   s    2778E%++00J(..66M0778EGGE001E1K1KLn""9b1g	4$))G,,, 

"D27; ~v{{'<'<= E?s!DDgEs   AB? ?)C;(	C66C;r   )r   r   r   r   r   rl   r   __classcell__)r   s   @r   r   r      s    (< <r   r   c                     [        U R                  5      n[        5       R                  SSU0SS9nUR	                  5       $ )zReturns the uri for resource.Nr7   zcloudresourcemanager.operations)params
collection)r0   r<   r$   ParseSelfLink)resourcer4   operation_refs      r   GetUrir      sG    "8==1,$&,,
l+2 - 4- 
			!!r   c           
          / nU H<  nUR                  [        R                  " XaR                  R                  5      5        M>     UR                  SU -   SUR                  X4US9S9$ )z.Returns a failed operation with error details.r-   T)r   rV   r   )r<   r   r   )appendr   rO   StatusDetailsValueListEntryrT   )r/   messageserror_detailsr   r   details_messagesitems          r   GetFailedOperationr      s|     d t__%J%JKM  
		>)OO:J  L 
 
M Mr   )'r   r\   apitools.base.pyr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corecore_exceptionsr   r   googlecloudsdk.core.consoler   rA   googlecloudsdk.core.utilr	   ruamel.yamlr   OPERATIONS_API_V1r=   Errorr   r   r$   r(   r+   r0   r5   r:   r>   rK   rR   rW   objectrC   rD   r   r   r   r   r   r   <module>r      s    $  % ' , = ) $ C *    Z%%  / A  1  0 .  1 A-/&59A
N-v -`f <44 <*"Mr   