3dcadf6ed061e9c341c37a5c69b4339a8f0a59b2
1 # Copyright 2021 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from osmclient
.common
import utils
20 from osmclient
.common
.exceptions
import ClientException
, NotFound
25 def __init__(self
, http
=None, client
=None):
28 self
._apiName
= "/admin"
29 self
._apiVersion
= "/v1"
30 self
._apiResource
= "/paas"
31 self
._apiBase
= "{}{}{}".format(
32 self
._apiName
, self
._apiVersion
, self
._apiResource
35 def _is_id(self
, name
):
36 return utils
.validate_uuid4(name
)
38 def create(self
, paas
):
41 paas (dict): includes the PaaS information.
46 self
._client
.get_token()
47 http_code
, resp
= self
._http
.post_cmd(
48 endpoint
=self
._apiBase
, postfields_dict
=paas
50 resp
= json
.loads(resp
) if resp
else {}
52 raise ClientException("unexpected response from server - {}".format(resp
))
53 print("PaaS {} created with ID {}".format(paas
["name"], resp
["id"]))
55 def update(self
, name
, paas
):
56 """Updates a PaaS based on name or ID.
58 name (str): PaaS name or ID.
59 paas (dict): includes the new PaaS information to update.
62 NotFound: if PaaS does not exists in DB.
65 self
._client
.get_token()
67 if not self
._is
_id
(name
):
68 paas_id
= self
.get(name
)["_id"]
70 endpoint
="{}/{}".format(self
._apiBase
, paas_id
), postfields_dict
=paas
73 raise NotFound("PaaS {} not found".format(name
))
75 def get_id(self
, name
):
76 """Returns a PaaS ID from a PaaS name.
78 name (str): PaaS name.
80 NotFound: if PaaS does not exists in DB.
82 for paas
in self
.list():
83 if name
== paas
["name"]:
85 raise NotFound("PaaS {} not found".format(name
))
87 def delete(self
, name
, force
=False):
88 """Deletes a PaaS based on name or ID.
90 name (str): PaaS name or ID.
91 force (bool): if True, PaaS is deleted without any check.
93 NotFound: if PaaS does not exists in DB.
94 ClientException: if delete fails.
96 self
._client
.get_token()
98 querystring
= "?FORCE=True" if force
else ""
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
)
106 raise NotFound("PaaS {} not found".format(name
))
108 print("Deletion in progress")
109 elif http_code
== 204:
113 raise ClientException("Failed to delete PaaS {} - {}".format(name
, msg
))
115 def list(self
, cmd_filter
=None):
116 """Returns a list of PaaS"""
117 self
._client
.get_token()
120 filter_string
= "?{}".format(cmd_filter
)
121 _
, resp
= self
._http
.get2_cmd("{}{}".format(self
._apiBase
, filter_string
))
123 return json
.loads(resp
)
127 """Returns a PaaS based on name or id.
129 name (str): PaaS name or ID.
132 NotFound: if PaaS does not exists in DB.
133 ClientException: if get fails.
135 self
._client
.get_token()
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
))
146 raise NotFound("PaaS {} not found".format(name
))