1 |
|
# Copyright 2017 Sandvine |
2 |
|
# |
3 |
|
# All Rights Reserved. |
4 |
|
# |
5 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
6 |
|
# not use this file except in compliance with the License. You may obtain |
7 |
|
# a copy of the License at |
8 |
|
# |
9 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
# |
11 |
|
# Unless required by applicable law or agreed to in writing, software |
12 |
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
13 |
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 |
|
# License for the specific language governing permissions and limitations |
15 |
|
# under the License. |
16 |
|
|
17 |
1 |
""" |
18 |
|
OSM package API handling |
19 |
|
""" |
20 |
|
|
21 |
1 |
from osmclient.common.exceptions import ClientException |
22 |
1 |
from osmclient.common.exceptions import NotFound |
23 |
1 |
from osmclient.common import utils |
24 |
|
|
25 |
|
|
26 |
1 |
class Package(object): |
27 |
1 |
def __init__(self, http=None, upload_http=None, client=None): |
28 |
1 |
self._client = client |
29 |
1 |
self._http = http |
30 |
1 |
self._upload_http = upload_http |
31 |
|
|
32 |
1 |
def _wait_for_package(self, pkg_type): |
33 |
0 |
if "vnfd" in pkg_type["type"]: |
34 |
0 |
get_method = self._client.vnfd.get |
35 |
0 |
elif "nsd" in pkg_type["type"]: |
36 |
0 |
get_method = self._client.nsd.get |
37 |
|
else: |
38 |
0 |
raise ClientException("no valid package type found") |
39 |
|
|
40 |
|
# helper method to check if pkg exists |
41 |
0 |
def check_exists(func): |
42 |
0 |
try: |
43 |
0 |
func() |
44 |
0 |
except NotFound: |
45 |
0 |
return False |
46 |
0 |
return True |
47 |
|
|
48 |
0 |
return utils.wait_for_value( |
49 |
|
lambda: check_exists(lambda: get_method(pkg_type["name"])) |
50 |
|
) |
51 |
|
|
52 |
1 |
def get_key_val_from_pkg(self, descriptor_file): |
53 |
0 |
return utils.get_key_val_from_pkg(descriptor_file) |
54 |
|
|
55 |
1 |
def wait_for_upload(self, filename): |
56 |
|
"""wait(block) for an upload to succeed. |
57 |
|
The filename passed is assumed to be a descriptor tarball. |
58 |
|
""" |
59 |
1 |
pkg_type = utils.get_key_val_from_pkg(filename) |
60 |
|
|
61 |
0 |
if pkg_type is None: |
62 |
0 |
raise ClientException("Cannot determine package type") |
63 |
|
|
64 |
0 |
if not self._wait_for_package(pkg_type): |
65 |
0 |
raise ClientException("package {} failed to upload".format(filename)) |
66 |
|
|
67 |
1 |
def upload(self, filename): |
68 |
1 |
resp = self._upload_http.post_cmd(formfile=("package", filename)) |
69 |
1 |
if not resp or "transaction_id" not in resp: |
70 |
1 |
raise ClientException("failed to upload package") |