pdu: list, create, show, delete
[osm/LW-UI.git] / lib / osm / osmclient / clientv2.py
index 8fbb0d2..d900af6 100644 (file)
@@ -37,7 +37,7 @@ class Client(object):
         self._user_endpoint = 'admin/v1/users'
         self._host = os.getenv('OSM_SERVER', "localhost")
         self._so_port = 9999
-        self._base_path = "https://{0}:{1}/osm".format(self._host, self._so_port)
+        self._base_path = 'https://{0}:{1}/osm'.format(self._host, self._so_port)
 
     def auth(self, args):
         result = {'error': True, 'data': ''}
@@ -251,12 +251,14 @@ class Client(object):
             result['data'] = Util.json_loads_byteified(r.text)
         return result
 
-    def nsd_list(self, token):
+    def nsd_list(self, token, filter=None):
         result = {'error': True, 'data': ''}
         headers = {"Content-Type": "application/yaml", "accept": "application/json",
                    'Authorization': 'Bearer {}'.format(token['id'])}
-
-        _url = "{0}/nsd/v1/ns_descriptors_content".format(self._base_path)
+        query_path = ''
+        if filter:
+            query_path = '?_admin.type='+filter
+        _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(self._base_path, query_path)
         try:
             r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
         except Exception as e:
@@ -269,12 +271,14 @@ class Client(object):
 
         return result
 
-    def vnfd_list(self, token):
+    def vnfd_list(self, token, filter=None):
         result = {'error': True, 'data': ''}
         headers = {"Content-Type": "application/yaml", "accept": "application/json",
                    'Authorization': 'Bearer {}'.format(token['id'])}
-
-        _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
+        query_path = ''
+        if filter:
+            query_path = '?_admin.type='+filter
+        _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self._base_path, query_path)
         try:
             r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
         except Exception as e:
@@ -321,6 +325,23 @@ class Client(object):
 
         return result
 
+    def pdu_list(self, token):
+        result = {'error': True, 'data': ''}
+        headers = {"Content-Type": "application/yaml", "accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+        _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
+        try:
+            r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
+        except Exception as e:
+            log.exception(e)
+            result['data'] = str(e)
+            return result
+        if r.status_code == requests.codes.ok:
+            result['error'] = False
+        result['data'] = Util.json_loads_byteified(r.text)
+
+        return result
+
     def nsd_delete(self, token, id):
         result = {'error': True, 'data': ''}
         headers = {"Content-Type": "application/yaml", "accept": "application/json",
@@ -405,6 +426,7 @@ class Client(object):
 
         try:
             self._create_base_pkg('nsd', pkg_name)
+            headers['Content-Filename'] = pkg_name + '.tar.gz'
             r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
         except Exception as e:
             log.exception(e)
@@ -516,6 +538,11 @@ class Client(object):
             return result
         if r.status_code == requests.codes.no_content:
             result['error'] = False
+        else:
+            try:
+                result['data'] = r.json()
+            except Exception as e:
+                result['data'] = {}
 
         return result
 
@@ -542,6 +569,11 @@ class Client(object):
             return result
         if r.status_code == requests.codes.no_content:
             result['error'] = False
+        else:
+            try:
+                result['data'] = r.json()
+            except Exception as e:
+                result['data'] = {}
 
         return result
 
@@ -625,7 +657,9 @@ class Client(object):
                             "short-name": str(pkg_name),
                             "vdu": [],
                             "description": "",
-                            "mgmt-interface": {},
+                            "mgmt-interface": {
+                                "cp": ""
+                            },
                             "id": str(pkg_name),
                             "version": "1.0",
                             "internal-vld": [],
@@ -787,6 +821,24 @@ class Client(object):
         result['data'] = Util.json_loads_byteified(r.text)
         return result
 
+    def pdu_create(self, token, pdu_data):
+        result = {'error': True, 'data': ''}
+        headers = {"Content-Type": "application/yaml", "accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+
+        _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
+
+        try:
+            r = requests.post(_url, json=pdu_data, verify=False, headers=headers)
+        except Exception as e:
+            log.exception(e)
+            result['data'] = str(e)
+            return result
+        if r.status_code == requests.codes.created:
+            result['error'] = False
+        result['data'] = Util.json_loads_byteified(r.text)
+        return result
+
     def ns_op_list(self, token, id):
         result = {'error': True, 'data': ''}
         headers = {"Content-Type": "application/json", "accept": "application/json",
@@ -861,6 +913,23 @@ class Client(object):
             result['data'] = Util.json_loads_byteified(r.text)
         return result
 
+    def pdu_delete(self, token, id):
+        result = {'error': True, 'data': ''}
+        headers = {"Content-Type": "application/yaml", "accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+        _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
+        try:
+            r = requests.delete(_url, params=None, verify=False, headers=headers)
+        except Exception as e:
+            log.exception(e)
+            result['data'] = str(e)
+            return result
+        if r:
+            result['error'] = False
+        if r.status_code != requests.codes.no_content:
+            result['data'] = Util.json_loads_byteified(r.text)
+        return result
+
     def ns_get(self, token, id):
         result = {'error': True, 'data': ''}
         headers = {"Content-Type": "application/json", "accept": "application/json",
@@ -895,6 +964,23 @@ class Client(object):
         result['data'] = Util.json_loads_byteified(r.text)
         return result
 
+    def pdu_get(self, token, id):
+        result = {'error': True, 'data': ''}
+        headers = {"Content-Type": "application/json", "accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+        _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
+
+        try:
+            r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
+        except Exception as e:
+            log.exception(e)
+            result['data'] = str(e)
+            return result
+        if r.status_code == requests.codes.ok:
+            result['error'] = False
+        result['data'] = Util.json_loads_byteified(r.text)
+        return result
+
     def ns_alarm_create(self, token, id, alarm_payload):
         result = {'error': True, 'data': ''}
         headers = {"Content-Type": "application/json",