
                            d Z ddlmZ ddlmZ ddlmZ ddl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dlmZ ddlZdZdZ G d dej0                        ZefdZefdZefdZefdZd Zd Zd Z d Z!d Z"d Z#d Z$ G d de%      Z& G d de%      Z' G d  d!ej0                        Z(d" Z)d# Z*y)$zCRM API Operations utilities.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)apis)	resources)yaml)progress_tracker)retryv1v3c                       e Zd Zy)OperationErrorN)__name__
__module____qualname__     9lib/googlecloudsdk/api_lib/resource_manager/operations.pyr   r   &   s    r   r   c                 .    t        j                  d|       S Ncloudresourcemanager)r   GetClientInstanceversions    r   OperationsClientr   *       			 6	@@r   c                 f    t         j                  j                         }|j                  d|        |S r   )r	   REGISTRYCloneRegisterApiByName)r   registrys     r   OperationsRegistryr$   .   s,    %%'(
3W=	/r   c                 ,    t        |       j                  S N)r   
operationsr   s    r   OperationsServicer(   4   s    	'	"	-	--r   c                 .    t        j                  d|       S r   )r   GetMessagesModuler   s    r   OperationsMessagesr+   8   r   r   c                     | t        d      d  S )Noperations/)len)operation_names    r   OperationNameToIdr0   <   s    	M*+	,,r   c                 $    dj                  |       S )Nzoperations/{0})formatoperation_ids    r   OperationIdToNamer5   @   s    		 	 	..r   c                 d    t               j                  t               j                  |             S )N)operationsId)r(   Getr+   (CloudresourcemanagerOperationsGetRequestr3   s    r   GetOperationr:   D   s2    			 	 CC# D %
& &r   c                     t        t              j                  t        t              j	                  t        |                   S )N)name)r(   OPERATIONS_API_V3r8   r+   r9   r5   r3   s    r   GetOperationV3r>   J   s?    	,	-	1	1
EE$\2 F 4
5 5r   c                     dj                  | j                        }t        j                  |d      5 }t	               }t        |      }|j                  ||       cd d d        S # 1 sw Y   y xY w)NzWaiting for [{0}] to finishF)autotick)r2   r<   trackerProgressTrackerOperationRetryerOperationPollerRetryPollOperation)	operationwait_messageptretryerpollers        r   WaitForOperationrK   Q   sV    .55innE,|e< GR F%%fi8 =<<s   'A$$A-c                 l    t        j                  | j                        }t        j                  ||      S r&   )r   MessageToDictresponseDictToMessage)rF   response_message_typeraw_dicts      r   ExtractOperationResponserR   Y   s,    ##I$6$67(			*?	@@r   c                     t        j                  |       }t        j                  |t               j                  j
                        S r&   )r   rM   rO   r+   	OperationResponseValue)messagerQ   s     r   ToOperationResponserW   ^   s;    ##G,(			 2 4 > > L L
N Nr   c                   B    e Zd ZdZd ddddfdZd Zd Zd	 Zd
 Zd Z	y)rC   zA wrapper around a Retryer that works with CRM operations.

  Uses predefined constants for retry timing, so all CRM operation commands can
  share their retry timing settings.
  c                  ,    t        j                  d      S )N   )timesleepr   r   r   <lambda>zOperationRetryer.<lambda>l   s    tzz!}r   i  i i N  c                 J    || _         || _        || _        || _        || _        y r&   )_pre_start_sleep_max_retry_ms_max_wait_ms_wait_ceiling_ms_first_retry_sleep_ms)selfpre_start_sleepmax_retry_msmax_wait_mswait_ceiling_msfirst_retry_sleep_mss         r   __init__zOperationRetryer.__init__k   s,     ,D%D#D+D!5Dr   c                     | j                          | j                         j                  fd| j                  | j                        S )Nc                  &    j                         S r&   )Poll)rF   operation_pollers   r   r]   z5OperationRetryer.RetryPollOperation.<locals>.<lambda>z   s     %%i0r   )should_retry_ifsleep_ms)r_   _RetryerRetryOnResult_ShouldRetryrc   )rd   rn   rF   s    ``r   rE   z#OperationRetryer.RetryPollOperationw   sB    ==?((0))++ ) - -r   c                 Z    t        j                  d| j                  | j                        S )N   )exponential_sleep_multiplierrg   rh   )r   Retryerra   rb   )rd   s    r   rq   zOperationRetryer._Retryer~   s(    ==%&%%--/ /r   c                 |    t        |t        j                        r| j                  ||      S | j	                  |      S r&   )
isinstancer   	HttpError_CheckTimePassedBelowMax_CheckResultNotExceptionrd   resultstates      r   rs   zOperationRetryer._ShouldRetry   s6    &*../**6599((00r   c                 :    |j                   | j                  kD  r|y)NT)time_passed_msr`   r}   s      r   r{   z)OperationRetryer._CheckTimePassedBelowMax   s    d000lr   c                 @    t        |t              r||j                   S r&   )ry   	Exceptiondone)rd   r~   s     r   r|   z)OperationRetryer._CheckResultNotException   s    &)$l{{?r   N)
r   r   r   __doc__rj   rE   rq   rs   r{   r|   r   r   r   rC   rC   d   s6      5 !$$(
6-/1

r   rC   c                       e Zd ZddZd Zy)rD   Nc                     || _         y r&   )_progress_tracker)rd   r   s     r   rj   zOperationPoller.__init__   s
    -Dr   c                     | j                   r| j                   j                          t        t        |j                              }|j
                  r|j                  rt        |      |S r&   )r   Tickr:   r0   r<   r   errorOperationFailedException)rd   rF   latests      r   rm   zOperationPoller.Poll   sN    
!!#+INN;<F{{v||$V,,Mr   r&   )r   r   r   rj   rm   r   r   r   rD   rD      s    .r   rD   c                   "     e Zd ZdZ fdZ xZS )r   z Exception for failed operations.c                    t        |j                        }|j                  j                  }|j                  j                  }dj                  |||      }	 t        j                  |j                        }|j                  dg       }|r|dt        j                  |      z   z  }t        t         | G  |       y # t        t        t        j                  j                  f$ r}|d| dz  }Y d }~Jd }~ww xY w)Nz Operation [{0}] failed: {1}: {2}details
z+
(Failed to parse or format error details: ))r0   r<   r   coderV   r2   r   MessageToPyValuegetr
   dump	TypeErrorAttributeErrorruamel	YAMLErrorsuperr   rj   )
rd   operation_with_errorop_id
error_codeerror_messagerV   error_py_valuer   e	__class__s
            r   rj   z!OperationFailedException.__init__   s    2778E%++00J(..66M077z8EGGE001E1K1KLn""9b1g	4$))G,,, 

"D27; ~v{{'<'<= E?s!DDgEs   AB9 9(C4!	C//C4)r   r   r   r   rj   __classcell__)r   s   @r   r   r      s    (< <r   r   c                     t        | j                        }t               j                  dd|id      }|j	                         S )zReturns the uri for resource.Nr7   zcloudresourcemanager.operations)params
collection)r0   r<   r$   ParseSelfLink)resourcer4   operation_refs      r   GetUrir      sG    "8==1,$&,,
l+2 - 4- 
			!!r   c           	          g }|D ];  }|j                  t        j                  ||j                  j                               = |j                  d| z   d|j                  |||            S )z.Returns a failed operation with error details.r-   T)r   rV   r   )r<   r   r   )appendr   rO   StatusDetailsValueListEntryrT   )r/   messageserror_detailsr   r   details_messagesitems          r   GetFailedOperationr      s     d tX__%J%JKM  
		>)OO=:J  L 
 
M Mr   )+r   
__future__r   r   r   r[   apitools.base.pyr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corecore_exceptionsr	   r
   googlecloudsdk.core.consoler   rA   googlecloudsdk.core.utilr   ruamel.yamlr   OPERATIONS_API_V1r=   Errorr   r   r$   r(   r+   r0   r5   r:   r>   rK   rR   rW   objectrC   rD   r   r   r   r   r   r   <module>r      s    $ &  '  % ' , = ) $ C *    Z%%  / A  1  0 .  1 A-/&59A
N-v -`f <44 <*"Mr   