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
.patch(_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
.no_content
:
126 result
['error'] = False
128 result
['data'] = Util
.json_loads_byteified(r
.text
)
131 def user_delete(self
, token
, id):
132 result
= {'error': True, 'data': ''}
133 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
134 'Authorization': 'Bearer {}'.format(token
['id'])}
136 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
138 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
139 except Exception as e
:
141 result
['data'] = str(e
)
143 if r
.status_code
== requests
.codes
.no_content
:
144 result
['error'] = False
146 result
['data'] = Util
.json_loads_byteified(r
.text
)
149 def get_user_info(self
, token
, id):
150 result
= {'error': True, 'data': ''}
151 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
152 'Authorization': 'Bearer {}'.format(token
['id'])}
153 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
155 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
156 except Exception as e
:
158 result
['data'] = str(e
)
160 if r
.status_code
== requests
.codes
.ok
:
161 result
['error'] = False
162 result
['data'] = Util
.json_loads_byteified(r
.text
)
165 def project_list(self
, token
):
166 result
= {'error': True, 'data': ''}
167 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
168 'Authorization': 'Bearer {}'.format(token
['id'])}
170 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
172 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
173 except Exception as e
:
175 result
['data'] = str(e
)
177 if r
.status_code
== requests
.codes
.ok
:
178 result
['error'] = False
179 result
['data'] = Util
.json_loads_byteified(r
.text
)
183 def project_get(self
, token
, id):
184 result
= {'error': True, 'data': ''}
185 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
186 'Authorization': 'Bearer {}'.format(token
['id'])}
187 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
189 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
190 except Exception as e
:
192 result
['data'] = str(e
)
194 if r
.status_code
== requests
.codes
.ok
:
195 result
['error'] = False
196 result
['data'] = Util
.json_loads_byteified(r
.text
)
199 def project_create(self
, token
, project_data
):
201 result
= {'error': True, 'data': ''}
202 headers
= {"Content-Type": "application/json", "accept": "application/json",
203 'Authorization': 'Bearer {}'.format(token
['id'])}
205 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
208 r
= requests
.post(_url
, json
=project_data
, verify
=False, headers
=headers
)
209 except Exception as e
:
211 result
['data'] = str(e
)
213 if r
.status_code
== requests
.codes
.created
:
214 result
['error'] = False
215 result
['data'] = Util
.json_loads_byteified(r
.text
)
218 def project_edit(self
, token
, id, project_data
):
220 result
= {'error': True, 'data': ''}
221 headers
= {"Content-Type": "application/json", "accept": "application/json",
222 'Authorization': 'Bearer {}'.format(token
['id'])}
224 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
227 r
= requests
.put(_url
, json
=project_data
, verify
=False, headers
=headers
)
228 except Exception as e
:
230 result
['data'] = str(e
)
232 if r
.status_code
== requests
.codes
.no_content
:
233 result
['error'] = False
234 result
['data'] = Util
.json_loads_byteified(r
.text
)
237 def project_delete(self
, token
, id):
238 result
= {'error': True, 'data': ''}
239 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
240 'Authorization': 'Bearer {}'.format(token
['id'])}
242 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
244 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
245 except Exception as e
:
247 result
['data'] = str(e
)
249 if r
.status_code
== requests
.codes
.no_content
:
250 result
['error'] = False
252 result
['data'] = Util
.json_loads_byteified(r
.text
)
255 def nsd_list(self
, token
):
256 result
= {'error': True, 'data': ''}
257 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
258 'Authorization': 'Bearer {}'.format(token
['id'])}
260 _url
= "{0}/nsd/v1/ns_descriptors_content".format(self
._base
_path
)
262 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
263 except Exception as e
:
265 result
['data'] = str(e
)
267 if r
.status_code
== requests
.codes
.ok
:
268 result
['error'] = False
269 result
['data'] = Util
.json_loads_byteified(r
.text
)
273 def vnfd_list(self
, token
):
274 result
= {'error': True, 'data': ''}
275 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
276 'Authorization': 'Bearer {}'.format(token
['id'])}
278 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
280 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
281 except Exception as e
:
283 result
['data'] = str(e
)
285 if r
.status_code
== requests
.codes
.ok
:
286 result
['error'] = False
287 result
['data'] = Util
.json_loads_byteified(r
.text
)
291 def ns_list(self
, token
):
292 result
= {'error': True, 'data': ''}
293 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
294 'Authorization': 'Bearer {}'.format(token
['id'])}
295 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
297 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
298 except Exception as e
:
300 result
['data'] = str(e
)
302 if r
.status_code
== requests
.codes
.ok
:
303 result
['error'] = False
304 result
['data'] = Util
.json_loads_byteified(r
.text
)
308 def vnf_list(self
, token
):
309 result
= {'error': True, 'data': ''}
310 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
311 'Authorization': 'Bearer {}'.format(token
['id'])}
312 _url
= "{0}/nslcm/v1/vnfrs".format(self
._base
_path
)
314 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
315 except Exception as e
:
317 result
['data'] = str(e
)
319 if r
.status_code
== requests
.codes
.ok
:
320 result
['error'] = False
321 result
['data'] = Util
.json_loads_byteified(r
.text
)
325 def nsd_delete(self
, token
, id):
326 result
= {'error': True, 'data': ''}
327 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
328 'Authorization': 'Bearer {}'.format(token
['id'])}
330 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
332 r
= requests
.delete(_url
, params
=None, verify
=False,headers
=headers
)
333 except Exception as e
:
335 result
['data'] = str(e
)
337 if r
.status_code
== requests
.codes
.ok
:
338 result
['error'] = False
339 result
['data'] = Util
.json_loads_byteified(r
.text
)
342 def vnfd_delete(self
, token
, id):
343 result
= {'error': True, 'data': ''}
344 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
345 'Authorization': 'Bearer {}'.format(token
['id'])}
347 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
349 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
350 except Exception as e
:
352 result
['data'] = str(e
)
355 result
['error'] = False
356 if r
.status_code
!= requests
.codes
.no_content
:
357 result
['data'] = Util
.json_loads_byteified(r
.text
)
360 def nsd_onboard(self
, token
, package
):
361 result
= {'error': True, 'data': ''}
362 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
363 'Authorization': 'Bearer {}'.format(token
['id'])}
364 with
open('/tmp/'+package
.name
, 'wb+') as destination
:
365 for chunk
in package
.chunks():
366 destination
.write(chunk
)
367 headers
['Content-File-MD5'] = self
.md5(open('/tmp/'+package
.name
, 'rb'))
368 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
370 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
371 except Exception as e
:
373 result
['data'] = str(e
)
375 if r
.status_code
== requests
.codes
.created
:
376 result
['error'] = False
377 result
['data'] = Util
.json_loads_byteified(r
.text
)
380 def vnfd_onboard(self
, token
, package
):
381 result
= {'error': True, 'data': ''}
382 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
383 'Authorization': 'Bearer {}'.format(token
['id'])}
384 with
open('/tmp/'+package
.name
, 'wb+') as destination
:
385 for chunk
in package
.chunks():
386 destination
.write(chunk
)
387 headers
['Content-File-MD5'] = self
.md5(open('/tmp/'+package
.name
, 'rb'))
388 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
390 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
391 except Exception as e
:
393 result
['data'] = str(e
)
395 if r
.status_code
== requests
.codes
.created
:
396 result
['error'] = False
397 result
['data'] = Util
.json_loads_byteified(r
.text
)
400 def nsd_clone(self
, token
, id):
401 result
= {'error': True, 'data': ''}
402 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
403 'Authorization': 'Bearer {}'.format(token
['id'])}
405 # get the package onboarded
406 tar_pkg
= self
.get_nsd_pkg(token
, id)
407 tarf
= tarfile
.open(fileobj
=tar_pkg
)
408 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
409 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
411 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
414 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
416 except Exception as e
:
418 result
['data'] = str(e
)
420 if r
.status_code
== requests
.codes
.created
:
421 result
['error'] = False
422 if r
.status_code
== requests
.codes
.conflict
:
423 result
['data'] = "Invalid ID."
427 def vnfd_clone(self
, token
, id):
428 result
= {'error': True, 'data': ''}
429 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
430 'Authorization': 'Bearer {}'.format(token
['id'])}
432 # get the package onboarded
433 tar_pkg
= self
.get_vnfd_pkg(token
, id)
434 tarf
= tarfile
.open(fileobj
=tar_pkg
)
436 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
437 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
439 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
442 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
444 except Exception as e
:
446 result
['data'] = str(e
)
448 if r
.status_code
== requests
.codes
.created
:
449 result
['error'] = False
450 if r
.status_code
== requests
.codes
.conflict
:
451 result
['data'] = "Invalid ID."
455 def nsd_update(self
, token
, id, data
):
456 result
= {'error': True, 'data': ''}
457 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
458 'Authorization': 'Bearer {}'.format(token
['id'])}
460 # get the package onboarded
461 tar_pkg
= self
.get_nsd_pkg(token
, id)
462 tarf
= tarfile
.open(fileobj
=tar_pkg
)
464 tarf
= self
._descriptor
_update
(tarf
, data
)
465 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
467 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
470 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
472 except Exception as e
:
474 result
['data'] = str(e
)
476 if r
.status_code
== requests
.codes
.no_content
:
477 result
['error'] = False
481 def vnfd_update(self
, token
, id, data
):
482 result
= {'error': True, 'data': ''}
483 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
484 'Authorization': 'Bearer {}'.format(token
['id'])}
486 # get the package onboarded
487 tar_pkg
= self
.get_vnfd_pkg(token
, id)
488 tarf
= tarfile
.open(fileobj
=tar_pkg
)
490 tarf
= self
._descriptor
_update
(tarf
, data
)
491 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
493 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
496 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
498 except Exception as e
:
500 result
['data'] = str(e
)
502 if r
.status_code
== requests
.codes
.no_content
:
503 result
['error'] = False
507 def get_nsd_pkg(self
, token
, id):
508 result
= {'error': True, 'data': ''}
509 headers
= { "accept": "application/zip",
510 'Authorization': 'Bearer {}'.format(token
['id'])}
512 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
514 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
515 except Exception as e
:
517 result
['data'] = str(e
)
519 if r
.status_code
== requests
.codes
.ok
:
520 result
['error'] = False
521 tarf
= StringIO
.StringIO(r
.content
)
525 def get_vnfd_pkg(self
, token
, id):
526 result
= {'error': True, 'data': ''}
527 headers
= {"accept": "application/zip",
528 'Authorization': 'Bearer {}'.format(token
['id'])}
529 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
531 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
532 except Exception as e
:
534 result
['data'] = str(e
)
536 if r
.status_code
== requests
.codes
.ok
:
537 result
['error'] = False
538 tarf
= StringIO
.StringIO(r
.content
)
542 def _descriptor_update(self
, tarf
, data
):
543 # extract the package on a tmp directory
544 tarf
.extractall('/tmp')
546 for name
in tarf
.getnames():
547 if name
.endswith(".yaml") or name
.endswith(".yml"):
548 with
open('/tmp/' + name
, 'w') as outfile
:
549 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
552 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
555 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
559 def _descriptor_clone(self
, tarf
, descriptor_type
):
560 # extract the package on a tmp directory
561 tarf
.extractall('/tmp')
563 for name
in tarf
.getnames():
564 if name
.endswith(".yaml") or name
.endswith(".yml"):
565 with
open('/tmp/' + name
, 'r') as outfile
:
566 yaml_object
= yaml
.load(outfile
)
568 if descriptor_type
== 'nsd':
569 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
571 nsd
['id'] = 'clone_' + nsd
['id']
572 nsd
['name'] = 'clone_' +nsd
['name']
573 nsd
['short-name'] = 'clone_' +nsd
['short-name']
574 elif descriptor_type
== 'vnfd':
575 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
576 for vnfd
in vnfd_list
:
577 vnfd
['id'] = 'clone_' + vnfd
['id']
578 vnfd
['name'] = 'clone_' + vnfd
['name']
579 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
582 with
open('/tmp/' + name
, 'w') as yaml_file
:
583 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
586 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
589 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
593 def nsd_get(self
, token
, id):
594 result
= {'error': True, 'data': ''}
595 headers
= {'Content-Type': 'application/yaml',
596 'Authorization': 'Bearer {}'.format(token
['id'])}
597 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
599 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
600 except Exception as e
:
602 result
['data'] = str(e
)
604 if r
.status_code
== requests
.codes
.ok
:
605 result
['error'] = False
606 return yaml
.load(r
.text
)
609 result
['data'] = r
.json()
610 except Exception as e
:
614 def vnfd_get(self
, token
, id):
615 result
= {'error': True, 'data': ''}
616 headers
= {'Content-Type': 'application/yaml',
617 'Authorization': 'Bearer {}'.format(token
['id'])}
618 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
620 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
621 except Exception as e
:
623 result
['data'] = str(e
)
625 if r
.status_code
== requests
.codes
.ok
:
626 result
['error'] = False
627 return yaml
.load(r
.text
)
630 result
['data'] = r
.json()
631 except Exception as e
:
635 def nsd_artifacts(self
, token
, id):
636 result
= {'error': True, 'data': ''}
637 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
638 'Authorization': 'Bearer {}'.format(token
['id'])}
639 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
641 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
642 except Exception as e
:
644 result
['data'] = str(e
)
646 if r
.status_code
== requests
.codes
.ok
:
647 result
['error'] = False
648 result
['data'] = r
.text
651 result
['data'] = r
.json()
652 except Exception as e
:
657 def vnf_packages_artifacts(self
, token
, id):
658 result
= {'error': True, 'data': ''}
659 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
660 'Authorization': 'Bearer {}'.format(token
['id'])}
661 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
663 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
664 except Exception as e
:
666 result
['data'] = str(e
)
668 if r
.status_code
== requests
.codes
.ok
:
669 result
['error'] = False
670 result
['data'] = r
.text
673 result
['data'] = r
.json()
674 except Exception as e
:
679 def ns_create(self
, token
, ns_data
):
680 result
= {'error': True, 'data': ''}
681 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
682 'Authorization': 'Bearer {}'.format(token
['id'])}
684 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
687 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
688 except Exception as e
:
690 result
['data'] = str(e
)
692 if r
.status_code
== requests
.codes
.ok
:
693 result
['error'] = False
694 result
['data'] = Util
.json_loads_byteified(r
.text
)
697 def ns_op_list(self
, token
, id):
698 result
= {'error': True, 'data': ''}
699 headers
= {"Content-Type": "application/json", "accept": "application/json",
700 'Authorization': 'Bearer {}'.format(token
['id'])}
701 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
704 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
705 except Exception as e
:
707 result
['data'] = str(e
)
709 if r
.status_code
== requests
.codes
.ok
:
710 result
['error'] = False
711 result
['data'] = Util
.json_loads_byteified(r
.text
)
715 def ns_op(self
, token
, id):
716 result
= {'error': True, 'data': ''}
717 headers
= {"Content-Type": "application/json", "accept": "application/json",
718 'Authorization': 'Bearer {}'.format(token
['id'])}
719 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
722 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
723 except Exception as e
:
725 result
['data'] = str(e
)
727 if r
.status_code
== requests
.codes
.ok
:
728 result
['error'] = False
729 result
['data'] = Util
.json_loads_byteified(r
.text
)
733 def ns_action(self
, token
, id, action_payload
):
734 result
= {'error': True, 'data': ''}
735 headers
= {"Content-Type": "application/json", "accept": "application/json",
736 'Authorization': 'Bearer {}'.format(token
['id'])}
738 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
741 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
742 except Exception as e
:
744 result
['data'] = str(e
)
746 if r
.status_code
== requests
.codes
.created
:
747 result
['error'] = False
748 result
['data'] = Util
.json_loads_byteified(r
.text
)
751 def ns_delete(self
, token
, id, force
=None):
752 result
= {'error': True, 'data': ''}
753 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
754 'Authorization': 'Bearer {}'.format(token
['id'])}
757 query_path
= '?FORCE=true'
758 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
760 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
761 except Exception as e
:
763 result
['data'] = str(e
)
766 result
['error'] = False
767 if r
.status_code
!= requests
.codes
.no_content
:
768 result
['data'] = Util
.json_loads_byteified(r
.text
)
771 def ns_get(self
, token
, id):
772 result
= {'error': True, 'data': ''}
773 headers
= {"Content-Type": "application/json", "accept": "application/json",
774 'Authorization': 'Bearer {}'.format(token
['id'])}
775 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
778 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
779 except Exception as e
:
781 result
['data'] = str(e
)
783 if r
.status_code
== requests
.codes
.ok
:
784 result
['error'] = False
785 result
['data'] = Util
.json_loads_byteified(r
.text
)
788 def vnf_get(self
, token
, id):
789 result
= {'error': True, 'data': ''}
790 headers
= {"Content-Type": "application/json", "accept": "application/json",
791 'Authorization': 'Bearer {}'.format(token
['id'])}
792 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
795 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
796 except Exception as e
:
798 result
['data'] = str(e
)
800 if r
.status_code
== requests
.codes
.ok
:
801 result
['error'] = False
802 result
['data'] = Util
.json_loads_byteified(r
.text
)
805 def ns_alarm_create(self
, token
, id, alarm_payload
):
806 result
= {'error': True, 'data': ''}
807 headers
= {"Content-Type": "application/json",
808 'Authorization': 'Bearer {}'.format(token
['id'])}
809 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
811 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
812 except Exception as e
:
814 result
['data'] = str(e
)
816 if r
.status_code
== requests
.codes
.ok
:
817 result
['error'] = False
818 #result['data'] = Util.json_loads_byteified(r.text)
819 result
['data'] = r
.text
822 def ns_metric_export(self
, token
, id, metric_payload
):
823 result
= {'error': True, 'data': ''}
824 headers
= {"Content-Type": "application/json",
825 'Authorization': 'Bearer {}'.format(token
['id'])}
826 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
828 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
829 except Exception as e
:
831 result
['data'] = str(e
)
833 if r
.status_code
== requests
.codes
.ok
:
834 result
['error'] = False
835 #result['data'] = Util.json_loads_byteified(r.text)
836 result
['data'] = r
.text
839 def vim_list(self
, token
):
840 result
= {'error': True, 'data': ''}
841 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
842 'Authorization': 'Bearer {}'.format(token
['id'])}
843 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
845 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
846 except Exception as e
:
848 result
['data'] = str(e
)
850 if r
.status_code
== requests
.codes
.ok
:
851 result
['error'] = False
852 result
['data'] = Util
.json_loads_byteified(r
.text
)
856 def vim_delete(self
, token
, id):
857 result
= {'error': True, 'data': ''}
858 headers
= { "accept": "application/json",
859 'Authorization': 'Bearer {}'.format(token
['id'])}
860 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
862 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
863 except Exception as e
:
865 result
['data'] = str(e
)
867 if r
.status_code
== requests
.codes
.accepted
:
868 result
['error'] = False
870 result
['data'] = r
.text
873 def vim_get(self
, token
, id):
875 result
= {'error': True, 'data': ''}
876 headers
= {"Content-Type": "application/json", "accept": "application/json",
877 'Authorization': 'Bearer {}'.format(token
['id'])}
878 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
881 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
882 except Exception as e
:
884 result
['data'] = str(e
)
886 if r
.status_code
== requests
.codes
.ok
:
887 result
['error'] = False
888 result
['data'] = Util
.json_loads_byteified(r
.text
)
891 def vim_create(self
, token
, vim_data
):
893 result
= {'error': True, 'data': ''}
894 headers
= {"Content-Type": "application/json", "accept": "application/json",
895 'Authorization': 'Bearer {}'.format(token
['id'])}
897 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
900 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
901 except Exception as e
:
903 result
['data'] = str(e
)
905 if r
.status_code
== requests
.codes
.created
:
906 result
['error'] = False
907 result
['data'] = Util
.json_loads_byteified(r
.text
)
910 def sdn_list(self
, token
):
911 result
= {'error': True, 'data': ''}
912 headers
= {"accept": "application/json",
913 'Authorization': 'Bearer {}'.format(token
['id'])}
914 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
916 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
917 except Exception as e
:
919 result
['data'] = str(e
)
921 if r
.status_code
== requests
.codes
.ok
:
922 result
['error'] = False
923 result
['data'] = Util
.json_loads_byteified(r
.text
)
926 def sdn_delete(self
, token
, id):
927 result
= {'error': True, 'data': ''}
928 headers
= {"accept": "application/json",
929 'Authorization': 'Bearer {}'.format(token
['id'])}
930 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
932 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
933 except Exception as e
:
935 result
['data'] = str(e
)
937 if r
.status_code
== requests
.codes
.accepted
:
938 result
['error'] = False
940 result
['data'] = r
.text
943 def sdn_get(self
, token
, id):
944 result
= {'error': True, 'data': ''}
945 headers
= {"accept": "application/json",
946 'Authorization': 'Bearer {}'.format(token
['id'])}
947 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
950 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
951 except Exception as e
:
953 result
['data'] = str(e
)
955 if r
.status_code
== requests
.codes
.ok
:
956 result
['error'] = False
957 result
['data'] = Util
.json_loads_byteified(r
.text
)
961 def sdn_create(self
, token
, sdn_data
):
962 result
= {'error': True, 'data': ''}
963 headers
= {"Content-Type": "application/json", "accept": "application/json",
964 'Authorization': 'Bearer {}'.format(token
['id'])}
966 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
969 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
970 except Exception as e
:
972 result
['data'] = str(e
)
974 if r
.status_code
== requests
.codes
.created
:
975 result
['error'] = False
976 result
['data'] = Util
.json_loads_byteified(r
.text
)
982 hash_md5
= hashlib
.md5()
983 for chunk
in iter(lambda: f
.read(1024), b
""):
984 hash_md5
.update(chunk
)
985 return hash_md5
.hexdigest()