using http codes in sol005 client whenever possible
[osm/osmclient.git] / osmclient / sol005 / vim.py
index 0dcc4ad..6e15e5b 100644 (file)
@@ -22,6 +22,7 @@ from osmclient.common import utils
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
 import yaml
+import json
 
 
 class Vim(object):
@@ -30,42 +31,89 @@ class Vim(object):
         self._client = client
         self._apiName = '/admin'
         self._apiVersion = '/v1'
-        self._apiResource = '/vims'
+        self._apiResource = '/vim_accounts'
         self._apiBase = '{}{}{}'.format(self._apiName,
                                         self._apiVersion, self._apiResource)
-    def create(self, name, vim_access):
+    def create(self, name, vim_access, sdn_controller=None, sdn_port_mapping=None):
         if 'vim-type' not in vim_access:
             #'openstack' not in vim_access['vim-type']):
             raise Exception("vim type not provided")
 
         vim_account = {}
-        vim_config = {'hello': 'hello'}
         vim_account['name'] = name
         vim_account = self.update_vim_account_dict(vim_account, vim_access)
 
         vim_config = {}
         if 'config' in vim_access and vim_access['config'] is not None:
-           vim_config = yaml.load(vim_access['config'])
+            vim_config = yaml.safe_load(vim_access['config'])
+        if sdn_controller:
+            sdnc = self._client.sdnc.get(sdn_controller)
+            vim_config['sdn-controller'] = sdnc['_id']
+        if sdn_port_mapping:
+            with open(sdn_port_mapping, 'r') as f:
+                vim_config['sdn-port-mapping'] = yaml.safe_load(f.read())
+        if vim_config:
+            vim_account['config'] = vim_config
+            #vim_account['config'] = json.dumps(vim_config)
 
-        vim_account['config'] = vim_config
-
-        resp = self._http.post_cmd(endpoint=self._apiBase,
+        http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
                                        postfields_dict=vim_account)
-        if not resp or 'id' not in resp:
-            raise ClientException('failed to create vim {}: {}'.format(
-                                  name, resp))
-        else:
+        #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:
+            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):
+    def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping):
         vim = self.get(vim_name)
-        resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']),
+
+        vim_config = {}
+        if 'config' in vim_account:
+            if config=="" and (sdncontroller or sdn_port_mapping):
+                raise ClientException("clearing config is incompatible with updating SDN info")
+            if config=="":
+                vim_config = None
+            else:
+                vim_config = yaml.safe_load(vim_account['config'])
+        if sdn_controller:
+            sdnc = self._client.sdnc.get(sdn_controller)
+            vim_config['sdn-controller'] = sdnc['_id']
+        if sdn_port_mapping:
+            with open(sdn_port_mapping, 'r') as f:
+                vim_config['sdn-port-mapping'] = yaml.safe_load(f.read())
+        vim_account['config'] = vim_config
+        #vim_account['config'] = json.dumps(vim_config)
+        http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']),
                                        postfields_dict=vim_account)
-        if not resp or '_id' not in resp:
-            raise ClientException('failed to update vim: '.format(
-                                  resp))
+        #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 update vim {} - {}".format(vim_name, msg))
 
     def update_vim_account_dict(self, vim_account, vim_access):
         vim_account['vim_type'] = vim_access['vim-type']
@@ -84,18 +132,29 @@ class Vim(object):
                 return vim['uuid']
         raise NotFound("vim {} not found".format(name))
 
-    def delete(self, vim_name):
+    def delete(self, vim_name, force=False):
         vim_id = vim_name
         if not utils.validate_uuid4(vim_name):
             vim_id = self.get_id(vim_name)
-        http_code, resp = self._http.delete_cmd('{}/{}'.format(self._apiBase,vim_id))
+        querystring = ''
+        if force:
+            querystring = '?FORCE=True'
+        http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
+                                         vim_id, querystring))
+        #print 'HTTP CODE: {}'.format(http_code)
         #print 'RESP: {}'.format(resp)
         if http_code == 202:
             print 'Deletion in progress'
         elif http_code == 204:
             print 'Deleted'
         else:
-            raise ClientException("failed to delete vim {} - {}".format(vim_name, resp))
+            msg = ""
+            if resp:
+                try:
+                    msg = json.loads(resp)
+                except ValueError:
+                    msg = resp
+            raise ClientException("failed to delete vim {} - {}".format(vim_name, msg))
 
     def list(self, filter=None):
         """Returns a list of VIM accounts