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.
16 OSM K8s cluster API handling
19 from osmclient
.common
import utils
20 from osmclient
.common
.exceptions
import NotFound
21 from osmclient
.common
.exceptions
import ClientException
27 def __init__(self
, http
=None, client
=None):
30 self
._logger
= logging
.getLogger("osmclient")
31 self
._apiName
= "/admin"
32 self
._apiVersion
= "/v1"
33 self
._apiResource
= "/vca"
34 self
._apiBase
= "{}{}{}".format(
35 self
._apiName
, self
._apiVersion
, self
._apiResource
38 def create(self
, name
, vca
):
39 self
._logger
.debug("")
40 self
._client
.get_token()
41 http_code
, resp
= self
._http
.post_cmd(
42 endpoint
=self
._apiBase
, postfields_dict
=vca
44 resp
= json
.loads(resp
) if resp
else {}
46 raise ClientException("unexpected response from server - {}".format(resp
))
49 def update(self
, name
, vca
):
50 self
._logger
.debug("")
51 self
._client
.get_token()
52 vca_id
= self
.get(name
)["_id"]
54 endpoint
="{}/{}".format(self
._apiBase
, vca_id
),
58 def get_id(self
, name
):
59 self
._logger
.debug("")
60 """Returns a VCA id from a VCA name"""
61 for vca
in self
.list():
62 if name
== vca
["name"]:
64 raise NotFound("VCA {} not found".format(name
))
66 def delete(self
, name
, force
=False):
67 self
._logger
.debug("")
68 self
._client
.get_token()
70 if not utils
.validate_uuid4(name
):
71 vca_id
= self
.get_id(name
)
72 querystring
= "?FORCE=True" if force
else ""
73 http_code
, resp
= self
._http
.delete_cmd(
74 "{}/{}{}".format(self
._apiBase
, vca_id
, querystring
)
77 print("Deletion in progress")
78 elif http_code
== 204:
82 raise ClientException("failed to delete VCA {} - {}".format(name
, msg
))
84 def list(self
, cmd_filter
=None):
85 """Returns a list of K8s clusters"""
86 self
._logger
.debug("")
87 self
._client
.get_token()
90 filter_string
= "?{}".format(cmd_filter
)
91 _
, resp
= self
._http
.get2_cmd("{}{}".format(self
._apiBase
, filter_string
))
93 return json
.loads(resp
)
97 """Returns a VCA based on name or id"""
98 self
._logger
.debug("")
99 self
._client
.get_token()
101 if not utils
.validate_uuid4(name
):
102 vca_id
= self
.get_id(name
)
104 _
, resp
= self
._http
.get2_cmd("{}/{}".format(self
._apiBase
, vca_id
))
105 resp
= json
.loads(resp
) if resp
else {}
106 if "_id" not in resp
:
107 raise ClientException("failed to get VCA info: {}".format(resp
))
110 raise NotFound("VCA {} not found".format(name
))