Replace deprecated yaml.load by yaml.safe_load
[osm/osmclient.git] / osmclient / sol005 / nsd.py
index af1e2ef..bbfed17 100644 (file)
@@ -21,11 +21,11 @@ OSM nsd API handling
 from osmclient.common.exceptions import NotFound
 from osmclient.common.exceptions import ClientException
 from osmclient.common import utils
-import yaml
 import json
 import magic
+from os.path import basename
 #from os import stat
-#from os.path import basename
+
 
 class Nsd(object):
 
@@ -44,7 +44,7 @@ class Nsd(object):
         if filter:
             filter_string = '?{}'.format(filter)
         resp = self._http.get_cmd('{}{}'.format(self._apiBase, filter_string))
-        #print yaml.safe_dump(resp)
+        #print(yaml.safe_dump(resp))
         if resp:
             return resp
         return list()
@@ -65,7 +65,7 @@ class Nsd(object):
         # It is redundant, since the previous one already gets the whole nsdinfo
         # The only difference is that a different primitive is exercised
         resp = self._http.get_cmd('{}/{}'.format(self._apiBase, nsd['_id']))
-        #print yaml.safe_dump(resp)
+        #print(yaml.safe_dump(resp))
         if resp:
             return resp
         raise NotFound("nsd {} not found".format(name))
@@ -75,8 +75,8 @@ class Nsd(object):
         headers = self._client._headers
         headers['Accept'] = 'application/binary'
         http_code, resp = self._http.get2_cmd('{}/{}/{}'.format(self._apiBase, nsd['_id'], thing))
-        #print 'HTTP CODE: {}'.format(http_code)
-        #print 'RESP: {}'.format(resp)
+        #print('HTTP CODE: {}'.format(http_code))
+        #print('RESP: {}'.format(resp))
         if http_code in (200, 201, 202, 204):
             if resp:
                 #store in a file
@@ -106,17 +106,17 @@ class Nsd(object):
             querystring = '?FORCE=True'
         http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
                                          nsd['_id'], querystring))
-        #print 'HTTP CODE: {}'.format(http_code)
-        #print 'RESP: {}'.format(resp)
+        #print('HTTP CODE: {}'.format(http_code))
+        #print('RESP: {}'.format(resp))
         if http_code == 202:
-            print 'Deletion in progress'
+            print('Deletion in progress')
         elif http_code == 204:
-            print 'Deleted'
+            print('Deleted')
         else:
             msg = ""
             if resp:
                 try:
-                    resp = json.loads(resp)
+                    msg = json.loads(resp)
                 except ValueError:
                     msg = resp
             raise ClientException("failed to delete nsd {} - {}".format(name, msg))
@@ -127,9 +127,10 @@ class Nsd(object):
             raise ClientException(
                      "failed to guess MIME type for file '{}'".format(filename))
         headers= self._client._headers
-        if mime_type in ['application/yaml', 'text/plain']:
-            headers['Content-Type'] = 'application/yaml'
-        elif mime_type == 'application/gzip':
+        headers['Content-Filename'] = basename(filename)
+        if mime_type in ['application/yaml', 'text/plain', 'application/json']:
+            headers['Content-Type'] = 'text/plain'
+        elif mime_type in ['application/gzip', 'application/x-gzip']:
             headers['Content-Type'] = 'application/gzip'
             #headers['Content-Type'] = 'application/binary'
             # Next three lines are to be removed in next version
@@ -143,7 +144,7 @@ class Nsd(object):
                   )
         headers["Content-File-MD5"] = utils.md5(filename)
         http_header = ['{}: {}'.format(key,val)
-                      for (key,val) in headers.items()]
+                      for (key,val) in list(headers.items())]
         self._http.set_http_header(http_header)
         if update_endpoint:
             http_code, resp = self._http.put_cmd(endpoint=update_endpoint, filename=filename)
@@ -156,23 +157,25 @@ class Nsd(object):
                                             self._apiVersion, self._apiResource)
             endpoint = '{}{}'.format(self._apiBase,ow_string)
             http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename)
-        #print 'HTTP CODE: {}'.format(http_code)
-        #print 'RESP: {}'.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']
+            print(resp['id'])
+        elif http_code == 204:
+            print('Updated')
         else:
-            msg = ""
+            msg = "Error {}".format(http_code)
             if resp:
                 try:
-                    msg = json.loads(resp)
+                    msg = "{} - {}".format(msg, json.loads(resp))
                 except ValueError:
-                    msg = resp
-            raise ClientException("failed to create/update nsd - {}".format(name, msg))
+                    msg = "{} - {}".format(msg, resp)
+            raise ClientException("failed to create/update nsd - {}".format(msg))
 
     def update(self, name, filename):
         nsd = self.get(name)