
    E                         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Zedz   Zedz   ZdZd Zd Zd Zd Zd Zd Zy)z,Common helper methods for Services commands.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)apis_internal)log)
properties)	transport)retryzgcloud services operations zdescribe {0}zwait {0}zservicemanagement.servicesc                  .    t        j                  dd      S )Nservicemanagementv1)r   _GetMessagesModule     4lib/googlecloudsdk/api_lib/services/services_util.pyGetMessagesModuler   '   s    		)	)*=t	DDr   c                      ddl m}  t        j                  j                  j
                  j                         }| j                  t        j                  |      }t        j                  dd|      S )zDGet a client instance for service management without resource quota.r   )
transports)response_encodingenable_resource_quotar   r   )http_client)googlecloudsdk.core.credentialsr   r
   VALUESbillingquota_projectIsExplicitlySetGetApitoolsTransportr   ENCODINGr   _GetClientInstance)r   r   r   s      r   GetClientInstancer"   ,   si     9 --==? //!**1 0 3+ 
	)	)4[
: :r   c                     | r5t         j                  j                  j                  j	                  |        | S t         j                  j                  j                  j                  d      } | S )zValidate the project ID, if supplied, otherwise return the default project.

  Args:
    project_id: The ID of the project to validate. If None, gcloud's default
                project's ID will be returned.

  Returns:
    The validated project ID.
  T)required)r
   r   coreprojectValidateGet)
project_ids    r   GetValidatedProjectr*   @   s\     ""++J7 
 ""''//33T3BJ	r   c                     fd}g }	 t        j                  ddd      j                  || |gdd       |r|d
   S dS # t         j                  $ r% t	        j
                  d	j                  |             w xY w)aZ  Wait till the operation is done.

  Args:
    name: The name of operation.
    get_op_func: The function that gets the operation.

  Raises:
    exceptions.OperationErrorException: when the getting operation API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  c                 h     |       }|j                   r|j                  |       |j                    S )N)doneappend)nameresultopget_op_funcs      r   _CheckOpzWaitOperation.<locals>._CheckOp`   s,    	T	B	wwmmBww;r   g?i'  i@w )exponential_sleep_multiplierwait_ceiling_msmax_wait_msTi  )should_retry_ifsleep_mszTTimed out while waiting for operation {0}. Note that the operation is still pending.r   N)r   RetryerRetryOnResultMaxRetrialsExceptionr   TimeoutErrorformat)r/   r2   r3   r0   s    `  r   WaitOperationr>   Q   s     &	D	MM%("$ %2MtVndT %2 %K &$&	 
	#	# D

!
! #66<fTlD DDs   -A   8A8c                    | j                   s9t        j                  j                  dj	                  | j
                               y| j                  rXt        j                  dj	                  | j
                  | j                  j                  | j                  j                              t        j                  j                  dj	                  | j
                               y)zPrint the operation.

  Args:
    op: The long running operation.

  Raises:
    OperationErrorException: if the operation fails.

  Returns:
    Nothing.
  %Operation "{0}" is still in progress.N@The operation "{0}" resulted in a failure "{1}".
Details: "{2}".&Operation "{0}" finished successfully.)r-   r	   statusPrintr=   r/   errorr   OperationErrorExceptionmessagedetailsr1   s    r   PrintOperationrJ   u   s     
JJ<CCBGGLM
XX

,
,Krww(("((*:*:;= = **;BB277KLr   c                    | j                   s9t        j                  j                  dj	                  | j
                               y| j                  rXt        j                  dj	                  | j
                  | j                  j                  | j                  j                              | j                  rnt        j                  j                  dj	                  | j
                  t        j                  t        j                  | j                        ddd                   yt        j                  j                  d	j	                  | j
                               y)
zPrint the operation with response.

  Args:
    op: The long running operation.

  Raises:
    OperationErrorException: if the operation fails.

  Returns:
    Nothing.
  r@   NrA   z%Operation [{0}] complete. Result: {1}T   ),:)	sort_keysindent
separatorsrB   )r-   r	   rC   rD   r=   r/   rE   r   rF   rG   rH   responsejsondumpsr   MessageToDictrI   s    r   PrintOperationWithResponserV      s     
JJ<CCBGGLM
XX

,
,Krww(("((*:*:;= = [[JJ<CC


""2;;/!		#$ % JJ=DDRWWMNr   )__doc__
__future__r   r   r   rS   apitools.base.pyr   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer	   r
   r   googlecloudsdk.core.utilr   OP_BASE_CMDOP_DESCRIBE_CMDOP_WAIT_CMDSERVICES_COLLECTIONr   r"   r*   r>   rJ   rV   r   r   r   <module>rb      sn     3 &  '  % 6 5 # * ) * ,.J&2 E
:("!'HM,Or   