1# -*- coding: utf-8 -*- #
2# Copyright 2019 Google Inc. 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"""Shared resource flags for `gcloud service-directory` commands."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.calliope.concepts import concepts
22from googlecloudsdk.calliope.concepts import deps
23from googlecloudsdk.command_lib.util.concepts import concept_parsers
24from googlecloudsdk.core import properties
25
26_PROJECT = properties.VALUES.core.project
27
28
29def ProjectAttributeConfig():
30  """Gets project resource attribute with default value."""
31  return concepts.ResourceParameterAttributeConfig(
32      name='project',
33      help_text='The name of the project for the {resource}.',
34      fallthroughs=[deps.PropertyFallthrough(_PROJECT)])
35
36
37def LocationAttributeConfig():
38  """Gets location resource attribute."""
39  return concepts.ResourceParameterAttributeConfig(
40      name='location', help_text='The name of the region for the {resource}.')
41
42
43def NamespaceAttributeConfig():
44  """Gets namespace resource attribute."""
45  return concepts.ResourceParameterAttributeConfig(
46      name='namespace',
47      help_text='The name of the namespace for the {resource}.')
48
49
50def ServiceAttributeConfig():
51  """Gets service resource attribute."""
52  return concepts.ResourceParameterAttributeConfig(
53      name='service', help_text='The name of the service for the {resource}.')
54
55
56def EndpointAttributeConfig():
57  """Gets endpoint resource attribute."""
58  return concepts.ResourceParameterAttributeConfig(
59      name='endpoint', help_text='The name of the endpoint for the {resource}.')
60
61
62def GetProjectResourceSpec():
63  """Gets project resource spec."""
64  return concepts.ResourceSpec(
65      'servicedirectory.projects',
66      resource_name='project',
67      projectsId=ProjectAttributeConfig())
68
69
70def GetLocationResourceSpec():
71  """Gets location resource spec."""
72  return concepts.ResourceSpec(
73      'servicedirectory.projects.locations',
74      resource_name='location',
75      locationsId=LocationAttributeConfig(),
76      projectsId=ProjectAttributeConfig())
77
78
79def GetNamespaceResourceSpec():
80  """Gets namespace resource spec."""
81  return concepts.ResourceSpec(
82      'servicedirectory.projects.locations.namespaces',
83      resource_name='namespace',
84      namespacesId=NamespaceAttributeConfig(),
85      locationsId=LocationAttributeConfig(),
86      projectsId=ProjectAttributeConfig())
87
88
89def GetServiceResourceSpec():
90  """Gets service resource spec."""
91  return concepts.ResourceSpec(
92      'servicedirectory.projects.locations.namespaces.services',
93      resource_name='service',
94      servicesId=ServiceAttributeConfig(),
95      namespacesId=NamespaceAttributeConfig(),
96      locationsId=LocationAttributeConfig(),
97      projectsId=ProjectAttributeConfig())
98
99
100def GetEndpointResourceSpec():
101  """Gets endpoint resource spec."""
102  return concepts.ResourceSpec(
103      'servicedirectory.projects.locations.namespaces.services.endpoints',
104      resource_name='endpoint',
105      endpointsId=EndpointAttributeConfig(),
106      servicesId=ServiceAttributeConfig(),
107      namespacesId=NamespaceAttributeConfig(),
108      locationsId=LocationAttributeConfig(),
109      projectsId=ProjectAttributeConfig())
110
111
112def AddProjectResourceArg(parser, verb, positional=True):
113  """Adds a resource argument for a Service Directory project."""
114  name = 'project' if positional else '--project'
115  return concept_parsers.ConceptParser.ForResource(
116      name,
117      GetProjectResourceSpec(),
118      'The Service Directory project {}'.format(verb),
119      required=True).AddToParser(parser)
120
121
122def AddLocationResourceArg(parser, verb, positional=True):
123  """Adds a resource argument for a Service Directory location."""
124  name = 'location' if positional else '--location'
125  return concept_parsers.ConceptParser.ForResource(
126      name,
127      GetLocationResourceSpec(),
128      'The Service Directory location {}'.format(verb),
129      required=True).AddToParser(parser)
130
131
132def AddNamespaceResourceArg(parser, verb, positional=True):
133  """Adds a resource argument for a Service Directory namespace."""
134  name = 'namespace' if positional else '--namespace'
135  return concept_parsers.ConceptParser.ForResource(
136      name,
137      GetNamespaceResourceSpec(),
138      'The Service Directory namespace {}'.format(verb),
139      required=True).AddToParser(parser)
140
141
142def AddServiceResourceArg(parser, verb, positional=True):
143  """Adds a resource argument for a Service Directory service."""
144  name = 'service' if positional else '--service'
145  return concept_parsers.ConceptParser.ForResource(
146      name,
147      GetServiceResourceSpec(),
148      'The Service Directory service {}'.format(verb),
149      required=True).AddToParser(parser)
150
151
152def AddEndpointResourceArg(parser, verb, positional=True):
153  """Adds a resource argument for a Service Directory endpoint."""
154  name = 'endpoint' if positional else '--endpoint'
155  return concept_parsers.ConceptParser.ForResource(
156      name,
157      GetEndpointResourceSpec(),
158      'The Service Directory endpoint {}'.format(verb),
159      required=True).AddToParser(parser)
160