def Run(self, args):
        rollout_ref = args.CONCEPTS.rollout.Parse()
        try:
            rollout_obj = rollout.RolloutClient().Get(
                rollout_ref.RelativeName())
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error)

        release_ref = resources.REGISTRY.ParseRelativeName(
            rollout_ref.Parent().RelativeName(),
            collection=
            'clouddeploy.projects.locations.deliveryPipelines.releases')
        try:
            release_obj = release.ReleaseClient().Get(
                release_ref.RelativeName())
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error)

        prompt = 'Approving rollout {} from {} to target {}.\n\n'.format(
            rollout_ref.Name(), release_ref.Name(), rollout_obj.targetId)
        release_util.PrintDiff(release_ref, release_obj, prompt=prompt)

        console_io.PromptContinue(cancel_on_no=True)

        return rollout.RolloutClient().Approve(rollout_ref.RelativeName(),
                                               True)
  def Run(self, args):
    rollout_ref = args.CONCEPTS.rollout.Parse()

    console_io.PromptContinue(
        message='Rejecting rollout {}.\n'.format(rollout_ref.RelativeName()),
        cancel_on_no=True)

    return rollout.RolloutClient().Approve(rollout_ref.RelativeName(), False)
def CheckIfInProgressRollout(release_ref, release_obj, to_target_id):
    """Checks if there are any rollouts in progress for the given release.

  Args:
    release_ref: protorpc.messages.Message, release resource object.
    release_obj: apitools.base.protorpclite.messages.Message, release message
      object.
    to_target_id: string, target id (e.g. 'prod')

  Raises:
    googlecloudsdk.command_lib.deploy.exceptions.RolloutInProgressError
  """
    rollouts = list(rollout.RolloutClient().List(release_ref.RelativeName(),
                                                 IN_PROGRESS_FILTER_TEMPLATE,
                                                 limit=1))

    if rollouts:
        raise exceptions.RolloutInProgressError(release_obj.name, to_target_id)
def GetSucceededRollout(target_ref, pipeline_ref, limit=None):
    """Gets a successfully deployed rollouts for the releases associated with the specified target and index.

  Args:
    target_ref: protorpc.messages.Message, target object.
    pipeline_ref: protorpc.messages.Message, pipeline object.
    limit: int, the maximum number of `Rollout` objects to return.

  Returns:
    a rollout object or None if no rollouts in the target.
  """
    filter_str = DEPLOYED_ROLLOUT_FILTER_TEMPLATE.format(target_ref.Name())
    parent = WILDCARD_RELEASE_NAME_TEMPLATE.format(pipeline_ref.RelativeName())

    return rollout.RolloutClient().List(release_name=parent,
                                        filter_str=filter_str,
                                        order_by=SUCCEED_ROLLOUT_ORDERBY,
                                        limit=limit)
def ListPendingRollouts(target_ref, pipeline_ref):
    """Lists the rollouts in PENDING_APPROVAL state for the releases associated with the specified target.

  The rollouts must be approvalState=NEEDS_APPROVAL and
  state=PENDING_APPROVAL. The returned list is sorted by rollout's create
  time.

  Args:
    target_ref: protorpc.messages.Message, target object.
    pipeline_ref: protorpc.messages.Message, pipeline object.

  Returns:
    a sorted list of rollouts.
  """
    filter_str = PENDING_APPROVAL_FILTER_TEMPLATE.format(target_ref.Name())
    parent = WILDCARD_RELEASE_NAME_TEMPLATE.format(pipeline_ref.RelativeName())

    return rollout.RolloutClient().List(release_name=parent,
                                        filter_str=filter_str,
                                        order_by=PENDING_ROLLOUT_ORDERBY)
def CreateRollout(release_ref,
                  to_target,
                  rollout_id=None,
                  annotations=None,
                  labels=None,
                  description=None):
    """Creates a rollout by calling the rollout create API and waits for the operation to finish.

  Args:
    release_ref: protorpc.messages.Message, release resource object.
    to_target: str, the target to create create the rollout in.
    rollout_id: str, rollout ID.
    annotations: dict[str,str], a dict of annotation (key,value) pairs that
      allow clients to store small amounts of arbitrary data in cloud deploy
      resources.
    labels: dict[str,str], a dict of label (key,value) pairs that can be used to
      select cloud deploy resources and to find collections of cloud deploy
      resources that satisfy certain conditions.
    description: str, rollout description.

  Raises:
      ListRolloutsError: an error occurred calling rollout list API.

  Returns:
    The rollout resource created.
  """
    final_rollout_id = rollout_id
    if not final_rollout_id:
        filter_str = ROLLOUT_IN_TARGET_FILTER_TEMPLATE.format(to_target)
        try:
            rollouts = rollout.RolloutClient().List(release_ref.RelativeName(),
                                                    filter_str)
            final_rollout_id = ComputeRolloutID(release_ref.Name(), to_target,
                                                rollouts)
        except apitools_exceptions.HttpError:
            raise cd_exceptions.ListRolloutsError(release_ref.RelativeName())

    resource_dict = release_ref.AsDict()
    rollout_ref = resources.REGISTRY.Parse(
        final_rollout_id,
        collection=_ROLLOUT_COLLECTION,
        params={
            'projectsId': resource_dict.get('projectsId'),
            'locationsId': resource_dict.get('locationsId'),
            'deliveryPipelinesId': resource_dict.get('deliveryPipelinesId'),
            'releasesId': release_ref.Name(),
        })
    rollout_obj = client_util.GetMessagesModule().Rollout(
        name=rollout_ref.RelativeName(),
        targetId=to_target,
        description=description)

    log.status.Print('Creating rollout {} in target {}'.format(
        rollout_ref.RelativeName(), to_target))
    operation = rollout.RolloutClient().Create(rollout_ref, rollout_obj,
                                               annotations, labels)
    operation_ref = resources.REGISTRY.ParseRelativeName(
        operation.name, collection='clouddeploy.projects.locations.operations')

    client_util.OperationsClient().WaitForOperation(
        operation, operation_ref,
        'Waiting for rollout creation operation to complete')
    return rollout.RolloutClient().Get(rollout_ref.RelativeName())