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