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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
16 OSM K8s cluster API handling
19 from osmclient
.common
import utils
20 from osmclient
.common
import wait
as WaitForStatus
21 from osmclient
.common
.exceptions
import NotFound
22 from osmclient
.common
.exceptions
import ClientException
27 class K8scluster(object):
28 def __init__(self
, http
=None, client
=None):
31 self
._logger
= logging
.getLogger("osmclient.k8scluster")
32 self
._apiName
= "/admin"
33 self
._apiVersion
= "/v1"
34 self
._apiResource
= "/k8sclusters"
35 self
._apiBase
= "{}{}{}".format(
36 self
._apiName
, self
._apiVersion
, self
._apiResource
39 def _get_vim_account(self
, vim_account
):
40 vim
= self
._client
.vim
.get(vim_account
)
42 raise NotFound("cannot find vim account '{}'".format(vim_account
))
46 def _wait(self
, id, wait_time
, deleteFlag
=False):
47 self
._logger
.debug("")
48 self
._client
.get_token()
49 # Endpoint to get operation status
50 apiUrlStatus
= "{}{}{}".format(self
._apiName
, self
._apiVersion
, self
._apiResource
)
51 # Wait for status for VIM instance creation/deletion
52 if isinstance(wait_time
, bool):
53 wait_time
= WaitForStatus
.TIMEOUT_VIM_OPERATION
54 WaitForStatus
.wait_for_status(
60 deleteFlag
=deleteFlag
,
63 def create(self
, name
, k8s_cluster
, wait
=False):
64 self
._client
.get_token()
65 vim_account
= self
._get
_vim
_account
(k8s_cluster
["vim_account"])
66 k8s_cluster
["vim_account"] = vim_account
["_id"]
67 if "vca" in vim_account
:
68 k8s_cluster
["vca_id"] = vim_account
["vca"]
69 http_code
, resp
= self
._http
.post_cmd(
70 endpoint
=self
._apiBase
, postfields_dict
=k8s_cluster
73 self
._logger
.debug('HTTP CODE: {}'.format(http_code
))
74 self
._logger
.debug('RESP: {}'.format(resp
))
77 resp
= json
.loads(resp
)
78 if not resp
or "id" not in resp
:
79 raise ClientException("unexpected response from server - {}".format(resp
))
81 # Wait for status for VIM instance creation
82 self
._wait
(resp
.get("id"), wait
)
88 # msg = json.loads(resp)
91 # raise ClientException("failed to add K8s cluster {} - {}".format(name, msg))
93 def update(self
, name
, k8s_cluster
, wait
=False):
94 self
._client
.get_token()
95 cluster
= self
.get(name
)
96 if "vim_account" in k8s_cluster
:
97 vim_account
= self
._get
_vim
_account
(k8s_cluster
["vim_account"])
98 k8s_cluster
["vim_account"] = vim_account
["_id"]
99 if "vca" in vim_account
:
100 k8s_cluster
["vca_id"] = vim_account
["vca"]
101 http_code
, resp
= self
._http
.patch_cmd(
102 endpoint
="{}/{}".format(self
._apiBase
, cluster
["_id"]),
103 postfields_dict
=k8s_cluster
,
107 wait_id
= cluster
["_id"]
108 self
._wait
(wait_id
, wait
)
110 self
._logger
.debug('HTTP CODE: {}'.format(http_code
))
111 self
._logger
.debug('RESP: {}'.format(resp
))
113 if http_code
in (200, 201, 202, 204):
119 msg
= json
.loads(resp
)
122 raise ClientException("failed to update K8s cluster {} - {}".format(name
, msg
))
124 def get_id(self
, name
):
125 """Returns a K8s cluster id from a K8s cluster name"""
126 for cluster
in self
.list():
127 if name
== cluster
["name"]:
128 return cluster
["_id"]
129 raise NotFound("K8s cluster {} not found".format(name
))
131 def delete(self
, name
, force
=False, wait
=False):
132 self
._client
.get_token()
134 if not utils
.validate_uuid4(name
):
135 cluster_id
= self
.get_id(name
)
138 querystring
= "?FORCE=True"
139 http_code
, resp
= self
._http
.delete_cmd(
140 "{}/{}{}".format(self
._apiBase
, cluster_id
, querystring
)
143 self
._logger
.debug('HTTP CODE: {}'.format(http_code
))
144 self
._logger
.debug('RESP: {}'.format(resp
))
151 resp
= json
.loads(resp
)
152 wait_id
= resp
.get("id")
154 self
._wait
(wait_id
, wait
, deleteFlag
=True)
156 print("Deletion in progress")
157 elif http_code
== 204:
163 # msg = json.loads(resp)
166 raise ClientException(
167 "failed to delete K8s cluster {} - {}".format(name
, msg
)
170 def list(self
, filter=None):
171 """Returns a list of K8s clusters"""
172 self
._client
.get_token()
175 filter_string
= "?{}".format(filter)
176 _
, resp
= self
._http
.get2_cmd("{}{}".format(self
._apiBase
, filter_string
))
178 return json
.loads(resp
)
182 """Returns a K8s cluster based on name or id"""
183 self
._client
.get_token()
185 if not utils
.validate_uuid4(name
):
186 cluster_id
= self
.get_id(name
)
188 _
, resp
= self
._http
.get2_cmd("{}/{}".format(self
._apiBase
, cluster_id
))
190 resp
= json
.loads(resp
)
191 if not resp
or "_id" not in resp
:
192 raise ClientException("failed to get K8s cluster info: {}".format(resp
))
195 raise NotFound("K8s cluster {} not found".format(name
))