o
    <                     @   s   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ed ZdZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )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                   C   s   t ddS )Nservicemanagementv1)r   _GetMessagesModule r   r   J/tmp/google-cloud-sdk/lib/googlecloudsdk/api_lib/services/services_util.pyGetMessagesModule'   s   r   c                  C   s:   ddl m}  tjjj }| jtj	|d}t
jd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   r   r   r   GetClientInstance,   s   	r   c                 C   s.   | rt jjj|  | S t jjjjd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_idr   r   r   GetValidatedProject@   s
   
r%   c                    sh    fdd}g }zt jddddj|| |gddd	 W n t jy+   td
| w |r2|d S dS )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                    s     | }|j r|| |j  S )N)doneappend)nameresultopget_op_funcr   r   _CheckOp`   s   
zWaitOperation.<locals>._CheckOpg?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(   r,   r-   r)   r   r+   r   WaitOperationQ   s"   
r8   c                 C   sZ   | j stjd| j dS | jr!td| j| jj	| jj
tjd| j dS )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Printr7   r(   errorr   OperationErrorExceptionmessagedetailsr*   r   r   r   PrintOperationu   s   rC   c                 C   s   | j stjd| j dS | jr!td| j| jj	| jj
| jr<tjd| jtjt| jdddd dS tjd	| j dS )
zPrint the operation with response.

  Args:
    op: The long running operation.

  Raises:
    OperationErrorException: if the operation fails.

  Returns:
    Nothing.
  r9   Nr:   %Operation [{0}] complete. Result: {1}T   ,:	sort_keysindent
separatorsr;   )r&   r   r<   r=   r7   r(   r>   r   r?   r@   rA   responsejsondumpsr   MessageToDictrB   r   r   r   PrintOperationWithResponse   s&   


rQ   c                 C   s   | j stjd| j dS | jr&| jj}|dd}t	
d| j|| jrAtjd| jtjt| jddd	d
 dS tjd| j dS )zPrint the operation with response for update consumer policy.

  Args:
    op: The long running operation.

  Raises:
    OperationErrorException: if the operation fails.

  Returns:
    Nothing.
  r9   NzPlease specify forcez,Please specify --bypass-api-usage-check flagz/The operation "{0}" resulted in a failure "{1}"rD   TrE   rF   rI   r;   )r&   r   r<   r=   r7   r(   r>   r@   replacer   r?   rM   rN   rO   r   rP   )r*   error_messagemodified_messager   r   r   1PrintOperationWithResponseForUpdateConsumerPolicy   s4   
rU   )__doc__
__future__r   r   r   rN   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%   r8   rC   rQ   rU   r   r   r   r   <module>   s.   $