コード例 #1
0
def WaitForOperation(client,
                     messages,
                     operation_name,
                     operation_description=None,
                     project=None,
                     timeout=180):
    """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.
  """
    tick_increment = 1  # every second(s)
    ticks = 0
    message = ('Waiting for {0}[{1}]'.format(
        operation_description + ' ' if operation_description else '',
        operation_name))
    request = messages.DeploymentmanagerOperationsGetRequest(
        project=project, operation=operation_name)
    with progress_tracker.ProgressTracker(message, autotick=False) as ticker:
        while ticks < timeout:
            operation = client.operations.Get(request)
            # Operation status is one of PENDING, RUNNING, DONE
            if operation.status == 'DONE':
                if operation.error:
                    raise exceptions.OperationError(
                        'Error in Operation [{0}]: {1}'.format(
                            operation_name,
                            dm_util.RenderMessageAsYaml(operation.error)))
                else:  # Operation succeeded
                    return operation

            ticks += tick_increment
            ticker.Tick()
            time.sleep(tick_increment)

        # Timeout exceeded
        raise exceptions.Error(
            'Wait for Operation [{0}] exceeded timeout [{1}].'.format(
                operation_name, str(timeout)))
コード例 #2
0
def TemplateContentsFor(messages, template_path):
    """Build a TemplateContents message from a local template or url.

  Args:
    messages: The API message to use.
    template_path: Path to the config yaml file, with an optional list of
      imports.

  Returns:
    The TemplateContents message from the template at template_path.

  Raises:
    Error if the provided file is not a template.
  """
    config_obj = importer.BuildConfig(template=template_path)

    if not config_obj.IsTemplate():
        raise exceptions.Error('The provided file must be a template.')

    template_name = config_obj.GetBaseName()
    schema_name = template_name + '.schema'
    file_type = 'JINJA' if template_name.endswith('.jinja') else 'PYTHON'

    imports = importer.CreateImports(messages, config_obj)

    template = ''
    schema = ''

    # Find schema and template from imports
    for item in imports:
        if item.name == template_name:
            template = item.content
        elif item.name == schema_name:
            schema = item.content

    # Remove schema and template from imports
    imports = [
        item for item in imports
        if item.name not in [template_name, schema_name]
    ]

    return messages.TemplateContents(imports=imports,
                                     schema=schema,
                                     template=template,
                                     interpreter=file_type)