1# -*- coding: utf-8 -*- #
2# Copyright 2018 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"""API library for managing the autoscalers of a managed instance group."""
16
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22
23def _IsZonalGroup(ref):
24  """Checks if reference to instance group is zonal."""
25  return ref.Collection() == 'compute.instanceGroupManagers'
26
27
28class Client(object):
29  """API client class for MIG Autoscalers."""
30
31  def __init__(self, client=None):
32    self._client = client
33
34  @property
35  def _service(self):
36    raise NotImplementedError
37
38  def _ScopeRequest(self, request, igm_ref):
39    raise NotImplementedError
40
41  @property
42  def message_type(self):
43    return self._client.messages.Autoscaler
44
45  def Update(self, igm_ref, autoscaler_resource):
46    request = self._service.GetRequestType('Update')(
47        project=igm_ref.project,
48        autoscaler=autoscaler_resource.name,
49        autoscalerResource=autoscaler_resource)
50    self._ScopeRequest(request, igm_ref)
51    return self._client.MakeRequests([(self._service, 'Update', request)])
52
53  def Patch(self, igm_ref, autoscaler_resource):
54    request = self._service.GetRequestType('Patch')(
55        project=igm_ref.project,
56        autoscaler=autoscaler_resource.name,
57        autoscalerResource=autoscaler_resource)
58    self._ScopeRequest(request, igm_ref)
59    return self._client.MakeRequests([(self._service, 'Patch', request)])
60
61  def Insert(self, igm_ref, autoscaler_resource):
62    request = self._service.GetRequestType('Insert')(
63        project=igm_ref.project,
64        autoscaler=autoscaler_resource,
65    )
66    self._ScopeRequest(request, igm_ref)
67    return self._client.MakeRequests([(self._service, 'Insert', request)])
68
69  def Delete(self, igm_ref, autoscaler_name):
70    request = self._service.GetRequestType('Delete')(
71        project=igm_ref.project,
72        autoscaler=autoscaler_name)
73    self._ScopeRequest(request, igm_ref)
74    return self._client.MakeRequests([(self._service, 'Delete', request)])
75
76
77class RegionalClient(Client):
78
79  @property
80  def _service(self):
81    return self._client.apitools_client.regionAutoscalers
82
83  def _ScopeRequest(self, request, igm_ref):
84    request.region = igm_ref.region
85
86
87class ZonalClient(Client):
88
89  @property
90  def _service(self):
91    return self._client.apitools_client.autoscalers
92
93  def _ScopeRequest(self, request, igm_ref):
94    request.zone = igm_ref.zone
95
96
97def GetClient(client, igm_ref):
98  if _IsZonalGroup(igm_ref):
99    return ZonalClient(client)
100  else:
101    return RegionalClient(client)
102