some fix on delete action
[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
28 logging.basicConfig(level=logging.DEBUG)
29 log = logging.getLogger('helper.py')
30
31
32 class Client(object):
33 def __init__(self):
34 self._token_endpoint = 'admin/v1/tokens'
35 self._user_endpoint = 'admin/v1/users'
36 self._host = os.getenv('OSM_SERVER', "localhost")
37 self._so_port = 9999
38 self._base_path = "https://{0}:{1}/osm".format(self._host, self._so_port)
39
40 def auth(self, args):
41 result = {'error': True, 'data': ''}
42 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
43 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
44 try:
45 r = requests.post(token_url, json=args, verify=False, headers=headers)
46 except Exception as e:
47 log.exception(e)
48 result['data'] = str(e)
49 return result
50 if r.status_code == requests.codes.ok:
51 result['error'] = False
52
53 result['data'] = Util.json_loads_byteified(r.text)
54
55 return result
56
57 def switch_project(self, args):
58 result = {'error': True, 'data': ''}
59 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
60 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
61 try:
62 r = requests.post(token_url, json=args, verify=False, headers=headers)
63 except Exception as e:
64 log.exception(e)
65 result['data'] = str(e)
66 return result
67 if r.status_code == requests.codes.ok:
68 result['error'] = False
69
70 result['data'] = Util.json_loads_byteified(r.text)
71
72 return result
73
74 def user_list(self, token):
75 result = {'error': True, 'data': ''}
76 headers = {"Content-Type": "application/json", "accept": "application/json",
77 'Authorization': 'Bearer {}'.format(token['id'])}
78
79 _url = "{0}/admin/v1/users".format(self._base_path)
80 try:
81 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
82 except Exception as e:
83 log.exception(e)
84 result['data'] = str(e)
85 return result
86 if r.status_code == requests.codes.ok:
87 result['error'] = False
88 result['data'] = Util.json_loads_byteified(r.text)
89
90 return result
91
92 def user_create(self, token, user_data):
93 result = {'error': True, 'data': ''}
94 headers = {"Content-Type": "application/json", "accept": "application/json",
95 'Authorization': 'Bearer {}'.format(token['id'])}
96
97 _url = "{0}/admin/v1/users".format(self._base_path)
98
99 try:
100 r = requests.post(_url, json=user_data, verify=False, headers=headers)
101 except Exception as e:
102 log.exception(e)
103 result['data'] = str(e)
104 return result
105 print r.status_code
106 if r.status_code == requests.codes.created:
107 result['error'] = False
108 result['data'] = Util.json_loads_byteified(r.text)
109 return result
110
111 def user_delete(self, token, id):
112 result = {'error': True, 'data': ''}
113 headers = {"Content-Type": "application/yaml", "accept": "application/json",
114 'Authorization': 'Bearer {}'.format(token['id'])}
115
116 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
117 try:
118 r = requests.delete(_url, params=None, 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 == requests.codes.no_content:
124 result['error'] = False
125 else:
126 result['data'] = Util.json_loads_byteified(r.text)
127 return result
128
129 def get_user_info(self, token, id):
130 result = {'error': True, 'data': ''}
131 headers = {"Content-Type": "application/yaml", "accept": "application/json",
132 'Authorization': 'Bearer {}'.format(token['id'])}
133 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
134 try:
135 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
136 except Exception as e:
137 log.exception(e)
138 result['data'] = str(e)
139 return result
140 if r.status_code == requests.codes.ok:
141 result['error'] = False
142 result['data'] = Util.json_loads_byteified(r.text)
143 return result
144
145 def project_list(self, token):
146 result = {'error': True, 'data': ''}
147 headers = {"Content-Type": "application/yaml", "accept": "application/json",
148 'Authorization': 'Bearer {}'.format(token['id'])}
149
150 _url = "{0}/admin/v1/projects".format(self._base_path)
151 try:
152 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
153 except Exception as e:
154 log.exception(e)
155 result['data'] = str(e)
156 return result
157 if r.status_code == requests.codes.ok:
158 result['error'] = False
159 result['data'] = Util.json_loads_byteified(r.text)
160
161 return result
162
163 def project_get(self, token, id):
164 result = {'error': True, 'data': ''}
165 headers = {"Content-Type": "application/yaml", "accept": "application/json",
166 'Authorization': 'Bearer {}'.format(token['id'])}
167 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
168 try:
169 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
170 except Exception as e:
171 log.exception(e)
172 result['data'] = str(e)
173 return result
174 if r.status_code == requests.codes.ok:
175 result['error'] = False
176 result['data'] = Util.json_loads_byteified(r.text)
177 return result
178
179 def project_create(self, token, project_data):
180
181 result = {'error': True, 'data': ''}
182 headers = {"Content-Type": "application/json", "accept": "application/json",
183 'Authorization': 'Bearer {}'.format(token['id'])}
184
185 _url = "{0}/admin/v1/projects".format(self._base_path)
186
187 try:
188 r = requests.post(_url, json=project_data, verify=False, headers=headers)
189 except Exception as e:
190 log.exception(e)
191 result['data'] = str(e)
192 return result
193 print r.status_code
194 if r.status_code == requests.codes.created:
195 result['error'] = False
196 result['data'] = Util.json_loads_byteified(r.text)
197 return result
198
199 def project_edit(self, token, id, 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/{1}".format(self._base_path, id)
206
207 try:
208 r = requests.put(_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 print r.status_code
214 if r.status_code == requests.codes.no_content:
215 result['error'] = False
216 result['data'] = Util.json_loads_byteified(r.text)
217 return result
218
219 def project_delete(self, token, id):
220 result = {'error': True, 'data': ''}
221 headers = {"Content-Type": "application/yaml", "accept": "application/json",
222 'Authorization': 'Bearer {}'.format(token['id'])}
223
224 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
225 try:
226 r = requests.delete(_url, params=None, verify=False, headers=headers)
227 except Exception as e:
228 log.exception(e)
229 result['data'] = str(e)
230 return result
231 if r.status_code == requests.codes.no_content:
232 result['error'] = False
233 else:
234 result['data'] = Util.json_loads_byteified(r.text)
235 return result
236
237 def nsd_list(self, token):
238 result = {'error': True, 'data': ''}
239 headers = {"Content-Type": "application/yaml", "accept": "application/json",
240 'Authorization': 'Bearer {}'.format(token['id'])}
241
242 _url = "{0}/nsd/v1/ns_descriptors_content".format(self._base_path)
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 == requests.codes.ok:
250 result['error'] = False
251 result['data'] = Util.json_loads_byteified(r.text)
252
253 return result
254
255 def vnfd_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}/vnfpkgm/v1/vnf_packages_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 ns_list(self, token):
274 result = {'error': True, 'data': ''}
275 headers = {"Content-Type": "application/yaml", "accept": "application/json",
276 'Authorization': 'Bearer {}'.format(token['id'])}
277 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
278 try:
279 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
280 except Exception as e:
281 log.exception(e)
282 result['data'] = str(e)
283 return result
284 if r.status_code == requests.codes.ok:
285 result['error'] = False
286 result['data'] = Util.json_loads_byteified(r.text)
287
288 return result
289
290 def vnf_list(self, token):
291 result = {'error': True, 'data': ''}
292 headers = {"Content-Type": "application/yaml", "accept": "application/json",
293 'Authorization': 'Bearer {}'.format(token['id'])}
294 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
295 try:
296 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
297 except Exception as e:
298 log.exception(e)
299 result['data'] = str(e)
300 return result
301 if r.status_code == requests.codes.ok:
302 result['error'] = False
303 result['data'] = Util.json_loads_byteified(r.text)
304
305 return result
306
307 def nsd_delete(self, token, id):
308 result = {'error': True, 'data': ''}
309 headers = {"Content-Type": "application/yaml", "accept": "application/json",
310 'Authorization': 'Bearer {}'.format(token['id'])}
311
312 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
313 try:
314 r = requests.delete(_url, params=None, verify=False,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 return result
323
324 def vnfd_delete(self, token, id):
325 result = {'error': True, 'data': ''}
326 headers = {"Content-Type": "application/yaml", "accept": "application/json",
327 'Authorization': 'Bearer {}'.format(token['id'])}
328
329 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
330 try:
331 r = requests.delete(_url, params=None, verify=False, headers=headers)
332 except Exception as e:
333 log.exception(e)
334 result['data'] = str(e)
335 return result
336 if r:
337 result['error'] = False
338 if r.status_code != requests.codes.no_content:
339 result['data'] = Util.json_loads_byteified(r.text)
340 return result
341
342 def nsd_onboard(self, token, package):
343 result = {'error': True, 'data': ''}
344 headers = {"Content-Type": "application/gzip", "accept": "application/json",
345 'Authorization': 'Bearer {}'.format(token['id'])}
346 with open('/tmp/'+package.name, 'wb+') as destination:
347 for chunk in package.chunks():
348 destination.write(chunk)
349 headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb'))
350 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
351 try:
352 r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers)
353 except Exception as e:
354 log.exception(e)
355 result['data'] = str(e)
356 return result
357 if r.status_code == requests.codes.created:
358 result['error'] = False
359 result['data'] = Util.json_loads_byteified(r.text)
360 return result
361
362 def vnfd_onboard(self, token, package):
363 result = {'error': True, 'data': ''}
364 headers = {"Content-Type": "application/gzip", "accept": "application/json",
365 'Authorization': 'Bearer {}'.format(token['id'])}
366 with open('/tmp/'+package.name, 'wb+') as destination:
367 for chunk in package.chunks():
368 destination.write(chunk)
369 headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb'))
370 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
371 try:
372 r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers)
373 except Exception as e:
374 log.exception(e)
375 result['data'] = str(e)
376 return result
377 if r.status_code == requests.codes.created:
378 result['error'] = False
379 result['data'] = Util.json_loads_byteified(r.text)
380 return result
381
382 def nsd_update(self, token, id, data):
383 result = {'error': True, 'data': ''}
384 headers = {"Content-Type": "application/gzip", "accept": "application/json",
385 'Authorization': 'Bearer {}'.format(token['id'])}
386
387 # get the package onboarded
388 tar_pkg = self.get_nsd_pkg(token, id)
389 tarf = tarfile.open(fileobj=tar_pkg)
390
391 tarf = self._descriptor_update(tarf, data)
392 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
393
394 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
395
396 try:
397 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
398 headers=headers)
399 except Exception as e:
400 log.exception(e)
401 result['data'] = str(e)
402 return result
403 if r.status_code == requests.codes.no_content:
404 result['error'] = False
405
406 return result
407
408 def vnfd_update(self, token, id, data):
409 result = {'error': True, 'data': ''}
410 headers = {"Content-Type": "application/gzip", "accept": "application/json",
411 'Authorization': 'Bearer {}'.format(token['id'])}
412
413 # get the package onboarded
414 tar_pkg = self.get_vnfd_pkg(token, id)
415 tarf = tarfile.open(fileobj=tar_pkg)
416
417 tarf = self._descriptor_update(tarf, data)
418 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
419
420 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
421
422 try:
423 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
424 headers=headers)
425 except Exception as e:
426 log.exception(e)
427 result['data'] = str(e)
428 return result
429 if r.status_code == requests.codes.no_content:
430 result['error'] = False
431
432 return result
433
434 def get_nsd_pkg(self, token, id):
435 result = {'error': True, 'data': ''}
436 headers = { "accept": "application/zip",
437 'Authorization': 'Bearer {}'.format(token['id'])}
438
439 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
440 try:
441 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
442 except Exception as e:
443 log.exception(e)
444 result['data'] = str(e)
445 return result
446 if r.status_code == requests.codes.ok:
447 result['error'] = False
448 tarf = StringIO.StringIO(r.content)
449 return tarf
450 return result
451
452 def get_vnfd_pkg(self, token, id):
453 result = {'error': True, 'data': ''}
454 headers = {"accept": "application/zip",
455 'Authorization': 'Bearer {}'.format(token['id'])}
456 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
457 try:
458 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
459 print r.status_code
460 except Exception as e:
461 log.exception(e)
462 result['data'] = str(e)
463 return result
464 if r.status_code == requests.codes.ok:
465 result['error'] = False
466 tarf = StringIO.StringIO(r.content)
467 return tarf
468 return result
469
470 def _descriptor_update(self, tarf, data):
471 print tarf.getnames()
472 # extract the package on a tmp directory
473 tarf.extractall('/tmp')
474
475 for name in tarf.getnames():
476 if name.endswith(".yaml") or name.endswith(".yml"):
477 with open('/tmp/' + name, 'w') as outfile:
478 yaml.safe_dump(data, outfile, default_flow_style=False)
479 break
480
481 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
482 # tarf_temp = tarfile.open("pippo.tar.gz", "w:gz")
483 print tarf_temp.getnames()
484 # tarf_temp.add('/tmp/'+tarf.getnames()[0])
485 for tarinfo in tarf:
486 # if tarinfo.name.startswith(tarf.getnames()[0]):
487 # new_name = tarinfo.name[len(tarf.getnames()[0]):]
488 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
489 print tarf_temp.getnames()
490 tarf_temp.close()
491 return tarf
492
493 def nsd_get(self, token, id):
494 result = {'error': True, 'data': ''}
495 headers = {'Content-Type': 'application/yaml',
496 'Authorization': 'Bearer {}'.format(token['id'])}
497 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
498 try:
499 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
500 except Exception as e:
501 log.exception(e)
502 result['data'] = str(e)
503 return result
504 if r.status_code == requests.codes.ok:
505 result['error'] = False
506 return yaml.load(r.text)
507 else:
508 try:
509 result['data'] = r.json()
510 except Exception as e:
511 result['data'] = {}
512 return result
513
514 def vnfd_get(self, token, id):
515 result = {'error': True, 'data': ''}
516 headers = {'Content-Type': 'application/yaml',
517 'Authorization': 'Bearer {}'.format(token['id'])}
518 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
519 try:
520 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
521 except Exception as e:
522 log.exception(e)
523 result['data'] = str(e)
524 return result
525 if r.status_code == requests.codes.ok:
526 result['error'] = False
527 return yaml.load(r.text)
528 else:
529 try:
530 result['data'] = r.json()
531 except Exception as e:
532 result['data'] = {}
533 return result
534
535 def nsd_artifacts(self, token, id):
536 result = {'error': True, 'data': ''}
537 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
538 'Authorization': 'Bearer {}'.format(token['id'])}
539 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
540 try:
541 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
542 except Exception as e:
543 log.exception(e)
544 result['data'] = str(e)
545 return result
546 if r.status_code == requests.codes.ok:
547 result['error'] = False
548 result['data'] = r.text
549 else:
550 try:
551 result['data'] = r.json()
552 except Exception as e:
553 result['data'] = {}
554
555 return result
556
557 def vnf_packages_artifacts(self, token, id):
558 result = {'error': True, 'data': ''}
559 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
560 'Authorization': 'Bearer {}'.format(token['id'])}
561 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
562 try:
563 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
564 except Exception as e:
565 log.exception(e)
566 result['data'] = str(e)
567 return result
568 if r.status_code == requests.codes.ok:
569 result['error'] = False
570 result['data'] = r.text
571 else:
572 try:
573 result['data'] = r.json()
574 except Exception as e:
575 result['data'] = {}
576
577 return result
578
579 def ns_create(self, token, ns_data):
580 result = {'error': True, 'data': ''}
581 headers = {"Content-Type": "application/yaml", "accept": "application/json",
582 'Authorization': 'Bearer {}'.format(token['id'])}
583
584 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
585
586 try:
587 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
588 except Exception as e:
589 log.exception(e)
590 result['data'] = str(e)
591 return result
592 if r.status_code == requests.codes.ok:
593 result['error'] = False
594 result['data'] = Util.json_loads_byteified(r.text)
595 return result
596
597 def ns_op_list(self, token, id):
598 result = {'error': True, 'data': ''}
599 headers = {"Content-Type": "application/json", "accept": "application/json",
600 'Authorization': 'Bearer {}'.format(token['id'])}
601 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
602
603 try:
604 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
605 except Exception as e:
606 log.exception(e)
607 result['data'] = str(e)
608 return result
609 if r.status_code == requests.codes.ok:
610 result['error'] = False
611 result['data'] = Util.json_loads_byteified(r.text)
612
613 return result
614
615 def ns_op(self, token, id):
616 result = {'error': True, 'data': ''}
617 headers = {"Content-Type": "application/json", "accept": "application/json",
618 'Authorization': 'Bearer {}'.format(token['id'])}
619 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
620
621 try:
622 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
623 except Exception as e:
624 log.exception(e)
625 result['data'] = str(e)
626 return result
627 if r.status_code == requests.codes.ok:
628 result['error'] = False
629 result['data'] = Util.json_loads_byteified(r.text)
630
631 return result
632
633 def ns_action(self, token, id, action_payload):
634 result = {'error': True, 'data': ''}
635 headers = {"Content-Type": "application/json", "accept": "application/json",
636 'Authorization': 'Bearer {}'.format(token['id'])}
637
638 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
639
640 try:
641 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
642 except Exception as e:
643 log.exception(e)
644 result['data'] = str(e)
645 return result
646 print r.status_code
647 if r.status_code == requests.codes.created:
648 result['error'] = False
649 result['data'] = Util.json_loads_byteified(r.text)
650 return result
651
652 def ns_delete(self, token, id, force=None):
653 result = {'error': True, 'data': ''}
654 headers = {"Content-Type": "application/yaml", "accept": "application/json",
655 'Authorization': 'Bearer {}'.format(token['id'])}
656 query_path = ''
657 if force:
658 query_path = '?FORCE=true'
659 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
660 try:
661 r = requests.delete(_url, params=None, verify=False, headers=headers)
662 except Exception as e:
663 log.exception(e)
664 result['data'] = str(e)
665 return result
666 if r:
667 result['error'] = False
668 if r.status_code != requests.codes.no_content:
669 result['data'] = Util.json_loads_byteified(r.text)
670 return result
671
672 def ns_get(self, token, id):
673 result = {'error': True, 'data': ''}
674 headers = {"Content-Type": "application/json", "accept": "application/json",
675 'Authorization': 'Bearer {}'.format(token['id'])}
676 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
677
678 try:
679 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
680 except Exception as e:
681 log.exception(e)
682 result['data'] = str(e)
683 return result
684 if r.status_code == requests.codes.ok:
685 result['error'] = False
686 result['data'] = Util.json_loads_byteified(r.text)
687 return result
688
689 def vnf_get(self, token, id):
690 result = {'error': True, 'data': ''}
691 headers = {"Content-Type": "application/json", "accept": "application/json",
692 'Authorization': 'Bearer {}'.format(token['id'])}
693 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
694
695 try:
696 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
697 except Exception as e:
698 log.exception(e)
699 result['data'] = str(e)
700 return result
701 if r.status_code == requests.codes.ok:
702 result['error'] = False
703 result['data'] = Util.json_loads_byteified(r.text)
704 return result
705
706 def ns_alarm_create(self, token, id, alarm_payload):
707 result = {'error': True, 'data': ''}
708 headers = {"Content-Type": "application/json",
709 'Authorization': 'Bearer {}'.format(token['id'])}
710 _url = "{0}/test/message/alarm_request".format(self._base_path)
711 try:
712 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
713 except Exception as e:
714 log.exception(e)
715 result['data'] = str(e)
716 return result
717 print r.status_code
718 if r.status_code == requests.codes.ok:
719 result['error'] = False
720 #result['data'] = Util.json_loads_byteified(r.text)
721 result['data'] = r.text
722 return result
723
724 def ns_metric_export(self, token, id, metric_payload):
725 result = {'error': True, 'data': ''}
726 headers = {"Content-Type": "application/json",
727 'Authorization': 'Bearer {}'.format(token['id'])}
728 _url = "{0}/test/message/metric_request".format(self._base_path)
729 try:
730 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
731 except Exception as e:
732 log.exception(e)
733 result['data'] = str(e)
734 return result
735 print r.status_code
736 if r.status_code == requests.codes.ok:
737 result['error'] = False
738 #result['data'] = Util.json_loads_byteified(r.text)
739 result['data'] = r.text
740 return result
741
742 def vim_list(self, token):
743 result = {'error': True, 'data': ''}
744 headers = {"Content-Type": "application/yaml", "accept": "application/json",
745 'Authorization': 'Bearer {}'.format(token['id'])}
746 _url = "{0}/admin/v1/vims".format(self._base_path)
747 try:
748 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
749 except Exception as e:
750 log.exception(e)
751 result['data'] = str(e)
752 return result
753 if r.status_code == requests.codes.ok:
754 result['error'] = False
755 result['data'] = Util.json_loads_byteified(r.text)
756
757 return result
758
759 def vim_delete(self, token, id):
760 result = {'error': True, 'data': ''}
761 headers = { "accept": "application/json",
762 'Authorization': 'Bearer {}'.format(token['id'])}
763 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
764 try:
765 r = requests.delete(_url, params=None, verify=False, headers=headers)
766 except Exception as e:
767 log.exception(e)
768 result['data'] = str(e)
769 return result
770 print r.status_code
771 if r.status_code == requests.codes.accepted:
772 result['error'] = False
773 else:
774 result['data'] = r.text
775 return result
776
777 def vim_get(self, token, id):
778
779 result = {'error': True, 'data': ''}
780 headers = {"Content-Type": "application/json", "accept": "application/json",
781 'Authorization': 'Bearer {}'.format(token['id'])}
782 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
783
784 try:
785 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
786 except Exception as e:
787 log.exception(e)
788 result['data'] = str(e)
789 return result
790 if r.status_code == requests.codes.ok:
791 result['error'] = False
792 result['data'] = Util.json_loads_byteified(r.text)
793 return result
794
795 def vim_create(self, token, vim_data):
796
797 result = {'error': True, 'data': ''}
798 headers = {"Content-Type": "application/json", "accept": "application/json",
799 'Authorization': 'Bearer {}'.format(token['id'])}
800
801 _url = "{0}/admin/v1/vims".format(self._base_path)
802
803 try:
804 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
805 except Exception as e:
806 log.exception(e)
807 result['data'] = str(e)
808 return result
809 print r.status_code
810 if r.status_code == requests.codes.created:
811 result['error'] = False
812 result['data'] = Util.json_loads_byteified(r.text)
813 return result
814
815 def sdn_list(self, token):
816 result = {'error': True, 'data': ''}
817 headers = {"accept": "application/json",
818 'Authorization': 'Bearer {}'.format(token['id'])}
819 _url = "{0}/admin/v1/sdns".format(self._base_path)
820 try:
821 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
822 except Exception as e:
823 log.exception(e)
824 result['data'] = str(e)
825 return result
826 if r.status_code == requests.codes.ok:
827 result['error'] = False
828 result['data'] = Util.json_loads_byteified(r.text)
829 return result
830
831 def sdn_delete(self, token, id):
832 result = {'error': True, 'data': ''}
833 headers = {"accept": "application/json",
834 'Authorization': 'Bearer {}'.format(token['id'])}
835 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
836 try:
837 r = requests.delete(_url, params=None, verify=False, headers=headers)
838 except Exception as e:
839 log.exception(e)
840 result['data'] = str(e)
841 return result
842 print r.status_code
843 if r.status_code == requests.codes.accepted:
844 result['error'] = False
845 else:
846 result['data'] = r.text
847 return result
848
849 def sdn_get(self, token, id):
850 result = {'error': True, 'data': ''}
851 headers = {"accept": "application/json",
852 'Authorization': 'Bearer {}'.format(token['id'])}
853 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
854
855 try:
856 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
857 except Exception as e:
858 log.exception(e)
859 result['data'] = str(e)
860 return result
861 if r.status_code == requests.codes.ok:
862 result['error'] = False
863 result['data'] = Util.json_loads_byteified(r.text)
864 return result
865
866
867 def sdn_create(self, token, sdn_data):
868 result = {'error': True, 'data': ''}
869 headers = {"Content-Type": "application/json", "accept": "application/json",
870 'Authorization': 'Bearer {}'.format(token['id'])}
871
872 _url = "{0}/admin/v1/sdns".format(self._base_path)
873
874 try:
875 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
876 except Exception as e:
877 log.exception(e)
878 result['data'] = str(e)
879 return result
880 print r.status_code
881 if r.status_code == requests.codes.created:
882 result['error'] = False
883 result['data'] = Util.json_loads_byteified(r.text)
884 return result
885
886
887 @staticmethod
888 def md5(f):
889 hash_md5 = hashlib.md5()
890 for chunk in iter(lambda: f.read(1024), b""):
891 hash_md5.update(chunk)
892 return hash_md5.hexdigest()