Example #1
0
def FallBackToDeployedProxyRevision(args):
    """If `args` provides no revision, adds the deployed revision, if unambiguous.

  Args:
    args: a dictionary of resource identifiers which identifies an API proxy and
      an environment, to which the deployed revision should be added.

  Raises:
    EntityNotFoundError: no deployment that matches `args` exists.
    AmbiguousRequestError: more than one deployment matches `args`.
  """
    deployments = apigee.DeploymentsClient.List(args)

    if not deployments:
        error_identifier = collections.OrderedDict([
            ("organization", args["organizationsId"]),
            ("environment", args["environmentsId"]), ("api", args["apisId"])
        ])
        raise errors.EntityNotFoundError("deployment", error_identifier,
                                         "undeploy")

    if len(deployments) > 1:
        message = "Found more than one deployment that matches this request.\n"
        raise errors.AmbiguousRequestError(message + yaml.dump(deployments))

    deployed_revision = deployments[0]["revision"]
    log.status.Print("Using deployed revision `%s`" % deployed_revision)
    args["revisionsId"] = deployed_revision
Example #2
0
  def Run(self, args):
    """Run the describe command."""
    identifiers = args.CONCEPTS.revision.Parse().AsDict()
    if identifiers["revisionsId"] == "auto":
      del identifiers["revisionsId"]
      defaults.FallBackToDeployedProxyRevision(identifiers)

    # Deployments have no identifier of their own, so the API to get deployment
    # details looks like a List call with all possible parents specified.
    deployments = apigee.DeploymentsClient.List(identifiers)
    if not deployments:
      raise errors.EntityNotFoundError("deployment", identifiers, "GET")
    return deployments[0]
Example #3
0
def ResourceListFromPrompt(name, list_func, end_empty_message=None):
  """Returns a list of resources selected by the user.

  Args:
    name: the entity name for the resources being selected.
    list_func: a zero argument function that will return a list of existing
      resources.
    end_empty_message: text for the menu option that will return an empty list.
  """
  resource_list = list_func()
  if not resource_list:
    docs_name = resource_args.ENTITIES[name].docs_name
    raise errors.EntityNotFoundError(
        message=("Could not find any %s to select. Check that at least one %s "
                 "has been created and is properly configued for use." %
                 (docs_name, docs_name)))

  chosen = []
  available = None
  menu_option = len(resource_list) + 1
  while menu_option != len(resource_list):
    if menu_option < len(chosen):
      # Falls within the "chosen" menu options. Remove the resource at exactly
      # the selected slot.
      chosen = chosen[:menu_option] + chosen[menu_option + 1:]
    elif menu_option < len(resource_list):
      # Falls within the "available" menu options.
      index = menu_option - len(chosen)
      chosen.append(available[index])
    available = [item for item in resource_list if item not in chosen]

    options = ["Remove `%s`" % item for item in chosen]
    options += ["Add `%s`" % item for item in available]
    if chosen:
      message = "Currently selected: %s" % ", ".join(chosen)
      options.append("Done")
    else:
      message = "No %s selected yet" % resource_args.ENTITIES[name].docs_name
      if end_empty_message is not None:
        options.append(end_empty_message)
    menu_option = console_io.PromptChoice(options, message=message)
  return chosen