WIM handler
[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.status_code == requests.codes.ok:
444 result['error'] = False
445 result['data'] = Util.json_loads_byteified(r.text)
446 return result
447
448 def vnfd_delete(self, token, id):
449 result = {'error': True, 'data': ''}
450 headers = {"Content-Type": "application/yaml", "accept": "application/json",
451 'Authorization': 'Bearer {}'.format(token['id'])}
452
453 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
454 try:
455 r = requests.delete(_url, params=None, verify=False, headers=headers)
456 except Exception as e:
457 log.exception(e)
458 result['data'] = str(e)
459 return result
460 if r:
461 result['error'] = False
462 if r.status_code != requests.codes.no_content:
463 result['data'] = Util.json_loads_byteified(r.text)
464 return result
465
466 def nst_onboard(self, token, template):
467 result = {'error': True, 'data': ''}
468 headers = {"Content-Type": "application/gzip", "accept": "application/json",
469 'Authorization': 'Bearer {}'.format(token['id'])}
470 _url = "{0}/nst/v1/netslice_templates_content".format(self._base_path)
471 try:
472 fileName, fileExtension = os.path.splitext(template.name)
473 if fileExtension == '.gz':
474 headers["Content-Type"] = "application/gzip"
475 else:
476 headers["Content-Type"] = "application/yaml"
477 r = requests.post(_url, data=template, verify=False, headers=headers)
478 except Exception as e:
479 log.exception(e)
480 result['data'] = str(e)
481 return result
482 if r.status_code == requests.codes.created:
483 result['error'] = False
484 result['data'] = Util.json_loads_byteified(r.text)
485 return result
486
487 def nsd_onboard(self, token, package):
488 result = {'error': True, 'data': ''}
489 headers = {"Content-Type": "application/gzip", "accept": "application/json",
490 'Authorization': 'Bearer {}'.format(token['id'])}
491 with open('/tmp/' + package.name, 'wb+') as destination:
492 for chunk in package.chunks():
493 destination.write(chunk)
494 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
495 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
496 try:
497 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
498 except Exception as e:
499 log.exception(e)
500 result['data'] = str(e)
501 return result
502 if r.status_code == requests.codes.created:
503 result['error'] = False
504 result['data'] = Util.json_loads_byteified(r.text)
505 return result
506
507 def vnfd_onboard(self, token, package):
508 result = {'error': True, 'data': ''}
509 headers = {"Content-Type": "application/gzip", "accept": "application/json",
510 'Authorization': 'Bearer {}'.format(token['id'])}
511 with open('/tmp/' + package.name, 'wb+') as destination:
512 for chunk in package.chunks():
513 destination.write(chunk)
514 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
515 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
516 try:
517 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
518 except Exception as e:
519 log.exception(e)
520 result['data'] = str(e)
521 return result
522 if r.status_code == requests.codes.created:
523 result['error'] = False
524 result['data'] = Util.json_loads_byteified(r.text)
525 return result
526
527 def nsd_create_pkg_base(self, token, pkg_name):
528 result = {'error': True, 'data': ''}
529 headers = {"Content-Type": "application/gzip", "accept": "application/json",
530 'Authorization': 'Bearer {}'.format(token['id'])}
531
532 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
533
534 try:
535 self._create_base_pkg('nsd', pkg_name)
536 headers['Content-Filename'] = pkg_name + '.tar.gz'
537 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
538 except Exception as e:
539 log.exception(e)
540 result['data'] = str(e)
541 return result
542 if r.status_code == requests.codes.created:
543 result['data'] = r.json()
544 result['error'] = False
545 if r.status_code == requests.codes.conflict:
546 result['data'] = "Invalid ID."
547 return result
548
549 def vnfd_create_pkg_base(self, token, pkg_name):
550 result = {'error': True, 'data': ''}
551 headers = {"Content-Type": "application/gzip", "accept": "application/json",
552 'Authorization': 'Bearer {}'.format(token['id'])}
553
554 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
555
556 try:
557 self._create_base_pkg('vnfd', pkg_name)
558 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
559 except Exception as e:
560 log.exception(e)
561 result['data'] = str(e)
562 return result
563 if r.status_code == requests.codes.created:
564 result['data'] = r.json()
565 result['error'] = False
566 if r.status_code == requests.codes.conflict:
567 result['data'] = "Invalid ID."
568 return result
569
570 def nsd_clone(self, token, id):
571 result = {'error': True, 'data': ''}
572 headers = {"Content-Type": "application/gzip", "accept": "application/json",
573 'Authorization': 'Bearer {}'.format(token['id'])}
574
575 # get the package onboarded
576 tar_pkg = self.get_nsd_pkg(token, id)
577 tarf = tarfile.open(fileobj=tar_pkg)
578 tarf = self._descriptor_clone(tarf, 'nsd')
579 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
580
581 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
582
583 try:
584 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
585 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.created:
591 result['error'] = False
592 if r.status_code == requests.codes.conflict:
593 result['data'] = "Invalid ID."
594
595 return result
596
597 def vnfd_clone(self, token, id):
598 result = {'error': True, 'data': ''}
599 headers = {"Content-Type": "application/gzip", "accept": "application/json",
600 'Authorization': 'Bearer {}'.format(token['id'])}
601
602 # get the package onboarded
603 tar_pkg = self.get_vnfd_pkg(token, id)
604 tarf = tarfile.open(fileobj=tar_pkg)
605
606 tarf = self._descriptor_clone(tarf, 'vnfd')
607 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
608
609 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
610
611 try:
612 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
613 headers=headers)
614 except Exception as e:
615 log.exception(e)
616 result['data'] = str(e)
617 return result
618 if r.status_code == requests.codes.created:
619 result['error'] = False
620 if r.status_code == requests.codes.conflict:
621 result['data'] = "Invalid ID."
622
623 return result
624
625 def nst_content_update(self, token, id, template):
626 result = {'error': True, 'data': ''}
627 headers = {"Content-Type": "application/yaml", "accept": "application/json",
628 'Authorization': 'Bearer {}'.format(token['id'])}
629 _url = "{0}/nst/v1/netslice_templates/{1}/nst_content".format(self._base_path,id)
630 try:
631 r = requests.put(_url, data=template, verify=False, headers=headers)
632 except Exception as e:
633 log.exception(e)
634 result['data'] = str(e)
635 return result
636 if r.status_code == requests.codes.no_content:
637 result['error'] = False
638 return result
639
640 def nsd_update(self, token, id, data):
641 result = {'error': True, 'data': ''}
642 headers = {"Content-Type": "application/gzip", "accept": "application/json",
643 'Authorization': 'Bearer {}'.format(token['id'])}
644
645 # get the package onboarded
646 tar_pkg = self.get_nsd_pkg(token, id)
647 tarf = tarfile.open(fileobj=tar_pkg)
648
649 tarf = self._descriptor_update(tarf, data)
650 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
651
652 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
653
654 try:
655 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
656 headers=headers)
657 except Exception as e:
658 log.exception(e)
659 result['data'] = str(e)
660 return result
661 if r.status_code == requests.codes.no_content:
662 result['error'] = False
663 else:
664 try:
665 result['data'] = r.json()
666 except Exception as e:
667 result['data'] = {}
668
669 return result
670
671 def vnfd_update(self, token, id, data):
672 result = {'error': True, 'data': ''}
673 headers = {"Content-Type": "application/gzip", "accept": "application/json",
674 'Authorization': 'Bearer {}'.format(token['id'])}
675
676 # get the package onboarded
677 tar_pkg = self.get_vnfd_pkg(token, id)
678 tarf = tarfile.open(fileobj=tar_pkg)
679
680 tarf = self._descriptor_update(tarf, data)
681 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
682
683 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
684
685 try:
686 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
687 headers=headers)
688 except Exception as e:
689 log.exception(e)
690 result['data'] = str(e)
691 return result
692 if r.status_code == requests.codes.no_content:
693 result['error'] = False
694 else:
695 try:
696 result['data'] = r.json()
697 except Exception as e:
698 result['data'] = {}
699
700 return result
701
702 def get_nsd_pkg(self, token, id):
703 result = {'error': True, 'data': ''}
704 headers = {"accept": "application/zip",
705 'Authorization': 'Bearer {}'.format(token['id'])}
706
707 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
708 try:
709 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
710 except Exception as e:
711 log.exception(e)
712 result['data'] = str(e)
713 return result
714 if r.status_code == requests.codes.ok:
715 result['error'] = False
716 tarf = StringIO.StringIO(r.content)
717 return tarf
718 return result
719
720 def get_vnfd_pkg(self, token, id):
721 result = {'error': True, 'data': ''}
722 headers = {"accept": "application/zip",
723 'Authorization': 'Bearer {}'.format(token['id'])}
724 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
725 try:
726 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
727 except Exception as e:
728 log.exception(e)
729 result['data'] = str(e)
730 return result
731 if r.status_code == requests.codes.ok:
732 result['error'] = False
733 tarf = StringIO.StringIO(r.content)
734 return tarf
735 return result
736
737 def _descriptor_update(self, tarf, data):
738 # extract the package on a tmp directory
739 tarf.extractall('/tmp')
740
741 for name in tarf.getnames():
742 if name.endswith(".yaml") or name.endswith(".yml"):
743 with open('/tmp/' + name, 'w') as outfile:
744 yaml.safe_dump(data, outfile, default_flow_style=False)
745 break
746
747 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
748
749 for tarinfo in tarf:
750 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
751 tarf_temp.close()
752 return tarf
753
754 def _create_base_pkg(self, descriptor_type, pkg_name):
755 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
756 if descriptor_type == 'nsd':
757 descriptor = {
758 "nsd:nsd-catalog": {
759 "nsd": [
760 {
761 "short-name": str(pkg_name),
762 "vendor": "OSM Composer",
763 "description": str(pkg_name) + " descriptor",
764 "vld": [],
765 "constituent-vnfd": [],
766 "version": "1.0",
767 "id": str(pkg_name),
768 "name": str(pkg_name)
769 }
770 ]
771 }
772 }
773
774 elif descriptor_type == 'vnfd':
775 descriptor = {
776 "vnfd:vnfd-catalog": {
777 "vnfd": [
778 {
779 "short-name": str(pkg_name),
780 "vdu": [],
781 "description": "",
782 "mgmt-interface": {
783 "cp": ""
784 },
785 "id": str(pkg_name),
786 "version": "1.0",
787 "internal-vld": [],
788 "connection-point": [],
789 "name": str(pkg_name)
790 }
791 ]
792 }
793 }
794
795 if not os.path.exists(os.path.dirname(filename)):
796 try:
797 os.makedirs(os.path.dirname(filename))
798 except OSError as exc: # Guard against race condition
799 if exc.errno != errno.EEXIST:
800 raise
801
802 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
803 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
804
805 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
806 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml', pkg_name + '/' + pkg_name + '.yaml', recursive=False)
807 tarf_temp.close()
808
809 def _descriptor_clone(self, tarf, descriptor_type):
810 # extract the package on a tmp directory
811 tarf.extractall('/tmp')
812
813 for name in tarf.getnames():
814 if name.endswith(".yaml") or name.endswith(".yml"):
815 with open('/tmp/' + name, 'r') as outfile:
816 yaml_object = yaml.load(outfile)
817
818 if descriptor_type == 'nsd':
819 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
820 for nsd in nsd_list:
821 nsd['id'] = 'clone_' + nsd['id']
822 nsd['name'] = 'clone_' + nsd['name']
823 nsd['short-name'] = 'clone_' + nsd['short-name']
824 elif descriptor_type == 'vnfd':
825 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
826 for vnfd in vnfd_list:
827 vnfd['id'] = 'clone_' + vnfd['id']
828 vnfd['name'] = 'clone_' + vnfd['name']
829 vnfd['short-name'] = 'clone_' + vnfd['short-name']
830
831 with open('/tmp/' + name, 'w') as yaml_file:
832 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
833 break
834
835 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
836
837 for tarinfo in tarf:
838 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
839 tarf_temp.close()
840 return tarf
841
842 def nsd_get(self, token, id):
843 result = {'error': True, 'data': ''}
844 headers = {'Content-Type': 'application/yaml',
845 'Authorization': 'Bearer {}'.format(token['id'])}
846 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
847 try:
848 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
849 except Exception as e:
850 log.exception(e)
851 result['data'] = str(e)
852 return result
853 if r.status_code == requests.codes.ok:
854 result['error'] = False
855 return yaml.load(r.text)
856 else:
857 try:
858 result['data'] = r.json()
859 except Exception as e:
860 result['data'] = {}
861 return result
862
863 def vnfd_get(self, token, id):
864 result = {'error': True, 'data': ''}
865 headers = {'Content-Type': 'application/yaml',
866 'Authorization': 'Bearer {}'.format(token['id'])}
867 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
868 try:
869 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
870 except Exception as e:
871 log.exception(e)
872 result['data'] = str(e)
873 return result
874 if r.status_code == requests.codes.ok:
875 result['error'] = False
876 return yaml.load(r.text)
877 else:
878 try:
879 result['data'] = r.json()
880 except Exception as e:
881 result['data'] = {}
882 return result
883
884 def nsd_artifacts(self, token, id):
885 result = {'error': True, 'data': ''}
886 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
887 'Authorization': 'Bearer {}'.format(token['id'])}
888 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
889 try:
890 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
891 except Exception as e:
892 log.exception(e)
893 result['data'] = str(e)
894 return result
895 if r.status_code == requests.codes.ok:
896 result['error'] = False
897 result['data'] = r.text
898 else:
899 try:
900 result['data'] = r.json()
901 except Exception as e:
902 result['data'] = {}
903
904 return result
905
906 def vnf_packages_artifacts(self, token, id):
907 result = {'error': True, 'data': ''}
908 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
909 'Authorization': 'Bearer {}'.format(token['id'])}
910 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
911 try:
912 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
913 except Exception as e:
914 log.exception(e)
915 result['data'] = str(e)
916 return result
917 if r.status_code == requests.codes.ok:
918 result['error'] = False
919 result['data'] = r.text
920 else:
921 try:
922 result['data'] = r.json()
923 except Exception as e:
924 result['data'] = {}
925
926 return result
927
928 def nsi_create(self, token, nsi_data):
929 result = {'error': True, 'data': ''}
930 headers = {"Content-Type": "application/yaml", "accept": "application/json",
931 'Authorization': 'Bearer {}'.format(token['id'])}
932
933 _url = "{0}/nsilcm/v1/netslice_instances_content".format(self._base_path)
934
935 try:
936 r = requests.post(_url, json=nsi_data, verify=False, headers=headers)
937 except Exception as e:
938 log.exception(e)
939 result['data'] = str(e)
940 return result
941 if r.status_code == requests.codes.ok:
942 result['error'] = False
943 result['data'] = Util.json_loads_byteified(r.text)
944 return result
945
946 def ns_create(self, token, ns_data):
947 result = {'error': True, 'data': ''}
948 headers = {"Content-Type": "application/yaml", "accept": "application/json",
949 'Authorization': 'Bearer {}'.format(token['id'])}
950
951 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
952
953 try:
954 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
955 except Exception as e:
956 log.exception(e)
957 result['data'] = str(e)
958 return result
959 if r.status_code == requests.codes.ok:
960 result['error'] = False
961 result['data'] = Util.json_loads_byteified(r.text)
962 return result
963
964 def pdu_create(self, token, pdu_data):
965 result = {'error': True, 'data': ''}
966 headers = {"Content-Type": "application/yaml", "accept": "application/json",
967 'Authorization': 'Bearer {}'.format(token['id'])}
968
969 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
970
971 try:
972 r = requests.post(_url, json=pdu_data, verify=False, headers=headers)
973 except Exception as e:
974 log.exception(e)
975 result['data'] = str(e)
976 return result
977 if r.status_code == requests.codes.created:
978 result['error'] = False
979 result['data'] = Util.json_loads_byteified(r.text)
980 return result
981
982 def ns_op_list(self, token, id):
983 result = {'error': True, 'data': ''}
984 headers = {"Content-Type": "application/json", "accept": "application/json",
985 'Authorization': 'Bearer {}'.format(token['id'])}
986 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
987
988 try:
989 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
990 except Exception as e:
991 log.exception(e)
992 result['data'] = str(e)
993 return result
994 if r.status_code == requests.codes.ok:
995 result['error'] = False
996 result['data'] = Util.json_loads_byteified(r.text)
997
998 return result
999
1000 def nsi_op_list(self, token, id):
1001 result = {'error': True, 'data': ''}
1002 headers = {"Content-Type": "application/json", "accept": "application/json",
1003 'Authorization': 'Bearer {}'.format(token['id'])}
1004 _url = "{0}/nsilcm/v1/nsi_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
1005
1006 try:
1007 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1008 except Exception as e:
1009 log.exception(e)
1010 result['data'] = str(e)
1011 return result
1012 if r.status_code == requests.codes.ok:
1013 result['error'] = False
1014 result['data'] = Util.json_loads_byteified(r.text)
1015
1016 return result
1017
1018 def ns_op(self, token, id):
1019 result = {'error': True, 'data': ''}
1020 headers = {"Content-Type": "application/json", "accept": "application/json",
1021 'Authorization': 'Bearer {}'.format(token['id'])}
1022 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
1023
1024 try:
1025 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1026 except Exception as e:
1027 log.exception(e)
1028 result['data'] = str(e)
1029 return result
1030 if r.status_code == requests.codes.ok:
1031 result['error'] = False
1032 result['data'] = Util.json_loads_byteified(r.text)
1033
1034 return result
1035
1036 def ns_action(self, token, id, action_payload):
1037 result = {'error': True, 'data': ''}
1038 headers = {"Content-Type": "application/json", "accept": "application/json",
1039 'Authorization': 'Bearer {}'.format(token['id'])}
1040
1041 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
1042
1043 try:
1044 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
1045 except Exception as e:
1046 log.exception(e)
1047 result['data'] = str(e)
1048 return result
1049 if r.status_code == requests.codes.created:
1050 result['error'] = False
1051 result['data'] = Util.json_loads_byteified(r.text)
1052 return result
1053
1054 def nsi_delete(self, token, id, force=None):
1055 result = {'error': True, 'data': ''}
1056 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1057 'Authorization': 'Bearer {}'.format(token['id'])}
1058 query_path = ''
1059 if force:
1060 query_path = '?FORCE=true'
1061 _url = "{0}/nsilcm/v1/netslice_instances_content/{1}{2}".format(self._base_path, id, query_path)
1062 try:
1063 r = requests.delete(_url, params=None, verify=False, headers=headers)
1064 except Exception as e:
1065 log.exception(e)
1066 result['data'] = str(e)
1067 return result
1068 if r:
1069 result['error'] = False
1070 if r.status_code != requests.codes.no_content:
1071 result['data'] = Util.json_loads_byteified(r.text)
1072 return result
1073
1074 def ns_delete(self, token, id, force=None):
1075 result = {'error': True, 'data': ''}
1076 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1077 'Authorization': 'Bearer {}'.format(token['id'])}
1078 query_path = ''
1079 if force:
1080 query_path = '?FORCE=true'
1081 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
1082 try:
1083 r = requests.delete(_url, params=None, verify=False, headers=headers)
1084 except Exception as e:
1085 log.exception(e)
1086 result['data'] = str(e)
1087 return result
1088 if r:
1089 result['error'] = False
1090 if r.status_code != requests.codes.no_content:
1091 result['data'] = Util.json_loads_byteified(r.text)
1092 return result
1093
1094 def pdu_delete(self, token, id):
1095 result = {'error': True, 'data': ''}
1096 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1097 'Authorization': 'Bearer {}'.format(token['id'])}
1098 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1099 try:
1100 r = requests.delete(_url, params=None, verify=False, headers=headers)
1101 except Exception as e:
1102 log.exception(e)
1103 result['data'] = str(e)
1104 return result
1105 if r:
1106 result['error'] = False
1107 if r.status_code != requests.codes.no_content:
1108 result['data'] = Util.json_loads_byteified(r.text)
1109 return result
1110
1111 def nsi_get(self, token, id):
1112 result = {'error': True, 'data': ''}
1113 headers = {"Content-Type": "application/json", "accept": "application/json",
1114 'Authorization': 'Bearer {}'.format(token['id'])}
1115 _url = "{0}/nsilcm/v1/netslice_instances/{1}".format(self._base_path, id)
1116
1117 try:
1118 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1119 except Exception as e:
1120 log.exception(e)
1121 result['data'] = str(e)
1122 return result
1123 if r.status_code == requests.codes.ok:
1124 result['error'] = False
1125 result['data'] = Util.json_loads_byteified(r.text)
1126 return result
1127
1128 def ns_get(self, token, id):
1129 result = {'error': True, 'data': ''}
1130 headers = {"Content-Type": "application/json", "accept": "application/json",
1131 'Authorization': 'Bearer {}'.format(token['id'])}
1132 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
1133
1134 try:
1135 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1136 except Exception as e:
1137 log.exception(e)
1138 result['data'] = str(e)
1139 return result
1140 if r.status_code == requests.codes.ok:
1141 result['error'] = False
1142 result['data'] = Util.json_loads_byteified(r.text)
1143 return result
1144
1145 def vnf_get(self, token, id):
1146 result = {'error': True, 'data': ''}
1147 headers = {"Content-Type": "application/json", "accept": "application/json",
1148 'Authorization': 'Bearer {}'.format(token['id'])}
1149 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
1150
1151 try:
1152 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1153 except Exception as e:
1154 log.exception(e)
1155 result['data'] = str(e)
1156 return result
1157 if r.status_code == requests.codes.ok:
1158 result['error'] = False
1159 result['data'] = Util.json_loads_byteified(r.text)
1160 return result
1161
1162 def pdu_get(self, token, id):
1163 result = {'error': True, 'data': ''}
1164 headers = {"Content-Type": "application/json", "accept": "application/json",
1165 'Authorization': 'Bearer {}'.format(token['id'])}
1166 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1167
1168 try:
1169 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1170 except Exception as e:
1171 log.exception(e)
1172 result['data'] = str(e)
1173 return result
1174 if r.status_code == requests.codes.ok:
1175 result['error'] = False
1176 result['data'] = Util.json_loads_byteified(r.text)
1177 return result
1178
1179 def ns_alarm_create(self, token, id, alarm_payload):
1180 result = {'error': True, 'data': ''}
1181 headers = {"Content-Type": "application/json",
1182 'Authorization': 'Bearer {}'.format(token['id'])}
1183 _url = "{0}/test/message/alarm_request".format(self._base_path)
1184 try:
1185 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
1186 except Exception as e:
1187 log.exception(e)
1188 result['data'] = str(e)
1189 return result
1190 if r.status_code == requests.codes.ok:
1191 result['error'] = False
1192 # result['data'] = Util.json_loads_byteified(r.text)
1193 result['data'] = r.text
1194 return result
1195
1196 def ns_metric_export(self, token, id, metric_payload):
1197 result = {'error': True, 'data': ''}
1198 headers = {"Content-Type": "application/json",
1199 'Authorization': 'Bearer {}'.format(token['id'])}
1200 _url = "{0}/test/message/metric_request".format(self._base_path)
1201 try:
1202 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
1203 except Exception as e:
1204 log.exception(e)
1205 result['data'] = str(e)
1206 return result
1207 if r.status_code == requests.codes.ok:
1208 result['error'] = False
1209 # result['data'] = Util.json_loads_byteified(r.text)
1210 result['data'] = r.text
1211 return result
1212
1213 def wim_list(self, token):
1214 result = {'error': True, 'data': ''}
1215 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1216 'Authorization': 'Bearer {}'.format(token['id'])}
1217 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1218 try:
1219 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1220 except Exception as e:
1221 log.exception(e)
1222 result['data'] = str(e)
1223 return result
1224 if r.status_code == requests.codes.ok:
1225 result['error'] = False
1226 result['data'] = Util.json_loads_byteified(r.text)
1227
1228 return result
1229
1230 def vim_list(self, token):
1231 result = {'error': True, 'data': ''}
1232 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1233 'Authorization': 'Bearer {}'.format(token['id'])}
1234 _url = "{0}/admin/v1/vims".format(self._base_path)
1235 try:
1236 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1237 except Exception as e:
1238 log.exception(e)
1239 result['data'] = str(e)
1240 return result
1241 if r.status_code == requests.codes.ok:
1242 result['error'] = False
1243 result['data'] = Util.json_loads_byteified(r.text)
1244
1245 return result
1246
1247 def wim_delete(self, token, id):
1248 result = {'error': True, 'data': ''}
1249 headers = {"accept": "application/json",
1250 'Authorization': 'Bearer {}'.format(token['id'])}
1251 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1252 try:
1253 r = requests.delete(_url, params=None, verify=False, headers=headers)
1254 except Exception as e:
1255 log.exception(e)
1256 result['data'] = str(e)
1257 return result
1258 if r.status_code == requests.codes.accepted:
1259 result['error'] = False
1260 else:
1261 result['data'] = r.text
1262 return result
1263
1264 def vim_delete(self, token, id):
1265 result = {'error': True, 'data': ''}
1266 headers = {"accept": "application/json",
1267 'Authorization': 'Bearer {}'.format(token['id'])}
1268 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1269 try:
1270 r = requests.delete(_url, params=None, verify=False, headers=headers)
1271 except Exception as e:
1272 log.exception(e)
1273 result['data'] = str(e)
1274 return result
1275 if r.status_code == requests.codes.accepted:
1276 result['error'] = False
1277 else:
1278 result['data'] = r.text
1279 return result
1280
1281 def wim_get(self, token, id):
1282
1283 result = {'error': True, 'data': ''}
1284 headers = {"Content-Type": "application/json", "accept": "application/json",
1285 'Authorization': 'Bearer {}'.format(token['id'])}
1286 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1287
1288 try:
1289 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1290 except Exception as e:
1291 log.exception(e)
1292 result['data'] = str(e)
1293 return result
1294 if r.status_code == requests.codes.ok:
1295 result['error'] = False
1296 result['data'] = Util.json_loads_byteified(r.text)
1297 return result
1298
1299 def vim_get(self, token, id):
1300
1301 result = {'error': True, 'data': ''}
1302 headers = {"Content-Type": "application/json", "accept": "application/json",
1303 'Authorization': 'Bearer {}'.format(token['id'])}
1304 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1305
1306 try:
1307 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1308 except Exception as e:
1309 log.exception(e)
1310 result['data'] = str(e)
1311 return result
1312 if r.status_code == requests.codes.ok:
1313 result['error'] = False
1314 result['data'] = Util.json_loads_byteified(r.text)
1315 return result
1316
1317 def wim_create(self, token, wim_data):
1318 result = {'error': True, 'data': ''}
1319 headers = {"Content-Type": "application/json", "accept": "application/json",
1320 'Authorization': 'Bearer {}'.format(token['id'])}
1321
1322 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1323
1324 try:
1325 r = requests.post(_url, json=wim_data, verify=False, headers=headers)
1326 except Exception as e:
1327 log.exception(e)
1328 result['data'] = str(e)
1329 return result
1330 if r.status_code == requests.codes.created:
1331 result['error'] = False
1332 result['data'] = Util.json_loads_byteified(r.text)
1333 return result
1334
1335 def vim_create(self, token, vim_data):
1336
1337 result = {'error': True, 'data': ''}
1338 headers = {"Content-Type": "application/json", "accept": "application/json",
1339 'Authorization': 'Bearer {}'.format(token['id'])}
1340
1341 _url = "{0}/admin/v1/vims".format(self._base_path)
1342
1343 try:
1344 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1345 except Exception as e:
1346 log.exception(e)
1347 result['data'] = str(e)
1348 return result
1349 if r.status_code == requests.codes.created:
1350 result['error'] = False
1351 result['data'] = Util.json_loads_byteified(r.text)
1352 return result
1353
1354 def sdn_list(self, token):
1355 result = {'error': True, 'data': ''}
1356 headers = {"accept": "application/json",
1357 'Authorization': 'Bearer {}'.format(token['id'])}
1358 _url = "{0}/admin/v1/sdns".format(self._base_path)
1359 try:
1360 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1361 except Exception as e:
1362 log.exception(e)
1363 result['data'] = str(e)
1364 return result
1365 if r.status_code == requests.codes.ok:
1366 result['error'] = False
1367 result['data'] = Util.json_loads_byteified(r.text)
1368 return result
1369
1370 def sdn_delete(self, token, id):
1371 result = {'error': True, 'data': ''}
1372 headers = {"accept": "application/json",
1373 'Authorization': 'Bearer {}'.format(token['id'])}
1374 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1375 try:
1376 r = requests.delete(_url, params=None, verify=False, headers=headers)
1377 except Exception as e:
1378 log.exception(e)
1379 result['data'] = str(e)
1380 return result
1381 if r.status_code == requests.codes.accepted:
1382 result['error'] = False
1383 else:
1384 result['data'] = r.text
1385 return result
1386
1387 def sdn_get(self, token, id):
1388 result = {'error': True, 'data': ''}
1389 headers = {"accept": "application/json",
1390 'Authorization': 'Bearer {}'.format(token['id'])}
1391 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1392
1393 try:
1394 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1395 except Exception as e:
1396 log.exception(e)
1397 result['data'] = str(e)
1398 return result
1399 if r.status_code == requests.codes.ok:
1400 result['error'] = False
1401 result['data'] = Util.json_loads_byteified(r.text)
1402 return result
1403
1404 def sdn_create(self, token, sdn_data):
1405 result = {'error': True, 'data': ''}
1406 headers = {"Content-Type": "application/json", "accept": "application/json",
1407 'Authorization': 'Bearer {}'.format(token['id'])}
1408
1409 _url = "{0}/admin/v1/sdns".format(self._base_path)
1410
1411 try:
1412 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1413 except Exception as e:
1414 log.exception(e)
1415 result['data'] = str(e)
1416 return result
1417 if r.status_code == requests.codes.created:
1418 result['error'] = False
1419 result['data'] = Util.json_loads_byteified(r.text)
1420 return result
1421
1422 @staticmethod
1423 def md5(f):
1424 hash_md5 = hashlib.md5()
1425 for chunk in iter(lambda: f.read(1024), b""):
1426 hash_md5.update(chunk)
1427 return hash_md5.hexdigest()