fix wait option when operation fails
[osm/osmclient.git] / osmclient / sol005 / sdncontroller.py
index 2a4d409..5b85ef4 100644 (file)
@@ -20,10 +20,11 @@ OSM SDN controller API handling
 
 from osmclient.common import utils
 from osmclient.common import wait as WaitForStatus
+from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-from osmclient.common.exceptions import OsmHttpException
 import json
 import logging
+import yaml
 
 
 class SdnController(object):
@@ -38,16 +39,18 @@ class SdnController(object):
                                         self._apiVersion, self._apiResource)
 
     # SDNC '--wait' option
-    def _wait(self, id, deleteFlag=False):
+    def _wait(self, id, wait_time, deleteFlag=False):
         self._logger.debug("")
         self._client.get_token()
         # Endpoint to get operation status
         apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/sdns')
         # Wait for status for SDN instance creation/update/deletion
+        if isinstance(wait_time, bool):
+            wait_time = WaitForStatus.TIMEOUT_SDNC_OPERATION
         WaitForStatus.wait_for_status(
             'SDNC',
             str(id),
-            WaitForStatus.TIMEOUT_SDNC_OPERATION,
+            wait_time,
             apiUrlStatus,
             self._http.get2_cmd,
             deleteFlag=deleteFlag)
@@ -66,6 +69,8 @@ class SdnController(object):
 
     def create(self, name, sdn_controller, wait=False):
         self._logger.debug("")
+        if 'config' in sdn_controller and isinstance(sdn_controller["config"], str):
+            sdn_controller["config"] = yaml.safe_load(sdn_controller["config"])
         self._client.get_token()
         http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
                                        postfields_dict=sdn_controller)
@@ -75,11 +80,11 @@ class SdnController(object):
         if resp:
             resp = json.loads(resp)
         if not resp or 'id' not in resp:
-            raise OsmHttpException('unexpected response from server - {}'.format(
+            raise ClientException('unexpected response from server - {}'.format(
                                   resp))
         if wait:
             # Wait for status for SDNC instance creation
-            self._wait(resp.get('id'))
+            self._wait(resp.get('id'), wait)
         print(resp['id'])
         #else:
         #    msg = ""
@@ -92,6 +97,8 @@ class SdnController(object):
 
     def update(self, name, sdn_controller, wait=False):
         self._logger.debug("")
+        if 'config' in sdn_controller and isinstance(sdn_controller["config"], str):
+            sdn_controller["config"] = yaml.safe_load(sdn_controller["config"])
         self._client.get_token()
         sdnc = self.get(name)
         sdnc_id_for_wait = self._get_id_for_wait(name)
@@ -105,9 +112,9 @@ class SdnController(object):
             # Use the previously obtained id instead.
             wait_id = sdnc_id_for_wait
             # Wait for status for VI instance update
-            self._wait(wait_id)
-        else:
-            pass
+            self._wait(wait_id, wait)
+        else:
+            pass
         #else:
         #    msg = ""
         #    if resp:
@@ -132,7 +139,7 @@ class SdnController(object):
         if http_code == 202:
             if wait:
                 # Wait for status for SDNC instance deletion
-                self._wait(sdnc_id_for_wait, deleteFlag=True)
+                self._wait(sdnc_id_for_wait, wait, deleteFlag=True)
             else:
                 print('Deletion in progress')
         elif http_code == 204:
@@ -140,13 +147,13 @@ class SdnController(object):
         elif resp and 'result' in resp:
             print('Deleted')
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise OsmHttpException("failed to delete SDN controller {} - {}".format(name, msg))
+            msg = resp or ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete SDN controller {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """Returns a list of SDN controllers