Example #1
0
    def _PromptForScopeList(self, ambiguous_refs, attributes, resource_type,
                            choice_resources, raise_on_prompt_failure):
        """Prompt to resolve abiguous resources.  Either returns str or throws."""
        # targetInstances -> target instances
        resource_name = utils.CamelCaseToOutputFriendly(resource_type)
        # Resource names should be surrounded by brackets while choices should not
        names = ['[{0}]'.format(name) for name, _, _ in ambiguous_refs]
        # Print deprecation state for choices.
        choice_names = []
        choice_mapping = []
        for attribute in attributes:
            for choice_resource in choice_resources[attribute]:
                deprecated = choice_resource.deprecated
                if deprecated:
                    choice_name = '{0} ({1})'.format(choice_resource.name,
                                                     deprecated.state)
                else:
                    choice_name = choice_resource.name

                if len(attributes) > 1:
                    choice_name = '{0}: {1}'.format(attribute, choice_name)

                choice_mapping.append((attribute, choice_resource.name))
                choice_names.append(choice_name)

        title = utils.ConstructList(
            'For the following {0}:'.format(resource_name), names)
        idx = console_io.PromptChoice(options=choice_names,
                                      message='{0}choose a {1}:'.format(
                                          title, ' or '.join(attributes)))
        if idx is None:
            raise_on_prompt_failure()
        else:
            return choice_mapping[idx]
Example #2
0
def _PromptForUpgrade(ref, args):
  """Prompts the user to confirm upgrade of instance."""
  scope_name = 'zone'
  resource_type = utils.CollectionToResourceType(ref.Collection())
  resource_name = utils.CamelCaseToOutputFriendly(resource_type)

  prompt_item = '[{0}] in [{1}]'.format(ref.Name(), getattr(ref, scope_name))
  prompt_title = 'The following {0} will be upgraded from {1} to {2}:'.format(
      resource_name, args.source_os, args.target_os)

  buf = io.StringIO()
  fmt = 'list[title="{title}",always-display-title]'.format(title=prompt_title)
  resource_printer.Print(prompt_item, fmt, out=buf)
  prompt_message = buf.getvalue()

  if not console_io.PromptContinue(message=prompt_message):
    raise calliope_exceptions.ToolException('Upgrade aborted by user.')
  def _PromptDidYouMeanScope(self, ambiguous_refs, attribute, resource_type,
                             suggested_resource, raise_on_prompt_failure):
    """Prompts "did you mean <scope>".  Returns str or None, or raises."""

    # targetInstances -> target instances
    resource_name = utils.CamelCaseToOutputFriendly(resource_type)
    names = [name for name, _, _ in ambiguous_refs]
    message = 'Did you mean {0} [{1}] for {2}: [{3}]?'.format(
        attribute, suggested_resource, resource_name, ', '.join(names))

    try:
      if console_io.PromptContinue(message=message, default=True,
                                   throw_if_unattended=True):
        return suggested_resource
      else:
        return None
    except console_io.UnattendedPromptError:
      raise_on_prompt_failure()