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"""Interconnect."""
16
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22
23class Interconnect(object):
24  """Abstracts Interconnect resource."""
25
26  def __init__(self, ref, compute_client=None):
27    self.ref = ref
28    self._compute_client = compute_client
29
30  @property
31  def _client(self):
32    return self._compute_client.apitools_client
33
34  def _MakeCreateRequestTuple(self, description, location, interconnect_type,
35                              requested_link_count, link_type, admin_enabled,
36                              noc_contact_email, customer_name):
37    return (self._client.interconnects, 'Insert',
38            self._messages.ComputeInterconnectsInsertRequest(
39                project=self.ref.project,
40                interconnect=self._messages.Interconnect(
41                    name=self.ref.Name(),
42                    description=description,
43                    interconnectType=interconnect_type,
44                    linkType=link_type,
45                    nocContactEmail=noc_contact_email,
46                    requestedLinkCount=requested_link_count,
47                    location=location,
48                    adminEnabled=admin_enabled,
49                    customerName=customer_name)))
50
51  def _MakePatchRequestTuple(self, description, location, interconnect_type,
52                             requested_link_count, link_type, admin_enabled,
53                             noc_contact_email, labels, label_fingerprint):
54    kwargs = {}
55    if labels is not None:
56      kwargs['labels'] = labels
57    if label_fingerprint is not None:
58      kwargs['labelFingerprint'] = label_fingerprint
59    return (self._client.interconnects, 'Patch',
60            self._messages.ComputeInterconnectsPatchRequest(
61                interconnect=self.ref.Name(),
62                interconnectResource=self._messages.Interconnect(
63                    name=None,
64                    description=description,
65                    interconnectType=interconnect_type,
66                    linkType=link_type,
67                    nocContactEmail=noc_contact_email,
68                    requestedLinkCount=requested_link_count,
69                    location=location,
70                    adminEnabled=admin_enabled,
71                    **kwargs),
72                project=self.ref.project))
73
74  def _MakeDeleteRequestTuple(self):
75    return (self._client.interconnects, 'Delete',
76            self._messages.ComputeInterconnectsDeleteRequest(
77                project=self.ref.project, interconnect=self.ref.Name()))
78
79  def _MakeDescribeRequestTuple(self):
80    return (self._client.interconnects, 'Get',
81            self._messages.ComputeInterconnectsGetRequest(
82                project=self.ref.project, interconnect=self.ref.Name()))
83
84  def _MakeGetDiagnosticsRequestTuple(self):
85    return (self._client.interconnects, 'GetDiagnostics',
86            self._messages.ComputeInterconnectsGetDiagnosticsRequest(
87                project=self.ref.project, interconnect=self.ref.Name()))
88
89  @property
90  def _messages(self):
91    return self._compute_client.messages
92
93  def Create(self,
94             description='',
95             location=None,
96             interconnect_type=None,
97             requested_link_count=None,
98             link_type=None,
99             admin_enabled=False,
100             noc_contact_email=None,
101             customer_name=None,
102             only_generate_request=False):
103    """Create an interconnect."""
104    requests = [
105        self._MakeCreateRequestTuple(description, location, interconnect_type,
106                                     requested_link_count, link_type,
107                                     admin_enabled, noc_contact_email,
108                                     customer_name)
109    ]
110    if not only_generate_request:
111      resources = self._compute_client.MakeRequests(requests)
112      return resources[0]
113    return requests
114
115  def Delete(self, only_generate_request=False):
116    requests = [self._MakeDeleteRequestTuple()]
117    if not only_generate_request:
118      return self._compute_client.MakeRequests(requests)
119    return requests
120
121  def Describe(self, only_generate_request=False):
122    requests = [self._MakeDescribeRequestTuple()]
123    if not only_generate_request:
124      resources = self._compute_client.MakeRequests(requests)
125      return resources[0]
126    return requests
127
128  def GetDiagnostics(self, only_generate_request=False):
129    requests = [self._MakeGetDiagnosticsRequestTuple()]
130    if not only_generate_request:
131      resources = self._compute_client.MakeRequests(requests)
132      return resources[0]
133    return requests
134
135  def Patch(self,
136            description='',
137            location=None,
138            interconnect_type=None,
139            requested_link_count=None,
140            link_type=None,
141            admin_enabled=False,
142            noc_contact_email=None,
143            only_generate_request=False,
144            labels=None,
145            label_fingerprint=None):
146    """Patch an interconnect."""
147    requests = [
148        self._MakePatchRequestTuple(description, location, interconnect_type,
149                                    requested_link_count, link_type,
150                                    admin_enabled, noc_contact_email, labels,
151                                    label_fingerprint)
152    ]
153    if not only_generate_request:
154      resources = self._compute_client.MakeRequests(requests)
155      return resources[0]
156    return requests
157