X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fsol005%2Fk8scluster.py;h=ff6822e256c2511424399e981a7a5819a39a7c0f;hb=8f4936bbd22fc700b9d1e64eb6b056bf7744b0a4;hp=0b99a3793ed53f91f0891362926c4867c1e86fae;hpb=95686bbc69ded243c346f94dceb0bee567572fb7;p=osm%2Fosmclient.git diff --git a/osmclient/sol005/k8scluster.py b/osmclient/sol005/k8scluster.py index 0b99a37..ff6822e 100644 --- a/osmclient/sol005/k8scluster.py +++ b/osmclient/sol005/k8scluster.py @@ -17,15 +17,18 @@ OSM K8s cluster API handling """ from osmclient.common import utils +from osmclient.common import wait as WaitForStatus from osmclient.common.exceptions import NotFound from osmclient.common.exceptions import ClientException import json +import logging class K8scluster(object): def __init__(self, http=None, client=None): self._http = http self._client = client + self._logger = logging.getLogger("osmclient.k8scluster") self._apiName = "/admin" self._apiVersion = "/v1" self._apiResource = "/k8sclusters" @@ -33,25 +36,52 @@ class K8scluster(object): self._apiName, self._apiVersion, self._apiResource ) - def create(self, name, k8s_cluster): - def get_vim_account_id(vim_account): - vim = self._client.vim.get(vim_account) - if vim is None: - raise NotFound("cannot find vim account '{}'".format(vim_account)) - return vim["_id"] + def _get_vim_account(self, vim_account): + vim = self._client.vim.get(vim_account) + if vim is None: + raise NotFound("cannot find vim account '{}'".format(vim_account)) + return vim + # K8S '--wait' option + def _wait(self, id, wait_time, deleteFlag=False): + self._logger.debug("") self._client.get_token() - k8s_cluster["vim_account"] = get_vim_account_id(k8s_cluster["vim_account"]) + # Endpoint to get operation status + apiUrlStatus = "{}{}{}".format( + self._apiName, self._apiVersion, self._apiResource + ) + # Wait for status for VIM instance creation/deletion + if isinstance(wait_time, bool): + wait_time = WaitForStatus.TIMEOUT_VIM_OPERATION + WaitForStatus.wait_for_status( + "K8S", + str(id), + wait_time, + apiUrlStatus, + self._http.get2_cmd, + deleteFlag=deleteFlag, + ) + + def create(self, name, k8s_cluster, wait=False): + self._client.get_token() + vim_account = self._get_vim_account(k8s_cluster["vim_account"]) + k8s_cluster["vim_account"] = vim_account["_id"] + if "vca" in vim_account: + k8s_cluster["vca_id"] = vim_account["vca"] http_code, resp = self._http.post_cmd( endpoint=self._apiBase, postfields_dict=k8s_cluster ) - # print 'HTTP CODE: {}'.format(http_code) - # print 'RESP: {}'.format(resp) - # if http_code in (200, 201, 202, 204): + + self._logger.debug("HTTP CODE: {}".format(http_code)) + self._logger.debug("RESP: {}".format(resp)) + if resp: resp = json.loads(resp) if not resp or "id" not in resp: raise ClientException("unexpected response from server - {}".format(resp)) + if wait: + # Wait for status for VIM instance creation + self._wait(resp.get("id"), wait) print(resp["id"]) # else: # msg = "" @@ -62,25 +92,38 @@ class K8scluster(object): # msg = resp # raise ClientException("failed to add K8s cluster {} - {}".format(name, msg)) - def update(self, name, k8s_cluster): + def update(self, name, k8s_cluster, wait=False): self._client.get_token() cluster = self.get(name) - http_code, resp = self._http.put_cmd( + if "vim_account" in k8s_cluster: + vim_account = self._get_vim_account(k8s_cluster["vim_account"]) + k8s_cluster["vim_account"] = vim_account["_id"] + if "vca" in vim_account: + k8s_cluster["vca_id"] = vim_account["vca"] + http_code, resp = self._http.patch_cmd( endpoint="{}/{}".format(self._apiBase, cluster["_id"]), postfields_dict=k8s_cluster, ) - # print 'HTTP CODE: {}'.format(http_code) - # print 'RESP: {}'.format(resp) - # if http_code in (200, 201, 202, 204): - # pass - # else: - # msg = "" - # if resp: - # try: - # msg = json.loads(resp) - # except ValueError: - # msg = resp - # raise ClientException("failed to update K8s cluster {} - {}".format(name, msg)) + + if wait: + wait_id = cluster["_id"] + self._wait(wait_id, wait) + + self._logger.debug("HTTP CODE: {}".format(http_code)) + self._logger.debug("RESP: {}".format(resp)) + + if http_code in (200, 201, 202, 204): + print("Updated") + else: + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException( + "failed to update K8s cluster {} - {}".format(name, msg) + ) def get_id(self, name): """Returns a K8s cluster id from a K8s cluster name""" @@ -89,7 +132,7 @@ class K8scluster(object): return cluster["_id"] raise NotFound("K8s cluster {} not found".format(name)) - def delete(self, name, force=False): + def delete(self, name, force=False, wait=False): self._client.get_token() cluster_id = name if not utils.validate_uuid4(name): @@ -100,10 +143,21 @@ class K8scluster(object): http_code, resp = self._http.delete_cmd( "{}/{}{}".format(self._apiBase, cluster_id, querystring) ) - # print 'HTTP CODE: {}'.format(http_code) - # print 'RESP: {}'.format(resp) + + self._logger.debug("HTTP CODE: {}".format(http_code)) + self._logger.debug("RESP: {}".format(resp)) + if http_code == 202: - print("Deletion in progress") + if wait: + wait_id = cluster_id + + if resp: + resp = json.loads(resp) + wait_id = resp.get("id") + + self._wait(wait_id, wait, deleteFlag=True) + else: + print("Deletion in progress") elif http_code == 204: print("Deleted") else: