Fix Bug 934. OSM CLI crashes for listing operations with insufficient permissions
[osm/osmclient.git] / osmclient / sol005 / vim.py
index 43911a6..b6f1614 100644 (file)
@@ -22,6 +22,7 @@ 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 yaml
 import json
 import logging
@@ -94,24 +95,24 @@ class Vim(object):
                                        postfields_dict=vim_account)
         #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 VIM 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 vim {} - {}".format(name, msg))
+        #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 VIM 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 vim {} - {}".format(name, msg))
 
     def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping, wait=False):
         self._logger.debug("")
@@ -138,23 +139,23 @@ class Vim(object):
                                        postfields_dict=vim_account)
         # 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 = vim_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 = vim_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 vim {} - {}".format(vim_name, msg))
+            pass
+        #else:
+        #    msg = ""
+        #    if resp:
+        #        try:
+        #            msg = json.loads(resp)
+        #        except ValueError:
+        #            msg = resp
+        #    raise ClientException("failed to update vim {} - {}".format(vim_name, msg))
 
     def update_vim_account_dict(self, vim_account, vim_access):
         self._logger.debug("")
@@ -209,7 +210,7 @@ class Vim(object):
                     msg = json.loads(resp)
                 except ValueError:
                     msg = resp
-            raise ClientException("failed to delete vim {} - {}".format(vim_name, msg))
+            raise OsmHttpException("failed to delete vim {} - {}".format(vim_name, msg))
 
     def list(self, filter=None):
         """Returns a list of VIM accounts
@@ -219,11 +220,11 @@ class Vim(object):
         filter_string = ''
         if filter:
             filter_string = '?{}'.format(filter)
-        resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
+        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
         if not resp:
             return list()
         vim_accounts = []
-        for datacenter in resp:
+        for datacenter in json.loads(resp):
             vim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id']
                         if '_id' in datacenter else None})
         return vim_accounts
@@ -236,11 +237,12 @@ class Vim(object):
         vim_id = name
         if not utils.validate_uuid4(name):
             vim_id = self.get_id(name)
-        resp = self._http.get_cmd('{}/{}'.format(self._apiBase,vim_id))
-        if not resp or '_id' not in resp:
-            raise ClientException('failed to get vim info: '.format(
-                                  resp))
-        else:
-            return resp
+        _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,vim_id))
+#        if not resp or '_id' not in resp:
+#            raise ClientException('failed to get vim info: '.format(
+#                                  resp))
+#        else:
+        if resp:
+            return json.loads(resp)
         raise NotFound("vim {} not found".format(name))