Feature 10909: Heal operation for VDU
[osm/osmclient.git] / osmclient / sol005 / vca.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 K8s cluster API handling
17 """
18
19 from osmclient.common import utils
20 from osmclient.common.exceptions import NotFound
21 from osmclient.common.exceptions import ClientException
22 import json
23
24
25 class VCA(object):
26 def __init__(self, http=None, client=None):
27 self._http = http
28 self._client = client
29 self._apiName = "/admin"
30 self._apiVersion = "/v1"
31 self._apiResource = "/vca"
32 self._apiBase = "{}{}{}".format(
33 self._apiName, self._apiVersion, self._apiResource
34 )
35
36 def create(self, name, vca):
37 self._client.get_token()
38 http_code, resp = self._http.post_cmd(
39 endpoint=self._apiBase, postfields_dict=vca
40 )
41 resp = json.loads(resp) if resp else {}
42 if "id" not in resp:
43 raise ClientException("unexpected response from server - {}".format(resp))
44 print(resp["id"])
45
46 def update(self, name, vca):
47 self._client.get_token()
48 vca_id = self.get(name)["_id"]
49 self._http.patch_cmd(
50 endpoint="{}/{}".format(self._apiBase, vca_id),
51 postfields_dict=vca,
52 )
53
54 def get_id(self, name):
55 """Returns a VCA id from a VCA name"""
56 for vca in self.list():
57 if name == vca["name"]:
58 return vca["_id"]
59 raise NotFound("VCA {} not found".format(name))
60
61 def delete(self, name, force=False):
62 self._client.get_token()
63 vca_id = name
64 if not utils.validate_uuid4(name):
65 vca_id = self.get_id(name)
66 querystring = "?FORCE=True" if force else ""
67 http_code, resp = self._http.delete_cmd(
68 "{}/{}{}".format(self._apiBase, vca_id, querystring)
69 )
70 if http_code == 202:
71 print("Deletion in progress")
72 elif http_code == 204:
73 print("Deleted")
74 else:
75 msg = resp or ""
76 raise ClientException("failed to delete VCA {} - {}".format(name, msg))
77
78 def list(self, cmd_filter=None):
79 """Returns a list of K8s clusters"""
80 self._client.get_token()
81 filter_string = ""
82 if cmd_filter:
83 filter_string = "?{}".format(cmd_filter)
84 _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
85 if resp:
86 return json.loads(resp)
87 return list()
88
89 def get(self, name):
90 """Returns a VCA based on name or id"""
91 self._client.get_token()
92 vca_id = name
93 if not utils.validate_uuid4(name):
94 vca_id = self.get_id(name)
95 try:
96 _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, vca_id))
97 resp = json.loads(resp) if resp else {}
98 if "_id" not in resp:
99 raise ClientException("failed to get VCA info: {}".format(resp))
100 return resp
101 except NotFound:
102 raise NotFound("VCA {} not found".format(name))