Add missing wait_for_package api method
authorMike Marchetti <mmarchetti@sandvine.com>
Tue, 14 Aug 2018 16:15:16 +0000 (12:15 -0400)
committerMike Marchetti <mmarchetti@sandvine.com>
Tue, 14 Aug 2018 18:40:43 +0000 (14:40 -0400)
Signed-off-by: Mike Marchetti <mmarchetti@sandvine.com>
Change-Id: Iec32a4795174c35d4e478184b4d4cfc65ab288f6

osmclient/sol005/package.py

index 887539b..12412e4 100644 (file)
@@ -21,6 +21,7 @@ OSM package API handling
 #from os import stat
 #from os.path import basename
 from osmclient.common.exceptions import ClientException
+from osmclient.common.exceptions import NotFound
 from osmclient.common import utils
 import json
 
@@ -33,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: