92ef49e6bd8ee06ae9c1b4c7ea6e032f8b324af8
[osm/osmclient.git] / osmclient / sol005 / k8scluster.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13 #
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 K8scluster(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 = "/k8sclusters"
32 self._apiBase = "{}{}{}".format(
33 self._apiName, self._apiVersion, self._apiResource
34 )
35
36 def _get_vim_account(self, vim_account):
37 vim = self._client.vim.get(vim_account)
38 if vim is None:
39 raise NotFound("cannot find vim account '{}'".format(vim_account))
40 return vim
41
42 def create(self, name, k8s_cluster):
43 self._client.get_token()
44 vim_account = self._get_vim_account(k8s_cluster["vim_account"])
45 k8s_cluster["vim_account"] = vim_account["_id"]
46 if "vca" in vim_account:
47 k8s_cluster["vca_id"] = vim_account["vca"]
48 http_code, resp = self._http.post_cmd(
49 endpoint=self._apiBase, postfields_dict=k8s_cluster
50 )
51 # print 'HTTP CODE: {}'.format(http_code)
52 # print 'RESP: {}'.format(resp)
53 # if http_code in (200, 201, 202, 204):
54 if resp:
55 resp = json.loads(resp)
56 if not resp or "id" not in resp:
57 raise ClientException("unexpected response from server - {}".format(resp))
58 print(resp["id"])
59 # else:
60 # msg = ""
61 # if resp:
62 # try:
63 # msg = json.loads(resp)
64 # except ValueError:
65 # msg = resp
66 # raise ClientException("failed to add K8s cluster {} - {}".format(name, msg))
67
68 def update(self, name, k8s_cluster):
69 self._client.get_token()
70 cluster = self.get(name)
71 if "vim_account" in k8s_cluster:
72 vim_account = self._get_vim_account(k8s_cluster["vim_account"])
73 k8s_cluster["vim_account"] = vim_account["_id"]
74 if "vca" in vim_account:
75 k8s_cluster["vca_id"] = vim_account["vca"]
76 http_code, resp = self._http.put_cmd(
77 endpoint="{}/{}".format(self._apiBase, cluster["_id"]),
78 postfields_dict=k8s_cluster,
79 )
80 # print 'HTTP CODE: {}'.format(http_code)
81 # print 'RESP: {}'.format(resp)
82 # if http_code in (200, 201, 202, 204):
83 # pass
84 # else:
85 # msg = ""
86 # if resp:
87 # try:
88 # msg = json.loads(resp)
89 # except ValueError:
90 # msg = resp
91 # raise ClientException("failed to update K8s cluster {} - {}".format(name, msg))
92
93 def get_id(self, name):
94 """Returns a K8s cluster id from a K8s cluster name"""
95 for cluster in self.list():
96 if name == cluster["name"]:
97 return cluster["_id"]
98 raise NotFound("K8s cluster {} not found".format(name))
99
100 def delete(self, name, force=False):
101 self._client.get_token()
102 cluster_id = name
103 if not utils.validate_uuid4(name):
104 cluster_id = self.get_id(name)
105 querystring = ""
106 if force:
107 querystring = "?FORCE=True"
108 http_code, resp = self._http.delete_cmd(
109 "{}/{}{}".format(self._apiBase, cluster_id, querystring)
110 )
111 # print 'HTTP CODE: {}'.format(http_code)
112 # print 'RESP: {}'.format(resp)
113 if http_code == 202:
114 print("Deletion in progress")
115 elif http_code == 204:
116 print("Deleted")
117 else:
118 msg = resp or ""
119 # if resp:
120 # try:
121 # msg = json.loads(resp)
122 # except ValueError:
123 # msg = resp
124 raise ClientException(
125 "failed to delete K8s cluster {} - {}".format(name, msg)
126 )
127
128 def list(self, filter=None):
129 """Returns a list of K8s clusters"""
130 self._client.get_token()
131 filter_string = ""
132 if filter:
133 filter_string = "?{}".format(filter)
134 _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
135 if resp:
136 return json.loads(resp)
137 return list()
138
139 def get(self, name):
140 """Returns a K8s cluster based on name or id"""
141 self._client.get_token()
142 cluster_id = name
143 if not utils.validate_uuid4(name):
144 cluster_id = self.get_id(name)
145 try:
146 _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, cluster_id))
147 if resp:
148 resp = json.loads(resp)
149 if not resp or "_id" not in resp:
150 raise ClientException("failed to get K8s cluster info: {}".format(resp))
151 return resp
152 except NotFound:
153 raise NotFound("K8s cluster {} not found".format(name))