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.')
Beispiel #2
0
    def CreateReference(self, args, default=None):
        # Check if scope was provided
        has_region = bool(getattr(args, 'region', None))
        has_zone = bool(getattr(args, 'zone', None))
        has_global = bool(getattr(args, 'global', None))

        null = object()

        # Check if only one kind of scope can be provided
        only_zone_prompt = ((getattr(args, 'zone', null) is not null)
                            and (getattr(args, 'region', null) is null))
        only_region_prompt = ((getattr(args, 'region', null) is not null)
                              and (getattr(args, 'zone', null) is null))

        if not (has_region or has_zone or has_global):
            if default == ScopeType.global_scope:
                has_global = True
            elif default == ScopeType.regional_scope:
                has_region = True
            elif default == ScopeType.zonal_scope:
                has_zone = True

        ref = None
        try:
            params = {}
            if has_region:
                params['region'] = args.region
            if has_zone:
                params['zone'] = args.zone
            ref = self.resources.Parse(args.name, params=params)
        except resources.UnknownCollectionException:
            ref = None

        if ref is None:
            if has_global:
                ref = self.CreateGlobalReference(
                    args.name, resource_type=self.global_resource_type)
            elif has_region or only_region_prompt:
                ref = self.CreateRegionalReference(
                    args.name,
                    args.region,
                    resource_type=self.regional_resource_type)
            elif has_zone or only_zone_prompt:
                ref = self.CreateZonalReference(
                    args.name,
                    args.zone,
                    resource_type=self.zonal_resource_type)
            else:
                ref = self.PromptForMultiScopedReferences(
                    [args.name],
                    scope_names=['zone', 'region'],
                    scope_services=[self.compute.zones, self.compute.regions],
                    resource_types=[
                        self.zonal_resource_type, self.regional_resource_type
                    ],
                    flag_names=['--zone', '--region'])[0]

        valid_collections = [
            'compute.{0}'.format(resource_type) for resource_type in [
                self.zonal_resource_type, self.regional_resource_type,
                self.global_resource_type
            ] if resource_type is not None
        ]

        if ref.Collection() not in valid_collections:
            raise calliope_exceptions.ToolException(
                'You must pass in a reference to a global or regional resource.'
            )

        ref_resource_type = utils.CollectionToResourceType(ref.Collection())
        if ref_resource_type == self.global_resource_type:
            self._service = self.global_service
        elif ref_resource_type == self.regional_resource_type:
            self._service = self.regional_service
        else:
            self._service = self.zonal_service
        return ref