
    Y                        S 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Jr  SS
KJr  SSKJr  SrSrSr0 SSSS._SSSS._SSSS._SSSS._SSSS._SSSS._SSSS._SSSS._SSSS._SSSS._S SSS._S!SSS._S"SSS._SSSS._S#S$S#S._S%S&S'S._rS(S)/rS* rS+ rS, rS- rS. rS/ r " S0 S1\5      r   S4S3 jr g2)5z3Utilities for waiting on Compute Engine operations.    )absolute_import)division)unicode_literals)
exceptions)batch_helper)path_simplifier)single_request_helper)	time_util)log)
propertiesi        createSnapshotcreatedcreatepastpresent%recreateInstancesInstanceGroupManager	recreatedrecreatecreateFirewallSecurityPolicydeleteFirewallSecurityPolicydeleteddelete createNetworkEdgeSecurityService deleteNetworkEdgeSecurityServicecreatePSCServiceEndpointdeletePscForwardingRulecreateRegionPscForwardingRuledeleteRegionPscForwardingRulereleaseExternalIpv6AddressreserveExternalIpv6AddressinsertupdateupdatedinvalidateCachezcompleted invalidation forzcomplete invalidation forz'ReservationSubBlocks.performMaintenancez!ReservationSubBlocks.reportFaultyc                     [          H0  nU R                  U5      (       d  M  [         R                  U5      s  $    SSS.$ )Nr&   r%   r   )'_HUMAN_FRIENDLY_OPERATION_TYPE_SUFFIXESendswithget)op_typess     -lib/googlecloudsdk/api_lib/compute/waiters.py_HumanFriendlyNamesForOpr/   z   s;    2a488;; 3 	11    c                     [        U 5      S   $ )Nr   r/   r,   s    r.    _HumanFriendlyNameForOpPastTenser4      s    	!'	*6	22r0   c                     [        U 5      S   $ )Nr   r2   r3   s    r.   #_HumanFriendlyNameForOpPresentTenser6      s    	!'	*9	55r0   c                     [        U 5      S:H  $ )Nr   )r4   r3   s    r.   _IsDeleteOpr8      s    	)'	2i	??r0   c                    U R                   =(       d    /  H  nUR                  UR                  5        M      U R                  (       aD  U R                  R                  =(       d    /  H   nUR                  U R
                  U45        M"     gg)z5Records any warnings and errors into the given lists.N)warningsappendmessageerrorerrorshttpErrorStatusCode)	operationr:   r>   warningr=   s        r.   _RecordProblemsrB      si    ##)r)gOOGOO$ *__''-2-mmY22E:; . r0   c           
          U  VVs/ s H  u  p#UR                   PM     nnnUR                  SSR                  [        U S   S   R                  5      [
        SR                  U5      S945        gs  snnf )z@Adds error messages stating that the given operations timed out.NzDid not {action} the following resources within {timeout}s: {links}. These operations may still be underway remotely and may still succeed; use gcloud list and describe commands or https://console.developers.google.com/ to check resource stater   z, )actiontimeoutlinks)
targetLinkr;   formatr6   operationType_POLLING_TIMEOUT_SECjoin)
operationsr>   r@   _pending_resourcess        r.   _RecordUnfinishedOperationsrO      s{    @JK
	y++
K--
! 	4mA,, '		+, 	 	  Ls   A0c                       \ rS rSrSr     SS jrS rS rS rS r	S	 r
S
 rS rS rS rS rS rS r\4S jr\4S jr\4S jr\4S jrSrg)OperationData   a  Holds all information necessary to poll given operation.

Attributes:
  operation: An Operation object to poll.
  operation_service: The service that can be used to get operation object.
  resource_service: The service of the collection being mutated by the
    operation. If the operation type is not delete, this service is used to
    fetch the mutated object after the operation is done.
  project: str, The project to which the resource belong.
  resize_request_name: str, Name of the resize request being created.
  no_followup: str, If True, do not send followup GET request.
  followup_override: str, Overrides the target resource name when it is
    different from the resource name which is used to poll.
  always_return_operation: If true, always return operation object even if the
    operation fails.
  errors: An output parameter for capturing errors.
  warnings: An output parameter for capturing warnings.
Nc	                     Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        / U l        / U l	        g N)
r@   operation_serviceresource_serviceprojectresize_request_nameno_followupfollowup_overridealways_return_operationr>   r:   )	selfr@   rU   rV   rW   rX   rY   rZ   r[   s	            r.   __init__OperationData.__init__   s@     N.,L2".#: DKDMr0   c                    [        U[        5      (       d  gU R                  UR                  :H  =(       a    U R                  UR                  :H  =(       ay    U R                  UR                  :H  =(       aY    U R
                  UR
                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )NF)
isinstancerQ   r@   rW   rU   rV   rY   rZ   r\   os     r.   __eq__OperationData.__eq__   s    a''NNakk) :dllaii.G :""a&9&99:!!Q%7%77: -: ""a&9&99	;r0   c                    [        U R                  R                  5      [        U R                  5      -  [        U R                  5      -  [        U R
                  5      -  [        U R                  5      -  [        U R                  5      -  $ rT   )hashr@   selfLinkrW   rU   rV   rY   rZ   r\   s    r.   __hash__OperationData.__hash__   sv    (()D,>>4))*+-1$2G2G-HI4##$%'+D,B,B'CD Er0   c                     X:X  + $ rT    ra   s     r.   __ne__OperationData.__ne__   s
    =r0   c                     Xl         g)zF"Updates the operation.

Args:
  operation: Operation to be assigned.
N)r@   )r\   r@   s     r.   SetOperationOperationData.SetOperation   s	     Nr0   c                     [        U R                  R                  S5      (       d  gU R                  U R                  R                  R                  :H  $ )NglobalOrganizationOperationsF)hasattrrU   clientrs   rh   s    r.   IsGlobalOrganizationOperation+OperationData.IsGlobalOrganizationOperation   sM    4))0013 3""""))FFG Hr0   c                     U R                   R                  S5      nUR                  R                  nU R                  R
                  U:H  $ )z&Returns true if the operation is done.Get)rU   GetResponseTypeStatusValueValuesEnumDONEr@   status)r\   operation_typedones      r.   IsDoneOperationData.IsDone   s>    ++;;EBN//44D>>  D((r0   c                 <    SU R                   R                  5       ;   $ )NWait)rU   GetMethodsListrh   s    r.   _SupportOperationWait#OperationData._SupportOperationWait   s    T++::<<<r0   c           	         U R                   R                  nU R                  (       a*  U R                  R	                  S5      " U R                  S9nOUcZ  [
        R                  R                  SR                  [        U R                   R                  5      R                  5       5      5        gUR                  S5      nUS   nU R                  R	                  S5      " US9nU R                   R                  (       a0  [        R                  " U R                   R                  5      Ul        OJU R                   R                   (       a/  [        R                  " U R                   R                   5      Ul        U R                  R#                  S5      R$                  nSU;   a  S	U;  a  UR'                  S
S	5        US   n[)        U5      S:X  a  U R*                  (       a  US-   U R*                  -   nUS   nUR                  S5      S   n[,         H  n	U R                   R                  R/                  U	5      (       d  M/  SR                  UR                  S5      S   UR                  S5      S   UR                  S5      S   UR                  S5      S   5      n  O   [1        X'U5        U R2                  =(       d    [        R                  " U5      n
[1        X&U
5        U$ )z8"Generates apitools request message to get the resource.ry   )rW   Nz{0}.
/)securityPolicycrossSiteNetworkglobal      z/resizeRequests/   z{}/{}/{}/{}i)r@   rG   rW   rV   GetRequestTyper   r}   writerH   r4   rI   
capitalizesplitzoner   NameregionGetMethodConfigordered_paramsr$   lenrX   &_MULTI_NESTED_RESOURCE_OPERATION_TYPES
startswithsetattrrZ   )r\   target_linkrequest
token_listflexible_resource_idresource_params
name_fieldparent_resource_fieldparent_resource_namer,   resource_names              r.   ResourceGetRequest OperationData.ResourceGetRequest  sm    ..++K||%%44U;,, g 
	

,,,..8jl< 	= 	$$S)j'^%%44U;-/g~~$))$..*=*=>gl			&++DNN,A,ABgn++;;n  	o-O+Q) $J
?q  
	!	!,,t/G/GG 	 .a0(..s3B7 <'>>''227;;!.!5!5$R($R($R($R(	"
  < g.BC**Oo.B.B;.OMG/Nr0   c                    U R                   (       a?  U R                  R                  U5      " U R                  R                  U R                   S9nOaU R                  R                  R                  S5      nSUS   -   nU R                  R                  U5      " U R                  R                  US9nU R                  R                  (       a1  [        R                  " U R                  R                  5      Ul        U$ U R                  R                  (       a/  [        R                  " U R                  R                  5      Ul	        U$ )z9Generates apitools request message to poll the operation.)r@   rW   -zorganizations/r   )r@   parentId)
rW   rU   r   r@   namer   r   r   r   r   )r\   verbr   r   	parent_ids        r.   _OperationRequestOperationData._OperationRequestB  s     ||&&55d;NN''?g >>&&,,S1j"Z]2i&&55d;NN'')=g~~$))$..*=*=>gl N 
		&++DNN,A,ABgnNr0   c                 $    U R                  S5      $ )z=Generates apitools request message for operations.get method.ry   r   rh   s    r.   OperationGetRequest!OperationData.OperationGetRequestT  s    !!%((r0   c                 $    U R                  S5      $ )z>Generates apitools request message for operations.wait method.r   r   rh   s    r.   OperationWaitRequest"OperationData.OperationWaitRequestX  s    !!&))r0   c                 F    U" U5      $ ! [         R                   a  n[        R                  " U5      nU R                  R                  UR                  R                  UR                  45        [        U R                  U R                  U R                  5        e S nAff = frT   )apitools_exceptions	HttpErrorhttp_exceptionsHttpExceptionr>   r;   r=   status_coder<   rB   r@   r:   )r\   methodr   ehttp_errs        r.   _CallServiceOperationData._CallService\  sy    G_((  ..q1h
kk(..44h6F6FGHdnndmmT[[A	s   
 B A=BB c                    U R                  5       n[        R                  " 5       nSnSn [        R                  " 5       U-
  U:  al  U R                  R	                  SSR                  U R                  R                  5      45        [        U R                  U R                  U R                  5        g U R                  U R                  R                  U5      U l        U R                  5       (       a,  [        U R                  U R                  U R                  5        g[!        US-   U5      n[        R"                  " U5        GM  ! [        R                   a     gf = f)z.Polls the operation with operation Get method.r   r   Noperation {} timed outr   )r   r
   CurrentTimeSecr>   r;   rH   r@   r   rB   r:   r   rU   ry   r   r   r   minSleep)r\   timeout_secget_requeststartpoll_time_intervalmax_poll_intervals         r.   _PollUntilDoneUsingOperationGet-OperationData._PollUntilDoneUsingOperationGete  s   **,K$$&E
		!	!	#e	+k	9+224>>3F3FGH	Jt{{C**4+A+A+E+E+68
 
t{{C1A57HIoo()#  !** s   4+E EEc                    U R                  5       n[        R                  " 5       nU R                  5       (       d  [        R                  " 5       U-
  U:  al  U R                  R                  SSR                  U R                  R                  5      45        [        U R                  U R                  U R                  5        g U R                  U R                  R                  U5      U l        U R                  5       (       d  M  [        U R                  U R                  U R                  5        g! [        R                   a     gf = f)z*Polls the operation with operation method.Nr   )r   r
   r   r   r>   r;   rH   r@   r   rB   r:   r   rU   r   r   r   )r\   r   wait_requestr   s       r.    _PollUntilDoneUsingOperationWait.OperationData._PollUntilDoneUsingOperationWait  s    ,,.L$$&Ekkmm		!	!	#e	+k	9+224>>3F3FGH	Jt{{C**4+A+A+F+F+79 kkmm DNNDMM4;;? !** s   +D2 2E	E	c                     U R                  5       (       a  gU R                  5       (       a  U R                  U5        gU R                  U5        g)z%Polls the operation until it is done.N)r   r   r   r   )r\   r   s     r.   PollUntilDoneOperationData.PollUntilDone  s;    {{}}!!##
++K8
**;7r0   c                 l   U R                  U5        U R                  (       dx  U R                  R                  (       d\  [	        U R                  R
                  5      (       d7  U R                  5       n U R                  U R                  R                  U5      $ ggg! [        R                   a     gf = f)z3Get the resource which is touched by the operation.N)r   rY   r@   r=   r8   rI   r   r   rV   ry   r   r   )r\   r   resource_get_requests      r.   	GetResultOperationData.GetResult  s    {#DNN$8$8$$B& B&!446  !6!6!:!:!57 	7	B&$8 !** s   3%B B32B3)
r[   r>   rZ   rY   r@   rU   rW   rX   rV   r:   )NNFNF)__name__
__module____qualname____firstlineno____doc__r]   rc   ri   rm   rp   rv   r   r   r   r   r   r   r   rJ   r   r   r   r   __static_attributes__rl   r0   r.   rQ   rQ      s    4 #.;E
H)==~$)* 9M *4 :N @& '; 8 #7 
r0   rQ   Nc              #   	  #    U (       d  gU=(       d    [         n0 n/ n	U  H<  n
XU
R                  R                  '   U	R                  U
R                  [        45        M>     [
        R                  " 5       nSnU S   R                  R                  S5      nU	(       Ga^  U(       a  UR                  5         / n/ n[        R                  " SU	5        U	 GH  u  n
nXR                     nUR                  U
5        UR                  nUR                  nU
R                  UR                  R                   :X  Ga/  [#        XU5        U
R$                  (       a(  U
R$                  S:w  a  UR&                  (       a  U
v   OM  U
R(                  (       a  M  UR*                  (       a  U
v   M  [-        U
R.                  5      (       d+  UR1                  5       nU(       a  UR                  USU45        U
R2                  (       ag  U(       a]  [        R                  R5                  SR7                  [9        U
R.                  5      R;                  5       U
R2                  5      5        GM  GM  GM  UR=                  5       (       a'  UR?                  5       nUR                  USU45        GM  URA                  5       nUR                  USU45        GM     X-   nU(       d  g[B        RD                  RF                  RH                  RK                  5       (       d2  [M        U5      S:X  a#  US   u  nnn[N        RP                  " UUUS	9u  nnO[R        RT                  " UXS
9u  nnSnU	n[W        U5      n/ n	[Y        U5       H  u  nn[[        UU5      (       aA  U	R                  U[        45        UR                  UR                  R                   :w  a  SnMV  MX  Uca  U(       aZ  US   S   S:X  aN  UR]                  S5      n UU   u  n
n!U!S-  n!U!S:  a  U	R                  U
U!45        SnM  UR                  U 5        M  Uv   M     UR_                  U5        U	(       d  gU(       a  GM  [
        R                  " 5       U-
  U:  a<  U(       d#  [        R                  " SU5        [a        X5        gUR_                  U5        g[c        US-   [d        5      n[        R                  " SU5        [
        Rf                  " U5        U	(       a  GM]  gg7f)aS  Blocks until the given operations are done or until a timeout is reached.

Args:
  operations_data: A list of OperationData objects holding Operations to poll.
  http: An HTTP object.
  batch_url: The URL to which batch requests should be sent.
  warnings: An output parameter for capturing warnings.
  errors: An output parameter for capturing errors.
  progress_tracker: progress tracker to tick while waiting for operations to
                    finish.
  timeout: The maximum amount of time, in seconds, to wait for the
    operations to reach the DONE state.
  log_result: Whether the Operation Waiter should print the result in past
    tense of each request.

Yields:
  The resources pointed to by the operations' targetLink fields if
  the operation type is not delete. Only resources whose
  corresponding operations reach done are yielded.
Nr   ry   zOperations to inspect: %s   z{0} [{1}].
r   r   )servicer   request_body)requestshttp	batch_urlTFi  zTimeout of %ss reached.zSleeping for %ss.)4rJ   r@   rg   r;    _SERVICE_UNAVAILABLE_RETRY_COUNTr
   r   rU   rz   Tickr   debugrp   rV   r}   r{   r|   rB   r?   r[   r=   rY   r8   rI   r   rG   r   rH   r4   r   rv   r   r   r   VALUEScomputeforce_batch_requestGetBoolr   r	   MakeSingleRequestr   MakeRequestslist	enumerater`   popextendrO   r   _MAX_TIME_BETWEEN_POLLS_SECr   )"operations_datar   r   r:   r>   progress_trackerrE   
log_resultoperation_detailsunprocessed_operationsr@   r   	sleep_secr~   resource_requestsoperation_requestsrM   datarU   rV   r   r   r   r   r   	responsesrequest_errorsall_doneprevious_operationscurrent_errorsseqresponser=   retry_counts"                                     r.   WaitForOperationsr    st    < 

++' "i6?i))223!!			>? # 
"
"
$%) #1%77GGN.II)+AB.	1112d 	"00..			^AAFF	F
 		V4 ))))S0))O ??

 /
 92233++-'$$&6w%GH J
**

>00.))++5:<9M9MO P %/ --//,,.'

#
#%6w$G
H--/'

#
#%6$H
I /B !5H%%99AACCMQ&.qk#gv|"7"I"I&|#Ei #/";";$#=i H 1.)N"9-X	Hn	-	-%%78	
 ??nBBGGG( H>!3DQ3G33N ""1%!4S!9	;q?
 
'
'K(@
A( --
+ .. MM.! "  !E)G3		+W5#$:C
  	n% IM#>?III!9-OOI_ 	s   S5S<:S<)NNT)!r   
__future__r   r   r   apitools.base.pyr   r   googlecloudsdk.api_lib.computer   r   r	   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.utilr
   googlecloudsdk.corer   r   rJ   r   r   r)   r   r/   r4   r6   r8   rB   rO   objectrQ   r  rl   r0   r.   <module>r     s   : &  ' > 7 : @ E 5 # *  #$  A+A+
 ,.A+ #%A+ #%A+" ')#A+* ')+A+2 !3A+:  ;A+B $&CA+J $&KA+R !#SA+Z !#[A+b cA+j kA+r sA+z ,.{A+ 'R .'* &236@<,yF yD Dr0   