def PrintOperationWithResponse(op):
    """Print the operation with response.

  Args:
    op: The long running operation.

  Raises:
    OperationErrorException: if the operation fails.

  Returns:
    Nothing.
  """
    if not op.done:
        log.status.Print('Operation "{0}" is still in progress.'.format(
            op.name))
        return
    if op.error:
        raise exceptions.OperationErrorException(
            'The operation "{0}" resulted in a failure "{1}".\nDetails: "{2}".'
            .format(op.name, op.error.message, op.error.details))
    if op.response:
        log.status.Print('Operation [{0}] complete. Result: {1}'.format(
            op.name,
            json.dumps(encoding.MessageToDict(op.response),
                       sort_keys=True,
                       indent=4,
                       separators=(',', ':'))))
    else:
        log.status.Print('Operation "{0}" finished successfully.'.format(
            op.name))
def WaitForOperation(operation_ref, client):
    """Waits for an operation to complete.

  Args:
    operation_ref: A reference to the operation on which to wait.
    client: The client object that contains the GetOperation request object.

  Raises:
    TimeoutError: if the operation does not complete in time.
    OperationErrorException: if the operation fails.

  Returns:
    The Operation object, if successful. Raises an exception on failure.
  """
    WaitForOperation.operation_response = None
    messages = GetMessagesModule()
    operation_id = operation_ref.operationsId

    def _CheckOperation(operation_id):  # pylint: disable=missing-docstring
        request = messages.ServicemanagementOperationsGetRequest(
            operationsId=operation_id, )

        result = client.operations.Get(request)

        if result.done:
            WaitForOperation.operation_response = result
            return True
        else:
            return False

    # Wait for no more than 30 minutes while retrying the Operation retrieval
    try:
        retry.Retryer(exponential_sleep_multiplier=1.1,
                      wait_ceiling_ms=10000,
                      max_wait_ms=30 * 60 * 1000).RetryOnResult(
                          _CheckOperation, [operation_id],
                          should_retry_if=False,
                          sleep_ms=1500)
    except retry.MaxRetrialsException:
        raise exceptions.TimeoutError('Timed out while waiting for '
                                      'operation {0}. Note that the operation '
                                      'is still pending.'.format(operation_id))

    # Check to see if the operation resulted in an error
    if WaitForOperation.operation_response.error is not None:
        raise exceptions.OperationErrorException(
            'The operation with ID {0} resulted in a failure.'.format(
                operation_id))

    # If we've gotten this far, the operation completed successfully,
    # so return the Operation object
    return WaitForOperation.operation_response
Esempio n. 3
0
def PrintOperation(op):
  """Print the operation.

  Args:
    op: The long running operation.

  Raises:
    OperationErrorException: if the operation fails.

  Returns:
    Nothing.
  """
  if not op.done:
    log.status.Print('Operation "{0}" is still in progress.'.format(op.name))
    return
  if op.error:
    raise exceptions.OperationErrorException(
        'The operation "{0}" resulted in a failure "{1}".\nDetails: "{2}".'.
        format(op.name, op.error.message, op.error.details))
  log.status.Print('Operation "{0}" finished successfully.'.format(op.name))