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