Beispiel #1
0
def CreateTaxonomyResourceArg(positional=False):
    return concept_parsers.ResourcePresentationSpec(
        'taxonomy' if positional else '--taxonomy',
        _GetProjectTaxonomyResourceSpec(),
        'A taxonomy reference.',
        required=True,
        prefixes=False)
def CreateAnnotationResourceArg(positional=False):
    return concept_parsers.ResourcePresentationSpec(
        'annotation' if positional else '--annotation',
        _GetProjectAnnotationResourceSpec(),
        'An annotation reference.',
        required=True,
        prefixes=False)
Beispiel #3
0
def GetAuthorityPresentationSpec(group_help, required=True, positional=True):
    return concept_parsers.ResourcePresentationSpec(
        name=_FormatArgName('authority', positional),
        concept_spec=_GetAuthorityResourceSpec(),
        group_help=group_help,
        required=required,
    )
Beispiel #4
0
    def _GenerateResourceArg(self):
        """Generates the flags to add to the parser that appear in the method path.

    Returns:
      {str, calliope.base.Argument}, A map of field name to argument.
    """
        if not self.resource_arg:
            return []

        # The anchor arg is positional unless explicitly overridden by the
        # attributes or for list commands (where everything should be a flag since
        # the parent resource collection is being used).
        anchor_arg_is_flag = (not self.resource_arg.is_positional
                              or self.method.IsList())
        if self.resource_arg.name_override:
            flag_name = self.resource_arg.name_override
        else:
            flag_name = self.resource_spec.anchor.name

        anchor_arg_name = ('--' +
                           flag_name if anchor_arg_is_flag else flag_name)
        no_gen = {n: '' for n in resource_arg_schema.IGNORED_FIELDS}
        no_gen.update({n: '' for n in self.resource_arg.removed_flags})

        concept = concept_parsers.ConceptParser([
            concept_parsers.ResourcePresentationSpec(
                anchor_arg_name,
                self.resource_spec,
                self.resource_arg.group_help,
                prefixes=False,
                required=True,
                flag_name_overrides=no_gen)
        ])
        return [concept]
Beispiel #5
0
def AddReimageResourcesToParser(parser):
    """Add TPU resource args to parser for reimage command."""
    custom_help = {'tpu': 'The Cloud TPU to reimage.'}

    resource_specs = LoadTPUResourceSpecs(custom_help)
    presentation_specs = []
    for arg in (spec for spec in resource_specs if spec.name in custom_help):
        presentation_specs.append(
            concept_parsers.ResourcePresentationSpec(
                TPU_YAML_SPEC_TEMPLATE[arg.name]['flag_name'],
                arg.GenerateResourceSpec(),
                arg.group_help,
                flag_name_overrides={
                    n: ''
                    for n in TPU_YAML_SPEC_TEMPLATE[arg.name]['removed_flags']
                },
                required=True))
    concept_parsers.ConceptParser(presentation_specs).AddToParser(parser)
    # Not using Tensorflow resource arg here due to parsing conflict with zone
    # attribute and its ultimately passed only as string to API
    base.Argument(
        '--version',
        required=True,
        help='The Tensorflow version to Reimage Cloud TPU with.').AddToParser(
            parser)
Beispiel #6
0
def CreateNotificationChannelResourceArg(arg_name, extra_help, required=True,
                                         plural=False):
  """Create a resource argument for a Cloud Monitoring Notification Channel.

  Args:
    arg_name: str, the name for the arg.
    extra_help: str, the extra_help to describe the resource. This should start
      with the verb, such as 'to update', that is acting on the resource.
    required: bool, if the arg is required.
    plural: bool, if True, use a resource argument that returns a list.

  Returns:
    the PresentationSpec for the resource argument.
  """
  if plural:
    help_stem = 'Names of one or more Notification Channels '
  else:
    help_stem = 'Name of the Notification Channel '

  return concept_parsers.ResourcePresentationSpec(
      arg_name,
      GetNotificationChannelResourceSpec(),
      help_stem + extra_help,
      required=required,
      plural=plural)
Beispiel #7
0
def CreateAssetResourceArg(positional=False):
    return concept_parsers.ResourcePresentationSpec(
        'asset' if positional else '--asset',
        _GetAssetResourceSpec(),
        group_help='The asset reference.',
        required=True,
        prefixes=False)
Beispiel #8
0
def CreateTopicResourceArg(verb, positional=True, plural=False):
    """Create a resource argument for a Cloud Pub/Sub Topic.

  Args:
    verb: str, the verb to describe the resource, such as 'to update'.
    positional: bool, if True, means that the topic ID is a positional rather
      than a flag. If not positional, this also creates a '--topic-project' flag
      as subscriptions and topics do not need to be in the same project.
    plural: bool, if True, use a resource argument that returns a list.

  Returns:
    the PresentationSpec for the resource argument.
  """
    if positional:
        name = 'topic'
        flag_name_overrides = {}
    else:
        name = '--topic' if not plural else '--topics'
        flag_name_overrides = {'project': '--topic-project'}
    help_stem = 'Name of the topic'
    if plural:
        help_stem = 'One or more topics'
    return concept_parsers.ResourcePresentationSpec(
        name,
        GetTopicResourceSpec(),
        '{} {}'.format(help_stem, verb),
        required=True,
        flag_name_overrides=flag_name_overrides,
        plural=plural,
        prefixes=True)
Beispiel #9
0
def CreateRepoResourcePresentationSpec(verb, positional=True):
  name = 'repo' if positional else '--repo'
  return concept_parsers.ResourcePresentationSpec(
      name,
      GetRepoResourceSpec(),
      'Name of the Cloud Source repository {}.'.format(verb),
      required=True,
  )
Beispiel #10
0
def CreateConditionResourceArg(verb):
  help_text = 'The name of the Condition to {}.'.format(verb)
  return concept_parsers.ResourcePresentationSpec(
      'condition',
      GetConditionResourceSpec(),
      help_text,
      required=True,
      prefixes=False)
Beispiel #11
0
def CreateTopicResourcePresentationSpec(verb, help_text, group):
    """Create add_topic, remove_topic or update_topic specs."""
    name = '--' + verb + '-topic'

    return concept_parsers.ResourcePresentationSpec(name,
                                                    GetTopicResourceSpec(),
                                                    help_text,
                                                    prefixes=True,
                                                    group=group)
Beispiel #12
0
def CreateAlertPolicyResourceArg(verb, positional=True):
  if positional:
    name = 'alert_policy'
  else:
    name = '--policy'
  help_text = 'Name of the Alert Policy ' + verb

  return concept_parsers.ResourcePresentationSpec(
      name,
      GetAlertPolicyResourceSpec(),
      help_text,
      required=True)
Beispiel #13
0
def CreateAnnotationResourceArg(plural=False, positional=False, required=True):
    name = 'annotation'
    help_text = 'An annotation reference.'
    if plural:
        name = 'annotations'
        help_text = 'A comma separated list of annotation references.'
    return concept_parsers.ResourcePresentationSpec(
        name if positional else ('--' + name),
        _GetProjectAnnotationResourceSpec(),
        help_text,
        plural=plural,
        prefixes=False,
        required=required)
Beispiel #14
0
def GetAuthorityNotePresentationSpec(base_name,
                                     group_help,
                                     required=True,
                                     positional=True):
    return concept_parsers.ResourcePresentationSpec(
        name=_FormatArgName(base_name, positional),
        concept_spec=_GetNoteResourceSpec(),
        group_help=group_help,
        required=required,
        flag_name_overrides={
            'project': _FormatArgName('{}-project'.format(base_name),
                                      positional),
        },
    )
Beispiel #15
0
def _GetPresentationSpec(resource_spec_path, **kwargs):
  resource_spec = module_util.ImportModule(resource_spec_path)
  if callable(resource_spec):
    resource_spec = resource_spec()
  flag_name_overrides = kwargs.pop('flag_name_overrides', '')
  flag_name_overrides = {
      o.split(':')[0]: o.split(':')[1] if ':' in o else ''
      for o in flag_name_overrides.split(';')}
  prefixes = kwargs.pop('prefixes', False)
  return concept_parsers.ResourcePresentationSpec(
      kwargs.pop('name', resource_spec.name),
      resource_spec,
      'help text',
      flag_name_overrides=flag_name_overrides,
      prefixes=prefixes,
      **kwargs)
Beispiel #16
0
def CreateTopicResourcePresentationSpec(verb, group):
  """Create add_topic, remove_topic or update_topic specs."""
  name = '--' + verb + '-topic'
  help_text_prefix = 'The Cloud Pub/Sub topic to '

  # TODO(b/77914513): pass the string directly and
  # clean up the help text in update.
  if verb == 'add':
    help_text = help_text_prefix + 'add to the repository.'
  elif verb == 'remove':
    help_text = help_text_prefix + 'remove from the repository.'
  else:
    help_text = help_text_prefix + 'update in the repository.'

  return concept_parsers.ResourcePresentationSpec(
      name, GetTopicResourceSpec(), help_text, prefixes=True, group=group)
Beispiel #17
0
def GetAuthorityPresentationSpec(group_help,
                                 base_name='authority',
                                 required=True,
                                 positional=True,
                                 use_global_project_flag=True):
    """Construct a resource spec for an attestation authority flag."""
    flag_overrides = None
    if not use_global_project_flag:
        flag_overrides = {
            'project': _FormatArgName('{}-project'.format(base_name),
                                      positional),
        }
    return concept_parsers.ResourcePresentationSpec(
        name=_FormatArgName(base_name, positional),
        concept_spec=_GetAuthorityResourceSpec(),
        group_help=group_help,
        required=required,
        flag_name_overrides=flag_overrides,
    )
def CreateSubscriptionResourceArg(verb, plural=False):
  """Create a resource argument for a Cloud Pub/Sub Subscription.

  Args:
    verb: str, the verb to describe the resource, such as 'to update'.
    plural: bool, if True, use a resource argument that returns a list.

  Returns:
    the PresentationSpec for the resource argument.
  """
  if plural:
    help_stem = 'One or more subscriptions'
  else:
    help_stem = 'Name of the subscription'
  return concept_parsers.ResourcePresentationSpec(
      'subscription',
      GetSubscriptionResourceSpec(),
      '{} {}'.format(help_stem, verb),
      required=True,
      plural=plural)