
    C                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ	 ddl
mZ ddl
mZ d	Zd
ZdZd Z	 	 	 ddZ	 ddZ	 	 	 ddZy)z3A library used to interact with Operations objects.    )absolute_import)division)unicode_literals)
exceptions)progress_tracker)encoding)retryi` i  i  c                 t    dj                  | j                  t        j                  | j                              S )zReturns a human readable string representation from the operation.

  Args:
    error: A string representing the raw json of the operation error.

  Returns:
    A human readable string representation of the error.
  z%OperationError: code={0}, message={1})formatcoder   Decodemessage)errors    5lib/googlecloudsdk/api_lib/functions/v1/operations.pyOperationErrorToStringr       s-     
1	7	7jj(//%--0
     Nc                    |r |        |r|j                          | j                  j                  |      }|j                  r(t	        j
                  t        |j                              |r|D ]
  } ||        |j                  S )a  Helper function for getting the status of an operation.

  Args:
    client: The client used to make requests.
    get_request: A GetOperationRequest message.
    progress_tracker: progress_tracker.ProgressTracker, A reference for the
      progress tracker to tick, in case this function is used in a Retryer.
    try_set_invoker: function to try setting invoker, see above TODO.
    on_every_poll: list of functions to execute every time we poll. Functions
      should take in Operation as an argument.

  Returns:
    True if the operation succeeded without error.
    False if the operation is not yet done.

  Raises:
    FunctionsError: If the operation is finished with error.
  )Tick
operationsGetr   r   FunctionsErrorr   done)clientget_requestr   try_set_invokeron_every_pollopfunctions          r   _GetOperationStatusr   /   sq    2 [)"XX

#
#$:288$D
EE!rl "	.r   c           	      t   t        j                  |d      5 }t        j                  dt        t
              }	 |j                  t        | |g|||dd t               	 d	d	d	       y	# t        j                  $ r/ t        j                  dj                  |j                              w xY w# 1 sw Y   y	xY w)
aB  Wait for an operation to complete.

  No operation is done instantly. Wait for it to finish following this logic:
  * we wait 1s (jitter is also 1s)
  * we query service
  * if the operation is not finished we loop to first point
  * wait limit is 1820s - if we get to that point it means something is wrong
        and we can throw an exception

  Args:
    client:  The client used to make requests.
    get_request: A GetOperationRequest message.
    message: str, The string to print while polling.
    try_set_invoker: function to try setting invoker, see above TODO.
    on_every_poll: list of functions to execute every time we poll. Functions
      should take in Operation as an argument.

  Returns:
    True if the operation succeeded without error.

  Raises:
    FunctionsError: If the operation takes more than 1820s.
  F)autotick   )exponential_sleep_multipliermax_wait_mswait_ceiling_ms)r   r   r   c                     |  S )N )r   _s     r   <lambda>z#_WaitForOperation.<locals>.<lambda>   s    d(r   )should_retry_ifsleep_msz Operation {0} is taking too longN)console_progress_trackerProgressTrackerr	   RetryerMAX_WAIT_MSWAIT_CEILING_MSRetryOnResultr   SLEEP_MSWaitExceptionr   r   r   name)r   r   r   r   r   ptretryers          r   _WaitForOperationr7   V   s    6  //%HBmm%&'G

;
"$!0,
 3  
 IH&  %%
,
3
3K4D4D
E ' IHs   !B.%A))AB++B..B7c                 l    |d}|j                         }| j                  |_        t        |||||       y)aV  Initialize waiting for operation to finish.

  Generate get request based on the operation and wait for an operation
  to complete.

  Args:
    operation: The operation which we are waiting for.
    messages: GCF messages module.
    client: GCF client module.
    notice: str, displayed when waiting for the operation to finish.
    try_set_invoker: function to try setting invoker, see above TODO.
    on_every_poll: list of functions to execute every time we poll. Functions
      should take in Operation as an argument.

  Raises:
    FunctionsError: If the operation takes more than 620s.
  NzWaiting for operation to finish)"CloudfunctionsOperationsGetRequestr4   r7   )	operationmessagesr   noticer   r   requests          r   Waitr>      s7    2 ^.F779'',FGV_mLr   )NNN)NN)__doc__
__future__r   r   r   #googlecloudsdk.api_lib.functions.v1r   googlecloudsdk.core.consoler   r,   googlecloudsdk.core.utilr   r	   r/   r0   r2   r   r   r7   r>   r'   r   r   <module>rD      sd    : '  ' : T - *$ #P GK1p Mr   