
    )                         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
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\-  S-  rS rS r " S S\R2                  5      r " S S\R2                  5      r " S S\R2                  5      r " S S\
R:                  5      r " S S\5      r S r!S r" " S S\RF                  5      r$ " S S\$5      r%S  r&S! r'S" r(     S$S# jr)g)%zNUtilities for working with long running operations go/long-running-operation.
    )absolute_import)division)unicode_literalsN)encoding)
exceptions)requests)waiter)log)	resources   <   c                      U " U0 UD6$ ! [         R                   a)  n[        [        R                  " U5      5         SnAgSnAf[
        [        [        R                  4 a  n[        U5         SnAgSnAff = f)as  Wrapper for method(...) which re-raises operation-style errors.

Args:
  method: Original method to call.
  *args: Positional arguments to method.
  **kwargs: Keyword arguments to method.

Raises:
  MiscOperationError: If the method call itself raises one of the exceptions
    listed below. Otherwise, the original exception is raised. Preserves
    stack trace. Re-uses the error string from original error or in the case
    of HttpError, we synthesize human-friendly string from HttpException.
    However, HttpException is neither raised nor part of the stack trace.

Returns:
  Result of calling method(*args, **kwargs).
N)	apitools_exceptions	HttpError_ReraiseMiscOperationErrorapi_exceptionsHttpExceptionOperationErrorOperationTimeoutErrorapp_exceptionsError)methodargskwargshttp_errerrs        1lib/googlecloudsdk/api_lib/app/operations_util.pyCallAndCollectOpErrorsr   ,   sf    $$4"6""		&	& G~;;HEFF
/1E1E	F $s##$s    
 A9A"A9$A44A9c                 j    [         R                  " [        [        R                  " U 5      5      5        g)z$Transform and re-raise error helper.N)r   reraiseMiscOperationErrorsix	text_type)r   s    r   r   r   G   s    'c(:;<    c                       \ rS rSrSrSrg)r!   L   z;Wrapper exception for errors treated as operation failures. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r'   r$   r   r!   r!   L   s    Cr$   r!   c                       \ rS rSrSrg)r   P   r'   Nr(   r)   r*   r+   r-   r'   r$   r   r   r   P       r$   r   c                       \ rS rSrSrg)r   T   r'   Nr0   r'   r$   r   r   r   T   r1   r$   r   c                        \ rS rSrSrSrSrSrg)StatusX            r'   N)r(   r)   r*   r+   PENDING	COMPLETEDERRORr-   r'   r$   r   r5   r5   X   s    ')
%r$   r5   c                   $    \ rS rSrSrS rS rSrg)	Operation^   aD  Wrapper around Operation response objects for console output.

Attributes:
  project: String, name of the project.
  id: String, ID of operation.
  start_time: String, time the operation started.
  status: Status enum, either PENDING, COMPLETED, or Error.
  op_resource: messages.Operation, the original Operation resource.
c                     [         R                  R                  UR                  S5      nUR                  U l        UR                  5       U l        [        U5      U l	        [        U5      U l        Xl        g)z%Creates the operation wrapper object.appengine.apps.operationsN)r   REGISTRYParseRelativeNamenameappsIdprojectNameid_GetInsertTime
start_time	GetStatusstatusop_resource)selfop_responseress      r   __init__Operation.__init__i   sZ    



.
.{/?/?/JLC::DLhhjDG$[1DOK(DK"r$   c                 b   [        U[        5      =(       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  $ N)
isinstancer>   rF   rH   rJ   rL   rM   )rN   others     r   __eq__Operation.__eq__s   s    ui( 2LLEMM)2GGuxx2 OOu///2 KK5<<'	2
  1 113r$   )rH   rM   rF   rJ   rL   N)r(   r)   r*   r+   r,   rQ   rW   r-   r'   r$   r   r>   r>   ^   s    #3r$   r>   c                     U R                   (       d  [        R                  R                  $ U R                  (       a  [        R
                  R                  $ [        R                  R                  $ )zReturns string status for given operation.

Args:
  operation: A messages.Operation instance.

Returns:
  The status of the operation in string form.
)doner5   r:   rD   errorr<   r;   )	operations    r   rK   rK   |   sD     
>><<   r$   c                     U R                   (       d  gU R                   R                  nU H+  nUR                  S:X  d  M  UR                  R                  s  $    g)zFinds the insertTime property and return its string form.

Args:
  operation: A messages.Operation instance.

Returns:
  The time the operation started in string form or None if N/A.
N
insertTime)metadataadditionalPropertieskeyvaluestring_value)r\   
propertiesprops      r   rI   rI      sG     
		!!66*dxx<ZZ$$$ r$   c                   :    \ rS rSrSrS
S jrS rS rS rS r	S	r
g)AppEngineOperationPoller   z"A poller for appengine operations.Nc                 :    Xl         X l        [        5       U l        ga<  Sets up poller for appengine operations.

Args:
  operation_service: apitools.base.py.base_api.BaseApiService, api service
    for retrieving information about ongoing operation.
  operation_metadata_type: Message class for the Operation metadata (for
    instance, OperationMetadataV1, or OperationMetadataV1Beta).
N)operation_serviceoperation_metadata_typesetwarnings_seen)rN   rk   rl   s      r   rQ   !AppEngineOperationPoller.__init__   s     /#: Dr$   c           
         U R                  U5        UR                  (       a  [        R                  " SR	                  UR
                  [        R                  " [        R                  " U5      SS95      5        UR                  (       a=  [        [        R                  " [        R                  " UR                  5      5      5      eg[        R                  " SR	                  UR
                  5      5        g)z
Overrides.z%Operation [{0}] complete. Result: {1}   )indentTz/Operation [{0}] not complete. Waiting to retry.F)_LogNewWarningsrZ   r
   debugformatrD   jsondumpsr   MessageToDictr[   r   r   ExtractErrorMessageMessageToPyValuerN   r\   s     r   IsDoneAppEngineOperationPoller.IsDone   s    #~~	ii7>>
..
**X++I6q
AC D 
X99%%ioo68 9 	9II?FF r$   c                     U R                   R                  S5      nU" UR                  5       S9nU R                   R                  U5      nU R	                  U5        U$ )zrOverrides.

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

Returns:
  fetched operation message.
Get)rD   )rk   GetRequestTypeRelativeNamer   rs   )rN   operation_refrequest_typerequestr\   s        r   PollAppEngineOperationPoller.Poll   sW     ))88?L : : <=G&&**73I#r$   c                     U R                   (       a`  [        XR                   5      U R                  -
  nU H7  n[        R                  " US-   5        U R                  R                  U5        M9     g g )N
)rl   GetWarningsFromOperationrn   r
   warningadd)rN   r\   new_warningsr   s       r   rs   (AppEngineOperationPoller._LogNewWarnings   s`    ##-
113595G5GHl!'GdN#w' "	 $r$   c                     U$ )zSimply returns the operation.

Args:
  operation: api_name_messages.Operation.

Returns:
  the 'response' field of the Operation.
r'   r{   s     r   	GetResult"AppEngineOperationPoller.GetResult   s
     r$   )rl   rk   rn   rT   )r(   r)   r*   r+   r,   rQ   r|   r   rs   r   r-   r'   r$   r   rg   rg      s    *(	r$   rg   c                   8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )AppEngineOperationBuildPoller   z@Waits for a build to be present, or for the operation to finish.c                 .   > [         [        U ]  UU5        grj   )superr   rQ   )rN   rk   rl   	__class__s      r   rQ   &AppEngineOperationBuildPoller.__init__   s     

'78I8OQr$   c                 `   > [        XR                  5      (       a  g[        [        U ]  U5      $ )NT)GetBuildFromOperationrl   r   r   r|   )rN   r\   r   s     r   r|   $AppEngineOperationBuildPoller.IsDone   s*    Y(D(DEE.<YGGr$   r'   )	r(   r)   r*   r+   r,   rQ   r|   r-   __classcell__)r   s   @r   r   r      s    H
QH Hr$   r   c                     U R                   (       d  g [        R                  " U[        R                  " U R                   5      5      $ rT   )r_   r   JsonToMessageMessageToJson)r\   rl   s     r   GetMetadataFromOperationr      s8    						Y//0
2 2r$   c                 v    [        X5      nU(       a  UR                  (       d  g UR                  R                  $ rT   )r   createVersionMetadatacloudBuildIdr\   rl   r_   s      r   r   r      s,    %iI(	77		'	'	4	44r$   c                 r    [        X5      nU(       d
  [        5       $ [        S UR                   5       5      $ )Nc              3   $   #    U  H  ov   M     g 7frT   r'   ).0r   s     r   	<genexpr>+GetWarningsFromOperation.<locals>.<genexpr>	  s     5$4W$4s   )r   rm   r   r   s      r   r   r     s-    %iI(	5L	5H$4$45	55r$   c           
         U=(       d    [        U 5      nUR                  U5      (       a  UR                  U5      $ [        R                  R                  UR                  U5      nUc	  [        S-
  nUc  [        nUc  SR                  UR                  5       5      nUS-  n [        R                  " UUUSUSUS9nU$ ! [        R                   a%    [        SR                  UR                  5      5      ef = f)a  Wait until the operation is complete or times out.

Args:
  operation_service: The apitools service type for operations
  operation: The operation resource to wait on
  max_retries: Maximum number of times to poll the operation
  retry_interval: Frequency of polling in seconds
  operation_collection: The resource collection of the operation.
  message: str, the message to display while progress tracker displays.
  poller: AppEngineOperationPoller to poll with, defaulting to done.
Returns:
  The operation resource when it has completed
Raises:
  OperationError: if the operation contains an error.
  OperationTimeoutError: when the operation polling times out

r7   z&Waiting for operation [{}] to completei  g      ?)pre_start_sleep_msmax_retrialsexponential_sleep_multipliersleep_msz@Operation [{0}] timed out. This operation may still be underway.)rg   r|   r   r   rB   rC   rD   DEFAULT_OPERATION_MAX_TRIES DEFAULT_OPERATION_RETRY_INTERVALru   r   r	   WaitForTimeoutErrorr   )	rk   r\   max_retriesretry_intervaloperation_collectionmessagepollerr   completed_operations	            r   WaitForOperationr     s
   . @-.?@&]]9I&&$$66nn- -1K5N_6==""$&G D.6 .. %(! 
	 
		 6
 ":;A6%.^^<56 66s   #B? ?9C8)NNrA   NN)*r,   
__future__r   r   r   rv   apitools.base.pyr   r   r   enumgooglecloudsdk.api_lib.appr   googlecloudsdk.api_lib.utilr   r   r	   googlecloudsdk.corer
   r   r"   r   r   r   r   r   r!   r   r   Enumr5   objectr>   rK   rI   OperationPollerrg   r   r   r   r   r   r'   r$   r   <module>r      s    '  '  % >  C D 0 . * # ) 
 $%  !%EEK $6=
D)) DZ%% J,, TYY 3 3<!"%"@v55 @FH$< H*256 "&$(*E! 3r$   