1 |
|
# Copyright 2018 Telefonica |
2 |
|
# |
3 |
|
# All Rights Reserved. |
4 |
|
# |
5 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
6 |
|
# not use this file except in compliance with the License. You may obtain |
7 |
|
# a copy of the License at |
8 |
|
# |
9 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
# |
11 |
|
# Unless required by applicable law or agreed to in writing, software |
12 |
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
13 |
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 |
|
# License for the specific language governing permissions and limitations |
15 |
|
# under the License. |
16 |
|
|
17 |
1 |
""" |
18 |
|
OSM wim API handling |
19 |
|
""" |
20 |
|
|
21 |
1 |
from osmclient.common import utils |
22 |
1 |
from osmclient.common import wait as WaitForStatus |
23 |
1 |
from osmclient.common.exceptions import ClientException |
24 |
1 |
from osmclient.common.exceptions import NotFound |
25 |
1 |
import yaml |
26 |
1 |
import json |
27 |
1 |
import logging |
28 |
|
|
29 |
|
|
30 |
1 |
class Wim(object): |
31 |
1 |
def __init__(self, http=None, client=None): |
32 |
0 |
self._http = http |
33 |
0 |
self._client = client |
34 |
0 |
self._logger = logging.getLogger("osmclient") |
35 |
0 |
self._apiName = "/admin" |
36 |
0 |
self._apiVersion = "/v1" |
37 |
0 |
self._apiResource = "/wim_accounts" |
38 |
0 |
self._apiBase = "{}{}{}".format( |
39 |
|
self._apiName, self._apiVersion, self._apiResource |
40 |
|
) |
41 |
|
|
42 |
|
# WIM '--wait' option |
43 |
1 |
def _wait(self, id, wait_time, deleteFlag=False): |
44 |
0 |
self._logger.debug("") |
45 |
0 |
self._client.get_token() |
46 |
|
# Endpoint to get operation status |
47 |
0 |
apiUrlStatus = "{}{}{}".format(self._apiName, self._apiVersion, "/wim_accounts") |
48 |
|
# Wait for status for WIM instance creation/deletion |
49 |
0 |
if isinstance(wait_time, bool): |
50 |
0 |
wait_time = WaitForStatus.TIMEOUT_WIM_OPERATION |
51 |
0 |
WaitForStatus.wait_for_status( |
52 |
|
"WIM", |
53 |
|
str(id), |
54 |
|
wait_time, |
55 |
|
apiUrlStatus, |
56 |
|
self._http.get2_cmd, |
57 |
|
deleteFlag=deleteFlag, |
58 |
|
) |
59 |
|
|
60 |
1 |
def _get_id_for_wait(self, name): |
61 |
|
"""Returns id of name, or the id itself if given as argument""" |
62 |
0 |
self._logger.debug("") |
63 |
0 |
for wim in self.list(): |
64 |
0 |
if name == wim["uuid"] or name == wim["name"]: |
65 |
0 |
return wim["uuid"] |
66 |
0 |
return "" |
67 |
|
|
68 |
1 |
def create(self, name, wim_input, wim_port_mapping=None, wait=False): |
69 |
0 |
self._logger.debug("") |
70 |
0 |
self._client.get_token() |
71 |
0 |
if "wim_type" not in wim_input: |
72 |
0 |
raise Exception("wim type not provided") |
73 |
|
|
74 |
0 |
wim_account = wim_input |
75 |
0 |
wim_account["name"] = name |
76 |
|
|
77 |
0 |
wim_config = {} |
78 |
0 |
if "config" in wim_input and wim_input["config"] is not None: |
79 |
0 |
wim_config = yaml.safe_load(wim_input["config"]) |
80 |
0 |
if wim_port_mapping: |
81 |
0 |
with open(wim_port_mapping, "r") as f: |
82 |
0 |
wim_config["wim_port_mapping"] = yaml.safe_load(f.read()) |
83 |
0 |
if wim_config: |
84 |
0 |
wim_account["config"] = wim_config |
85 |
|
# wim_account['config'] = json.dumps(wim_config) |
86 |
|
|
87 |
0 |
http_code, resp = self._http.post_cmd( |
88 |
|
endpoint=self._apiBase, postfields_dict=wim_account |
89 |
|
) |
90 |
|
# print('HTTP CODE: {}'.format(http_code)) |
91 |
|
# print('RESP: {}'.format(resp)) |
92 |
|
# if http_code in (200, 201, 202, 204): |
93 |
0 |
if resp: |
94 |
0 |
resp = json.loads(resp) |
95 |
0 |
if not resp or "id" not in resp: |
96 |
0 |
raise ClientException("unexpected response from server - {}".format(resp)) |
97 |
0 |
if wait: |
98 |
|
# Wait for status for WIM instance creation |
99 |
0 |
self._wait(resp.get("id"), wait) |
100 |
0 |
print(resp["id"]) |
101 |
|
# else: |
102 |
|
# msg = "" |
103 |
|
# if resp: |
104 |
|
# try: |
105 |
|
# msg = json.loads(resp) |
106 |
|
# except ValueError: |
107 |
|
# msg = resp |
108 |
|
# raise ClientException("failed to create wim {} - {}".format(name, msg)) |
109 |
|
|
110 |
1 |
def update(self, wim_name, wim_account, wim_port_mapping=None, wait=False): |
111 |
0 |
self._logger.debug("") |
112 |
0 |
self._client.get_token() |
113 |
0 |
wim = self.get(wim_name) |
114 |
0 |
wim_id_for_wait = self._get_id_for_wait(wim_name) |
115 |
0 |
wim_config = {} |
116 |
0 |
if "config" in wim_account: |
117 |
0 |
if wim_account.get("config") == "" and (wim_port_mapping): |
118 |
0 |
raise ClientException( |
119 |
|
"clearing config is incompatible with updating SDN info" |
120 |
|
) |
121 |
0 |
if wim_account.get("config") == "": |
122 |
0 |
wim_config = None |
123 |
|
else: |
124 |
0 |
wim_config = yaml.safe_load(wim_account["config"]) |
125 |
0 |
if wim_port_mapping: |
126 |
0 |
with open(wim_port_mapping, "r") as f: |
127 |
0 |
wim_config["wim_port_mapping"] = yaml.safe_load(f.read()) |
128 |
0 |
wim_account["config"] = wim_config |
129 |
|
# wim_account['config'] = json.dumps(wim_config) |
130 |
0 |
http_code, resp = self._http.patch_cmd( |
131 |
|
endpoint="{}/{}".format(self._apiBase, wim["_id"]), |
132 |
|
postfields_dict=wim_account, |
133 |
|
) |
134 |
|
# print('HTTP CODE: {}'.format(http_code)) |
135 |
|
# print('RESP: {}'.format(resp)) |
136 |
|
# if http_code in (200, 201, 202, 204): |
137 |
0 |
if wait: |
138 |
|
# In this case, 'resp' always returns None, so 'resp['id']' cannot be used. |
139 |
|
# Use the previously obtained id instead. |
140 |
0 |
wait_id = wim_id_for_wait |
141 |
|
# Wait for status for WIM instance update |
142 |
0 |
self._wait(wait_id, wait) |
143 |
|
# else: |
144 |
|
# pass |
145 |
|
# else: |
146 |
|
# msg = "" |
147 |
|
# if resp: |
148 |
|
# try: |
149 |
|
# msg = json.loads(resp) |
150 |
|
# except ValueError: |
151 |
|
# msg = resp |
152 |
|
# raise ClientException("failed to update wim {} - {}".format(wim_name, msg)) |
153 |
|
|
154 |
1 |
def update_wim_account_dict(self, wim_account, wim_input): |
155 |
0 |
self._logger.debug("") |
156 |
0 |
self._logger.debug(str(wim_input)) |
157 |
0 |
wim_account["wim_type"] = wim_input["wim_type"] |
158 |
0 |
wim_account["description"] = wim_input["description"] |
159 |
0 |
wim_account["wim_url"] = wim_input["url"] |
160 |
0 |
wim_account["user"] = wim_input.get("wim-username") |
161 |
0 |
wim_account["password"] = wim_input.get("wim-password") |
162 |
0 |
return wim_account |
163 |
|
|
164 |
1 |
def get_id(self, name): |
165 |
|
"""Returns a WIM id from a WIM name""" |
166 |
0 |
self._logger.debug("") |
167 |
0 |
for wim in self.list(): |
168 |
0 |
if name == wim["name"]: |
169 |
0 |
return wim["uuid"] |
170 |
0 |
raise NotFound("wim {} not found".format(name)) |
171 |
|
|
172 |
1 |
def delete(self, wim_name, force=False, wait=False): |
173 |
0 |
self._logger.debug("") |
174 |
0 |
self._client.get_token() |
175 |
0 |
wim_id = wim_name |
176 |
0 |
wim_id_for_wait = self._get_id_for_wait(wim_name) |
177 |
0 |
if not utils.validate_uuid4(wim_name): |
178 |
0 |
wim_id = self.get_id(wim_name) |
179 |
0 |
querystring = "" |
180 |
0 |
if force: |
181 |
0 |
querystring = "?FORCE=True" |
182 |
0 |
http_code, resp = self._http.delete_cmd( |
183 |
|
"{}/{}{}".format(self._apiBase, wim_id, querystring) |
184 |
|
) |
185 |
|
# print('HTTP CODE: {}'.format(http_code)) |
186 |
|
# print('RESP: {}'.format(resp)) |
187 |
|
# print('WIM_ID: {}'.format(wim_id)) |
188 |
0 |
if http_code == 202: |
189 |
0 |
if wait: |
190 |
|
# 'resp' may be None. |
191 |
|
# In that case, 'resp['id']' cannot be used, so use the previously obtained id instead |
192 |
0 |
wait_id = wim_id_for_wait |
193 |
0 |
if resp: |
194 |
0 |
resp = json.loads(resp) |
195 |
0 |
wait_id = resp.get("id") |
196 |
|
# Wait for status for WIM account deletion |
197 |
0 |
self._wait(wait_id, wait, deleteFlag=True) |
198 |
|
else: |
199 |
0 |
print("Deletion in progress") |
200 |
0 |
elif http_code == 204: |
201 |
0 |
print("Deleted") |
202 |
|
else: |
203 |
0 |
msg = resp or "" |
204 |
|
# if resp: |
205 |
|
# try: |
206 |
|
# msg = json.loads(resp) |
207 |
|
# except ValueError: |
208 |
|
# msg = resp |
209 |
0 |
raise ClientException("failed to delete wim {} - {}".format(wim_name, msg)) |
210 |
|
|
211 |
1 |
def list(self, filter=None): |
212 |
|
"""Returns a list of VIM accounts""" |
213 |
0 |
self._logger.debug("") |
214 |
0 |
self._client.get_token() |
215 |
0 |
filter_string = "" |
216 |
0 |
if filter: |
217 |
0 |
filter_string = "?{}".format(filter) |
218 |
0 |
_, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string)) |
219 |
0 |
if not resp: |
220 |
0 |
return list() |
221 |
0 |
wim_accounts = [] |
222 |
0 |
for datacenter in json.loads(resp): |
223 |
0 |
wim_accounts.append( |
224 |
|
{ |
225 |
|
"name": datacenter["name"], |
226 |
|
"uuid": datacenter["_id"] if "_id" in datacenter else None, |
227 |
|
} |
228 |
|
) |
229 |
0 |
return wim_accounts |
230 |
|
|
231 |
1 |
def get(self, name): |
232 |
|
"""Returns a VIM account based on name or id""" |
233 |
0 |
self._logger.debug("") |
234 |
0 |
self._client.get_token() |
235 |
0 |
wim_id = name |
236 |
0 |
if not utils.validate_uuid4(name): |
237 |
0 |
wim_id = self.get_id(name) |
238 |
0 |
try: |
239 |
0 |
_, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, wim_id)) |
240 |
0 |
if resp: |
241 |
0 |
resp = json.loads(resp) |
242 |
0 |
if not resp or "_id" not in resp: |
243 |
0 |
raise ClientException("failed to get wim info: {}".format(resp)) |
244 |
0 |
return resp |
245 |
0 |
except NotFound: |
246 |
0 |
raise NotFound("wim '{}' not found".format(name)) |