X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fsol005%2Fpackage.py;h=12412e48c3becba43af11e7f6258935feb8423c4;hb=12ddbf356d8b97e3f4afc67ec6543814153850fa;hp=b2fe035f7aea9cc83ac9e0300aeb7743b6db3074;hpb=f157128052e59df5c37489926f7d5755714c3cbf;p=osm%2Fosmclient.git diff --git a/osmclient/sol005/package.py b/osmclient/sol005/package.py index b2fe035..12412e4 100644 --- a/osmclient/sol005/package.py +++ b/osmclient/sol005/package.py @@ -18,9 +18,6 @@ OSM package API handling """ -import tarfile -import re -import yaml #from os import stat #from os.path import basename from osmclient.common.exceptions import ClientException @@ -37,6 +34,39 @@ class Package(object): def get_key_val_from_pkg(self, descriptor_file): return utils.get_key_val_from_pkg(descriptor_file) + def _wait_for_package(self, pkg_type): + if 'vnfd' in pkg_type['type']: + get_method = self._client.vnfd.get + elif 'nsd' in pkg_type['type']: + get_method = self._client.nsd.get + else: + raise ClientException("no valid package type found") + + # helper method to check if pkg exists + def check_exists(func): + try: + func() + except NotFound: + return False + return True + + return utils.wait_for_value(lambda: + check_exists(lambda: + get_method(pkg_type['name']))) + + def wait_for_upload(self, filename): + """wait(block) for an upload to succeed. + The filename passed is assumed to be a descriptor tarball. + """ + pkg_type = utils.get_key_val_from_pkg(filename) + + if pkg_type is None: + raise ClientException("Cannot determine package type") + + if not self._wait_for_package(pkg_type): + raise ClientException("package {} failed to upload" + .format(filename)) + def upload(self, filename): pkg_type = utils.get_key_val_from_pkg(filename) if pkg_type is None: @@ -56,15 +86,24 @@ class Package(object): #headers['Content-Range'] = 'bytes 0-{}/{}'.format(file_size - 1, file_size) headers["Content-File-MD5"] = utils.md5(filename) http_header = ['{}: {}'.format(key,val) - for (key,val) in headers.items()] + for (key,val) in list(headers.items())] self._http.set_http_header(http_header) http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) - if resp: - resp = json.loads(resp) - #print 'RESP: {}'.format(yaml.safe_dump(resp)) - if not resp or 'id' not in resp: - raise ClientException("failed to upload package") + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) + print(resp['id']) else: - print resp['id'] - + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to upload package - {}".format(msg))