def Run(self, args):
    """Run 'deployments create'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns a struct containing the list of resources and list of
        outputs in the deployment.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ConfigError: Config file could not be read or parsed, or the
          deployment creation operation encountered an error.
    """
    deployment_ref = self.resources.Parse(
        args.deployment_name,
        params={'project': properties.VALUES.core.project.GetOrFail},
        collection='deploymentmanager.deployments')
    if (not args.IsSpecified('format')) and (args.async):
      args.format = flags.OPERATION_FORMAT

    deployment = self.messages.Deployment(
        name=deployment_ref.deployment,
        target=importer.BuildTargetConfig(self.messages,
                                          config=args.config,
                                          template=args.template,
                                          composite_type=args.composite_type,
                                          properties=args.properties)

    )

    self._SetMetadata(args, deployment)

    try:
      operation = self.client.deployments.Insert(
          self.messages.DeploymentmanagerDeploymentsInsertRequest(
              project=dm_base.GetProject(),
              deployment=deployment,
              preview=args.preview,
          )
      )

      # Fetch and print the latest fingerprint of the deployment.
      fingerprint = dm_api_util.FetchDeploymentFingerprint(
          self.client,
          self.messages,
          dm_base.GetProject(),
          deployment_ref.deployment)
      dm_util.PrintFingerprint(fingerprint)

    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(error, dm_api_util.HTTP_ERROR_FORMAT)
    if args.async:
      return operation
    else:
      op_name = operation.name
      try:
        dm_write.WaitForOperation(self.client,
                                  self.messages,
                                  op_name,
                                  operation_description='create',
                                  project=dm_base.GetProject(),
                                  timeout=OPERATION_TIMEOUT)
        log.status.Print('Create operation ' + op_name
                         + ' completed successfully.')
      except apitools_exceptions.HttpError as error:
        # TODO(b/37911296): Use gcloud default error handling.
        raise exceptions.HttpException(error, dm_api_util.HTTP_ERROR_FORMAT)
      except dm_exceptions.OperationError as error:
        response = self._HandleOperationError(error,
                                              args,
                                              operation,
                                              dm_base.GetProject(),
                                              deployment_ref)
        if getattr(args, 'automatic_rollback', False):
          args.format = flags.OPERATION_FORMAT
        return response

      return dm_api_util.FetchResourcesAndOutputs(
          self.client, self.messages, dm_base.GetProject(),
          deployment_ref.deployment,
          self.ReleaseTrack() is base.ReleaseTrack.ALPHA)
Example #2
0
    def Run(self, args):
        """Run 'deployments stop'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns boolean indicating whether stop operation succeeded.

    Raises:
      HttpException: An http error response was received while executing api
          request.
    """
        if args.fingerprint:
            fingerprint = dm_util.DecodeFingerprint(args.fingerprint)
        else:
            # If no fingerprint is present, default to an empty fingerprint.
            # TODO(b/34966984): Remove the empty default after cleaning up all
            # deployments that has no fingerprint
            fingerprint = dm_api_util.FetchDeploymentFingerprint(
                self.client, self.messages, dm_base.GetProject(),
                args.deployment_name) or b''
        try:
            operation = self.client.deployments.Stop(
                self.messages.DeploymentmanagerDeploymentsStopRequest(
                    project=dm_base.GetProject(),
                    deployment=args.deployment_name,
                    deploymentsStopRequest=(
                        self.messages.DeploymentsStopRequest(
                            fingerprint=fingerprint)),
                ))
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error,
                                           dm_api_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                operation = dm_write.WaitForOperation(
                    self.client,
                    self.messages,
                    op_name,
                    'stop',
                    dm_base.GetProject(),
                    timeout=OPERATION_TIMEOUT)
                dm_util.LogOperationStatus(operation, 'Stop')
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_api_util.HTTP_ERROR_FORMAT)
            try:
                # Fetch a list of the stopped resources.
                response = self.client.resources.List(
                    self.messages.DeploymentmanagerResourcesListRequest(
                        project=dm_base.GetProject(),
                        deployment=args.deployment_name,
                    ))
                # TODO(b/36055861): Pagination
                return response.resources if response.resources else []
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_api_util.HTTP_ERROR_FORMAT)
    def Run(self, args):
        """Run 'deployments cancel-preview'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      If --async=true, returns Operation to poll.
      Else, returns boolean indicating whether cancel preview operation
      succeeded.

    Raises:
      HttpException: An http error response was received while executing api
          request.
    """

        if args.fingerprint:
            fingerprint = dm_util.DecodeFingerprint(args.fingerprint)
        else:
            # If no fingerprint is present, default to an empty fingerprint.
            # TODO(b/34966984): Remove the empty default after cleaning up all
            # deployments that has no fingerprint
            fingerprint = dm_api_util.FetchDeploymentFingerprint(
                self.client,
                self.messages,
                dm_base.GetProject(),
                args.deployment_name,
            ) or ''

        try:
            operation = self.client.deployments.CancelPreview(
                self.messages.DeploymentmanagerDeploymentsCancelPreviewRequest(
                    project=dm_base.GetProject(),
                    deployment=args.deployment_name,
                    deploymentsCancelPreviewRequest=self.messages.
                    DeploymentsCancelPreviewRequest(fingerprint=fingerprint, ),
                ))
            # Fetch and print the latest fingerprint of the deployment.
            new_fingerprint = dm_api_util.FetchDeploymentFingerprint(
                self.client, self.messages, dm_base.GetProject(),
                args.deployment_name)
            dm_util.PrintFingerprint(new_fingerprint)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error,
                                           dm_api_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_write.WaitForOperation(self.client,
                                          self.messages,
                                          op_name,
                                          'cancel-preview',
                                          dm_base.GetProject(),
                                          timeout=OPERATION_TIMEOUT)
                log.status.Print('Cancel preview operation ' + op_name +
                                 ' completed successfully.')
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_api_util.HTTP_ERROR_FORMAT)
            try:
                # Fetch a list of the canceled resources.
                response = self.client.resources.List(
                    self.messages.DeploymentmanagerResourcesListRequest(
                        project=dm_base.GetProject(),
                        deployment=args.deployment_name,
                    ))
                # TODO(b/36052523): Pagination
                return response.resources if response.resources else []
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_api_util.HTTP_ERROR_FORMAT)