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
16"""Base classes for abstracting away common logic for Deployment Manager."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.api_lib.util import apis
23from googlecloudsdk.core import properties
24from googlecloudsdk.core import resources
25
26
27class DmApiVersion(object):
28  """An enum representing the API version of Deployment Manager.
29
30  The DM API version controls which version of DM API to use for a certain
31  command under certain release track.
32  """
33
34  class _VERSION(object):
35    """An enum representing the API version of Deployment Manager."""
36
37    # pylint: disable=redefined-builtin
38    def __init__(self, id, help_tag, help_note):
39      self.id = id
40      self.help_tag = help_tag
41      self.help_note = help_note
42
43    def __str__(self):
44      return self.id
45
46    def __eq__(self, other):
47      return self.id == other.id
48
49  V2 = _VERSION('v2', None, None)
50
51  ALPHA = _VERSION(
52      'alpha',
53      '{0}(ALPHA){0} '.format('*'),
54      'The DM API currently used is ALPHA and may change without notice.')
55
56  V2BETA = _VERSION(
57      'v2beta',
58      '{0}(V2BETA){0} '.format('*'),
59      'The DM API currently used is V2BETA and may change without notice.')
60
61  _ALL = (V2, ALPHA, V2BETA)
62
63
64class DmCommand(object):
65  """DmCommand is a base class for Deployment Manager commands."""
66
67  _dm_version = DmApiVersion.V2
68  _dm_client = None
69  _dm_messages = None
70  _dm_resources = None
71
72  def __init__(self):
73    pass
74
75  @property
76  def version(self):
77    return self._dm_version
78
79  @property
80  def client(self):
81    """Specifies the DM client."""
82    if self._dm_client is None:
83      self._dm_client = apis.GetClientInstance('deploymentmanager',
84                                               self._dm_version.id)
85    return self._dm_client
86
87  @property
88  def messages(self):
89    """Specifies the DM messages."""
90    if self._dm_messages is None:
91      self._dm_messages = apis.GetMessagesModule('deploymentmanager',
92                                                 self._dm_version.id)
93    return self._dm_messages
94
95  @property
96  def resources(self):
97    """Specifies the resources parser for DM resources."""
98    if self._dm_resources is None:
99      self._dm_resources = resources.REGISTRY.Clone()
100      self._dm_resources.RegisterApiByName('deploymentmanager',
101                                           self._dm_version.id)
102    return self._dm_resources
103
104
105def UseDmApi(api_version):
106  """Mark this command class to use given Deployment Manager API version.
107
108  Args:
109    api_version: DM API version to use for the command
110
111  Returns:
112    The decorator function
113  """
114  def InitApiHolder(cmd_class):
115    """Wrapper function for the decorator."""
116    # pylint: disable=protected-access
117    cmd_class._dm_version = api_version
118    return cmd_class
119  return InitApiHolder
120
121
122def GetProject():
123  return properties.VALUES.core.project.Get(required=True)
124