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