Standardize Formatting
[osm/osmclient.git] / osmclient / v1 / package.py
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 """
18 OSM package API handling
19 """
20
21 from osmclient.common.exceptions import ClientException
22 from osmclient.common.exceptions import NotFound
23 from osmclient.common import utils
24
25
26 class Package(object):
27 def __init__(self, http=None, upload_http=None, client=None):
28 self._client = client
29 self._http = http
30 self._upload_http = upload_http
31
32 def _wait_for_package(self, pkg_type):
33 if "vnfd" in pkg_type["type"]:
34 get_method = self._client.vnfd.get
35 elif "nsd" in pkg_type["type"]:
36 get_method = self._client.nsd.get
37 else:
38 raise ClientException("no valid package type found")
39
40 # helper method to check if pkg exists
41 def check_exists(func):
42 try:
43 func()
44 except NotFound:
45 return False
46 return True
47
48 return utils.wait_for_value(
49 lambda: check_exists(lambda: get_method(pkg_type["name"]))
50 )
51
52 def get_key_val_from_pkg(self, descriptor_file):
53 return utils.get_key_val_from_pkg(descriptor_file)
54
55 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 pkg_type = utils.get_key_val_from_pkg(filename)
60
61 if pkg_type is None:
62 raise ClientException("Cannot determine package type")
63
64 if not self._wait_for_package(pkg_type):
65 raise ClientException("package {} failed to upload".format(filename))
66
67 def upload(self, filename):
68 resp = self._upload_http.post_cmd(formfile=("package", filename))
69 if not resp or "transaction_id" not in resp:
70 raise ClientException("failed to upload package")