
    z6                     p   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	\R                  5      r " S
 S\R                  5      r " S S\5      r " S S\5      r " S S\R                  5      r " S S\R                  5      r " S S\5      r " S S\R(                  5      rg)z"Pollers for Serverless operations.    )absolute_import)division)print_function)unicode_literals)waiter)
exceptionsc                   0    \ rS rSrSrS rS rS rS rSr	g)	!DomainMappingResourceRecordPoller   z8Poll for when a DomainMapping first has resourceRecords.c                     Xl         g N_ops)selfopss     0lib/googlecloudsdk/command_lib/run/op_pollers.py__init__*DomainMappingResourceRecordPoller.__init__    s    I    c                 |    [        UR                  SS 5      (       a  gUR                  nU(       a  US   S   SL a  gg)NresourceRecordsTReadystatusF)getattrr   
conditions)r   mappingr   s      r   IsDone(DomainMappingResourceRecordPoller.IsDone#   s@    w~~0$77##J j)(3u<r   c                     U$ r    )r   r   s     r   	GetResult+DomainMappingResourceRecordPoller.GetResult.   s    Nr   c                 8    U R                   R                  U5      $ r   )r   GetDomainMapping)r   domain_mapping_refs     r   Poll&DomainMappingResourceRecordPoller.Poll1   s    99%%&899r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r   r!   r&   __static_attributes__r    r   r   r
   r
      s    @	:r   r
   c                   l    \ 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Srg)ConditionPoller5   zA poller for CloudRun resource creation or update.

Takes in a reference to a StagedProgressTracker, and updates it with progress.
Nc                 `   U Vs0 s H  oU[        5       _M     snU l        UbJ  U HD  nX5    Vs1 s H$  nXb;   d  M
  UR                  U5      (       a  M"  UiM&     snU R                  U'   MF     Xl        X l        [
        R                  U l        X@l        U R                  5         gs  snf s  snf )a  Initialize the ConditionPoller.

Start any unblocked stages in the tracker immediately.

Arguments:
  resource_getter: function, returns a resource with conditions.
  tracker: a StagedProgressTracker to keep updated. It must contain a stage
    for each condition in the dependencies map, if the dependencies map is
    provided.  The stage represented by each key can only start when the set
    of conditions in the corresponding value have all completed. If a
    condition should be managed by this ConditionPoller but depends on
    nothing, it should map to an empty set. Conditions in the tracker but
    *not* managed by the ConditionPoller should not appear in the dict.
  dependencies: Dict[str, Set[str]], The dependencies between conditions
    that are managed by this ConditionPoller. The values are the set of
    conditions that must become true before the key begins being worked on
    by the server.  If the entire dependencies dict is None, the poller will
    assume that all keys in the tracker are relevant and none have
    dependencies.
  ready_message: str, message to display in header of tracker when
    conditions are ready.
N)
set_dependencies
IsComplete_resource_getter_trackerr   Error_resource_fail_type_ready_message_StartUnblocked)r   resource_gettertrackerdependenciesready_messagekcs          r   r   ConditionPoller.__init__;   s    6 -44GqSU(G4D!
 "_!
$| $+$6$6q$9 $!
1  ,M)//D' 5
!
s   B&	B+B+B+c                 J    XR                   ;   =(       a    U R                   U   $ r   )r3   )r   	conditions     r   
_IsBlockedConditionPoller._IsBlockedf   s!    ***Lt/A/A)/LLr   c                 *    Uc  gUR                  5       $ )z~Overrides.

Args:
  conditions: A condition.Conditions object.

Returns:
  a bool indicates whether `conditions` is terminal.
F)
IsTerminalr   r   s     r   r   ConditionPoller.IsDonei   s       ""r   c                 $   UR                  5        H|  nX0R                  ;  a  M  X   S   nX   S   nU R                  X4U5        Uc  M9  U(       a+  U R                  X45      (       a  U R	                  X5          g Mk  U R                  X45        M~     g )Nmessager   )TerminalSubconditionsr3   _PossiblyUpdateMessage_PossiblyCompleteStage_PollTerminalSubconditions_PossiblyFailStage)r   r   conditions_messagerC   rK   r   s         r   rO   *ConditionPoller._PollTerminalSubconditionsv   s    557		,,	,%i0g$X.f
!!)6HI	&&y:: 
)
)*
I
 ; 		3! 8r   c                 l   U R                  5       nUb  UR                  5       (       d  gUR                  5       nU R                  R	                  U5        U R                  X#5        UR                  5       nUR                  5       (       ab  U R                  R	                  U R                  5        X@R                  ;   a  U R                  US5        U R                  R                  5         U$ UR                  5       (       a2  X@R                  ;   a  U R                  US5        U R                  U5      eU$ )zOverrides.

Args:
  unused_ref: A string representing the operation reference. Currently it
    must be 'deploy'.

Returns:
  A condition.Conditions object.
N)GetConditionsIsFreshDescriptiveMessager6   UpdateHeaderMessagerO   TerminalConditionIsReadyr9   r3   rN   TickIsFailedrP   r8   )r   
unused_refr   rQ   terminal_conditions        r   r&   ConditionPoller.Poll   s
    ##%J!3!3!5!5#668MM%%&89##JC#557
mm''(;(;<	11	1##$6=
mm  
					11	1 2D9$$%788r   c                 "    U R                  5       $ r   )r5   )r   s    r   GetResourceConditionPoller.GetResource   s      ""r   c                     XR                   ;  d   U R                   R                  U5      (       a  gU R                  U5      (       a  gX#:w  a  U R                   R                  X5        gg)zUpdate the stage message.

Args:
  condition: str, The name of the status condition.
  message: str, The new message to display
  conditions_message: str, The message from the conditions object we're
    displaying..
N)r6   r4   rD   UpdateStage)r   rC   rK   rQ   s       r   rM   &ConditionPoller._PossiblyUpdateMessage   sT     %)A)A))L)Ly!!$
mm	3 %r   c                 h    U R                   R                  5        H  nUR                  U5        M     g)zCTake care of the internal-to-this-class bookkeeping stage complete.N)r3   valuesdiscard)r   rC   requirementss      r   _RecordConditionComplete(ConditionPoller._RecordConditionComplete   s+     **1139% 4r   c                    XR                   ;  d   U R                   R                  U5      (       a  gU R                   R                  U5      (       d  gU R                  U5        U R	                  5         U R                   R                  X5        g)a<  Complete the stage if it's not already complete.

Make sure the necessary internal bookkeeping is done.

Args:
  condition: str, The name of the condition whose stage should be completed.
  message: str, The detailed message for the condition.

Returns:
  bool: True if stage was completed, False if no action taken
FT)r6   r4   	IsRunningri   r:   CompleteStager   rC   rK   s      r   rN   &ConditionPoller._PossiblyCompleteStage   sn     %)A)A))L)L ==""9--!!),MM	3r   c                 *   U R                    Hi  nXR                  ;  a  M  U R                  R                  U5      (       d  M6  U R                  U5      (       a  MN  U R                  R	                  U5        Mk     U R                  R                  5         g)zCall StartStage in the tracker for any not-started not-blocked tasks.

Record the fact that they're started in our internal bookkeeping.
N)r3   r6   	IsWaitingrD   
StartStagerZ   )r   r@   s     r   r:   ConditionPoller._StartUnblocked   sh     	
--			 	 	#	#DOOA,>,>  #	   	MMr   c                     XR                   ;  d   U R                   R                  U5      (       a  gU R                   R                  XR                  U5      U5        g)zPossibly fail the stage.

Args:
  condition: str, The name of the status whose stage failed.
  message: str, The detailed message for the condition.

Raises:
  DeploymentFailedError: If the 'Ready' condition failed.
N)r6   r4   	FailStager8   rn   s      r   rP   "ConditionPoller._PossiblyFailStage   sG     %)A)A))L)LMM++G4gr   c                     U$ )zOverrides.

Get terminal conditions as the polling result.

Args:
  conditions: A condition.Conditions object.

Returns:
  A condition.Conditions object.
r    rH   s     r   r!   ConditionPoller.GetResult  s
     r   c                 B    U R                  5       nUc  gUR                  $ zlReturns the resource conditions wrapped in condition.Conditions.

Returns:
  A condition.Conditions object.
N)r5   r   )r   resources     r   rT   ConditionPoller.GetConditions  s'     $$&Hr   )r3   r9   r8   r5   r6   )NzDone.)r(   r)   r*   r+   r,   r   rD   r   rO   r&   r`   rM   ri   rN   r:   rP   r!   rT   r-   r    r   r   r/   r/   5   sR     HO)VM#4&B#4$&4$
r   r/   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )ServiceConditionPolleri  zA ConditionPoller for services.c                 P   > [         TU ]  XU5        [        R                  U l        g r   superr   serverless_exceptionsDeploymentFailedErrorr8   )r   getterr<   r=   serv	__class__s        r   r   ServiceConditionPoller.__init__        	GVl34JJDr   r8   NNr(   r)   r*   r+   r,   r   r-   __classcell__r   s   @r   r~   r~     s    'K Kr   r~   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )WorkerPoolConditionPolleri%  z#A ConditionPoller for worker pools.c                 P   > [         TU ]  XU5        [        R                  U l        g r   r   )r   r   r<   r=   worker_poolr   s        r   r   "WorkerPoolConditionPoller.__init__(  r   r   r   r   r   r   s   @r   r   r   %  s    +K Kr   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	RevisionNameBasedPolleri-  z3Poll for the revision with the given name to exist.c                     Xl         X l        g r   _operations_revision_ref_getter)r   
operationsrevision_ref_getters      r   r    RevisionNameBasedPoller.__init__0  s    ! 3r   c                     [        U5      $ r   boolr   revision_objs     r   r   RevisionNameBasedPoller.IsDone4  s    r   c                 Z    U R                  U5      nU R                  R                  U5      $ r   )r   r   GetRevision)r   revision_namerevision_refs      r   r&   RevisionNameBasedPoller.Poll7  s)    ,,];L''55r   c                     U$ r   r    r   s     r   r!   !RevisionNameBasedPoller.GetResult;  s    r   r   N
r(   r)   r*   r+   r,   r   r   r&   r!   r-   r    r   r   r   r   -  s    ;46r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	NonceBasedRevisionPolleri?  z@To poll for exactly one revision with the given nonce to appear.c                     Xl         X l        g r   )r   
_namespace)r   r   namespace_refs      r   r   !NonceBasedRevisionPoller.__init__B  s    !#Or   c                     [        U5      $ r   r   r   	revisionss     r   r   NonceBasedRevisionPoller.IsDoneF  s    	?r   c                 N    U R                   R                  U R                  U5      $ r   )r   GetRevisionsByNoncer   )r   nonces     r   r&   NonceBasedRevisionPoller.PollI  s    //GGr   c                 ,    [        U5      S:X  a  US   $ g )N   r   )lenr   s     r   r!   "NonceBasedRevisionPoller.GetResultL  s    
9~q\r   )r   r   Nr   r    r   r   r   r   ?  s    H$Hr   r   c                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )ExecutionConditionPolleriR  zA ConditionPoller for jobs.c                 \   > [         TU ]  XU5        [        R                  U l        X0l        g r   )r   r   r   ExecutionFailedErrorr8   _terminal_condition)r   r   r<   r]   r=   r   s        r   r   !ExecutionConditionPoller.__init__U  s&    	GVl34IID1r   c                    UR                  5       nX0R                  ;  d  U R                  U5      (       a  gU R                  R                  USR	                  UR
                  R                  =(       d    SUR                  5      5        g)zMMaybe update the terminal condition stage message with number of completions.Nz{} / {} completer   )rX   r6   rD   rc   formatr   succeededCount
task_count)r   job_objr   r]   s       r   %_PotentiallyUpdateInstanceCompletions>ExecutionConditionPoller._PotentiallyUpdateInstanceCompletionsZ  sp    #557.$//3 3 MM!!NN)).Q0B0B	
r   c                     U R                  5       nUc  gUR                  U R                  5      nU R                  X5        U$ rz   )r5   rT   r   r   )r   r   r   s      r   rT   &ExecutionConditionPoller.GetConditionsi  sF     ##%G&&t'?'?@J 	..wCr   )r8   r   r   )
r(   r)   r*   r+   r,   r   r   rT   r-   r   r   s   @r   r   r   R  s    #2
 r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )WaitOperationPolleri  z<Poll for a long running operation using Wait instead of Get.c                 2   > [         TU ]  " U0 UD6  Xl        g r   )r   r   _messages_module)r   messages_moduleargskwargsr   s       r   r   WaitOperationPoller.__init__  s    	Gd%f%+r   c                     U R                   R                  S5      nU R                  R                  SS9nU R                   R	                  U" UR                  5       US95      $ )zrOverrides.

Args:
  operation_ref: googlecloudsdk.core.resources.Resource.

Returns:
  fetched operation message.
Wait10s)timeout)name%googleLongrunningWaitOperationRequest)operation_serviceGetRequestTyper   %GoogleLongrunningWaitOperationRequestr   RelativeName)r   operation_refrequest_typewait_reqs       r   r&   WaitOperationPoller.Poll  sk     ))88@L$$JJ K H !!&&++-2:	
 r   )r   )	r(   r)   r*   r+   r,   r   r&   r-   r   r   s   @r   r   r     s    D, r   r   N)r,   
__future__r   r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.runr   r   googlecloudsdk.coreOperationPollerr
   r/   r~   r   r   r   r   CloudOperationPollerr   r    r   r   <module>r      s   " ) &  % ' . N *:(>(> :0ef,, ePK_ KK Kf44 $v55 &+ +\&55 r   