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