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"""Command for updating interconnects."""
16
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.api_lib.compute import base_classes
23from googlecloudsdk.api_lib.compute.interconnects import client
24from googlecloudsdk.calliope import base
25from googlecloudsdk.command_lib.compute.interconnects import flags
26from googlecloudsdk.command_lib.util.args import labels_util
27
28
29def _ArgsCommon(cls, parser, support_labels=False):
30  """Shared arguments for update commands."""
31  cls.INTERCONNECT_ARG = flags.InterconnectArgument()
32  cls.INTERCONNECT_ARG.AddArgument(parser, operation_type='update')
33
34  parser.add_argument(
35      '--description',
36      help='An optional, textual description for the interconnect.')
37  flags.AddAdminEnabledForUpdate(parser)
38  flags.AddNocContactEmail(parser)
39  flags.AddRequestedLinkCountForUpdate(parser)
40  if support_labels:
41    labels_util.AddUpdateLabelsFlags(parser)
42
43
44@base.ReleaseTracks(base.ReleaseTrack.GA)
45class Update(base.UpdateCommand):
46  """Update a Compute Engine interconnect.
47
48  *{command}* is used to update interconnects. An interconnect represents a
49  single specific connection between Google and the customer.
50  """
51  INTERCONNECT_ARG = None
52
53  @classmethod
54  def Args(cls, parser):
55    _ArgsCommon(cls, parser)
56
57  def Collection(self):
58    return 'compute.interconnects'
59
60  def _DoRun(self, args, support_labels=False):
61    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
62    ref = self.INTERCONNECT_ARG.ResolveAsResource(args, holder.resources)
63    interconnect = client.Interconnect(ref, compute_client=holder.client)
64
65    labels = None
66    label_fingerprint = None
67    if support_labels:
68      labels_diff = labels_util.Diff.FromUpdateArgs(args)
69      if labels_diff.MayHaveUpdates():
70        old_interconnect = interconnect.Describe()
71        labels = labels_diff.Apply(
72            holder.client.messages.Interconnect.LabelsValue,
73            old_interconnect.labels).GetOrNone()
74        if labels is not None:
75          label_fingerprint = old_interconnect.labelFingerprint
76
77    return interconnect.Patch(
78        description=args.description,
79        interconnect_type=None,
80        requested_link_count=args.requested_link_count,
81        link_type=None,
82        admin_enabled=args.admin_enabled,
83        noc_contact_email=args.noc_contact_email,
84        location=None,
85        labels=labels,
86        label_fingerprint=label_fingerprint
87    )
88
89  def Run(self, args):
90    self._DoRun(args)
91
92
93@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA)
94class UpdateLabels(Update):
95  """Update a Compute Engine interconnect.
96
97  *{command}* is used to update interconnects. An interconnect represents a
98  single specific connection between Google and the customer.
99  """
100
101  @classmethod
102  def Args(cls, parser):
103    _ArgsCommon(cls, parser, support_labels=True)
104
105  def Run(self, args):
106    self._DoRun(args, support_labels=True)
107