1# -*- coding: utf-8 -*- #
2# Copyright 2016 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Module for resource_args API support."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.calliope.concepts import concepts
22from googlecloudsdk.command_lib.util.apis import yaml_data
23from googlecloudsdk.command_lib.util.concepts import concept_parsers
24from googlecloudsdk.command_lib.util.concepts import presentation_specs
25
26
27import six
28
29
30def AddResourceArgToParser(parser,
31                           resource_path,
32                           help_text,
33                           name=None,
34                           required=True):
35  """Adds a resource argument in a python command.
36
37  Args:
38    parser: the parser for the command.
39    resource_path: string, the resource_path which refers to the resources.yaml.
40    help_text: string, the help text of the resource argument.
41    name: string, the default is the name specified in the resources.yaml file.
42    required: boolean, the default is True because in most cases resource arg is
43      required.
44  """
45  resource_yaml_data = yaml_data.ResourceYAMLData.FromPath(resource_path)
46  resource_spec = concepts.ResourceSpec.FromYaml(resource_yaml_data.GetData())
47  concept_parsers.ConceptParser.ForResource(
48      name=(name if name else resource_yaml_data.GetArgName()),
49      resource_spec=resource_spec,
50      group_help=help_text,
51      required=required).AddToParser(parser)
52
53
54def GetResourcePresentationSpec(name, verb, resource_data,
55                                attribute_overrides=None,
56                                help_text='The {name} {verb}.',
57                                required=False, prefixes=True,
58                                positional=False):
59  """Build ResourcePresentationSpec for a Resource.
60
61  Args:
62    name: string, name of resource anchor argument.
63    verb: string, the verb to describe the resource, such as 'to create'.
64    resource_data: dict, the parsed data from a resources.yaml file under
65        command_lib/.
66    attribute_overrides: dict{string:string}, map of resource attribute names to
67      override in the generated resrouce spec.
68    help_text: string, the help text for the entire resource arg group. Should
69      have 2 format format specifiers (`{name}`, `{verb}`) to insert the
70      name and verb repectively.
71    required: bool, whether or not this resource arg is required.
72    prefixes: bool, if True the resource name will be used as a prefix for
73      the flags in the resource group.
74    positional: bool, if True, means that the resource arg is a positional
75      rather than a flag.
76  Returns:
77    ResourcePresentationSpec, presentation spec for resource.
78  """
79  arg_name = name if positional else '--' + name
80  arg_help = help_text.format(verb=verb, name=name)
81
82  if attribute_overrides:
83    for attribute_name, value in six.iteritems(attribute_overrides):
84      for attr in resource_data['attributes']:
85        if attr['attribute_name'] == attribute_name:
86          attr['attribute_name'] = value
87
88  return presentation_specs.ResourcePresentationSpec(
89      arg_name,
90      concepts.ResourceSpec.FromYaml(resource_data),
91      arg_help,
92      required=required,
93      prefixes=prefixes
94  )
95