k8sClusters; k8sRepos
[osm/LW-UI.git] / lib / osm / osmclient / clientv2.py
1 #
2 # Copyright 2018 EveryUP Srl
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 import errno
17 import requests
18 import logging
19 import tarfile
20 import yaml
21 import StringIO
22 from lib.util import Util
23 import hashlib
24 import os
25 import re
26 from requests.packages.urllib3.exceptions import InsecureRequestWarning
27
28 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
29
30 logging.basicConfig(level=logging.INFO)
31 log = logging.getLogger('helper.py')
32 logging.getLogger("urllib3").setLevel(logging.INFO)
33
34
35 class Client(object):
36 def __init__(self):
37 self._token_endpoint = 'admin/v1/tokens'
38 self._user_endpoint = 'admin/v1/users'
39 self._host = os.getenv('OSM_SERVER', "localhost")
40 self._so_port = 9999
41 self._base_path = 'https://{0}:{1}/osm'.format(self._host, self._so_port)
42
43 def auth(self, args):
44 result = {'error': True, 'data': ''}
45 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
46 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
47 try:
48 r = requests.post(token_url, json=args, verify=False, headers=headers)
49 except Exception as e:
50 log.exception(e)
51 result['data'] = str(e)
52 return result
53 if r.status_code in (200, 201, 202, 204):
54 result['error'] = False
55
56 result['data'] = Util.json_loads_byteified(r.text)
57
58 return result
59
60 def switch_project(self, args):
61 result = {'error': True, 'data': ''}
62 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
63 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
64 try:
65 r = requests.post(token_url, json=args, verify=False, headers=headers)
66 except Exception as e:
67 log.exception(e)
68 result['data'] = str(e)
69 return result
70 if r.status_code in (200, 201, 202, 204):
71 result['error'] = False
72
73 result['data'] = Util.json_loads_byteified(r.text)
74
75 return result
76
77 def role_list(self, token):
78 result = {'error': True, 'data': ''}
79 headers = {"Content-Type": "application/json", "accept": "application/json",
80 'Authorization': 'Bearer {}'.format(token['id'])}
81
82 _url = "{0}/admin/v1/roles".format(self._base_path)
83 try:
84 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
85 except Exception as e:
86 log.exception(e)
87 result['data'] = str(e)
88 return result
89 if r.status_code in (200, 201, 202, 204):
90 result['error'] = False
91 result['data'] = Util.json_loads_byteified(r.text)
92
93 return result
94
95 def role_create(self, token, role_data):
96 result = {'error': True, 'data': ''}
97 headers = {"Content-Type": "application/json", "accept": "application/json",
98 'Authorization': 'Bearer {}'.format(token['id'])}
99 _url = "{0}/admin/v1/roles".format(self._base_path)
100
101 try:
102 r = requests.post(_url, json=role_data, verify=False, headers=headers)
103 except Exception as e:
104 log.exception(e)
105 result['data'] = str(e)
106 return result
107 if r.status_code in (200, 201, 202, 204):
108 result['error'] = False
109 result['data'] = Util.json_loads_byteified(r.text)
110 return result
111
112 def role_update(self, token, role_id, role_data):
113 result = {'error': True, 'data': ''}
114 headers = {"Content-Type": "application/json", "accept": "application/json",
115 'Authorization': 'Bearer {}'.format(token['id'])}
116 _url = "{0}/admin/v1/roles/{1}".format(self._base_path, role_id)
117 try:
118 r = requests.patch(_url, json=role_data, verify=False, headers=headers)
119 except Exception as e:
120 log.exception(e)
121 result['data'] = str(e)
122 return result
123 if r.status_code in (200, 201, 202, 204):
124 result['error'] = False
125 else:
126 result['data'] = Util.json_loads_byteified(r.text)
127 return result
128
129 def role_delete(self, token, id, force=None):
130 result = {'error': True, 'data': ''}
131 headers = {"Content-Type": "application/json", "accept": "application/json",
132 'Authorization': 'Bearer {}'.format(token['id'])}
133 query_path = ''
134 if force:
135 query_path = '?FORCE=true'
136 _url = "{0}/admin/v1/roles/{1}{2}".format(self._base_path, id, query_path)
137 try:
138 r = requests.delete(_url, params=None, verify=False, headers=headers)
139 except Exception as e:
140 log.exception(e)
141 result['data'] = str(e)
142 return result
143 if r.status_code in (200, 201, 202, 204):
144 result['error'] = False
145 else:
146 result['data'] = Util.json_loads_byteified(r.text)
147 return result
148
149 def role_get(self, token, id):
150 result = {'error': True, 'data': ''}
151 headers = {"Content-Type": "application/json", "accept": "application/json",
152 'Authorization': 'Bearer {}'.format(token['id'])}
153
154 _url = "{0}/admin/v1/roles/{1}".format(self._base_path, id)
155 try:
156 r = requests.get(_url, params=None, verify=False, headers=headers)
157 except Exception as e:
158 log.exception(e)
159 result['data'] = str(e)
160 return result
161 if r.status_code in (200, 201, 202, 204):
162 result['error'] = False
163 result['data'] = Util.json_loads_byteified(r.text)
164 return result
165
166 def user_list(self, token):
167 result = {'error': True, 'data': ''}
168 headers = {"Content-Type": "application/json", "accept": "application/json",
169 'Authorization': 'Bearer {}'.format(token['id'])}
170
171 _url = "{0}/admin/v1/users".format(self._base_path)
172 try:
173 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
174 except Exception as e:
175 log.exception(e)
176 result['data'] = str(e)
177 return result
178 if r.status_code in (200, 201, 202, 204):
179 result['error'] = False
180 result['data'] = Util.json_loads_byteified(r.text)
181
182 return result
183
184 def user_create(self, token, user_data):
185 result = {'error': True, 'data': ''}
186 headers = {"Content-Type": "application/json", "accept": "application/json",
187 'Authorization': 'Bearer {}'.format(token['id'])}
188
189 _url = "{0}/admin/v1/users".format(self._base_path)
190
191 try:
192 r = requests.post(_url, json=user_data, verify=False, headers=headers)
193 except Exception as e:
194 log.exception(e)
195 result['data'] = str(e)
196 return result
197 if r.status_code in (200, 201, 202, 204):
198 result['error'] = False
199 result['data'] = Util.json_loads_byteified(r.text)
200 return result
201
202 def user_update(self, token, id, user_data):
203 result = {'error': True, 'data': ''}
204 headers = {"Content-Type": "application/json", "accept": "application/json",
205 'Authorization': 'Bearer {}'.format(token['id'])}
206
207 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
208 try:
209 r = requests.patch(_url, json=user_data, verify=False, headers=headers)
210 except Exception as e:
211 log.exception(e)
212 result['data'] = str(e)
213 return result
214 if r.status_code in (200, 201, 202, 204):
215 result['error'] = False
216 else:
217 result['data'] = Util.json_loads_byteified(r.text)
218 return result
219
220 def user_delete(self, token, id):
221 result = {'error': True, 'data': ''}
222 headers = {"Content-Type": "application/yaml", "accept": "application/json",
223 'Authorization': 'Bearer {}'.format(token['id'])}
224
225 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
226 try:
227 r = requests.delete(_url, params=None, verify=False, headers=headers)
228 except Exception as e:
229 log.exception(e)
230 result['data'] = str(e)
231 return result
232 if r.status_code in (200, 201, 202, 204):
233 result['error'] = False
234 else:
235 result['data'] = Util.json_loads_byteified(r.text)
236 return result
237
238 def get_user_info(self, token, id):
239 result = {'error': True, 'data': ''}
240 headers = {"Content-Type": "application/yaml", "accept": "application/json",
241 'Authorization': 'Bearer {}'.format(token['id'])}
242 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
243 try:
244 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
245 except Exception as e:
246 log.exception(e)
247 result['data'] = str(e)
248 return result
249 if r.status_code in (200, 201, 202, 204):
250 result['error'] = False
251 result['data'] = Util.json_loads_byteified(r.text)
252 return result
253
254 def get_projects(self, token, uuids):
255 result = {'error': False, 'data': ''}
256 headers = {"Content-Type": "application/yaml", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token['id'])}
258
259 projects = []
260 try:
261 for uuid in uuids:
262 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, uuid)
263 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
264 if r.status_code not in (200, 201, 202, 204):
265 raise Exception()
266 projects.append(Util.json_loads_byteified(r.text))
267 except Exception as e:
268 log.exception(e)
269 result['error'] = True
270 result['data'] = str(e)
271 return result
272 result['data'] = projects
273 return result
274
275 def project_list(self, token):
276 result = {'error': True, 'data': ''}
277 headers = {"Content-Type": "application/yaml", "accept": "application/json",
278 'Authorization': 'Bearer {}'.format(token['id'])}
279
280 _url = "{0}/admin/v1/projects".format(self._base_path)
281 try:
282 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
283 except Exception as e:
284 log.exception(e)
285 result['data'] = str(e)
286 return result
287 if r.status_code in (200, 201, 202, 204):
288 result['error'] = False
289 result['data'] = Util.json_loads_byteified(r.text)
290
291 return result
292
293 def project_get(self, token, id):
294 result = {'error': True, 'data': ''}
295 headers = {"Content-Type": "application/yaml", "accept": "application/json",
296 'Authorization': 'Bearer {}'.format(token['id'])}
297 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
298 try:
299 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
300 except Exception as e:
301 log.exception(e)
302 result['data'] = str(e)
303 return result
304 if r.status_code in (200, 201, 202, 204):
305 result['error'] = False
306 result['data'] = Util.json_loads_byteified(r.text)
307 return result
308
309 def project_create(self, token, project_data):
310
311 result = {'error': True, 'data': ''}
312 headers = {"Content-Type": "application/json", "accept": "application/json",
313 'Authorization': 'Bearer {}'.format(token['id'])}
314
315 _url = "{0}/admin/v1/projects".format(self._base_path)
316
317 try:
318 r = requests.post(_url, json=project_data, verify=False, headers=headers)
319 except Exception as e:
320 log.exception(e)
321 result['data'] = str(e)
322 return result
323 if r.status_code in (200, 201, 202, 204):
324 result['error'] = False
325 result['data'] = Util.json_loads_byteified(r.text)
326 return result
327
328 def project_edit(self, token, id, project_data):
329
330 result = {'error': True, 'data': ''}
331 headers = {"Content-Type": "application/json", "accept": "application/json",
332 'Authorization': 'Bearer {}'.format(token['id'])}
333
334 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
335
336 try:
337 r = requests.put(_url, json=project_data, verify=False, headers=headers)
338 except Exception as e:
339 log.exception(e)
340 result['data'] = str(e)
341 return result
342 if r.status_code in (200, 201, 202, 204):
343 result['error'] = False
344 return result
345
346 def project_delete(self, token, id):
347 result = {'error': True, 'data': ''}
348 headers = {"Content-Type": "application/yaml", "accept": "application/json",
349 'Authorization': 'Bearer {}'.format(token['id'])}
350
351 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
352 try:
353 r = requests.delete(_url, params=None, verify=False, headers=headers)
354 except Exception as e:
355 log.exception(e)
356 result['data'] = str(e)
357 return result
358 if r.status_code in (200, 201, 202, 204):
359 result['error'] = False
360 else:
361 result['data'] = Util.json_loads_byteified(r.text)
362 return result
363
364 def nst_details(self, token, id):
365 result = {'error': True, 'data': ''}
366 headers = {"Content-Type": "application/json", "accept": "application/json",
367 'Authorization': 'Bearer {}'.format(token['id'])}
368 _url = "{0}/nst/v1/netslice_templates/{1}".format(self._base_path,id)
369 try:
370 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
371 except Exception as e:
372 log.exception(e)
373 result['data'] = str(e)
374 return result
375 if r.status_code in (200, 201, 202, 204):
376 result['error'] = False
377 result['data'] = Util.json_loads_byteified(r.text)
378
379 return result
380
381 def nst_content(self, token, id):
382 result = {'error': True, 'data': ''}
383 headers = {"Content-Type": "application/json", "accept": "text/plain",
384 'Authorization': 'Bearer {}'.format(token['id'])}
385 _url = "{0}/nst/v1/netslice_templates/{1}/nst".format(self._base_path,id)
386 try:
387 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
388 except Exception as e:
389 log.exception(e)
390 result['data'] = str(e)
391 return result
392 if r.status_code in (200, 201, 202, 204):
393 result['error'] = False
394 result['data'] = Util.json2yaml(yaml.load(str(r.text)))
395
396 return result
397
398 def nst_list(self, token):
399 result = {'error': True, 'data': ''}
400 headers = {"Content-Type": "application/yaml", "accept": "application/json",
401 'Authorization': 'Bearer {}'.format(token['id'])}
402
403 _url = "{0}/nst/v1/netslice_templates".format(self._base_path)
404 try:
405 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
406 except Exception as e:
407 log.exception(e)
408 result['data'] = str(e)
409 return result
410 if r.status_code in (200, 201, 202, 204):
411 result['error'] = False
412 result['data'] = Util.json_loads_byteified(r.text)
413
414 return result
415
416 def nsd_list(self, token, filter=None):
417 result = {'error': True, 'data': ''}
418 headers = {"Content-Type": "application/yaml", "accept": "application/json",
419 'Authorization': 'Bearer {}'.format(token['id'])}
420 query_path = ''
421 if filter:
422 query_path = '?_admin.type='+filter
423 _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(self._base_path, query_path)
424 try:
425 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
426 except Exception as e:
427 log.exception(e)
428 result['data'] = str(e)
429 return result
430 if r.status_code in (200, 201, 202, 204):
431 result['error'] = False
432 result['data'] = Util.json_loads_byteified(r.text)
433
434 return result
435
436 def vnfd_list(self, token, filter=None):
437 result = {'error': True, 'data': ''}
438 headers = {"Content-Type": "application/yaml", "accept": "application/json",
439 'Authorization': 'Bearer {}'.format(token['id'])}
440 query_path = ''
441 if filter:
442 query_path = '?_admin.type='+filter
443 _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self._base_path, query_path)
444 try:
445 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
446 except Exception as e:
447 log.exception(e)
448 result['data'] = str(e)
449 return result
450 if r.status_code in (200, 201, 202, 204):
451 result['error'] = False
452 result['data'] = Util.json_loads_byteified(r.text)
453
454 return result
455
456 def nsi_list(self, token):
457 result = {'error': True, 'data': ''}
458 headers = {"Content-Type": "application/yaml", "accept": "application/json",
459 'Authorization': 'Bearer {}'.format(token['id'])}
460 _url = "{0}/nsilcm/v1/netslice_instances".format(self._base_path)
461 try:
462 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
463 except Exception as e:
464 log.exception(e)
465 result['data'] = str(e)
466 return result
467 if r.status_code in (200, 201, 202, 204):
468 result['error'] = False
469 result['data'] = Util.json_loads_byteified(r.text)
470
471 return result
472
473 def ns_list(self, token):
474 result = {'error': True, 'data': ''}
475 headers = {"Content-Type": "application/yaml", "accept": "application/json",
476 'Authorization': 'Bearer {}'.format(token['id'])}
477 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
478 try:
479 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
480 except Exception as e:
481 log.exception(e)
482 result['data'] = str(e)
483 return result
484 if r.status_code in (200, 201, 202, 204):
485 result['error'] = False
486 result['data'] = Util.json_loads_byteified(r.text)
487
488 return result
489
490 def vnf_list(self, token):
491 result = {'error': True, 'data': ''}
492 headers = {"Content-Type": "application/yaml", "accept": "application/json",
493 'Authorization': 'Bearer {}'.format(token['id'])}
494 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
495 try:
496 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
497 except Exception as e:
498 log.exception(e)
499 result['data'] = str(e)
500 return result
501 if r.status_code in (200, 201, 202, 204):
502 result['error'] = False
503 result['data'] = Util.json_loads_byteified(r.text)
504
505 return result
506
507 def pdu_list(self, token):
508 result = {'error': True, 'data': ''}
509 headers = {"Content-Type": "application/yaml", "accept": "application/json",
510 'Authorization': 'Bearer {}'.format(token['id'])}
511 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
512 try:
513 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
514 except Exception as e:
515 log.exception(e)
516 result['data'] = str(e)
517 return result
518 if r.status_code in (200, 201, 202, 204):
519 result['error'] = False
520 result['data'] = Util.json_loads_byteified(r.text)
521
522 return result
523
524 def nst_delete(self, token, id):
525 result = {'error': True, 'data': ''}
526 headers = {"Content-Type": "application/yaml", "accept": "application/json",
527 'Authorization': 'Bearer {}'.format(token['id'])}
528
529 _url = "{0}/nst/v1/netslice_templates/{1}?FORCE=True".format(self._base_path, id)
530 try:
531 r = requests.delete(_url, params=None, verify=False, headers=headers)
532 except Exception as e:
533 log.exception(e)
534 result['data'] = str(e)
535 return result
536 if r.status_code in (200, 201, 202, 204):
537 result['error'] = False
538
539 return result
540
541 def nsd_delete(self, token, id):
542 result = {'error': True, 'data': ''}
543 headers = {"Content-Type": "application/yaml", "accept": "application/json",
544 'Authorization': 'Bearer {}'.format(token['id'])}
545
546 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
547 try:
548 r = requests.delete(_url, params=None, verify=False, headers=headers)
549 except Exception as e:
550 log.exception(e)
551 result['data'] = str(e)
552 return result
553 if r:
554 result['error'] = False
555 if r.status_code != requests.codes.no_content:
556 result['data'] = Util.json_loads_byteified(r.text)
557 return result
558
559 def vnfd_delete(self, token, id):
560 result = {'error': True, 'data': ''}
561 headers = {"Content-Type": "application/yaml", "accept": "application/json",
562 'Authorization': 'Bearer {}'.format(token['id'])}
563
564 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
565 try:
566 r = requests.delete(_url, params=None, verify=False, headers=headers)
567 except Exception as e:
568 log.exception(e)
569 result['data'] = str(e)
570 return result
571 if r:
572 result['error'] = False
573 if r.status_code != requests.codes.no_content:
574 result['data'] = Util.json_loads_byteified(r.text)
575 return result
576
577 def nst_onboard(self, token, template):
578 result = {'error': True, 'data': ''}
579 headers = {"Content-Type": "application/gzip", "accept": "application/json",
580 'Authorization': 'Bearer {}'.format(token['id'])}
581 _url = "{0}/nst/v1/netslice_templates_content".format(self._base_path)
582 try:
583 fileName, fileExtension = os.path.splitext(template.name)
584 if fileExtension == '.gz':
585 headers["Content-Type"] = "application/gzip"
586 else:
587 headers["Content-Type"] = "application/yaml"
588 r = requests.post(_url, data=template, verify=False, headers=headers)
589 except Exception as e:
590 log.exception(e)
591 result['data'] = str(e)
592 return result
593 if r.status_code in (200, 201, 202, 204):
594 result['error'] = False
595 result['data'] = Util.json_loads_byteified(r.text)
596 return result
597
598 def nsd_onboard(self, token, package):
599 result = {'error': True, 'data': ''}
600 headers = {"Content-Type": "application/gzip", "accept": "application/json",
601 'Authorization': 'Bearer {}'.format(token['id'])}
602 with open('/tmp/' + package.name, 'wb+') as destination:
603 for chunk in package.chunks():
604 destination.write(chunk)
605 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
606 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
607 try:
608 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
609 except Exception as e:
610 log.exception(e)
611 result['data'] = str(e)
612 return result
613 if r.status_code in (200, 201, 202, 204):
614 result['error'] = False
615 result['data'] = Util.json_loads_byteified(r.text)
616 return result
617
618 def vnfd_onboard(self, token, package):
619 result = {'error': True, 'data': ''}
620 headers = {"Content-Type": "application/gzip", "accept": "application/json",
621 'Authorization': 'Bearer {}'.format(token['id'])}
622 with open('/tmp/' + package.name, 'wb+') as destination:
623 for chunk in package.chunks():
624 destination.write(chunk)
625 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
626 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
627 try:
628 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
629 except Exception as e:
630 log.exception(e)
631 result['data'] = str(e)
632 return result
633 if r.status_code in (200, 201, 202, 204):
634 result['error'] = False
635 result['data'] = Util.json_loads_byteified(r.text)
636 return result
637
638 def nsd_create_pkg_base(self, token, pkg_name):
639 result = {'error': True, 'data': ''}
640 headers = {"Content-Type": "application/gzip", "accept": "application/json",
641 'Authorization': 'Bearer {}'.format(token['id'])}
642
643 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
644
645 try:
646 self._create_base_pkg('nsd', pkg_name)
647 headers['Content-Filename'] = pkg_name + '.tar.gz'
648 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
649 except Exception as e:
650 log.exception(e)
651 result['data'] = str(e)
652 return result
653 if r.status_code in (200, 201, 202, 204):
654 result['data'] = r.json()
655 result['error'] = False
656 if r.status_code == requests.codes.conflict:
657 result['data'] = "Invalid ID."
658 return result
659
660 def vnfd_create_pkg_base(self, token, pkg_name):
661 result = {'error': True, 'data': ''}
662 headers = {"Content-Type": "application/gzip", "accept": "application/json",
663 'Authorization': 'Bearer {}'.format(token['id'])}
664
665 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
666
667 try:
668 self._create_base_pkg('vnfd', pkg_name)
669 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
670 except Exception as e:
671 log.exception(e)
672 result['data'] = str(e)
673 return result
674 if r.status_code in (200, 201, 202, 204):
675 result['data'] = r.json()
676 result['error'] = False
677 if r.status_code == requests.codes.conflict:
678 result['data'] = "Invalid ID."
679 return result
680
681 def nsd_clone(self, token, id):
682 result = {'error': True, 'data': ''}
683 headers = {"Content-Type": "application/gzip", "accept": "application/json",
684 'Authorization': 'Bearer {}'.format(token['id'])}
685
686 # get the package onboarded
687 tar_pkg = self.get_nsd_pkg(token, id)
688 tarf = tarfile.open(fileobj=tar_pkg)
689 tarf = self._descriptor_clone(tarf, 'nsd')
690 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
691
692 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
693
694 try:
695 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
696 headers=headers)
697 except Exception as e:
698 log.exception(e)
699 result['data'] = str(e)
700 return result
701 if r.status_code in (200, 201, 202, 204):
702 result['error'] = False
703 if r.status_code == requests.codes.conflict:
704 result['data'] = "Invalid ID."
705
706 return result
707
708 def vnfd_clone(self, token, id):
709 result = {'error': True, 'data': ''}
710 headers = {"Content-Type": "application/gzip", "accept": "application/json",
711 'Authorization': 'Bearer {}'.format(token['id'])}
712
713 # get the package onboarded
714 tar_pkg = self.get_vnfd_pkg(token, id)
715 tarf = tarfile.open(fileobj=tar_pkg)
716
717 tarf = self._descriptor_clone(tarf, 'vnfd')
718 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
719
720 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
721
722 try:
723 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
724 headers=headers)
725 except Exception as e:
726 log.exception(e)
727 result['data'] = str(e)
728 return result
729 if r.status_code in (200, 201, 202, 204):
730 result['error'] = False
731 if r.status_code == requests.codes.conflict:
732 result['data'] = "Invalid ID."
733
734 return result
735
736 def nst_content_update(self, token, id, template):
737 result = {'error': True, 'data': ''}
738 headers = {"Content-Type": "application/yaml", "accept": "application/json",
739 'Authorization': 'Bearer {}'.format(token['id'])}
740 _url = "{0}/nst/v1/netslice_templates/{1}/nst_content".format(self._base_path,id)
741 try:
742 r = requests.put(_url, data=template, verify=False, headers=headers)
743 except Exception as e:
744 log.exception(e)
745 result['data'] = str(e)
746 return result
747 if r.status_code in (200, 201, 202, 204):
748 result['error'] = False
749 return result
750
751 def nsd_update(self, token, id, data):
752 result = {'error': True, 'data': ''}
753 headers = {"Content-Type": "application/gzip", "accept": "application/json",
754 'Authorization': 'Bearer {}'.format(token['id'])}
755
756 # get the package onboarded
757 tar_pkg = self.get_nsd_pkg(token, id)
758 tarf = tarfile.open(fileobj=tar_pkg)
759
760 tarf = self._descriptor_update(tarf, data)
761 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
762
763 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
764
765 try:
766 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
767 headers=headers)
768 except Exception as e:
769 log.exception(e)
770 result['data'] = str(e)
771 return result
772 if r.status_code in (200, 201, 202, 204):
773 result['error'] = False
774 else:
775 try:
776 result['data'] = r.json()
777 except Exception as e:
778 result['data'] = {}
779
780 return result
781
782 def vnfd_update(self, token, id, data):
783 result = {'error': True, 'data': ''}
784 headers = {"Content-Type": "application/gzip", "accept": "application/json",
785 'Authorization': 'Bearer {}'.format(token['id'])}
786
787 # get the package onboarded
788 tar_pkg = self.get_vnfd_pkg(token, id)
789 tarf = tarfile.open(fileobj=tar_pkg)
790
791 tarf = self._descriptor_update(tarf, data)
792 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
793
794 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
795
796 try:
797 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
798 headers=headers)
799 except Exception as e:
800 log.exception(e)
801 result['data'] = str(e)
802 return result
803 if r.status_code in (200, 201, 202, 204):
804 result['error'] = False
805 else:
806 try:
807 result['data'] = r.json()
808 except Exception as e:
809 result['data'] = {}
810
811 return result
812
813 def get_nsd_pkg(self, token, id):
814 result = {'error': True, 'data': ''}
815 headers = {"accept": "application/zip",
816 'Authorization': 'Bearer {}'.format(token['id'])}
817
818 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
819 try:
820 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
821 except Exception as e:
822 log.exception(e)
823 result['data'] = str(e)
824 return result
825 if r.status_code in (200, 201, 202, 204):
826 result['error'] = False
827 tarf = StringIO.StringIO(r.content)
828 return tarf
829 return result
830
831 def get_vnfd_pkg(self, token, id):
832 result = {'error': True, 'data': ''}
833 headers = {"accept": "application/zip",
834 'Authorization': 'Bearer {}'.format(token['id'])}
835 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
836 try:
837 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
838 except Exception as e:
839 log.exception(e)
840 result['data'] = str(e)
841 return result
842 if r.status_code in (200, 201, 202, 204):
843 result['error'] = False
844 tarf = StringIO.StringIO(r.content)
845 return tarf
846 return result
847
848 def _descriptor_update(self, tarf, data):
849 # extract the package on a tmp directory
850 tarf.extractall('/tmp')
851 regex = re.compile(r"^[^/]+(/[^/]+\.(yaml|yml))$", re.U)
852 for name in tarf.getnames():
853 if regex.match(name):
854 with open('/tmp/' + name, 'w') as outfile:
855 yaml.safe_dump(data, outfile, default_flow_style=False)
856 break
857
858 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
859
860 for tarinfo in tarf:
861 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
862 tarf_temp.close()
863 return tarf
864
865 def _create_base_pkg(self, descriptor_type, pkg_name):
866 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
867 if descriptor_type == 'nsd':
868 descriptor = {
869 "nsd:nsd-catalog": {
870 "nsd": [
871 {
872 "short-name": str(pkg_name),
873 "vendor": "OSM Composer",
874 "description": str(pkg_name) + " descriptor",
875 "vld": [],
876 "constituent-vnfd": [],
877 "version": "1.0",
878 "id": str(pkg_name),
879 "name": str(pkg_name)
880 }
881 ]
882 }
883 }
884
885 elif descriptor_type == 'vnfd':
886 descriptor = {
887 "vnfd:vnfd-catalog": {
888 "vnfd": [
889 {
890 "short-name": str(pkg_name),
891 "vdu": [],
892 "description": "",
893 "mgmt-interface": {
894 "cp": ""
895 },
896 "id": str(pkg_name),
897 "version": "1.0",
898 "internal-vld": [],
899 "connection-point": [],
900 "name": str(pkg_name)
901 }
902 ]
903 }
904 }
905
906 if not os.path.exists(os.path.dirname(filename)):
907 try:
908 os.makedirs(os.path.dirname(filename))
909 except OSError as exc: # Guard against race condition
910 if exc.errno != errno.EEXIST:
911 raise
912
913 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
914 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
915
916 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
917 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml', pkg_name + '/' + pkg_name + '.yaml', recursive=False)
918 tarf_temp.close()
919
920 def _descriptor_clone(self, tarf, descriptor_type):
921 # extract the package on a tmp directory
922 tarf.extractall('/tmp')
923
924 for name in tarf.getnames():
925 if name.endswith(".yaml") or name.endswith(".yml"):
926 with open('/tmp/' + name, 'r') as outfile:
927 yaml_object = yaml.load(outfile)
928
929 if descriptor_type == 'nsd':
930 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
931 for nsd in nsd_list:
932 nsd['id'] = 'clone_' + nsd['id']
933 nsd['name'] = 'clone_' + nsd['name']
934 nsd['short-name'] = 'clone_' + nsd['short-name']
935 elif descriptor_type == 'vnfd':
936 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
937 for vnfd in vnfd_list:
938 vnfd['id'] = 'clone_' + vnfd['id']
939 vnfd['name'] = 'clone_' + vnfd['name']
940 vnfd['short-name'] = 'clone_' + vnfd['short-name']
941
942 with open('/tmp/' + name, 'w') as yaml_file:
943 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
944 break
945
946 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
947
948 for tarinfo in tarf:
949 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
950 tarf_temp.close()
951 return tarf
952
953 def nsd_get(self, token, id):
954 result = {'error': True, 'data': ''}
955 headers = {'Content-Type': 'application/yaml',
956 'Authorization': 'Bearer {}'.format(token['id'])}
957 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
958 try:
959 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
960 except Exception as e:
961 log.exception(e)
962 result['data'] = str(e)
963 return result
964 if r.status_code in (200, 201, 202, 204):
965 result['error'] = False
966 return yaml.load(r.text)
967 else:
968 try:
969 result['data'] = r.json()
970 except Exception as e:
971 result['data'] = {}
972 return result
973
974 def vnfd_get(self, token, id):
975 result = {'error': True, 'data': ''}
976 headers = {'Content-Type': 'application/yaml',
977 'Authorization': 'Bearer {}'.format(token['id'])}
978 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
979 try:
980 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
981 except Exception as e:
982 log.exception(e)
983 result['data'] = str(e)
984 return result
985 if r.status_code in (200, 201, 202, 204):
986 result['error'] = False
987 return yaml.load(r.text)
988 else:
989 try:
990 result['data'] = r.json()
991 except Exception as e:
992 result['data'] = {}
993 return result
994
995 def nsd_artifacts(self, token, id):
996 result = {'error': True, 'data': ''}
997 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
998 'Authorization': 'Bearer {}'.format(token['id'])}
999 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
1000 try:
1001 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1002 except Exception as e:
1003 log.exception(e)
1004 result['data'] = str(e)
1005 return result
1006 if r.status_code in (200, 201, 202, 204):
1007 result['error'] = False
1008 result['data'] = r.text
1009 else:
1010 try:
1011 result['data'] = r.json()
1012 except Exception as e:
1013 result['data'] = {}
1014
1015 return result
1016
1017 def vnf_packages_artifacts(self, token, id):
1018 result = {'error': True, 'data': ''}
1019 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
1020 'Authorization': 'Bearer {}'.format(token['id'])}
1021 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
1022 try:
1023 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1024 except Exception as e:
1025 log.exception(e)
1026 result['data'] = str(e)
1027 return result
1028 if r.status_code in (200, 201, 202, 204):
1029 result['error'] = False
1030 result['data'] = r.text
1031 else:
1032 try:
1033 result['data'] = r.json()
1034 except Exception as e:
1035 result['data'] = {}
1036
1037 return result
1038
1039 def nsi_create(self, token, nsi_data):
1040 result = {'error': True, 'data': ''}
1041 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1042 'Authorization': 'Bearer {}'.format(token['id'])}
1043
1044 _url = "{0}/nsilcm/v1/netslice_instances_content".format(self._base_path)
1045
1046 try:
1047 r = requests.post(_url, json=nsi_data, verify=False, headers=headers)
1048 except Exception as e:
1049 log.exception(e)
1050 result['data'] = str(e)
1051 return result
1052 if r.status_code in (200, 201, 202, 204):
1053 result['error'] = False
1054 result['data'] = Util.json_loads_byteified(r.text)
1055 return result
1056
1057 def ns_create(self, token, ns_data):
1058 result = {'error': True, 'data': ''}
1059 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1060 'Authorization': 'Bearer {}'.format(token['id'])}
1061
1062 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
1063
1064 try:
1065 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
1066 except Exception as e:
1067 log.exception(e)
1068 result['data'] = str(e)
1069 return result
1070 if r.status_code in (200, 201, 202, 204):
1071 result['error'] = False
1072 result['data'] = Util.json_loads_byteified(r.text)
1073 return result
1074
1075 def pdu_create(self, token, pdu_data):
1076 result = {'error': True, 'data': ''}
1077 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1078 'Authorization': 'Bearer {}'.format(token['id'])}
1079
1080 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
1081
1082 try:
1083 r = requests.post(_url, json=pdu_data, verify=False, headers=headers)
1084 except Exception as e:
1085 log.exception(e)
1086 result['data'] = str(e)
1087 return result
1088 if r.status_code in (200, 201, 202, 204):
1089 result['error'] = False
1090 result['data'] = Util.json_loads_byteified(r.text)
1091 return result
1092
1093 def ns_op_list(self, token, id):
1094 result = {'error': True, 'data': ''}
1095 headers = {"Content-Type": "application/json", "accept": "application/json",
1096 'Authorization': 'Bearer {}'.format(token['id'])}
1097 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
1098
1099 try:
1100 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1101 except Exception as e:
1102 log.exception(e)
1103 result['data'] = str(e)
1104 return result
1105 if r.status_code in (200, 201, 202, 204):
1106 result['error'] = False
1107 result['data'] = Util.json_loads_byteified(r.text)
1108
1109 return result
1110
1111 def nsi_op_list(self, token, id):
1112 result = {'error': True, 'data': ''}
1113 headers = {"Content-Type": "application/json", "accept": "application/json",
1114 'Authorization': 'Bearer {}'.format(token['id'])}
1115 _url = "{0}/nsilcm/v1/nsi_lcm_op_occs/?netsliceInstanceId={1}".format(self._base_path, id)
1116
1117 try:
1118 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1119 except Exception as e:
1120 log.exception(e)
1121 result['data'] = str(e)
1122 return result
1123 if r.status_code in (200, 201, 202, 204):
1124 result['error'] = False
1125 result['data'] = Util.json_loads_byteified(r.text)
1126
1127 return result
1128
1129 def ns_op(self, token, id):
1130 result = {'error': True, 'data': ''}
1131 headers = {"Content-Type": "application/json", "accept": "application/json",
1132 'Authorization': 'Bearer {}'.format(token['id'])}
1133 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
1134
1135 try:
1136 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1137 except Exception as e:
1138 log.exception(e)
1139 result['data'] = str(e)
1140 return result
1141 if r.status_code in (200, 201, 202, 204):
1142 result['error'] = False
1143 result['data'] = Util.json_loads_byteified(r.text)
1144
1145 return result
1146
1147 def ns_action(self, token, id, action_payload):
1148 result = {'error': True, 'data': ''}
1149 headers = {"Content-Type": "application/json", "accept": "application/json",
1150 'Authorization': 'Bearer {}'.format(token['id'])}
1151
1152 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
1153
1154 try:
1155 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
1156 except Exception as e:
1157 log.exception(e)
1158 result['data'] = str(e)
1159 return result
1160 if r.status_code in (200, 201, 202, 204):
1161 result['error'] = False
1162 result['data'] = Util.json_loads_byteified(r.text)
1163 return result
1164
1165 def nsi_delete(self, token, id, force=None):
1166 result = {'error': True, 'data': ''}
1167 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1168 'Authorization': 'Bearer {}'.format(token['id'])}
1169 query_path = ''
1170 if force:
1171 query_path = '?FORCE=true'
1172 _url = "{0}/nsilcm/v1/netslice_instances_content/{1}{2}".format(self._base_path, id, query_path)
1173 try:
1174 r = requests.delete(_url, params=None, verify=False, headers=headers)
1175 except Exception as e:
1176 log.exception(e)
1177 result['data'] = str(e)
1178 return result
1179 if r:
1180 result['error'] = False
1181 if r.status_code != requests.codes.no_content:
1182 result['data'] = Util.json_loads_byteified(r.text)
1183 return result
1184
1185 def ns_delete(self, token, id, force=None):
1186 result = {'error': True, 'data': ''}
1187 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1188 'Authorization': 'Bearer {}'.format(token['id'])}
1189 query_path = ''
1190 if force:
1191 query_path = '?FORCE=true'
1192 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
1193 try:
1194 r = requests.delete(_url, params=None, verify=False, headers=headers)
1195 except Exception as e:
1196 log.exception(e)
1197 result['data'] = str(e)
1198 return result
1199 if r:
1200 result['error'] = False
1201 if r.status_code != requests.codes.no_content:
1202 result['data'] = Util.json_loads_byteified(r.text)
1203 return result
1204
1205 def pdu_delete(self, token, id):
1206 result = {'error': True, 'data': ''}
1207 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1208 'Authorization': 'Bearer {}'.format(token['id'])}
1209 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1210 try:
1211 r = requests.delete(_url, params=None, verify=False, headers=headers)
1212 except Exception as e:
1213 log.exception(e)
1214 result['data'] = str(e)
1215 return result
1216 if r:
1217 result['error'] = False
1218 if r.status_code != requests.codes.no_content:
1219 result['data'] = Util.json_loads_byteified(r.text)
1220 return result
1221
1222 def nsi_get(self, token, id):
1223 result = {'error': True, 'data': ''}
1224 headers = {"Content-Type": "application/json", "accept": "application/json",
1225 'Authorization': 'Bearer {}'.format(token['id'])}
1226 _url = "{0}/nsilcm/v1/netslice_instances/{1}".format(self._base_path, id)
1227
1228 try:
1229 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1230 except Exception as e:
1231 log.exception(e)
1232 result['data'] = str(e)
1233 return result
1234 if r.status_code in (200, 201, 202, 204):
1235 result['error'] = False
1236 result['data'] = Util.json_loads_byteified(r.text)
1237 return result
1238
1239 def ns_get(self, token, id):
1240 result = {'error': True, 'data': ''}
1241 headers = {"Content-Type": "application/json", "accept": "application/json",
1242 'Authorization': 'Bearer {}'.format(token['id'])}
1243 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
1244
1245 try:
1246 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1247 except Exception as e:
1248 log.exception(e)
1249 result['data'] = str(e)
1250 return result
1251 if r.status_code in (200, 201, 202, 204):
1252 result['error'] = False
1253 result['data'] = Util.json_loads_byteified(r.text)
1254 return result
1255
1256 def vnf_get(self, token, id):
1257 result = {'error': True, 'data': ''}
1258 headers = {"Content-Type": "application/json", "accept": "application/json",
1259 'Authorization': 'Bearer {}'.format(token['id'])}
1260 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
1261
1262 try:
1263 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1264 except Exception as e:
1265 log.exception(e)
1266 result['data'] = str(e)
1267 return result
1268 if r.status_code in (200, 201, 202, 204):
1269 result['error'] = False
1270 result['data'] = Util.json_loads_byteified(r.text)
1271 return result
1272
1273 def pdu_get(self, token, id):
1274 result = {'error': True, 'data': ''}
1275 headers = {"Content-Type": "application/json", "accept": "application/json",
1276 'Authorization': 'Bearer {}'.format(token['id'])}
1277 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1278
1279 try:
1280 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1281 except Exception as e:
1282 log.exception(e)
1283 result['data'] = str(e)
1284 return result
1285 if r.status_code in (200, 201, 202, 204):
1286 result['error'] = False
1287 result['data'] = Util.json_loads_byteified(r.text)
1288 return result
1289
1290 def ns_alarm_create(self, token, id, alarm_payload):
1291 result = {'error': True, 'data': ''}
1292 headers = {"Content-Type": "application/json",
1293 'Authorization': 'Bearer {}'.format(token['id'])}
1294 _url = "{0}/test/message/alarm_request".format(self._base_path)
1295 try:
1296 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
1297 except Exception as e:
1298 log.exception(e)
1299 result['data'] = str(e)
1300 return result
1301 if r.status_code in (200, 201, 202, 204):
1302 result['error'] = False
1303 # result['data'] = Util.json_loads_byteified(r.text)
1304 result['data'] = r.text
1305 return result
1306
1307 def ns_metric_export(self, token, id, metric_payload):
1308 result = {'error': True, 'data': ''}
1309 headers = {"Content-Type": "application/json",
1310 'Authorization': 'Bearer {}'.format(token['id'])}
1311 _url = "{0}/test/message/metric_request".format(self._base_path)
1312 try:
1313 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
1314 except Exception as e:
1315 log.exception(e)
1316 result['data'] = str(e)
1317 return result
1318 if r.status_code in (200, 201, 202, 204):
1319 result['error'] = False
1320 # result['data'] = Util.json_loads_byteified(r.text)
1321 result['data'] = r.text
1322 return result
1323
1324 def wim_list(self, token):
1325 result = {'error': True, 'data': ''}
1326 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1327 'Authorization': 'Bearer {}'.format(token['id'])}
1328 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1329 try:
1330 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1331 except Exception as e:
1332 log.exception(e)
1333 result['data'] = str(e)
1334 return result
1335 if r.status_code in (200, 201, 202, 204):
1336 result['error'] = False
1337 result['data'] = Util.json_loads_byteified(r.text)
1338
1339 return result
1340
1341 def vim_list(self, token):
1342 result = {'error': True, 'data': ''}
1343 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1344 'Authorization': 'Bearer {}'.format(token['id'])}
1345 _url = "{0}/admin/v1/vims".format(self._base_path)
1346 try:
1347 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1348 except Exception as e:
1349 log.exception(e)
1350 result['data'] = str(e)
1351 return result
1352 if r.status_code in (200, 201, 202, 204):
1353 result['error'] = False
1354 result['data'] = Util.json_loads_byteified(r.text)
1355
1356 return result
1357
1358 def wim_delete(self, token, id):
1359 result = {'error': True, 'data': ''}
1360 headers = {"accept": "application/json",
1361 'Authorization': 'Bearer {}'.format(token['id'])}
1362 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1363 try:
1364 r = requests.delete(_url, params=None, verify=False, headers=headers)
1365 except Exception as e:
1366 log.exception(e)
1367 result['data'] = str(e)
1368 return result
1369 if r.status_code in (200, 201, 202, 204):
1370 result['error'] = False
1371 else:
1372 result['data'] = r.text
1373 return result
1374
1375 def vim_delete(self, token, id):
1376 result = {'error': True, 'data': ''}
1377 headers = {"accept": "application/json",
1378 'Authorization': 'Bearer {}'.format(token['id'])}
1379 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1380 try:
1381 r = requests.delete(_url, params=None, verify=False, headers=headers)
1382 except Exception as e:
1383 log.exception(e)
1384 result['data'] = str(e)
1385 return result
1386 if r.status_code in (200, 201, 202, 204):
1387 result['error'] = False
1388 else:
1389 result['data'] = r.text
1390 return result
1391
1392 def wim_get(self, token, id):
1393
1394 result = {'error': True, 'data': ''}
1395 headers = {"Content-Type": "application/json", "accept": "application/json",
1396 'Authorization': 'Bearer {}'.format(token['id'])}
1397 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1398
1399 try:
1400 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1401 except Exception as e:
1402 log.exception(e)
1403 result['data'] = str(e)
1404 return result
1405 if r.status_code in (200, 201, 202, 204):
1406 result['error'] = False
1407 result['data'] = Util.json_loads_byteified(r.text)
1408 return result
1409
1410 def vim_get(self, token, id):
1411
1412 result = {'error': True, 'data': ''}
1413 headers = {"Content-Type": "application/json", "accept": "application/json",
1414 'Authorization': 'Bearer {}'.format(token['id'])}
1415 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1416
1417 try:
1418 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1419 except Exception as e:
1420 log.exception(e)
1421 result['data'] = str(e)
1422 return result
1423 if r.status_code in (200, 201, 202, 204):
1424 result['error'] = False
1425 result['data'] = Util.json_loads_byteified(r.text)
1426 return result
1427
1428 def wim_create(self, token, wim_data):
1429 result = {'error': True, 'data': ''}
1430 headers = {"Content-Type": "application/json", "accept": "application/json",
1431 'Authorization': 'Bearer {}'.format(token['id'])}
1432
1433 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1434
1435 try:
1436 r = requests.post(_url, json=wim_data, verify=False, headers=headers)
1437 except Exception as e:
1438 log.exception(e)
1439 result['data'] = str(e)
1440 return result
1441 if r.status_code in (200, 201, 202, 204):
1442 result['error'] = False
1443 result['data'] = Util.json_loads_byteified(r.text)
1444 return result
1445
1446 def vim_create(self, token, vim_data):
1447
1448 result = {'error': True, 'data': ''}
1449 headers = {"Content-Type": "application/json", "accept": "application/json",
1450 'Authorization': 'Bearer {}'.format(token['id'])}
1451
1452 _url = "{0}/admin/v1/vims".format(self._base_path)
1453
1454 try:
1455 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1456 except Exception as e:
1457 log.exception(e)
1458 result['data'] = str(e)
1459 return result
1460 if r.status_code in (200, 201, 202, 204):
1461 result['error'] = False
1462 result['data'] = Util.json_loads_byteified(r.text)
1463 return result
1464
1465 def sdn_list(self, token):
1466 result = {'error': True, 'data': ''}
1467 headers = {"accept": "application/json",
1468 'Authorization': 'Bearer {}'.format(token['id'])}
1469 _url = "{0}/admin/v1/sdns".format(self._base_path)
1470 try:
1471 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1472 except Exception as e:
1473 log.exception(e)
1474 result['data'] = str(e)
1475 return result
1476 if r.status_code in (200, 201, 202, 204):
1477 result['error'] = False
1478 result['data'] = Util.json_loads_byteified(r.text)
1479 return result
1480
1481 def sdn_delete(self, token, id):
1482 result = {'error': True, 'data': ''}
1483 headers = {"accept": "application/json",
1484 'Authorization': 'Bearer {}'.format(token['id'])}
1485 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1486 try:
1487 r = requests.delete(_url, params=None, verify=False, headers=headers)
1488 except Exception as e:
1489 log.exception(e)
1490 result['data'] = str(e)
1491 return result
1492 if r.status_code in (200, 201, 202, 204):
1493 result['error'] = False
1494 else:
1495 result['data'] = r.text
1496 return result
1497
1498 def sdn_get(self, token, id):
1499 result = {'error': True, 'data': ''}
1500 headers = {"accept": "application/json",
1501 'Authorization': 'Bearer {}'.format(token['id'])}
1502 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1503
1504 try:
1505 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1506 except Exception as e:
1507 log.exception(e)
1508 result['data'] = str(e)
1509 return result
1510 if r.status_code in (200, 201, 202, 204):
1511 result['error'] = False
1512 result['data'] = Util.json_loads_byteified(r.text)
1513 return result
1514
1515 def sdn_create(self, token, sdn_data):
1516 result = {'error': True, 'data': ''}
1517 headers = {"Content-Type": "application/json", "accept": "application/json",
1518 'Authorization': 'Bearer {}'.format(token['id'])}
1519
1520 _url = "{0}/admin/v1/sdns".format(self._base_path)
1521
1522 try:
1523 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1524 except Exception as e:
1525 log.exception(e)
1526 result['data'] = str(e)
1527 return result
1528 if r.status_code in (200, 201, 202, 204):
1529 result['error'] = False
1530 result['data'] = Util.json_loads_byteified(r.text)
1531 return result
1532
1533 def k8sc_get(self, token, id):
1534 result = {'error': True, 'data': ''}
1535 headers = {"accept": "application/json",
1536 'Authorization': 'Bearer {}'.format(token['id'])}
1537 _url = "{0}/admin/v1/k8sclusters/{1}".format(self._base_path, id)
1538 try:
1539 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1540 except Exception as e:
1541 log.exception(e)
1542 result['data'] = str(e)
1543 return result
1544 if r.status_code in (200, 201, 202, 204):
1545 result['error'] = False
1546 result['data'] = Util.json_loads_byteified(r.text)
1547 return result
1548
1549 def k8sc_list(self, token):
1550 result = {'error': True, 'data': ''}
1551 headers = {"accept": "application/json",
1552 'Authorization': 'Bearer {}'.format(token['id'])}
1553 _url = "{0}/admin/v1/k8sclusters".format(self._base_path)
1554 try:
1555 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1556 except Exception as e:
1557 log.exception(e)
1558 result['data'] = str(e)
1559 return result
1560 if r.status_code in (200, 201, 202, 204):
1561 result['error'] = False
1562 result['data'] = Util.json_loads_byteified(r.text)
1563 return result
1564
1565 def k8sc_create(self, token, cluster_data):
1566 result = {'error': True, 'data': ''}
1567 headers = {"Content-Type": "application/json", "accept": "application/json",
1568 'Authorization': 'Bearer {}'.format(token['id'])}
1569
1570 _url = "{0}/admin/v1/k8sclusters".format(self._base_path)
1571
1572 try:
1573 r = requests.post(_url, json=cluster_data, verify=False, headers=headers)
1574 except Exception as e:
1575 log.exception(e)
1576 result['data'] = str(e)
1577 return result
1578 if r.status_code in (200, 201, 202, 204):
1579 result['error'] = False
1580 result['data'] = Util.json_loads_byteified(r.text)
1581 return result
1582
1583 def k8sc_update(self, token, id, cluster_data):
1584 result = {'error': True, 'data': ''}
1585 headers = {"Content-Type": "application/json", "accept": "application/json",
1586 'Authorization': 'Bearer {}'.format(token['id'])}
1587
1588 _url = "{0}/admin/v1/k8sclusters/{1}".format(self._base_path, id)
1589 try:
1590 r = requests.patch(_url, json=cluster_data, verify=False, headers=headers)
1591 except Exception as e:
1592 log.exception(e)
1593 result['data'] = str(e)
1594 return result
1595 if r.status_code in (200, 201, 202, 204):
1596 result['error'] = False
1597 else:
1598 result['data'] = Util.json_loads_byteified(r.text)
1599 return result
1600
1601 def k8sc_delete(self, token, id):
1602 result = {'error': True, 'data': ''}
1603 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1604 'Authorization': 'Bearer {}'.format(token['id'])}
1605
1606 _url = "{0}/admin/v1/k8sclusters/{1}".format(self._base_path, id)
1607 try:
1608 r = requests.delete(_url, params=None, verify=False, headers=headers)
1609 except Exception as e:
1610 log.exception(e)
1611 result['data'] = str(e)
1612 return result
1613 if r.status_code in (200, 201, 202, 204):
1614 result['error'] = False
1615 else:
1616 result['data'] = Util.json_loads_byteified(r.text)
1617 return result
1618
1619 def k8sr_get(self, token, id):
1620 result = {'error': True, 'data': ''}
1621 headers = {"accept": "application/json",
1622 'Authorization': 'Bearer {}'.format(token['id'])}
1623 _url = "{0}/admin/v1/k8srepos/{1}".format(self._base_path, id)
1624 try:
1625 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1626 except Exception as e:
1627 log.exception(e)
1628 result['data'] = str(e)
1629 return result
1630 if r.status_code in (200, 201, 202, 204):
1631 result['error'] = False
1632 result['data'] = Util.json_loads_byteified(r.text)
1633 return result
1634
1635 def k8sr_list(self, token):
1636 result = {'error': True, 'data': ''}
1637 headers = {"accept": "application/json",
1638 'Authorization': 'Bearer {}'.format(token['id'])}
1639 _url = "{0}/admin/v1/k8srepos".format(self._base_path)
1640 try:
1641 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1642 except Exception as e:
1643 log.exception(e)
1644 result['data'] = str(e)
1645 return result
1646 if r.status_code in (200, 201, 202, 204):
1647 result['error'] = False
1648 result['data'] = Util.json_loads_byteified(r.text)
1649 return result
1650
1651 def k8sr_create(self, token, cluster_data):
1652 result = {'error': True, 'data': ''}
1653 headers = {"Content-Type": "application/json", "accept": "application/json",
1654 'Authorization': 'Bearer {}'.format(token['id'])}
1655
1656 _url = "{0}/admin/v1/k8srepos".format(self._base_path)
1657
1658 try:
1659 r = requests.post(_url, json=cluster_data, verify=False, headers=headers)
1660 except Exception as e:
1661 log.exception(e)
1662 result['data'] = str(e)
1663 return result
1664 if r.status_code in (200, 201, 202, 204):
1665 result['error'] = False
1666 result['data'] = Util.json_loads_byteified(r.text)
1667 return result
1668
1669 def k8sr_update(self, token, id, cluster_data):
1670 result = {'error': True, 'data': ''}
1671 headers = {"Content-Type": "application/json", "accept": "application/json",
1672 'Authorization': 'Bearer {}'.format(token['id'])}
1673
1674 _url = "{0}/admin/v1/k8srepos/{1}".format(self._base_path, id)
1675 try:
1676 r = requests.patch(_url, json=cluster_data, verify=False, headers=headers)
1677 except Exception as e:
1678 log.exception(e)
1679 result['data'] = str(e)
1680 return result
1681 if r.status_code in (200, 201, 202, 204):
1682 result['error'] = False
1683 else:
1684 result['data'] = Util.json_loads_byteified(r.text)
1685 return result
1686
1687 def k8sr_delete(self, token, id):
1688 result = {'error': True, 'data': ''}
1689 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1690 'Authorization': 'Bearer {}'.format(token['id'])}
1691
1692 _url = "{0}/admin/v1/k8srepos/{1}".format(self._base_path, id)
1693 try:
1694 r = requests.delete(_url, params=None, verify=False, headers=headers)
1695 except Exception as e:
1696 log.exception(e)
1697 result['data'] = str(e)
1698 return result
1699 if r.status_code in (200, 201, 202, 204):
1700 result['error'] = False
1701 else:
1702 result['data'] = Util.json_loads_byteified(r.text)
1703 return result
1704
1705 @staticmethod
1706 def md5(f):
1707 hash_md5 = hashlib.md5()
1708 for chunk in iter(lambda: f.read(1024), b""):
1709 hash_md5.update(chunk)
1710 return hash_md5.hexdigest()