Fix Bug 934. OSM CLI crashes for listing operations with insufficient permissions
[osm/osmclient.git] / osmclient / sol005 / sdncontroller.py
index b2bbc37..2a4d409 100644 (file)
@@ -20,8 +20,8 @@ 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
 
@@ -67,26 +67,28 @@ class SdnController(object):
     def create(self, name, sdn_controller, wait=False):
         self._logger.debug("")
         self._client.get_token()
-        http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=sdn_controller)
-        # 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))
-            if wait:
-                # Wait for status for SDNC instance creation
-                self._wait(resp.get('id'))
-            print(resp['id'])
-        else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
+        http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
+                                       postfields_dict=sdn_controller)
+        #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 OsmHttpException('unexpected response from server - {}'.format(
+                                  resp))
+        if wait:
+            # Wait for status for SDNC instance creation
+            self._wait(resp.get('id'))
+        print(resp['id'])
+        #else:
+        #    msg = ""
+        #    if resp:
+        #        try:
+        #            msg = json.loads(resp)
+        #        except ValueError:
+        #            msg = resp
+        #    raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
 
     def update(self, name, sdn_controller, wait=False):
         self._logger.debug("")
@@ -97,23 +99,23 @@ class SdnController(object):
                                                postfields_dict=sdn_controller)
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
-        if http_code in (200, 201, 202, 204):
-            if wait:
-                # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
-                # 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
+        #if http_code in (200, 201, 202, 204):
+        if wait:
+            # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
+            # Use the previously obtained id instead.
+            wait_id = sdnc_id_for_wait
+            # Wait for status for VI instance update
+            self._wait(wait_id)
         else:
-            msg = ""
-            if resp:
-                try:
-                    msg = json.loads(resp)
-                except ValueError:
-                    msg = resp
-            raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
+            pass
+        #else:
+        #    msg = ""
+        #    if resp:
+        #        try:
+        #            msg = json.loads(resp)
+        #        except ValueError:
+        #            msg = resp
+        #    raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
 
     def delete(self, name, force=False, wait=False):
         self._logger.debug("")
@@ -144,7 +146,7 @@ class SdnController(object):
                     msg = json.loads(resp)
                 except ValueError:
                     msg = resp
-            raise ClientException("failed to delete SDN controller {} - {}".format(name, msg))
+            raise OsmHttpException("failed to delete SDN controller {} - {}".format(name, msg))
 
     def list(self, filter=None):
         """Returns a list of SDN controllers
@@ -154,10 +156,10 @@ class SdnController(object):
         filter_string = ''
         if filter:
             filter_string = '?{}'.format(filter)
-        resp = self._http.get_cmd('{}{}'.format(self._apiBase, filter_string))
-        # print('RESP: {}'.format(resp))
+        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
+        #print('RESP: {}'.format(resp))
         if resp:
-            return resp
+            return json.loads(resp)
         return list()
 
     def get(self, name):