user upadate: add/revoke project access
[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
17 import requests
18 import logging
19 import json
20 import tarfile
21 import yaml
22 import pyaml
23 import StringIO
24 from lib.util import Util
25 import hashlib
26 import os
27 from requests.packages.urllib3.exceptions import InsecureRequestWarning
28
29 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
30
31 logging.basicConfig(level=logging.INFO)
32 log = logging.getLogger('helper.py')
33 logging.getLogger("urllib3").setLevel(logging.INFO)
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 == requests.codes.ok:
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 == requests.codes.ok:
71 result['error'] = False
72
73 result['data'] = Util.json_loads_byteified(r.text)
74
75 return result
76
77 def user_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/users".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 == requests.codes.ok:
90 result['error'] = False
91 result['data'] = Util.json_loads_byteified(r.text)
92
93 return result
94
95 def user_create(self, token, user_data):
96 result = {'error': True, 'data': ''}
97 headers = {"Content-Type": "application/json", "accept": "application/json",
98 'Authorization': 'Bearer {}'.format(token['id'])}
99
100 _url = "{0}/admin/v1/users".format(self._base_path)
101
102 try:
103 r = requests.post(_url, json=user_data, verify=False, headers=headers)
104 except Exception as e:
105 log.exception(e)
106 result['data'] = str(e)
107 return result
108 if r.status_code == requests.codes.created:
109 result['error'] = False
110 result['data'] = Util.json_loads_byteified(r.text)
111 return result
112
113 def user_update(self, token, id, user_data):
114 result = {'error': True, 'data': ''}
115 headers = {"Content-Type": "application/json", "accept": "application/json",
116 'Authorization': 'Bearer {}'.format(token['id'])}
117
118 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
119 try:
120 r = requests.patch(_url, json=user_data, verify=False, headers=headers)
121 except Exception as e:
122 log.exception(e)
123 result['data'] = str(e)
124 return result
125 if r.status_code == requests.codes.no_content:
126 result['error'] = False
127 else:
128 result['data'] = Util.json_loads_byteified(r.text)
129 return result
130
131 def user_delete(self, token, id):
132 result = {'error': True, 'data': ''}
133 headers = {"Content-Type": "application/yaml", "accept": "application/json",
134 'Authorization': 'Bearer {}'.format(token['id'])}
135
136 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
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 == requests.codes.no_content:
144 result['error'] = False
145 else:
146 result['data'] = Util.json_loads_byteified(r.text)
147 return result
148
149 def get_user_info(self, token, id):
150 result = {'error': True, 'data': ''}
151 headers = {"Content-Type": "application/yaml", "accept": "application/json",
152 'Authorization': 'Bearer {}'.format(token['id'])}
153 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
154 try:
155 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
156 except Exception as e:
157 log.exception(e)
158 result['data'] = str(e)
159 return result
160 if r.status_code == requests.codes.ok:
161 result['error'] = False
162 result['data'] = Util.json_loads_byteified(r.text)
163 return result
164
165 def project_list(self, token):
166 result = {'error': True, 'data': ''}
167 headers = {"Content-Type": "application/yaml", "accept": "application/json",
168 'Authorization': 'Bearer {}'.format(token['id'])}
169
170 _url = "{0}/admin/v1/projects".format(self._base_path)
171 try:
172 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
173 except Exception as e:
174 log.exception(e)
175 result['data'] = str(e)
176 return result
177 if r.status_code == requests.codes.ok:
178 result['error'] = False
179 result['data'] = Util.json_loads_byteified(r.text)
180
181 return result
182
183 def project_get(self, token, id):
184 result = {'error': True, 'data': ''}
185 headers = {"Content-Type": "application/yaml", "accept": "application/json",
186 'Authorization': 'Bearer {}'.format(token['id'])}
187 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
188 try:
189 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
190 except Exception as e:
191 log.exception(e)
192 result['data'] = str(e)
193 return result
194 if r.status_code == requests.codes.ok:
195 result['error'] = False
196 result['data'] = Util.json_loads_byteified(r.text)
197 return result
198
199 def project_create(self, token, project_data):
200
201 result = {'error': True, 'data': ''}
202 headers = {"Content-Type": "application/json", "accept": "application/json",
203 'Authorization': 'Bearer {}'.format(token['id'])}
204
205 _url = "{0}/admin/v1/projects".format(self._base_path)
206
207 try:
208 r = requests.post(_url, json=project_data, verify=False, headers=headers)
209 except Exception as e:
210 log.exception(e)
211 result['data'] = str(e)
212 return result
213 if r.status_code == requests.codes.created:
214 result['error'] = False
215 result['data'] = Util.json_loads_byteified(r.text)
216 return result
217
218 def project_edit(self, token, id, project_data):
219
220 result = {'error': True, 'data': ''}
221 headers = {"Content-Type": "application/json", "accept": "application/json",
222 'Authorization': 'Bearer {}'.format(token['id'])}
223
224 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
225
226 try:
227 r = requests.put(_url, json=project_data, 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 == requests.codes.no_content:
233 result['error'] = False
234 result['data'] = Util.json_loads_byteified(r.text)
235 return result
236
237 def project_delete(self, token, id):
238 result = {'error': True, 'data': ''}
239 headers = {"Content-Type": "application/yaml", "accept": "application/json",
240 'Authorization': 'Bearer {}'.format(token['id'])}
241
242 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
243 try:
244 r = requests.delete(_url, params=None, verify=False, headers=headers)
245 except Exception as e:
246 log.exception(e)
247 result['data'] = str(e)
248 return result
249 if r.status_code == requests.codes.no_content:
250 result['error'] = False
251 else:
252 result['data'] = Util.json_loads_byteified(r.text)
253 return result
254
255 def nsd_list(self, token):
256 result = {'error': True, 'data': ''}
257 headers = {"Content-Type": "application/yaml", "accept": "application/json",
258 'Authorization': 'Bearer {}'.format(token['id'])}
259
260 _url = "{0}/nsd/v1/ns_descriptors_content".format(self._base_path)
261 try:
262 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
263 except Exception as e:
264 log.exception(e)
265 result['data'] = str(e)
266 return result
267 if r.status_code == requests.codes.ok:
268 result['error'] = False
269 result['data'] = Util.json_loads_byteified(r.text)
270
271 return result
272
273 def vnfd_list(self, token):
274 result = {'error': True, 'data': ''}
275 headers = {"Content-Type": "application/yaml", "accept": "application/json",
276 'Authorization': 'Bearer {}'.format(token['id'])}
277
278 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
279 try:
280 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
281 except Exception as e:
282 log.exception(e)
283 result['data'] = str(e)
284 return result
285 if r.status_code == requests.codes.ok:
286 result['error'] = False
287 result['data'] = Util.json_loads_byteified(r.text)
288
289 return result
290
291 def ns_list(self, token):
292 result = {'error': True, 'data': ''}
293 headers = {"Content-Type": "application/yaml", "accept": "application/json",
294 'Authorization': 'Bearer {}'.format(token['id'])}
295 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
296 try:
297 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
298 except Exception as e:
299 log.exception(e)
300 result['data'] = str(e)
301 return result
302 if r.status_code == requests.codes.ok:
303 result['error'] = False
304 result['data'] = Util.json_loads_byteified(r.text)
305
306 return result
307
308 def vnf_list(self, token):
309 result = {'error': True, 'data': ''}
310 headers = {"Content-Type": "application/yaml", "accept": "application/json",
311 'Authorization': 'Bearer {}'.format(token['id'])}
312 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
313 try:
314 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
315 except Exception as e:
316 log.exception(e)
317 result['data'] = str(e)
318 return result
319 if r.status_code == requests.codes.ok:
320 result['error'] = False
321 result['data'] = Util.json_loads_byteified(r.text)
322
323 return result
324
325 def nsd_delete(self, token, id):
326 result = {'error': True, 'data': ''}
327 headers = {"Content-Type": "application/yaml", "accept": "application/json",
328 'Authorization': 'Bearer {}'.format(token['id'])}
329
330 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
331 try:
332 r = requests.delete(_url, params=None, verify=False,headers=headers)
333 except Exception as e:
334 log.exception(e)
335 result['data'] = str(e)
336 return result
337 if r.status_code == requests.codes.ok:
338 result['error'] = False
339 result['data'] = Util.json_loads_byteified(r.text)
340 return result
341
342 def vnfd_delete(self, token, id):
343 result = {'error': True, 'data': ''}
344 headers = {"Content-Type": "application/yaml", "accept": "application/json",
345 'Authorization': 'Bearer {}'.format(token['id'])}
346
347 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
348 try:
349 r = requests.delete(_url, params=None, verify=False, headers=headers)
350 except Exception as e:
351 log.exception(e)
352 result['data'] = str(e)
353 return result
354 if r:
355 result['error'] = False
356 if r.status_code != requests.codes.no_content:
357 result['data'] = Util.json_loads_byteified(r.text)
358 return result
359
360 def nsd_onboard(self, token, package):
361 result = {'error': True, 'data': ''}
362 headers = {"Content-Type": "application/gzip", "accept": "application/json",
363 'Authorization': 'Bearer {}'.format(token['id'])}
364 with open('/tmp/'+package.name, 'wb+') as destination:
365 for chunk in package.chunks():
366 destination.write(chunk)
367 headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb'))
368 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
369 try:
370 r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers)
371 except Exception as e:
372 log.exception(e)
373 result['data'] = str(e)
374 return result
375 if r.status_code == requests.codes.created:
376 result['error'] = False
377 result['data'] = Util.json_loads_byteified(r.text)
378 return result
379
380 def vnfd_onboard(self, token, package):
381 result = {'error': True, 'data': ''}
382 headers = {"Content-Type": "application/gzip", "accept": "application/json",
383 'Authorization': 'Bearer {}'.format(token['id'])}
384 with open('/tmp/'+package.name, 'wb+') as destination:
385 for chunk in package.chunks():
386 destination.write(chunk)
387 headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb'))
388 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
389 try:
390 r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers)
391 except Exception as e:
392 log.exception(e)
393 result['data'] = str(e)
394 return result
395 if r.status_code == requests.codes.created:
396 result['error'] = False
397 result['data'] = Util.json_loads_byteified(r.text)
398 return result
399
400 def nsd_clone(self, token, id):
401 result = {'error': True, 'data': ''}
402 headers = {"Content-Type": "application/gzip", "accept": "application/json",
403 'Authorization': 'Bearer {}'.format(token['id'])}
404
405 # get the package onboarded
406 tar_pkg = self.get_nsd_pkg(token, id)
407 tarf = tarfile.open(fileobj=tar_pkg)
408 tarf = self._descriptor_clone(tarf, 'nsd')
409 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
410
411 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
412
413 try:
414 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
415 headers=headers)
416 except Exception as e:
417 log.exception(e)
418 result['data'] = str(e)
419 return result
420 if r.status_code == requests.codes.created:
421 result['error'] = False
422 if r.status_code == requests.codes.conflict:
423 result['data'] = "Invalid ID."
424
425 return result
426
427 def vnfd_clone(self, token, id):
428 result = {'error': True, 'data': ''}
429 headers = {"Content-Type": "application/gzip", "accept": "application/json",
430 'Authorization': 'Bearer {}'.format(token['id'])}
431
432 # get the package onboarded
433 tar_pkg = self.get_vnfd_pkg(token, id)
434 tarf = tarfile.open(fileobj=tar_pkg)
435
436 tarf = self._descriptor_clone(tarf, 'vnfd')
437 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
438
439 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
440
441 try:
442 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
443 headers=headers)
444 except Exception as e:
445 log.exception(e)
446 result['data'] = str(e)
447 return result
448 if r.status_code == requests.codes.created:
449 result['error'] = False
450 if r.status_code == requests.codes.conflict:
451 result['data'] = "Invalid ID."
452
453 return result
454
455 def nsd_update(self, token, id, data):
456 result = {'error': True, 'data': ''}
457 headers = {"Content-Type": "application/gzip", "accept": "application/json",
458 'Authorization': 'Bearer {}'.format(token['id'])}
459
460 # get the package onboarded
461 tar_pkg = self.get_nsd_pkg(token, id)
462 tarf = tarfile.open(fileobj=tar_pkg)
463
464 tarf = self._descriptor_update(tarf, data)
465 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
466
467 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
468
469 try:
470 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
471 headers=headers)
472 except Exception as e:
473 log.exception(e)
474 result['data'] = str(e)
475 return result
476 if r.status_code == requests.codes.no_content:
477 result['error'] = False
478
479 return result
480
481 def vnfd_update(self, token, id, data):
482 result = {'error': True, 'data': ''}
483 headers = {"Content-Type": "application/gzip", "accept": "application/json",
484 'Authorization': 'Bearer {}'.format(token['id'])}
485
486 # get the package onboarded
487 tar_pkg = self.get_vnfd_pkg(token, id)
488 tarf = tarfile.open(fileobj=tar_pkg)
489
490 tarf = self._descriptor_update(tarf, data)
491 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
492
493 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
494
495 try:
496 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
497 headers=headers)
498 except Exception as e:
499 log.exception(e)
500 result['data'] = str(e)
501 return result
502 if r.status_code == requests.codes.no_content:
503 result['error'] = False
504
505 return result
506
507 def get_nsd_pkg(self, token, id):
508 result = {'error': True, 'data': ''}
509 headers = { "accept": "application/zip",
510 'Authorization': 'Bearer {}'.format(token['id'])}
511
512 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
513 try:
514 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
515 except Exception as e:
516 log.exception(e)
517 result['data'] = str(e)
518 return result
519 if r.status_code == requests.codes.ok:
520 result['error'] = False
521 tarf = StringIO.StringIO(r.content)
522 return tarf
523 return result
524
525 def get_vnfd_pkg(self, token, id):
526 result = {'error': True, 'data': ''}
527 headers = {"accept": "application/zip",
528 'Authorization': 'Bearer {}'.format(token['id'])}
529 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
530 try:
531 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
532 except Exception as e:
533 log.exception(e)
534 result['data'] = str(e)
535 return result
536 if r.status_code == requests.codes.ok:
537 result['error'] = False
538 tarf = StringIO.StringIO(r.content)
539 return tarf
540 return result
541
542 def _descriptor_update(self, tarf, data):
543 # extract the package on a tmp directory
544 tarf.extractall('/tmp')
545
546 for name in tarf.getnames():
547 if name.endswith(".yaml") or name.endswith(".yml"):
548 with open('/tmp/' + name, 'w') as outfile:
549 yaml.safe_dump(data, outfile, default_flow_style=False)
550 break
551
552 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
553
554 for tarinfo in tarf:
555 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
556 tarf_temp.close()
557 return tarf
558
559 def _descriptor_clone(self, tarf, descriptor_type):
560 # extract the package on a tmp directory
561 tarf.extractall('/tmp')
562
563 for name in tarf.getnames():
564 if name.endswith(".yaml") or name.endswith(".yml"):
565 with open('/tmp/' + name, 'r') as outfile:
566 yaml_object = yaml.load(outfile)
567
568 if descriptor_type == 'nsd':
569 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
570 for nsd in nsd_list:
571 nsd['id'] = 'clone_' + nsd['id']
572 nsd['name'] = 'clone_' +nsd['name']
573 nsd['short-name'] = 'clone_' +nsd['short-name']
574 elif descriptor_type == 'vnfd':
575 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
576 for vnfd in vnfd_list:
577 vnfd['id'] = 'clone_' + vnfd['id']
578 vnfd['name'] = 'clone_' + vnfd['name']
579 vnfd['short-name'] = 'clone_' + vnfd['short-name']
580
581
582 with open('/tmp/' + name, 'w') as yaml_file:
583 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
584 break
585
586 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
587
588 for tarinfo in tarf:
589 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
590 tarf_temp.close()
591 return tarf
592
593 def nsd_get(self, token, id):
594 result = {'error': True, 'data': ''}
595 headers = {'Content-Type': 'application/yaml',
596 'Authorization': 'Bearer {}'.format(token['id'])}
597 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
598 try:
599 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
600 except Exception as e:
601 log.exception(e)
602 result['data'] = str(e)
603 return result
604 if r.status_code == requests.codes.ok:
605 result['error'] = False
606 return yaml.load(r.text)
607 else:
608 try:
609 result['data'] = r.json()
610 except Exception as e:
611 result['data'] = {}
612 return result
613
614 def vnfd_get(self, token, id):
615 result = {'error': True, 'data': ''}
616 headers = {'Content-Type': 'application/yaml',
617 'Authorization': 'Bearer {}'.format(token['id'])}
618 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
619 try:
620 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
621 except Exception as e:
622 log.exception(e)
623 result['data'] = str(e)
624 return result
625 if r.status_code == requests.codes.ok:
626 result['error'] = False
627 return yaml.load(r.text)
628 else:
629 try:
630 result['data'] = r.json()
631 except Exception as e:
632 result['data'] = {}
633 return result
634
635 def nsd_artifacts(self, token, id):
636 result = {'error': True, 'data': ''}
637 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
638 'Authorization': 'Bearer {}'.format(token['id'])}
639 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
640 try:
641 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
642 except Exception as e:
643 log.exception(e)
644 result['data'] = str(e)
645 return result
646 if r.status_code == requests.codes.ok:
647 result['error'] = False
648 result['data'] = r.text
649 else:
650 try:
651 result['data'] = r.json()
652 except Exception as e:
653 result['data'] = {}
654
655 return result
656
657 def vnf_packages_artifacts(self, token, id):
658 result = {'error': True, 'data': ''}
659 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
660 'Authorization': 'Bearer {}'.format(token['id'])}
661 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
662 try:
663 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
664 except Exception as e:
665 log.exception(e)
666 result['data'] = str(e)
667 return result
668 if r.status_code == requests.codes.ok:
669 result['error'] = False
670 result['data'] = r.text
671 else:
672 try:
673 result['data'] = r.json()
674 except Exception as e:
675 result['data'] = {}
676
677 return result
678
679 def ns_create(self, token, ns_data):
680 result = {'error': True, 'data': ''}
681 headers = {"Content-Type": "application/yaml", "accept": "application/json",
682 'Authorization': 'Bearer {}'.format(token['id'])}
683
684 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
685
686 try:
687 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
688 except Exception as e:
689 log.exception(e)
690 result['data'] = str(e)
691 return result
692 if r.status_code == requests.codes.ok:
693 result['error'] = False
694 result['data'] = Util.json_loads_byteified(r.text)
695 return result
696
697 def ns_op_list(self, token, id):
698 result = {'error': True, 'data': ''}
699 headers = {"Content-Type": "application/json", "accept": "application/json",
700 'Authorization': 'Bearer {}'.format(token['id'])}
701 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
702
703 try:
704 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
705 except Exception as e:
706 log.exception(e)
707 result['data'] = str(e)
708 return result
709 if r.status_code == requests.codes.ok:
710 result['error'] = False
711 result['data'] = Util.json_loads_byteified(r.text)
712
713 return result
714
715 def ns_op(self, token, id):
716 result = {'error': True, 'data': ''}
717 headers = {"Content-Type": "application/json", "accept": "application/json",
718 'Authorization': 'Bearer {}'.format(token['id'])}
719 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
720
721 try:
722 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
723 except Exception as e:
724 log.exception(e)
725 result['data'] = str(e)
726 return result
727 if r.status_code == requests.codes.ok:
728 result['error'] = False
729 result['data'] = Util.json_loads_byteified(r.text)
730
731 return result
732
733 def ns_action(self, token, id, action_payload):
734 result = {'error': True, 'data': ''}
735 headers = {"Content-Type": "application/json", "accept": "application/json",
736 'Authorization': 'Bearer {}'.format(token['id'])}
737
738 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
739
740 try:
741 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
742 except Exception as e:
743 log.exception(e)
744 result['data'] = str(e)
745 return result
746 if r.status_code == requests.codes.created:
747 result['error'] = False
748 result['data'] = Util.json_loads_byteified(r.text)
749 return result
750
751 def ns_delete(self, token, id, force=None):
752 result = {'error': True, 'data': ''}
753 headers = {"Content-Type": "application/yaml", "accept": "application/json",
754 'Authorization': 'Bearer {}'.format(token['id'])}
755 query_path = ''
756 if force:
757 query_path = '?FORCE=true'
758 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
759 try:
760 r = requests.delete(_url, params=None, verify=False, headers=headers)
761 except Exception as e:
762 log.exception(e)
763 result['data'] = str(e)
764 return result
765 if r:
766 result['error'] = False
767 if r.status_code != requests.codes.no_content:
768 result['data'] = Util.json_loads_byteified(r.text)
769 return result
770
771 def ns_get(self, token, id):
772 result = {'error': True, 'data': ''}
773 headers = {"Content-Type": "application/json", "accept": "application/json",
774 'Authorization': 'Bearer {}'.format(token['id'])}
775 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
776
777 try:
778 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
779 except Exception as e:
780 log.exception(e)
781 result['data'] = str(e)
782 return result
783 if r.status_code == requests.codes.ok:
784 result['error'] = False
785 result['data'] = Util.json_loads_byteified(r.text)
786 return result
787
788 def vnf_get(self, token, id):
789 result = {'error': True, 'data': ''}
790 headers = {"Content-Type": "application/json", "accept": "application/json",
791 'Authorization': 'Bearer {}'.format(token['id'])}
792 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
793
794 try:
795 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
796 except Exception as e:
797 log.exception(e)
798 result['data'] = str(e)
799 return result
800 if r.status_code == requests.codes.ok:
801 result['error'] = False
802 result['data'] = Util.json_loads_byteified(r.text)
803 return result
804
805 def ns_alarm_create(self, token, id, alarm_payload):
806 result = {'error': True, 'data': ''}
807 headers = {"Content-Type": "application/json",
808 'Authorization': 'Bearer {}'.format(token['id'])}
809 _url = "{0}/test/message/alarm_request".format(self._base_path)
810 try:
811 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
812 except Exception as e:
813 log.exception(e)
814 result['data'] = str(e)
815 return result
816 if r.status_code == requests.codes.ok:
817 result['error'] = False
818 #result['data'] = Util.json_loads_byteified(r.text)
819 result['data'] = r.text
820 return result
821
822 def ns_metric_export(self, token, id, metric_payload):
823 result = {'error': True, 'data': ''}
824 headers = {"Content-Type": "application/json",
825 'Authorization': 'Bearer {}'.format(token['id'])}
826 _url = "{0}/test/message/metric_request".format(self._base_path)
827 try:
828 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
829 except Exception as e:
830 log.exception(e)
831 result['data'] = str(e)
832 return result
833 if r.status_code == requests.codes.ok:
834 result['error'] = False
835 #result['data'] = Util.json_loads_byteified(r.text)
836 result['data'] = r.text
837 return result
838
839 def vim_list(self, token):
840 result = {'error': True, 'data': ''}
841 headers = {"Content-Type": "application/yaml", "accept": "application/json",
842 'Authorization': 'Bearer {}'.format(token['id'])}
843 _url = "{0}/admin/v1/vims".format(self._base_path)
844 try:
845 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
846 except Exception as e:
847 log.exception(e)
848 result['data'] = str(e)
849 return result
850 if r.status_code == requests.codes.ok:
851 result['error'] = False
852 result['data'] = Util.json_loads_byteified(r.text)
853
854 return result
855
856 def vim_delete(self, token, id):
857 result = {'error': True, 'data': ''}
858 headers = { "accept": "application/json",
859 'Authorization': 'Bearer {}'.format(token['id'])}
860 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
861 try:
862 r = requests.delete(_url, params=None, verify=False, headers=headers)
863 except Exception as e:
864 log.exception(e)
865 result['data'] = str(e)
866 return result
867 if r.status_code == requests.codes.accepted:
868 result['error'] = False
869 else:
870 result['data'] = r.text
871 return result
872
873 def vim_get(self, token, id):
874
875 result = {'error': True, 'data': ''}
876 headers = {"Content-Type": "application/json", "accept": "application/json",
877 'Authorization': 'Bearer {}'.format(token['id'])}
878 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
879
880 try:
881 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
882 except Exception as e:
883 log.exception(e)
884 result['data'] = str(e)
885 return result
886 if r.status_code == requests.codes.ok:
887 result['error'] = False
888 result['data'] = Util.json_loads_byteified(r.text)
889 return result
890
891 def vim_create(self, token, vim_data):
892
893 result = {'error': True, 'data': ''}
894 headers = {"Content-Type": "application/json", "accept": "application/json",
895 'Authorization': 'Bearer {}'.format(token['id'])}
896
897 _url = "{0}/admin/v1/vims".format(self._base_path)
898
899 try:
900 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
901 except Exception as e:
902 log.exception(e)
903 result['data'] = str(e)
904 return result
905 if r.status_code == requests.codes.created:
906 result['error'] = False
907 result['data'] = Util.json_loads_byteified(r.text)
908 return result
909
910 def sdn_list(self, token):
911 result = {'error': True, 'data': ''}
912 headers = {"accept": "application/json",
913 'Authorization': 'Bearer {}'.format(token['id'])}
914 _url = "{0}/admin/v1/sdns".format(self._base_path)
915 try:
916 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
917 except Exception as e:
918 log.exception(e)
919 result['data'] = str(e)
920 return result
921 if r.status_code == requests.codes.ok:
922 result['error'] = False
923 result['data'] = Util.json_loads_byteified(r.text)
924 return result
925
926 def sdn_delete(self, token, id):
927 result = {'error': True, 'data': ''}
928 headers = {"accept": "application/json",
929 'Authorization': 'Bearer {}'.format(token['id'])}
930 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
931 try:
932 r = requests.delete(_url, params=None, verify=False, headers=headers)
933 except Exception as e:
934 log.exception(e)
935 result['data'] = str(e)
936 return result
937 if r.status_code == requests.codes.accepted:
938 result['error'] = False
939 else:
940 result['data'] = r.text
941 return result
942
943 def sdn_get(self, token, id):
944 result = {'error': True, 'data': ''}
945 headers = {"accept": "application/json",
946 'Authorization': 'Bearer {}'.format(token['id'])}
947 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
948
949 try:
950 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
951 except Exception as e:
952 log.exception(e)
953 result['data'] = str(e)
954 return result
955 if r.status_code == requests.codes.ok:
956 result['error'] = False
957 result['data'] = Util.json_loads_byteified(r.text)
958 return result
959
960
961 def sdn_create(self, token, sdn_data):
962 result = {'error': True, 'data': ''}
963 headers = {"Content-Type": "application/json", "accept": "application/json",
964 'Authorization': 'Bearer {}'.format(token['id'])}
965
966 _url = "{0}/admin/v1/sdns".format(self._base_path)
967
968 try:
969 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
970 except Exception as e:
971 log.exception(e)
972 result['data'] = str(e)
973 return result
974 if r.status_code == requests.codes.created:
975 result['error'] = False
976 result['data'] = Util.json_loads_byteified(r.text)
977 return result
978
979
980 @staticmethod
981 def md5(f):
982 hash_md5 = hashlib.md5()
983 for chunk in iter(lambda: f.read(1024), b""):
984 hash_md5.update(chunk)
985 return hash_md5.hexdigest()