
    }                         d Z ddlmZ ddlmZ ddlmZ ddl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Zd Zd Z	 	 ddZy)z%Base functions for DM write commands.    )absolute_import)division)unicode_literalsN)
exceptions)dm_util)log)
properties)progress_tracker)resource_printerc                     ||      }|sz|j                  |j                  dt        j                  j                  j
                  j                  id      }t        | ||j                  |j                  |j
                        } |||       |r.t        j                  dj                  |j                               |S )a  Executes the request, managing asynchronous behavior.

  Args:
    client: The API client to use.
    messages: The API message to use.
    resources: The API resource to use.
    request: The request to pass call.
    is_async: False if this call should poll for the Operation's success.
    call: Function that calls the appropriate API.
    logger: The log function to use for the operation. Should take the request
        and a boolean arg for async.

  Returns:
    The API response, probably an operation.

  Raises:
    HttpException: An http error response was received while executing api
        request.
    ToolException: Call encountered an error.
  projectzdeploymentmanager.operations)params
collection)r   zOperation [{0}] running....)Parsenamer	   VALUEScorer   	GetOrFailWaitForOperation	operationoperationTyper   Printformat)	clientmessages	resourcesrequestis_asynccallloggerresponseoperation_refs	            =lib/googlecloudsdk/command_lib/deployment_manager/dm_write.pyExecuter$   "   s    * '](	OO:,,1199CCD1 $ 3M  !H 	(II+228==AB	/    c                 z    t        j                         }t        j                  | d|       |j	                         S )zReturns a ready-to-print string representation from the operation error.

  Args:
    error: operation error object

  Returns:
    A ready-to-print string representation of the error.
  yaml)out)ioStringIOr   r   getvalue)errorerror_messages     r#   GetOperationErrorr.   K   s0     ++--M:				!!r%   c           	         d}d}dj                  |r|dz   nd|      }|j                  ||      }	t        j                  |d      5 }
||k  r| j                  j                  |	      }|j                  |j                  j                  j                  k(  rY|j                  rBt        j                  d	j                  |t        j                  |j                                    |cd
d
d
       S ||z  }|
j                          t!        j"                  |       ||k  rt        j$                  dj                  |t'        j(                  |                  # 1 sw Y   y
xY w)a  Wait for an operation to complete.

  Polls the operation requested approximately every second, showing a
  progress indicator. Returns when the operation has completed.

  Args:
    client: The API client to use.
    messages: The API message to use.
    operation_name: The name of the operation to wait on, as returned by
        operations.list.
    operation_description: A short description of the operation to wait on,
        such as 'create' or 'delete'. Will be displayed to the user.
    project: The name of the project that this operation belongs to.
    timeout: Number of seconds to wait for. Defaults to 3 minutes.

  Returns:
    The operation when it is done.

  Raises:
      HttpException: A http error response was received while executing api
          request. Will be raised if the operation cannot be found.
      OperationError: The operation finished with error(s).
      Error: The operation the timeout without completing.
     r   zWaiting for {0}[{1}]  )r   r   F)autotickzError in Operation [{0}]: {1}Nz0Wait for Operation [{0}] exceeded timeout [{1}].)r   %DeploymentmanagerOperationsGetRequestr
   ProgressTracker
operationsGetstatus	OperationStatusValueValuesEnumDONEr,   r   OperationErrorr   RenderMessageAsYamlTicktimesleepErrorsix	text_type)r   r   operation_nameoperation_descriptionr   timeouttick_incrementticksmessager   tickerr   s               r#   r   r   Y   sK   6 .
%#**%:c!' :: ; 1'''%@F
'/##''0i			X//EEJJ	J??))-44 '"="=ioo"NPQ Q  A@ ~ekkm
jj  '/  

:AACMM'2	45 5# A@s   BE//E8EE )NN   )__doc__
__future__r   r   r   r)   r?   )googlecloudsdk.api_lib.deployment_managerr   -googlecloudsdk.command_lib.deployment_managerr   googlecloudsdk.corer   r	   googlecloudsdk.core.consoler
   googlecloudsdk.core.resourcer   rB   r$   r.   r    r%   r#   <module>rT      sF     , &  ' 	  @ A # * 8 9 
&R" ,0+.55r%   