Addition of PaaS
[osm/osmclient.git] / osmclient / sol005 / paas.py
1 # Copyright 2021 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 OSM PaaS API handling
17 """
18
19 from osmclient.common import utils
20 from osmclient.common.exceptions import ClientException, NotFound
21 import json
22
23
24 class PAAS(object):
25 def __init__(self, http=None, client=None):
26 self._http = http
27 self._client = client
28 self._apiName = "/admin"
29 self._apiVersion = "/v1"
30 self._apiResource = "/paas"
31 self._apiBase = "{}{}{}".format(
32 self._apiName, self._apiVersion, self._apiResource
33 )
34
35 def _is_id(self, name):
36 return utils.validate_uuid4(name)
37
38 def create(self, paas):
39 """Create PaaS.
40 Args:
41 paas (dict): includes the PaaS information.
42
43 Raises:
44 ClientException
45 """
46 self._client.get_token()
47 http_code, resp = self._http.post_cmd(
48 endpoint=self._apiBase, postfields_dict=paas
49 )
50 resp = json.loads(resp) if resp else {}
51 if "id" not in resp:
52 raise ClientException("unexpected response from server - {}".format(resp))
53 print("PaaS {} created with ID {}".format(paas["name"], resp["id"]))
54
55 def update(self, name, paas):
56 """Updates a PaaS based on name or ID.
57 Args:
58 name (str): PaaS name or ID.
59 paas (dict): includes the new PaaS information to update.
60
61 Raises:
62 NotFound: if PaaS does not exists in DB.
63 """
64 paas_id = name
65 self._client.get_token()
66 try:
67 if not self._is_id(name):
68 paas_id = self.get(name)["_id"]
69 self._http.patch_cmd(
70 endpoint="{}/{}".format(self._apiBase, paas_id), postfields_dict=paas
71 )
72 except NotFound:
73 raise NotFound("PaaS {} not found".format(name))
74
75 def get_id(self, name):
76 """Returns a PaaS ID from a PaaS name.
77 Args:
78 name (str): PaaS name.
79 Raises:
80 NotFound: if PaaS does not exists in DB.
81 """
82 for paas in self.list():
83 if name == paas["name"]:
84 return paas["_id"]
85 raise NotFound("PaaS {} not found".format(name))
86
87 def delete(self, name, force=False):
88 """Deletes a PaaS based on name or ID.
89 Args:
90 name (str): PaaS name or ID.
91 force (bool): if True, PaaS is deleted without any check.
92 Raises:
93 NotFound: if PaaS does not exists in DB.
94 ClientException: if delete fails.
95 """
96 self._client.get_token()
97 paas_id = name
98 querystring = "?FORCE=True" if force else ""
99 try:
100 if not self._is_id(name):
101 paas_id = self.get_id(name)
102 http_code, resp = self._http.delete_cmd(
103 "{}/{}{}".format(self._apiBase, paas_id, querystring)
104 )
105 except NotFound:
106 raise NotFound("PaaS {} not found".format(name))
107 if http_code == 202:
108 print("Deletion in progress")
109 elif http_code == 204:
110 print("Deleted")
111 else:
112 msg = resp or ""
113 raise ClientException("Failed to delete PaaS {} - {}".format(name, msg))
114
115 def list(self, cmd_filter=None):
116 """Returns a list of PaaS"""
117 self._client.get_token()
118 filter_string = ""
119 if cmd_filter:
120 filter_string = "?{}".format(cmd_filter)
121 _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
122 if resp:
123 return json.loads(resp)
124 return list()
125
126 def get(self, name):
127 """Returns a PaaS based on name or id.
128 Args:
129 name (str): PaaS name or ID.
130
131 Raises:
132 NotFound: if PaaS does not exists in DB.
133 ClientException: if get fails.
134 """
135 self._client.get_token()
136 paas_id = name
137 try:
138 if not self._is_id(name):
139 paas_id = self.get_id(name)
140 _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, paas_id))
141 resp = json.loads(resp) if resp else {}
142 if "_id" not in resp:
143 raise ClientException("Failed to get PaaS info: {}".format(resp))
144 return resp
145 except NotFound:
146 raise NotFound("PaaS {} not found".format(name))