
    e                         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
Jr  SSKJr  S	rS
r " S S\5      r " S S\5      rg),Common utility functions for sql operations.    )absolute_import)division)unicode_literalsN)
exceptions)progress_tracker)retryi  iX  c                   >    \ rS rSrSrSrSrSrSr\	\
4S j5       rSrg)	_BaseOperations"   r      i  i N  c           
        ^ U4S jnSnU(       a	  U[         -  n[        R                  " USS9 n[        R                  " [
        R                  5        [        R                  " SU[
        R                  S9n UR                  U R                  TU/SU0U[
        R                  S9sSSS5        $ ! [        R                   a5    [        R                  " S	R!                  X R#                  U5      5      5      ef = f! , (       d  f       g= f)
a  Wait for a Cloud SQL operation to complete.

No operation is done instantly. Wait for it to finish following this logic:
First wait 1s, then query, then retry waiting exponentially more from 2s.
We want to limit to 20s between retries to maintain some responsiveness.
Finally, we want to limit the whole process to a conservative 300s. If we
get to that point it means something is wrong and we can throw an exception.

Args:
  sql_client: apitools.BaseApiClient, The client used to make requests.
  operation_ref: resources.Resource, A reference for the operation to poll.
  message: str, The string to print while polling.
  max_wait_seconds: integer or None, the number of seconds before the
      poller times out.

Returns:
  Operation: The polled operation.

Raises:
  OperationError: If the operation has an error code, is in UNKNOWN state,
      or if the operation takes more than max_wait_seconds when a value is
      specified.
c                 .  > [        U [        R                  5      (       a!  UR                  [        R
                  :  a  U eg[        U [        5      (       a  U eU R                  TR                  R                  R                  R                  :H  nU(       + $ )NT)
isinstancebase_exceptions	HttpErrortime_passed_msr   _HTTP_MAX_RETRY_MS	ExceptionstatusMESSAGES_MODULE	OperationStatusValueValuesEnumDONE)resultstateis_operation_done
sql_clients      ,lib/googlecloudsdk/api_lib/sql/operations.pyShouldRetryFunc9_BaseOperations.WaitForOperation.<locals>.ShouldRetryFuncH   st    	FO55	6	6/"D"DD,	FI	&	& --:+E+E+O+O+e+e+j+jj"""    NF)autotick   )exponential_sleep_multipliermax_wait_mswait_ceiling_msr   )should_retry_ifsleep_msziOperation {0} is taking longer than expected. You can continue waiting for the operation by running `{1}`)_MS_PER_SECONDconsole_progress_trackerProgressTrackertimesleepr   _PRE_START_SLEEP_SECr	   Retryer_WAIT_CEILING_MSRetryOnResultGetOperation_INITIAL_SLEEP_MSWaitExceptionr   OperationErrorformatGetOperationWaitCommand)	clsr   operation_refmessagemax_wait_secondsr    r&   ptretryers	    `       r   WaitForOperation _BaseOperations.WaitForOperation*   s    <# K$~5k	!	1	1%
!$&
jj556'(!)::<g
M$$z=9$+$66	 % 8
! 
!    M'':;A6 ; ;M J<LM 	MM
! 
!s   A	C=9.B11A	C::C==
D N)__name__
__module____qualname____firstlineno____doc__r/   r4   r1   r   classmethod_OPERATION_TIMEOUT_SECONDSr?   __static_attributes__rA   r"   r   r   r   "   s7    4
 )C	?M ?Mr"   r   c                   <    \ rS rSrSr\SS j5       r\S 5       rSrg)OperationsV1Beta4m   z4Common utility functions for sql operations V1Beta4.Nc                    U(       a  UR                  5          U R                  R                  U R                  R	                  UR
                  UR                  S95      nUR                  (       a  UR                  R                  (       am  UR                  R                  S   nSR                  UR                  5      nUR                  (       a  USUR                  -   -  n[        R                  " U5      $ UR                  U R                  R                   R"                  R$                  :X  a   [        R                  " UR                  5      $ U$ ! [         a  nUs SnA$ SnAff = f)a\  Helper function for getting the status of an operation for V1Beta4 API.

Args:
  sql_client: apitools.BaseApiClient, The client used to make requests.
  operation_ref: resources.Resource, A reference for the operation to poll.
  progress_tracker: progress_tracker.ProgressTracker, A reference for the
      progress tracker to tick, in case this function is used in a Retryer.

Returns:
  Operation: if the operation succeeded without error or  is not yet done.
  OperationError: If the operation has an error code or is in UNKNOWN state.
  Exception: Any other exception that can occur when calling Get
)project	operationNr   z[{}] )Tick
operationsGetr   SqlOperationsGetRequestrN   rO   r   errorerrorsr7   coder;   r   r6   r   r   r    SQL_OPERATION_STATUS_UNSPECIFIED)r   r:   r   opeerror_objectrU   s          r   r3   OperationsV1Beta4.GetOperationp   s       $$

$
$
<
<#++}7N7N = PQb 
xxBHHOOXX__Q'lmmL--.e			|++++&&u--	yyJ..88NNooo&&ryy11I   h	s   AE 
E)E$E)$E)c                 N    SR                  U R                  U R                  5      $ )Nz1gcloud beta sql operations wait --project {0} {1})r7   rN   rO   )r:   s    r   r8   )OperationsV1Beta4.GetOperationWaitCommand   s&    >EE}668 8r"   rA   )N)	rB   rC   rD   rE   rF   staticmethodr3   r8   rI   rA   r"   r   rK   rK   m   s,    <# #J 8 8r"   rK   )rF   
__future__r   r   r   r-   apitools.base.pyr   r   googlecloudsdk.api_lib.sqlgooglecloudsdk.core.consoler   r+   googlecloudsdk.core.utilr	   r*   rH   objectr   rK   rA   r"   r   <module>rf      sJ    3 &  '  : 1 T *  HMf HMV,8 ,8r"   