| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 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 |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 20 | from osmclient.common.exceptions import NotFound |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 21 | from osmclient.common.exceptions import ClientException |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 22 | import json |
| 23 | |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 24 | |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 25 | class K8scluster(object): |
| 26 | def __init__(self, http=None, client=None): |
| 27 | self._http = http |
| 28 | self._client = client |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 29 | self._apiName = "/admin" |
| 30 | self._apiVersion = "/v1" |
| 31 | self._apiResource = "/k8sclusters" |
| 32 | self._apiBase = "{}{}{}".format( |
| 33 | self._apiName, self._apiVersion, self._apiResource |
| 34 | ) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 35 | |
| 36 | def create(self, name, k8s_cluster): |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 37 | def get_vim_account_id(vim_account): |
| 38 | vim = self._client.vim.get(vim_account) |
| 39 | if vim is None: |
| 40 | raise NotFound("cannot find vim account '{}'".format(vim_account)) |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 41 | return vim["_id"] |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 42 | |
| 43 | self._client.get_token() |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 44 | k8s_cluster["vim_account"] = get_vim_account_id(k8s_cluster["vim_account"]) |
| 45 | http_code, resp = self._http.post_cmd( |
| 46 | endpoint=self._apiBase, postfields_dict=k8s_cluster |
| 47 | ) |
| 48 | # print 'HTTP CODE: {}'.format(http_code) |
| 49 | # print 'RESP: {}'.format(resp) |
| 50 | # if http_code in (200, 201, 202, 204): |
| pinoa | 70d6f18 | 2019-12-12 12:10:27 +0100 | [diff] [blame] | 51 | if resp: |
| 52 | resp = json.loads(resp) |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 53 | if not resp or "id" not in resp: |
| 54 | raise ClientException("unexpected response from server - {}".format(resp)) |
| 55 | print(resp["id"]) |
| 56 | # else: |
| pinoa | 70d6f18 | 2019-12-12 12:10:27 +0100 | [diff] [blame] | 57 | # msg = "" |
| 58 | # if resp: |
| 59 | # try: |
| 60 | # msg = json.loads(resp) |
| 61 | # except ValueError: |
| 62 | # msg = resp |
| 63 | # raise ClientException("failed to add K8s cluster {} - {}".format(name, msg)) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 64 | |
| 65 | def update(self, name, k8s_cluster): |
| 66 | self._client.get_token() |
| 67 | cluster = self.get(name) |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 68 | http_code, resp = self._http.put_cmd( |
| 69 | endpoint="{}/{}".format(self._apiBase, cluster["_id"]), |
| 70 | postfields_dict=k8s_cluster, |
| 71 | ) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 72 | # print 'HTTP CODE: {}'.format(http_code) |
| 73 | # print 'RESP: {}'.format(resp) |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 74 | # if http_code in (200, 201, 202, 204): |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 75 | # pass |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 76 | # else: |
| pinoa | 70d6f18 | 2019-12-12 12:10:27 +0100 | [diff] [blame] | 77 | # msg = "" |
| 78 | # if resp: |
| 79 | # try: |
| 80 | # msg = json.loads(resp) |
| 81 | # except ValueError: |
| 82 | # msg = resp |
| 83 | # raise ClientException("failed to update K8s cluster {} - {}".format(name, msg)) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 84 | |
| 85 | def get_id(self, name): |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 86 | """Returns a K8s cluster id from a K8s cluster name""" |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 87 | for cluster in self.list(): |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 88 | if name == cluster["name"]: |
| 89 | return cluster["_id"] |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 90 | raise NotFound("K8s cluster {} not found".format(name)) |
| 91 | |
| 92 | def delete(self, name, force=False): |
| 93 | self._client.get_token() |
| 94 | cluster_id = name |
| 95 | if not utils.validate_uuid4(name): |
| 96 | cluster_id = self.get_id(name) |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 97 | querystring = "" |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 98 | if force: |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 99 | querystring = "?FORCE=True" |
| 100 | http_code, resp = self._http.delete_cmd( |
| 101 | "{}/{}{}".format(self._apiBase, cluster_id, querystring) |
| 102 | ) |
| 103 | # print 'HTTP CODE: {}'.format(http_code) |
| 104 | # print 'RESP: {}'.format(resp) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 105 | if http_code == 202: |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 106 | print("Deletion in progress") |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 107 | elif http_code == 204: |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 108 | print("Deleted") |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 109 | else: |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 110 | msg = resp or "" |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 111 | # if resp: |
| 112 | # try: |
| 113 | # msg = json.loads(resp) |
| 114 | # except ValueError: |
| 115 | # msg = resp |
| 116 | raise ClientException( |
| 117 | "failed to delete K8s cluster {} - {}".format(name, msg) |
| 118 | ) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 119 | |
| 120 | def list(self, filter=None): |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 121 | """Returns a list of K8s clusters""" |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 122 | self._client.get_token() |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 123 | filter_string = "" |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 124 | if filter: |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 125 | filter_string = "?{}".format(filter) |
| 126 | _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string)) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 127 | if resp: |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 128 | return json.loads(resp) |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 129 | return list() |
| 130 | |
| 131 | def get(self, name): |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 132 | """Returns a K8s cluster based on name or id""" |
| garciadeblas | e79f0ca | 2019-11-05 00:40:11 +0100 | [diff] [blame] | 133 | self._client.get_token() |
| 134 | cluster_id = name |
| 135 | if not utils.validate_uuid4(name): |
| 136 | cluster_id = self.get_id(name) |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 137 | try: |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 138 | _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, cluster_id)) |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 139 | if resp: |
| 140 | resp = json.loads(resp) |
| beierlm | 95686bb | 2021-03-23 16:26:45 -0400 | [diff] [blame] | 141 | if not resp or "_id" not in resp: |
| 142 | raise ClientException("failed to get K8s cluster info: {}".format(resp)) |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 143 | return resp |
| 144 | except NotFound: |
| 145 | raise NotFound("K8s cluster {} not found".format(name)) |