| Frank Bryden | e570b10 | 2020-09-07 08:45:54 +0000 | [diff] [blame] | 1 | # Copyright 2020 ETSI OSM |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License |
| 15 | |
| 16 | from subprocess import run, PIPE |
| 17 | import requests |
| 18 | |
| 19 | class OSMException(Exception): |
| 20 | """A base class for MyProject exceptions.""" |
| 21 | |
| 22 | class ResourceException(OSMException): |
| 23 | def __init__(self, *args, **kwargs): |
| 24 | super().__init__(*args) |
| 25 | self.message = kwargs.get('message') |
| 26 | |
| 27 | class OSM: |
| 28 | def __init__(self, osm_hostname): |
| 29 | self.osm_hostname = osm_hostname |
| 30 | |
| 31 | def run_command(self, command): |
| 32 | p = run(["osm {}".format(command)], shell=True, stdout=PIPE, stderr=PIPE) |
| 33 | if p.returncode != 0: |
| 34 | print(p.stdout) |
| 35 | print(p.stderr) |
| 36 | raise ResourceException(message=p.stdout) |
| 37 | else: |
| 38 | return self.clean_output(p.stdout) |
| 39 | |
| 40 | def get_auth_token(self): |
| 41 | r = requests.post("https://{}:9999/osm/admin/v1/tokens".format(self.osm_hostname), verify=False, headers={"Accept":"application/json"}, json={ |
| 42 | "username": "admin", |
| 43 | "password": "admin", |
| 44 | "project": "admin" |
| 45 | }) |
| 46 | return r.json()["id"] |
| 47 | |
| 48 | def clean_output(self, output): |
| 49 | return output.decode("utf-8").strip() |
| 50 | |
| 51 | def create_nsd(self, filename): |
| 52 | return self.run_command("nsd-create {}".format(filename)) |
| 53 | |
| 54 | def create_vnfd(self, filename): |
| 55 | return self.run_command("vnfd-create {}".format(filename)) |
| 56 | |
| 57 | def delete_nsd(self, id): |
| 58 | return self.run_command("nsd-delete {}".format(id)) |
| 59 | |
| 60 | def delete_vnfd(self, id): |
| 61 | return self.run_command("vnfd-delete {}".format(id)) |