2 # Copyright 2018 EveryUP Srl
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
24 from lib
.util
import Util
27 from requests
.packages
.urllib3
.exceptions
import InsecureRequestWarning
29 requests
.packages
.urllib3
.disable_warnings(InsecureRequestWarning
)
31 logging
.basicConfig(level
=logging
.INFO
)
32 log
= logging
.getLogger('helper.py')
33 logging
.getLogger("urllib3").setLevel(logging
.INFO
)
37 self
._token
_endpoint
= 'admin/v1/tokens'
38 self
._user
_endpoint
= 'admin/v1/users'
39 self
._host
= os
.getenv('OSM_SERVER', "localhost")
41 self
._base
_path
= "https://{0}:{1}/osm".format(self
._host
, self
._so
_port
)
44 result
= {'error': True, 'data': ''}
45 token_url
= "{0}/{1}".format(self
._base
_path
, self
._token
_endpoint
)
46 headers
= {"Content-Type": "application/yaml", "accept": "application/json"}
48 r
= requests
.post(token_url
, json
=args
, verify
=False, headers
=headers
)
49 except Exception as e
:
51 result
['data'] = str(e
)
53 if r
.status_code
== requests
.codes
.ok
:
54 result
['error'] = False
56 result
['data'] = Util
.json_loads_byteified(r
.text
)
60 def switch_project(self
, args
):
61 result
= {'error': True, 'data': ''}
62 token_url
= "{0}/{1}".format(self
._base
_path
, self
._token
_endpoint
)
63 headers
= {"Content-Type": "application/yaml", "accept": "application/json"}
65 r
= requests
.post(token_url
, json
=args
, verify
=False, headers
=headers
)
66 except Exception as e
:
68 result
['data'] = str(e
)
70 if r
.status_code
== requests
.codes
.ok
:
71 result
['error'] = False
73 result
['data'] = Util
.json_loads_byteified(r
.text
)
77 def user_list(self
, token
):
78 result
= {'error': True, 'data': ''}
79 headers
= {"Content-Type": "application/json", "accept": "application/json",
80 'Authorization': 'Bearer {}'.format(token
['id'])}
82 _url
= "{0}/admin/v1/users".format(self
._base
_path
)
84 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
85 except Exception as e
:
87 result
['data'] = str(e
)
89 if r
.status_code
== requests
.codes
.ok
:
90 result
['error'] = False
91 result
['data'] = Util
.json_loads_byteified(r
.text
)
95 def user_create(self
, token
, user_data
):
96 result
= {'error': True, 'data': ''}
97 headers
= {"Content-Type": "application/json", "accept": "application/json",
98 'Authorization': 'Bearer {}'.format(token
['id'])}
100 _url
= "{0}/admin/v1/users".format(self
._base
_path
)
103 r
= requests
.post(_url
, json
=user_data
, verify
=False, headers
=headers
)
104 except Exception as e
:
106 result
['data'] = str(e
)
108 if r
.status_code
== requests
.codes
.created
:
109 result
['error'] = False
110 result
['data'] = Util
.json_loads_byteified(r
.text
)
113 def user_update(self
, token
, id, user_data
):
114 result
= {'error': True, 'data': ''}
115 headers
= {"Content-Type": "application/json", "accept": "application/json",
116 'Authorization': 'Bearer {}'.format(token
['id'])}
118 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
120 r
= requests
.post(_url
, json
=user_data
, verify
=False, headers
=headers
)
121 except Exception as e
:
123 result
['data'] = str(e
)
125 if r
.status_code
== requests
.codes
.created
:
126 result
['error'] = False
127 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
135 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
137 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
138 except Exception as e
:
140 result
['data'] = str(e
)
142 if r
.status_code
== requests
.codes
.no_content
:
143 result
['error'] = False
145 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
154 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
155 except Exception as e
:
157 result
['data'] = str(e
)
159 if r
.status_code
== requests
.codes
.ok
:
160 result
['error'] = False
161 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
169 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
171 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
172 except Exception as e
:
174 result
['data'] = str(e
)
176 if r
.status_code
== requests
.codes
.ok
:
177 result
['error'] = False
178 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
188 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
189 except Exception as e
:
191 result
['data'] = str(e
)
193 if r
.status_code
== requests
.codes
.ok
:
194 result
['error'] = False
195 result
['data'] = Util
.json_loads_byteified(r
.text
)
198 def project_create(self
, token
, project_data
):
200 result
= {'error': True, 'data': ''}
201 headers
= {"Content-Type": "application/json", "accept": "application/json",
202 'Authorization': 'Bearer {}'.format(token
['id'])}
204 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
207 r
= requests
.post(_url
, json
=project_data
, verify
=False, headers
=headers
)
208 except Exception as e
:
210 result
['data'] = str(e
)
212 if r
.status_code
== requests
.codes
.created
:
213 result
['error'] = False
214 result
['data'] = Util
.json_loads_byteified(r
.text
)
217 def project_edit(self
, token
, id, project_data
):
219 result
= {'error': True, 'data': ''}
220 headers
= {"Content-Type": "application/json", "accept": "application/json",
221 'Authorization': 'Bearer {}'.format(token
['id'])}
223 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
226 r
= requests
.put(_url
, json
=project_data
, verify
=False, headers
=headers
)
227 except Exception as e
:
229 result
['data'] = str(e
)
231 if r
.status_code
== requests
.codes
.no_content
:
232 result
['error'] = False
233 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
241 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
243 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
244 except Exception as e
:
246 result
['data'] = str(e
)
248 if r
.status_code
== requests
.codes
.no_content
:
249 result
['error'] = False
251 result
['data'] = Util
.json_loads_byteified(r
.text
)
254 def nsd_list(self
, token
):
255 result
= {'error': True, 'data': ''}
256 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token
['id'])}
259 _url
= "{0}/nsd/v1/ns_descriptors_content".format(self
._base
_path
)
261 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
262 except Exception as e
:
264 result
['data'] = str(e
)
266 if r
.status_code
== requests
.codes
.ok
:
267 result
['error'] = False
268 result
['data'] = Util
.json_loads_byteified(r
.text
)
272 def vnfd_list(self
, token
):
273 result
= {'error': True, 'data': ''}
274 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
275 'Authorization': 'Bearer {}'.format(token
['id'])}
277 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
279 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
280 except Exception as e
:
282 result
['data'] = str(e
)
284 if r
.status_code
== requests
.codes
.ok
:
285 result
['error'] = False
286 result
['data'] = Util
.json_loads_byteified(r
.text
)
290 def ns_list(self
, token
):
291 result
= {'error': True, 'data': ''}
292 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
293 'Authorization': 'Bearer {}'.format(token
['id'])}
294 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
296 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
297 except Exception as e
:
299 result
['data'] = str(e
)
301 if r
.status_code
== requests
.codes
.ok
:
302 result
['error'] = False
303 result
['data'] = Util
.json_loads_byteified(r
.text
)
307 def vnf_list(self
, token
):
308 result
= {'error': True, 'data': ''}
309 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
310 'Authorization': 'Bearer {}'.format(token
['id'])}
311 _url
= "{0}/nslcm/v1/vnfrs".format(self
._base
_path
)
313 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
314 except Exception as e
:
316 result
['data'] = str(e
)
318 if r
.status_code
== requests
.codes
.ok
:
319 result
['error'] = False
320 result
['data'] = Util
.json_loads_byteified(r
.text
)
324 def nsd_delete(self
, token
, id):
325 result
= {'error': True, 'data': ''}
326 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
327 'Authorization': 'Bearer {}'.format(token
['id'])}
329 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
331 r
= requests
.delete(_url
, params
=None, verify
=False,headers
=headers
)
332 except Exception as e
:
334 result
['data'] = str(e
)
336 if r
.status_code
== requests
.codes
.ok
:
337 result
['error'] = False
338 result
['data'] = Util
.json_loads_byteified(r
.text
)
341 def vnfd_delete(self
, token
, id):
342 result
= {'error': True, 'data': ''}
343 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
344 'Authorization': 'Bearer {}'.format(token
['id'])}
346 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
348 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
349 except Exception as e
:
351 result
['data'] = str(e
)
354 result
['error'] = False
355 if r
.status_code
!= requests
.codes
.no_content
:
356 result
['data'] = Util
.json_loads_byteified(r
.text
)
359 def nsd_onboard(self
, token
, package
):
360 result
= {'error': True, 'data': ''}
361 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
362 'Authorization': 'Bearer {}'.format(token
['id'])}
363 with
open('/tmp/'+package
.name
, 'wb+') as destination
:
364 for chunk
in package
.chunks():
365 destination
.write(chunk
)
366 headers
['Content-File-MD5'] = self
.md5(open('/tmp/'+package
.name
, 'rb'))
367 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
369 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
370 except Exception as e
:
372 result
['data'] = str(e
)
374 if r
.status_code
== requests
.codes
.created
:
375 result
['error'] = False
376 result
['data'] = Util
.json_loads_byteified(r
.text
)
379 def vnfd_onboard(self
, token
, package
):
380 result
= {'error': True, 'data': ''}
381 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
382 'Authorization': 'Bearer {}'.format(token
['id'])}
383 with
open('/tmp/'+package
.name
, 'wb+') as destination
:
384 for chunk
in package
.chunks():
385 destination
.write(chunk
)
386 headers
['Content-File-MD5'] = self
.md5(open('/tmp/'+package
.name
, 'rb'))
387 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
389 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
390 except Exception as e
:
392 result
['data'] = str(e
)
394 if r
.status_code
== requests
.codes
.created
:
395 result
['error'] = False
396 result
['data'] = Util
.json_loads_byteified(r
.text
)
399 def nsd_clone(self
, token
, id):
400 result
= {'error': True, 'data': ''}
401 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
402 'Authorization': 'Bearer {}'.format(token
['id'])}
404 # get the package onboarded
405 tar_pkg
= self
.get_nsd_pkg(token
, id)
406 tarf
= tarfile
.open(fileobj
=tar_pkg
)
407 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
408 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
410 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
413 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
415 except Exception as e
:
417 result
['data'] = str(e
)
419 if r
.status_code
== requests
.codes
.created
:
420 result
['error'] = False
421 if r
.status_code
== requests
.codes
.conflict
:
422 result
['data'] = "Invalid ID."
426 def vnfd_clone(self
, token
, id):
427 result
= {'error': True, 'data': ''}
428 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
429 'Authorization': 'Bearer {}'.format(token
['id'])}
431 # get the package onboarded
432 tar_pkg
= self
.get_vnfd_pkg(token
, id)
433 tarf
= tarfile
.open(fileobj
=tar_pkg
)
435 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
436 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
438 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
441 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
443 except Exception as e
:
445 result
['data'] = str(e
)
447 if r
.status_code
== requests
.codes
.created
:
448 result
['error'] = False
449 if r
.status_code
== requests
.codes
.conflict
:
450 result
['data'] = "Invalid ID."
454 def nsd_update(self
, token
, id, data
):
455 result
= {'error': True, 'data': ''}
456 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
457 'Authorization': 'Bearer {}'.format(token
['id'])}
459 # get the package onboarded
460 tar_pkg
= self
.get_nsd_pkg(token
, id)
461 tarf
= tarfile
.open(fileobj
=tar_pkg
)
463 tarf
= self
._descriptor
_update
(tarf
, data
)
464 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
466 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
469 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
471 except Exception as e
:
473 result
['data'] = str(e
)
475 if r
.status_code
== requests
.codes
.no_content
:
476 result
['error'] = False
480 def vnfd_update(self
, token
, id, data
):
481 result
= {'error': True, 'data': ''}
482 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
483 'Authorization': 'Bearer {}'.format(token
['id'])}
485 # get the package onboarded
486 tar_pkg
= self
.get_vnfd_pkg(token
, id)
487 tarf
= tarfile
.open(fileobj
=tar_pkg
)
489 tarf
= self
._descriptor
_update
(tarf
, data
)
490 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
492 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
495 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
497 except Exception as e
:
499 result
['data'] = str(e
)
501 if r
.status_code
== requests
.codes
.no_content
:
502 result
['error'] = False
506 def get_nsd_pkg(self
, token
, id):
507 result
= {'error': True, 'data': ''}
508 headers
= { "accept": "application/zip",
509 'Authorization': 'Bearer {}'.format(token
['id'])}
511 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
513 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
514 except Exception as e
:
516 result
['data'] = str(e
)
518 if r
.status_code
== requests
.codes
.ok
:
519 result
['error'] = False
520 tarf
= StringIO
.StringIO(r
.content
)
524 def get_vnfd_pkg(self
, token
, id):
525 result
= {'error': True, 'data': ''}
526 headers
= {"accept": "application/zip",
527 'Authorization': 'Bearer {}'.format(token
['id'])}
528 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
530 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
531 except Exception as e
:
533 result
['data'] = str(e
)
535 if r
.status_code
== requests
.codes
.ok
:
536 result
['error'] = False
537 tarf
= StringIO
.StringIO(r
.content
)
541 def _descriptor_update(self
, tarf
, data
):
542 # extract the package on a tmp directory
543 tarf
.extractall('/tmp')
545 for name
in tarf
.getnames():
546 if name
.endswith(".yaml") or name
.endswith(".yml"):
547 with
open('/tmp/' + name
, 'w') as outfile
:
548 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
551 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
554 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
558 def _descriptor_clone(self
, tarf
, descriptor_type
):
559 # extract the package on a tmp directory
560 tarf
.extractall('/tmp')
562 for name
in tarf
.getnames():
563 if name
.endswith(".yaml") or name
.endswith(".yml"):
564 with
open('/tmp/' + name
, 'r') as outfile
:
565 yaml_object
= yaml
.load(outfile
)
567 if descriptor_type
== 'nsd':
568 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
570 nsd
['id'] = 'clone_' + nsd
['id']
571 nsd
['name'] = 'clone_' +nsd
['name']
572 nsd
['short-name'] = 'clone_' +nsd
['short-name']
573 elif descriptor_type
== 'vnfd':
574 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
575 for vnfd
in vnfd_list
:
576 vnfd
['id'] = 'clone_' + vnfd
['id']
577 vnfd
['name'] = 'clone_' + vnfd
['name']
578 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
581 with
open('/tmp/' + name
, 'w') as yaml_file
:
582 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
585 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
588 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
592 def nsd_get(self
, token
, id):
593 result
= {'error': True, 'data': ''}
594 headers
= {'Content-Type': 'application/yaml',
595 'Authorization': 'Bearer {}'.format(token
['id'])}
596 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
598 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
599 except Exception as e
:
601 result
['data'] = str(e
)
603 if r
.status_code
== requests
.codes
.ok
:
604 result
['error'] = False
605 return yaml
.load(r
.text
)
608 result
['data'] = r
.json()
609 except Exception as e
:
613 def vnfd_get(self
, token
, id):
614 result
= {'error': True, 'data': ''}
615 headers
= {'Content-Type': 'application/yaml',
616 'Authorization': 'Bearer {}'.format(token
['id'])}
617 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
619 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
620 except Exception as e
:
622 result
['data'] = str(e
)
624 if r
.status_code
== requests
.codes
.ok
:
625 result
['error'] = False
626 return yaml
.load(r
.text
)
629 result
['data'] = r
.json()
630 except Exception as e
:
634 def nsd_artifacts(self
, token
, id):
635 result
= {'error': True, 'data': ''}
636 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
637 'Authorization': 'Bearer {}'.format(token
['id'])}
638 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
640 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
641 except Exception as e
:
643 result
['data'] = str(e
)
645 if r
.status_code
== requests
.codes
.ok
:
646 result
['error'] = False
647 result
['data'] = r
.text
650 result
['data'] = r
.json()
651 except Exception as e
:
656 def vnf_packages_artifacts(self
, token
, id):
657 result
= {'error': True, 'data': ''}
658 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
659 'Authorization': 'Bearer {}'.format(token
['id'])}
660 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
662 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
663 except Exception as e
:
665 result
['data'] = str(e
)
667 if r
.status_code
== requests
.codes
.ok
:
668 result
['error'] = False
669 result
['data'] = r
.text
672 result
['data'] = r
.json()
673 except Exception as e
:
678 def ns_create(self
, token
, ns_data
):
679 result
= {'error': True, 'data': ''}
680 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
681 'Authorization': 'Bearer {}'.format(token
['id'])}
683 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
686 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
687 except Exception as e
:
689 result
['data'] = str(e
)
691 if r
.status_code
== requests
.codes
.ok
:
692 result
['error'] = False
693 result
['data'] = Util
.json_loads_byteified(r
.text
)
696 def ns_op_list(self
, token
, id):
697 result
= {'error': True, 'data': ''}
698 headers
= {"Content-Type": "application/json", "accept": "application/json",
699 'Authorization': 'Bearer {}'.format(token
['id'])}
700 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
703 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
704 except Exception as e
:
706 result
['data'] = str(e
)
708 if r
.status_code
== requests
.codes
.ok
:
709 result
['error'] = False
710 result
['data'] = Util
.json_loads_byteified(r
.text
)
714 def ns_op(self
, token
, id):
715 result
= {'error': True, 'data': ''}
716 headers
= {"Content-Type": "application/json", "accept": "application/json",
717 'Authorization': 'Bearer {}'.format(token
['id'])}
718 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
721 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
722 except Exception as e
:
724 result
['data'] = str(e
)
726 if r
.status_code
== requests
.codes
.ok
:
727 result
['error'] = False
728 result
['data'] = Util
.json_loads_byteified(r
.text
)
732 def ns_action(self
, token
, id, action_payload
):
733 result
= {'error': True, 'data': ''}
734 headers
= {"Content-Type": "application/json", "accept": "application/json",
735 'Authorization': 'Bearer {}'.format(token
['id'])}
737 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
740 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
741 except Exception as e
:
743 result
['data'] = str(e
)
745 if r
.status_code
== requests
.codes
.created
:
746 result
['error'] = False
747 result
['data'] = Util
.json_loads_byteified(r
.text
)
750 def ns_delete(self
, token
, id, force
=None):
751 result
= {'error': True, 'data': ''}
752 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
753 'Authorization': 'Bearer {}'.format(token
['id'])}
756 query_path
= '?FORCE=true'
757 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
759 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
760 except Exception as e
:
762 result
['data'] = str(e
)
765 result
['error'] = False
766 if r
.status_code
!= requests
.codes
.no_content
:
767 result
['data'] = Util
.json_loads_byteified(r
.text
)
770 def ns_get(self
, token
, id):
771 result
= {'error': True, 'data': ''}
772 headers
= {"Content-Type": "application/json", "accept": "application/json",
773 'Authorization': 'Bearer {}'.format(token
['id'])}
774 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
777 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
778 except Exception as e
:
780 result
['data'] = str(e
)
782 if r
.status_code
== requests
.codes
.ok
:
783 result
['error'] = False
784 result
['data'] = Util
.json_loads_byteified(r
.text
)
787 def vnf_get(self
, token
, id):
788 result
= {'error': True, 'data': ''}
789 headers
= {"Content-Type": "application/json", "accept": "application/json",
790 'Authorization': 'Bearer {}'.format(token
['id'])}
791 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
794 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
795 except Exception as e
:
797 result
['data'] = str(e
)
799 if r
.status_code
== requests
.codes
.ok
:
800 result
['error'] = False
801 result
['data'] = Util
.json_loads_byteified(r
.text
)
804 def ns_alarm_create(self
, token
, id, alarm_payload
):
805 result
= {'error': True, 'data': ''}
806 headers
= {"Content-Type": "application/json",
807 'Authorization': 'Bearer {}'.format(token
['id'])}
808 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
810 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
811 except Exception as e
:
813 result
['data'] = str(e
)
815 if r
.status_code
== requests
.codes
.ok
:
816 result
['error'] = False
817 #result['data'] = Util.json_loads_byteified(r.text)
818 result
['data'] = r
.text
821 def ns_metric_export(self
, token
, id, metric_payload
):
822 result
= {'error': True, 'data': ''}
823 headers
= {"Content-Type": "application/json",
824 'Authorization': 'Bearer {}'.format(token
['id'])}
825 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
827 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
828 except Exception as e
:
830 result
['data'] = str(e
)
832 if r
.status_code
== requests
.codes
.ok
:
833 result
['error'] = False
834 #result['data'] = Util.json_loads_byteified(r.text)
835 result
['data'] = r
.text
838 def vim_list(self
, token
):
839 result
= {'error': True, 'data': ''}
840 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
841 'Authorization': 'Bearer {}'.format(token
['id'])}
842 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
844 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
845 except Exception as e
:
847 result
['data'] = str(e
)
849 if r
.status_code
== requests
.codes
.ok
:
850 result
['error'] = False
851 result
['data'] = Util
.json_loads_byteified(r
.text
)
855 def vim_delete(self
, token
, id):
856 result
= {'error': True, 'data': ''}
857 headers
= { "accept": "application/json",
858 'Authorization': 'Bearer {}'.format(token
['id'])}
859 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
861 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
862 except Exception as e
:
864 result
['data'] = str(e
)
866 if r
.status_code
== requests
.codes
.accepted
:
867 result
['error'] = False
869 result
['data'] = r
.text
872 def vim_get(self
, token
, id):
874 result
= {'error': True, 'data': ''}
875 headers
= {"Content-Type": "application/json", "accept": "application/json",
876 'Authorization': 'Bearer {}'.format(token
['id'])}
877 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
880 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
881 except Exception as e
:
883 result
['data'] = str(e
)
885 if r
.status_code
== requests
.codes
.ok
:
886 result
['error'] = False
887 result
['data'] = Util
.json_loads_byteified(r
.text
)
890 def vim_create(self
, token
, vim_data
):
892 result
= {'error': True, 'data': ''}
893 headers
= {"Content-Type": "application/json", "accept": "application/json",
894 'Authorization': 'Bearer {}'.format(token
['id'])}
896 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
899 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
900 except Exception as e
:
902 result
['data'] = str(e
)
904 if r
.status_code
== requests
.codes
.created
:
905 result
['error'] = False
906 result
['data'] = Util
.json_loads_byteified(r
.text
)
909 def sdn_list(self
, token
):
910 result
= {'error': True, 'data': ''}
911 headers
= {"accept": "application/json",
912 'Authorization': 'Bearer {}'.format(token
['id'])}
913 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
915 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
916 except Exception as e
:
918 result
['data'] = str(e
)
920 if r
.status_code
== requests
.codes
.ok
:
921 result
['error'] = False
922 result
['data'] = Util
.json_loads_byteified(r
.text
)
925 def sdn_delete(self
, token
, id):
926 result
= {'error': True, 'data': ''}
927 headers
= {"accept": "application/json",
928 'Authorization': 'Bearer {}'.format(token
['id'])}
929 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
931 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
932 except Exception as e
:
934 result
['data'] = str(e
)
936 if r
.status_code
== requests
.codes
.accepted
:
937 result
['error'] = False
939 result
['data'] = r
.text
942 def sdn_get(self
, token
, id):
943 result
= {'error': True, 'data': ''}
944 headers
= {"accept": "application/json",
945 'Authorization': 'Bearer {}'.format(token
['id'])}
946 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
949 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
950 except Exception as e
:
952 result
['data'] = str(e
)
954 if r
.status_code
== requests
.codes
.ok
:
955 result
['error'] = False
956 result
['data'] = Util
.json_loads_byteified(r
.text
)
960 def sdn_create(self
, token
, sdn_data
):
961 result
= {'error': True, 'data': ''}
962 headers
= {"Content-Type": "application/json", "accept": "application/json",
963 'Authorization': 'Bearer {}'.format(token
['id'])}
965 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
968 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
969 except Exception as e
:
971 result
['data'] = str(e
)
973 if r
.status_code
== requests
.codes
.created
:
974 result
['error'] = False
975 result
['data'] = Util
.json_loads_byteified(r
.text
)
981 hash_md5
= hashlib
.md5()
982 for chunk
in iter(lambda: f
.read(1024), b
""):
983 hash_md5
.update(chunk
)
984 return hash_md5
.hexdigest()