1# -*- coding: utf-8 -*- #
2# Copyright 2017 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"""Flags and helpers for the compute interconnects commands."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.calliope import actions as calliope_actions
22from googlecloudsdk.command_lib.compute import completers as compute_completers
23from googlecloudsdk.command_lib.compute import flags as compute_flags
24
25
26_INTERCONNECT_TYPE_CHOICES_GA = {
27    'DEDICATED': 'Dedicated private interconnect.',
28    'PARTNER': 'Partner interconnect. Only available to approved partners.',
29}
30
31_INTERCONNECT_TYPE_CHOICES_BETA_AND_ALPHA = {
32    'IT_PRIVATE':
33        'Dedicated private interconnect. (Warning: IT_PRIVATE is deprecated, '
34        'use DEDICATED instead.)',
35    'DEDICATED':
36        'Dedicated private interconnect.',
37    'PARTNER':
38        'Partner interconnect. Only available to approved partners.',
39}
40
41_LINK_TYPE_CHOICES = {
42    'LINK_TYPE_ETHERNET_10G_LR': '10Gbps Ethernet, LR Optics.',
43    'LINK_TYPE_ETHERNET_100G_LR': '100Gbps Ethernet, LR Optics.'
44}
45
46
47class InterconnectsCompleter(compute_completers.ListCommandCompleter):
48
49  def __init__(self, **kwargs):
50    super(InterconnectsCompleter, self).__init__(
51        collection='compute.interconnects',
52        list_command='alpha compute interconnects list --uri',
53        **kwargs)
54
55
56def InterconnectArgument(required=True, plural=False):
57  return compute_flags.ResourceArgument(
58      resource_name='interconnect',
59      completer=InterconnectsCompleter,
60      plural=plural,
61      required=required,
62      global_collection='compute.interconnects')
63
64
65def InterconnectArgumentForOtherResource(short_help,
66                                         required=True,
67                                         detailed_help=None):
68  return compute_flags.ResourceArgument(
69      name='--interconnect',
70      resource_name='interconnect',
71      completer=InterconnectsCompleter,
72      plural=False,
73      required=required,
74      global_collection='compute.interconnects',
75      short_help=short_help,
76      detailed_help=detailed_help)
77
78
79def GetInterconnectType(messages, interconnect_type_arg):
80  """Converts the interconnect type flag to a message enum.
81
82  Args:
83    messages: The API messages holder.
84    interconnect_type_arg: The interconnect type flag value.
85
86  Returns:
87    An InterconnectTypeValueValuesEnum of the flag value, or None if absent.
88  """
89  if interconnect_type_arg is None:
90    return None
91  else:
92    return messages.Interconnect.InterconnectTypeValueValuesEnum(
93        interconnect_type_arg)
94
95
96def GetLinkType(messages, link_type_arg):
97  """Converts the link type flag to a message enum.
98
99  Args:
100    messages: The API messages holder.
101    link_type_arg: The link type flag value.
102  Returns:
103    An LinkTypeValueValuesEnum of the flag value, or None if absent.
104  """
105  if link_type_arg is None:
106    return None
107  else:
108    return messages.Interconnect.LinkTypeValueValuesEnum(link_type_arg)
109
110
111def AddCreateCommonArgs(parser):
112  """Adds shared flags for create command to the argparse.ArgumentParser."""
113  AddAdminEnabled(parser)
114  AddDescription(parser)
115  AddCustomerName(parser)
116  AddLinkType(parser)
117  AddNocContactEmail(parser)
118  AddRequestedLinkCount(parser)
119
120
121def AddCreateGaArgs(parser):
122  """Adds GA flags for create command to the argparse.ArgumentParser."""
123  AddCreateCommonArgs(parser)
124  AddInterconnectTypeGA(parser)
125
126
127def AddCreateBetaArgs(parser):
128  """Adds beta flags for create command to the argparse.ArgumentParser."""
129  AddCreateCommonArgs(parser)
130  AddInterconnectTypeBetaAndAlpha(parser)
131
132
133def AddDescription(parser):
134  """Adds description flag to the argparse.ArgumentParser."""
135  parser.add_argument(
136      '--description',
137      help='An optional, textual description for the interconnect.')
138
139
140def AddInterconnectTypeGA(parser):
141  """Adds interconnect-type flag to the argparse.ArgumentParser."""
142  parser.add_argument(
143      '--interconnect-type',
144      choices=_INTERCONNECT_TYPE_CHOICES_GA,
145      required=True,
146      help="""\
147      Type of the interconnect.
148      """)
149
150
151def _ShouldShowDeprecatedWarning(value):
152  return value and value.upper() == 'IT_PRIVATE'
153
154
155def AddInterconnectTypeBetaAndAlpha(parser):
156  """Adds interconnect-type flag to the argparse.ArgumentParser."""
157  parser.add_argument(
158      '--interconnect-type',
159      choices=_INTERCONNECT_TYPE_CHOICES_BETA_AND_ALPHA,
160      action=calliope_actions.DeprecationAction(
161          'interconnect-type',
162          removed=False,
163          show_add_help=False,
164          show_message=_ShouldShowDeprecatedWarning,
165          warn=('IT_PRIVATE will be deprecated '
166                'for {flag_name}. '
167                'Please use DEDICATED instead.'),
168          error='Value IT_PRIVATE for {flag_name} has been removed. '
169                'Please use DEDICATED instead.'),
170      required=True,
171      help="""\
172      Type of the interconnect.
173      """)
174
175
176def AddLinkType(parser):
177  """Adds link-type flag to the argparse.ArgumentParser."""
178  link_types = _LINK_TYPE_CHOICES
179  parser.add_argument(
180      '--link-type',
181      choices=link_types,
182      required=True,
183      help="""\
184      Type of the link for the interconnect.
185      """)
186
187
188def AddRequestedLinkCount(parser):
189  """Adds requestedLinkCount flag to the argparse.ArgumentParser."""
190  parser.add_argument(
191      '--requested-link-count',
192      required=True,
193      type=int,
194      help="""\
195      Target number of physical links in the link bundle.
196      """)
197
198
199def AddRequestedLinkCountForUpdate(parser):
200  """Adds requestedLinkCount flag to the argparse.ArgumentParser."""
201  parser.add_argument(
202      '--requested-link-count',
203      type=int,
204      help="""\
205      Target number of physical links in the link bundle.
206      """)
207
208
209def AddNocContactEmail(parser):
210  """Adds nocContactEmail flag to the argparse.ArgumentParser."""
211  parser.add_argument(
212      '--noc-contact-email',
213      help="""\
214      Email address to contact the customer NOC for operations and maintenance
215      notifications regarding this interconnect.
216      """)
217
218
219def AddCustomerName(parser):
220  """Adds customerName flag to the argparse.ArgumentParser."""
221  parser.add_argument(
222      '--customer-name',
223      required=True,
224      help="""\
225      Customer name to put in the Letter of Authorization as the party
226      authorized to request an interconnect.
227      """)
228
229
230def AddAdminEnabled(parser):
231  """Adds adminEnabled flag to the argparse.ArgumentParser."""
232  admin_enabled_args = parser.add_mutually_exclusive_group()
233  admin_enabled_args.add_argument(
234      '--admin-enabled',
235      action='store_true',
236      default=None,
237      help="""\
238      Administrative status of the interconnect. If not provided on creation,
239      defaults to enabled.
240      When this is enabled, the interconnect is operational and will carry
241      traffic across any functioning linked interconnect attachments. Use
242      --no-admin-enabled to disable it.
243      """)
244
245
246def AddAdminEnabledForUpdate(parser):
247  """Adds adminEnabled flag to the argparse.ArgumentParser."""
248  admin_enabled_args = parser.add_mutually_exclusive_group()
249  admin_enabled_args.add_argument(
250      '--admin-enabled',
251      action='store_true',
252      default=None,
253      help="""\
254      Administrative status of the interconnect.
255      When this is enabled, the interconnect is operational and will carry
256      traffic across any functioning linked interconnect attachments. Use
257      --no-admin-enabled to disable it.
258      """)
259