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 creating partner customer interconnect attachments."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.api_lib.compute import base_classes
22from googlecloudsdk.api_lib.compute.interconnects.attachments import client
23from googlecloudsdk.calliope import base
24from googlecloudsdk.calliope import parser_errors
25from googlecloudsdk.command_lib.compute import flags as compute_flags
26from googlecloudsdk.command_lib.compute.interconnects.attachments import flags as attachment_flags
27from googlecloudsdk.command_lib.compute.routers import flags as router_flags
28from googlecloudsdk.core import log
29
30
31def PrintPairingKeyEpilog(pairing_key):
32  """Prints the pairing key help text upon command completion."""
33  message = """\
34      Please use the pairing key to provision the attachment with your partner:
35      {0}
36      """.format(pairing_key)
37  log.status.Print(message)
38
39
40@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA)
41class Create(base.CreateCommand):
42  """Create a Compute Engine partner interconnect attachment.
43
44  *{command}* is used to create partner interconnect attachments. A partner
45  interconnect attachment binds the underlying connectivity of a provider's
46  Interconnect to a path into and out of the customer's cloud network.
47  """
48  INTERCONNECT_ATTACHMENT_ARG = None
49  INTERCONNECT_ARG = None
50  ROUTER_ARG = None
51
52  @classmethod
53  def Args(cls, parser):
54
55    cls.ROUTER_ARG = router_flags.RouterArgumentForOtherResources()
56    cls.ROUTER_ARG.AddArgument(parser)
57
58    cls.INTERCONNECT_ATTACHMENT_ARG = (
59        attachment_flags.InterconnectAttachmentArgument())
60    cls.INTERCONNECT_ATTACHMENT_ARG.AddArgument(parser, operation_type='create')
61
62    attachment_flags.AddAdminEnabled(parser, default_behavior=False)
63    attachment_flags.AddEdgeAvailabilityDomain(parser)
64    attachment_flags.AddDescription(parser)
65    attachment_flags.AddMtu(parser)
66
67  def Run(self, args):
68    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
69    attachment_ref = self.INTERCONNECT_ATTACHMENT_ARG.ResolveAsResource(
70        args,
71        holder.resources,
72        scope_lister=compute_flags.GetDefaultScopeLister(holder.client))
73
74    interconnect_attachment = client.InterconnectAttachment(
75        attachment_ref, compute_client=holder.client)
76
77    if args.router_region is None:
78      args.router_region = attachment_ref.region
79
80    if args.router_region != attachment_ref.region:
81      raise parser_errors.ArgumentException(
82          'router-region must be same as the attachment region.')
83
84    router_ref = None
85    if args.router is not None:
86      router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
87
88    admin_enabled = attachment_flags.GetAdminEnabledFlag(args)
89
90    attachment = interconnect_attachment.CreateAlpha(
91        description=args.description,
92        router=router_ref,
93        attachment_type='PARTNER',
94        edge_availability_domain=args.edge_availability_domain,
95        admin_enabled=admin_enabled,
96        validate_only=getattr(args, 'dry_run', None),
97        mtu=getattr(args, 'mtu', None))
98    self._pairing_key = attachment.pairingKey
99    return attachment
100
101  def Epilog(self, resources_were_displayed=True):
102    PrintPairingKeyEpilog(self._pairing_key)
103
104
105@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
106class CreateAlpha(Create):
107  """Create a Compute Engine partner interconnect attachment.
108
109  *{command}* is used to create partner interconnect attachments. A partner
110  interconnect attachment binds the underlying connectivity of a provider's
111  Interconnect to a path into and out of the customer's cloud network.
112  """
113
114  @classmethod
115  def Args(cls, parser):
116    super(CreateAlpha, cls).Args(parser)
117    attachment_flags.AddDryRun(parser)
118