using http codes in sol005 client whenever possible
[osm/osmclient.git] / osmclient / sol005 / sdncontroller.py
index a7b9a47..39ad3be 100644 (file)
@@ -21,7 +21,8 @@ OSM SDN controller API handling
 from osmclient.common import utils
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-import yaml 
+import yaml
+import json
 
 
 class SdnController(object):
@@ -30,33 +31,75 @@ class SdnController(object):
         self._client = client
         self._apiName = '/admin'
         self._apiVersion = '/v1'
-        self._apiResource = '/sdn_controllers'
+        self._apiResource = '/sdns'
         self._apiBase = '{}{}{}'.format(self._apiName,
                                         self._apiVersion, self._apiResource)
     def create(self, name, sdn_controller):
-        if 'type' not in vim_access: 
-            raise Exception("type not provided")
+        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))
+            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))
 
-        resp = self._http.post_cmd(endpoint=self._apiBase,
+    def update(self, name, sdn_controller):
+        sdnc = self.get(name)
+        http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']),
                                        postfields_dict=sdn_controller)
-        if not resp or '_id' not in resp:
-            raise ClientException('failed to create SDN controller: '.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 SDN controller {} - {}".format(name, msg))
 
-    def delete(self, name):
+    def delete(self, name, force=False):
         sdn_controller = self.get(name)
-        http_code, resp = self._http.delete_cmd('{}/{}'.format(self._apiBase,sdn_controller['_id']))
+        querystring = ''
+        if force:
+            querystring = '?FORCE=True'
+        http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
+                                         sdn_controller['_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'
-        elif 'result' in resp:
+        elif resp and 'result' in resp:
             print 'Deleted'
         else:
-            raise ClientException("failed to delete vim {} - {}".format(name, resp))
+            msg = ""
+            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
@@ -65,6 +108,7 @@ class SdnController(object):
         if filter:
             filter_string = '?{}'.format(filter)
         resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
+        #print 'RESP: {}'.format(resp)
         if resp:
             return resp
         return list()