Ejemplo n.º 1
0
  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.
      ToolException: Config file could not be read or parsed, or the deployment
          creation operation encountered an error.
    """
    client = self.context['deploymentmanager-client']
    messages = self.context['deploymentmanager-messages']
    project = properties.VALUES.core.project.Get(required=True)

    deployment = messages.Deployment(
        name=args.deployment_name,
        target=importer.BuildTargetConfig(
            messages, args.config, args.properties),
    )
    if args.description:
      deployment.description = args.description

    try:
      operation = client.deployments.Insert(
          messages.DeploymentmanagerDeploymentsInsertRequest(
              project=project,
              deployment=deployment,
              preview=args.preview,
          )
      )
    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(dm_v2_util.GetError(error))
    if args.async:
      return operation
    else:
      op_name = operation.name
      try:
        dm_v2_util.WaitForOperation(op_name, project, self.context, 'create',
                                    OPERATION_TIMEOUT)
        log.status.Print('Create operation ' + op_name
                         + ' completed successfully.')
      except exceptions.ToolException:
        # Operation timed out or had errors. Print this warning, then still
        # show whatever operation can be gotten.
        log.error('Create operation ' + op_name
                  + ' has errors or failed to complete within '
                  + str(OPERATION_TIMEOUT) + ' seconds.')
      except apitools_exceptions.HttpError as error:
        raise exceptions.HttpException(dm_v2_util.GetError(error))

      return dm_v2_util.FetchResourcesAndOutputs(client, messages, project,
                                                 args.deployment_name)
Ejemplo n.º 2
0
    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 = self.messages.Deployment(
            name=args.deployment_name,
            target=importer.BuildTargetConfig(self.messages, args.config,
                                              args.properties),
        )
        if args.description:
            deployment.description = args.description

        try:
            operation = self.client.deployments.Insert(
                self.messages.DeploymentmanagerDeploymentsInsertRequest(
                    project=self.project,
                    deployment=deployment,
                    preview=args.preview,
                ))
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_v2_util.WaitForOperation(self.client, self.messages,
                                            op_name, self.project, 'create',
                                            OPERATION_TIMEOUT)
                log.status.Print('Create operation ' + op_name +
                                 ' completed successfully.')
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_v2_util.HTTP_ERROR_FORMAT)

            return dm_v2_util.FetchResourcesAndOutputs(self.client,
                                                       self.messages,
                                                       self.project,
                                                       args.deployment_name)
Ejemplo n.º 3
0
    def Run(self, args):
        """Run 'deployments update'.

    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.
    """
        deployment = dm_base.GetMessages().Deployment(
            name=args.deployment_name, )

        if args.config:
            deployment.target = importer.BuildTargetConfig(
                dm_base.GetMessages(), args.config, args.properties)
        elif (self.ReleaseTrack()
              in [base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA]
              and args.manifest_id):
            deployment.target = importer.BuildTargetConfigFromManifest(
                dm_base.GetClient(), dm_base.GetMessages(),
                dm_base.GetProject(), args.deployment_name, args.manifest_id,
                args.properties)
        # Get the fingerprint from the deployment to update.
        try:
            current_deployment = dm_base.GetClient().deployments.Get(
                dm_base.GetMessages().DeploymentmanagerDeploymentsGetRequest(
                    project=dm_base.GetProject(),
                    deployment=args.deployment_name))
            # Update the labels of the deployment
            if self.ReleaseTrack() in [base.ReleaseTrack.ALPHA]:
                update_labels = labels_util.GetUpdateLabelsDictFromArgs(args)
                remove_labels = labels_util.GetRemoveLabelsListFromArgs(args)
                current_labels = current_deployment.labels

                deployment.labels = dm_labels.UpdateLabels(
                    current_labels,
                    dm_base.GetMessages().DeploymentLabelEntry, update_labels,
                    remove_labels)

                # If no config or manifest_id are specified, but try to update labels,
                # only get current manifest when it is not a preveiw request
                if not args.config and not args.manifest_id:
                    if args.update_labels or args.remove_labels:
                        if not args.preview:
                            current_manifest = dm_v2_util.ExtractManifestName(
                                current_deployment)
                            deployment.target = importer.BuildTargetConfigFromManifest(
                                dm_base.GetClient(), dm_base.GetMessages(),
                                dm_base.GetProject(), args.deployment_name,
                                current_manifest)

            # If no fingerprint is present, default to an empty fingerprint.
            # This empty default can be removed once the fingerprint change is
            # fully implemented and all deployments have fingerprints.
            deployment.fingerprint = current_deployment.fingerprint or ''
            if args.description is None:
                deployment.description = current_deployment.description
            elif not args.description or args.description.isspace():
                deployment.description = None
            else:
                deployment.description = args.description
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)

        try:
            operation = dm_base.GetClient().deployments.Update(
                dm_base.GetMessages(
                ).DeploymentmanagerDeploymentsUpdateRequest(
                    deploymentResource=deployment,
                    project=dm_base.GetProject(),
                    deployment=args.deployment_name,
                    preview=args.preview,
                    createPolicy=(dm_base.GetMessages(
                    ).DeploymentmanagerDeploymentsUpdateRequest.
                                  CreatePolicyValueValuesEnum(
                                      args.create_policy)),
                    deletePolicy=(dm_base.GetMessages(
                    ).DeploymentmanagerDeploymentsUpdateRequest.
                                  DeletePolicyValueValuesEnum(
                                      args.delete_policy)),
                ))
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_write.WaitForOperation(op_name,
                                          'update',
                                          dm_base.GetProject(),
                                          timeout=OPERATION_TIMEOUT)
                log.status.Print('Update operation ' + op_name +
                                 ' completed successfully.')
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_v2_util.HTTP_ERROR_FORMAT)

            return dm_v2_util.FetchResourcesAndOutputs(dm_base.GetClient(),
                                                       dm_base.GetMessages(),
                                                       dm_base.GetProject(),
                                                       args.deployment_name)
Ejemplo n.º 4
0
    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 = dm_base.GetMessages().Deployment(
            name=args.deployment_name,
            target=importer.BuildTargetConfig(dm_base.GetMessages(),
                                              args.config, args.properties),
        )
        if self.ReleaseTrack() in [base.ReleaseTrack.ALPHA]:
            label_dict = labels_util.GetUpdateLabelsDictFromArgs(args)
            label_entry = []
            if label_dict:
                label_entry = [
                    dm_base.GetMessages().DeploymentLabelEntry(key=k, value=v)
                    for k, v in sorted(label_dict.iteritems())
                ]
                deployment.labels = label_entry

        if args.description:
            deployment.description = args.description

        try:
            operation = dm_base.GetClient().deployments.Insert(
                dm_base.GetMessages(
                ).DeploymentmanagerDeploymentsInsertRequest(
                    project=dm_base.GetProject(),
                    deployment=deployment,
                    preview=args.preview,
                ))
            # Fetch and print the latest fingerprint of the deployment.
            fingerprint = dm_v2_util.FetchDeploymentFingerprint(
                dm_base.GetClient(), dm_base.GetMessages(),
                dm_base.GetProject(), args.deployment_name)
            dm_util.PrintFingerprint(fingerprint)

        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_write.WaitForOperation(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:
                raise exceptions.HttpException(error,
                                               dm_v2_util.HTTP_ERROR_FORMAT)

            return dm_v2_util.FetchResourcesAndOutputs(dm_base.GetClient(),
                                                       dm_base.GetMessages(),
                                                       dm_base.GetProject(),
                                                       args.deployment_name)
  def Run(self, args):
    """Run 'deployments update'.

    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.
      ToolException: Config file could not be read or parsed, or the deployment
          creation operation encountered an error.
    """
    client = self.context['deploymentmanager-client']
    messages = self.context['deploymentmanager-messages']
    project = properties.VALUES.core.project.Get(required=True)

    deployment = messages.Deployment(
        name=args.deployment_name,
    )

    if args.config:
      deployment.target = importer.BuildTargetConfig(
          messages, args.config, args.properties)
    elif (self.ReleaseTrack() in [base.ReleaseTrack.ALPHA,
                                  base.ReleaseTrack.BETA]
          and args.manifest_id):
      deployment.target = importer.BuildTargetConfigFromManifest(
          client, messages, project, args.deployment_name, args.manifest_id,
          args.properties)
    # Get the fingerprint from the deployment to update.
    try:
      current_deployment = client.deployments.Get(
          messages.DeploymentmanagerDeploymentsGetRequest(
              project=project,
              deployment=args.deployment_name
          )
      )
      # If no fingerprint is present, default to an empty fingerprint.
      # This empty default can be removed once the fingerprint change is
      # fully implemented and all deployments have fingerprints.
      deployment.fingerprint = current_deployment.fingerprint or ''
    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)

    try:
      operation = client.deployments.Update(
          messages.DeploymentmanagerDeploymentsUpdateRequest(
              deploymentResource=deployment,
              project=project,
              deployment=args.deployment_name,
              preview=args.preview,
              createPolicy=(messages.DeploymentmanagerDeploymentsUpdateRequest
                            .CreatePolicyValueValuesEnum(args.create_policy)),
              deletePolicy=(messages.DeploymentmanagerDeploymentsUpdateRequest
                            .DeletePolicyValueValuesEnum(args.delete_policy)),
          )
      )
    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)
    if args.async:
      return operation
    else:
      op_name = operation.name
      try:
        dm_v2_util.WaitForOperation(client, messages, op_name, project,
                                    'update', OPERATION_TIMEOUT)
        log.status.Print('Update operation ' + op_name
                         + ' completed successfully.')
      except exceptions.ToolException:
        # Operation timed out or had errors. Print this warning, then still
        # show whatever operation can be gotten.
        log.error('Update operation ' + op_name
                  + ' has errors or failed to complete within '
                  + str(OPERATION_TIMEOUT) + ' seconds.')
      except apitools_exceptions.HttpError as error:
        raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)

      return dm_v2_util.FetchResourcesAndOutputs(client, messages, project,
                                                 args.deployment_name)
Ejemplo n.º 6
0
    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.
    """
        if ((not args.IsSpecified('format')) and
            (args. async or getattr(args, 'automatic_rollback', False))):
            args.format = flags.OPERATION_FORMAT

        deployment = dm_base.GetMessages().Deployment(
            name=args.
            deployment_name,  # TODO(b/37913150): Use resource parser.
            target=importer.BuildTargetConfig(dm_base.GetMessages(),
                                              args.config, args.properties),
        )

        self._SetMetadata(args, deployment)

        try:
            operation = dm_base.GetClient().deployments.Insert(
                dm_base.GetMessages(
                ).DeploymentmanagerDeploymentsInsertRequest(
                    project=dm_base.GetProject(),
                    deployment=deployment,
                    preview=args.preview,
                ))

            # Fetch and print the latest fingerprint of the deployment.
            fingerprint = dm_v2_util.FetchDeploymentFingerprint(
                dm_base.GetClient(), dm_base.GetMessages(),
                dm_base.GetProject(), args.deployment_name)
            dm_util.PrintFingerprint(fingerprint)

        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_write.WaitForOperation(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_v2_util.HTTP_ERROR_FORMAT)
            except dm_exceptions.OperationError as error:
                response = self._HandleOperationError(error, args, operation,
                                                      dm_base.GetProject())
                return response

            return dm_v2_util.FetchResourcesAndOutputs(dm_base.GetClient(),
                                                       dm_base.GetMessages(),
                                                       dm_base.GetProject(),
                                                       args.deployment_name)
Ejemplo n.º 7
0
    def Run(self, args):
        """Run 'deployments update'.

    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.
    """
        deployment = self.messages.Deployment(name=args.deployment_name, )

        if args.config:
            deployment.target = importer.BuildTargetConfig(
                self.messages, args.config, args.properties)
        elif (self.ReleaseTrack()
              in [base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA]
              and args.manifest_id):
            deployment.target = importer.BuildTargetConfigFromManifest(
                self.client, self.messages, self.project, args.deployment_name,
                args.manifest_id, args.properties)
        # Get the fingerprint from the deployment to update.
        try:
            current_deployment = self.client.deployments.Get(
                self.messages.DeploymentmanagerDeploymentsGetRequest(
                    project=self.project, deployment=args.deployment_name))
            # If no fingerprint is present, default to an empty fingerprint.
            # This empty default can be removed once the fingerprint change is
            # fully implemented and all deployments have fingerprints.
            deployment.fingerprint = current_deployment.fingerprint or ''
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)

        try:
            operation = self.client.deployments.Update(
                self.messages.DeploymentmanagerDeploymentsUpdateRequest(
                    deploymentResource=deployment,
                    project=self.project,
                    deployment=args.deployment_name,
                    preview=args.preview,
                    createPolicy=(
                        self.messages.DeploymentmanagerDeploymentsUpdateRequest
                        .CreatePolicyValueValuesEnum(args.create_policy)),
                    deletePolicy=(
                        self.messages.DeploymentmanagerDeploymentsUpdateRequest
                        .DeletePolicyValueValuesEnum(args.delete_policy)),
                ))
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, dm_v2_util.HTTP_ERROR_FORMAT)
        if args. async:
            return operation
        else:
            op_name = operation.name
            try:
                dm_v2_util.WaitForOperation(self.client, self.messages,
                                            op_name, self.project, 'update',
                                            OPERATION_TIMEOUT)
                log.status.Print('Update operation ' + op_name +
                                 ' completed successfully.')
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error,
                                               dm_v2_util.HTTP_ERROR_FORMAT)

            return dm_v2_util.FetchResourcesAndOutputs(self.client,
                                                       self.messages,
                                                       self.project,
                                                       args.deployment_name)