
    o)                        S 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\R*                  5      r " S S\R*                  5      r " S S\R*                  5      r " S S\R2                  5      r " S S\5      rS rS r " S S\R>                  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.
    N)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   D   s    'c(:;<    c                       \ rS rSrSrSrg)r   I   z;Wrapper exception for errors treated as operation failures. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r$   r!   r   r   r   I   s    Cr!   r   c                       \ rS rSrSrg)r   M   r$   Nr%   r&   r'   r(   r*   r$   r!   r   r   r   M       r!   r   c                       \ rS rSrSrg)r   Q   r$   Nr-   r$   r!   r   r   r   Q   r.   r!   r   c                        \ rS rSrSrSrSrSrg)StatusU            r$   N)r%   r&   r'   r(   PENDING	COMPLETEDERRORr*   r$   r!   r   r2   r2   U   s    ')
%r!   r2   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__f   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;   rC   rE   rG   rI   rJ   )rK   others     r   __eq__Operation.__eq__p   s    ui( 2LLEMM)2GGuxx2 OOu///2 KK5<<'	2
  1 113r!   )rE   rJ   rC   rG   rI   N)r%   r&   r'   r(   r)   rN   rT   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.
)doner2   r7   rA   errorr9   r8   )	operations    r   rH   rH   y   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)rY   
propertiesprops      r   rF   rF      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)rK   rh   ri   s      r   rN   !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)_LogNewWarningsrW   r   debugformatrA   jsondumpsr   MessageToDictrX   r   r   ExtractErrorMessageMessageToPyValuerK   rY   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)rA   )rh   GetRequestTypeRelativeNamer|   rp   )rK   operation_refrequest_typerequestrY   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
)ri   GetWarningsFromOperationrk   r   warningadd)rK   rY   new_warningsr   s       r   rp   (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$   rx   s     r   	GetResult"AppEngineOperationPoller.GetResult   s
     r!   )ri   rh   rk   rQ   )r%   r&   r'   r(   r)   rN   ry   r   rp   r   r*   r$   r!   r   rd   rd      s    *(	r!   rd   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rg   )superr   rN   )rK   rh   ri   	__class__s      r   rN   &AppEngineOperationBuildPoller.__init__   s     

'78I8OQr!   c                 `   > [        XR                  5      (       a  g[        [        U ]  U5      $ )NT)GetBuildFromOperationri   r   r   ry   )rK   rY   r   s     r   ry   $AppEngineOperationBuildPoller.IsDone   s*    Y(D(DEE.<YGGr!   r$   )	r%   r&   r'   r(   r)   rN   ry   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      $ rQ   )r\   r   JsonToMessageMessageToJson)rY   ri   s     r   GetMetadataFromOperationr      s8    						Y//0
2 2r!   c                 v    [        X5      nU(       a  UR                  (       d  g UR                  R                  $ rQ   )r   createVersionMetadatacloudBuildIdrY   ri   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rQ   r$   ).0r   s     r   	<genexpr>+GetWarningsFromOperation.<locals>.<genexpr>  s     5$4W$4s   )r   rj   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

r4   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.)rd   ry   r   r   r?   r@   rA   DEFAULT_OPERATION_MAX_TRIES DEFAULT_OPERATION_RETRY_INTERVALrr   r~   r   WaitForTimeoutErrorr   )	rh   rY   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)NNr>   NN)&r)   rs   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   Enumr2   objectr;   rH   rF   OperationPollerrd   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!   