
                            S r SSKJr  SSKJr  SSKJ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\R0                  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!\R0                  5      r(S" r)S# r*g)$zCRM API Operations utilities.    )absolute_import)division)unicode_literalsN)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+   4   s    	'	"	-	--r   c                 0    [         R                  " SU 5      $ r   )r   GetMessagesModuler   s    r   OperationsMessagesr.   8   r!   r   c                     U [        S5      S  $ )Noperations/)len)operation_names    r   OperationNameToIdr3   <   s    	M*+	,,r   c                 $    SR                  U 5      $ )Nzoperations/{0})formatoperation_ids    r   OperationIdToNamer8   @   s    		 	 	..r   c                 ^    [        5       R                  [        5       R                  U S95      $ )N)operationsId)r+   Getr.   (CloudresourcemanagerOperationsGetRequestr6   s    r   GetOperationr=   D   s2    			 	 CC# D %
& &r   c                     [        [        5      R                  [        [        5      R	                  [        U 5      S95      $ )N)name)r+   OPERATIONS_API_V3r;   r.   r<   r8   r6   s    r   GetOperationV3rA   J   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)r5   r?   trackerProgressTrackerOperationRetryerOperationPollerRetryPollOperation)	operationwait_messageptretryerpollers        r   WaitForOperationrN   Q   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)rI   response_message_typeraw_dicts      r   ExtractOperationResponserU   Y   s)    ##I$6$67(				@@r   c                     [         R                  " U 5      n[         R                  " U[        5       R                  R
                  5      $ r)   )r   rP   rR   r.   	OperationResponseValue)messagerT   s     r   ToOperationResponserZ   ^   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)rF   d   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>l   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__k   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)rI   operation_pollers   r   ra   5OperationRetryer.RetryPollOperation.<locals>.<lambda>z   s     %%i0r   )should_retry_ifsleep_ms)rd   _RetryerRetryOnResult_ShouldRetryrh   )ri   rt   rI   s    ``r   rH   #OperationRetryer.RetryPollOperationw   sB    ==?((0))++ ) - -r   c                 V    [         R                  " SU R                  U R                  S9$ )N   )exponential_sleep_multiplierrl   rm   )r   Retryerrf   rg   )ri   s    r   rx   OperationRetryer._Retryer~   s(    ==%&%%--/ /r   c                     [        U[        R                  5      (       a  U R                  X5      $ U R	                  U5      $ r)   )
isinstancer   	HttpError_CheckTimePassedBelowMax_CheckResultNotExceptionri   resultstates      r   rz   OperationRetryer._ShouldRetry   s7    &*..//**699((00r   c                 <    UR                   U R                  :  a  Ueg)NT)time_passed_msre   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)ri   r   s     r   r   )OperationRetryer._CheckResultNotException   s     &)$$l{{?r   )rh   re   rf   rd   rg   N)r   r   r   r   __doc__ro   rH   rx   rz   r   r   r   r   r   r   rF   rF   d   s6      5 !$$(
6-/1

r   rF   c                   $    \ rS rSrSS jrS rSrg)rG      Nc                     Xl         g r)   _progress_tracker)ri   r   s     r   ro   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=   r3   r?   r   errorOperationFailedException)ri   rI   latests      r   rs   OperationPoller.Poll   sN    
!!#+INN;<F{{v||$V,,Mr   r   r)   )r   r   r   r   ro   rs   r   r   r   r   rG   rG      s    .r   rG   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: ))r3   r?   r   coderY   r5   r   MessageToPyValuegetr
   dump	TypeErrorAttributeErrorruamel	YAMLErrorsuperr   ro   )
ri   operation_with_errorop_id
error_codeerror_messagerY   error_py_valuer   e	__class__s
            r   ro   !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   ro   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.Nr:   zcloudresourcemanager.operations)params
collection)r3   r?   r'   ParseSelfLink)resourcer7   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.r0   T)r   rY   r   )r?   r   r   )appendr   rR   StatusDetailsValueListEntryrW   )r2   messageserror_detailsr   r   details_messagesitems          r   GetFailedOperationr      s|     d t__%J%JKM  
		>)OO:J  L 
 
M Mr   )+r   
__future__r   r   r   r_   apitools.base.pyr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corecore_exceptionsr	   r
   googlecloudsdk.core.consoler   rD   googlecloudsdk.core.utilr   ruamel.yamlr   OPERATIONS_API_V1r@   Errorr   r    r'   r+   r.   r3   r8   r=   rA   rN   rU   rZ   objectrF   rG   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   