Feature 10962 Refactoring of osmclient commands
[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 import logging
24
25
26 class VCA(object):
27 def __init__(self, http=None, client=None):
28 self._http = http
29 self._client = client
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
36 )
37
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
43 )
44 resp = json.loads(resp) if resp else {}
45 if "id" not in resp:
46 raise ClientException("unexpected response from server - {}".format(resp))
47 print(resp["id"])
48
49 def update(self, name, vca):
50 self._logger.debug("")
51 self._client.get_token()
52 vca_id = self.get(name)["_id"]
53 self._http.patch_cmd(
54 endpoint="{}/{}".format(self._apiBase, vca_id),
55 postfields_dict=vca,
56 )
57
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"]:
63 return vca["_id"]
64 raise NotFound("VCA {} not found".format(name))
65
66 def delete(self, name, force=False):
67 self._logger.debug("")
68 self._client.get_token()
69 vca_id = name
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)
75 )
76 if http_code == 202:
77 print("Deletion in progress")
78 elif http_code == 204:
79 print("Deleted")
80 else:
81 msg = resp or ""
82 raise ClientException("failed to delete VCA {} - {}".format(name, msg))
83
84 def list(self, cmd_filter=None):
85 """Returns a list of K8s clusters"""
86 self._logger.debug("")
87 self._client.get_token()
88 filter_string = ""
89 if cmd_filter:
90 filter_string = "?{}".format(cmd_filter)
91 _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
92 if resp:
93 return json.loads(resp)
94 return list()
95
96 def get(self, name):
97 """Returns a VCA based on name or id"""
98 self._logger.debug("")
99 self._client.get_token()
100 vca_id = name
101 if not utils.validate_uuid4(name):
102 vca_id = self.get_id(name)
103 try:
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))
108 return resp
109 except NotFound:
110 raise NotFound("VCA {} not found".format(name))