working on clientv2: sdnctrlhandler 47/6347/2
authorlombardofr <lombardo@everyup.it>
Tue, 26 Jun 2018 18:17:07 +0000 (20:17 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 19 Jul 2018 12:12:21 +0000 (13:12 +0100)
Change-Id: I5ddd78fca60cf95caa971ec78639bea00aeec554
Signed-off-by: lombardofr <lombardo@everyup.it>
lib/osm/osmclient/clientv2.py
sdnctrlhandler/views.py
vimhandler/views.py

index 0f30cbe..7103459 100644 (file)
@@ -577,8 +577,6 @@ class Client(object):
             result['data'] = r.text
         return result
 
-
-
     def vim_get(self, token, id):
 
         result = {'error': True, 'data': ''}
@@ -617,6 +615,77 @@ class Client(object):
         result['data'] = Util.json_loads_byteified(r.text)
         return result
 
+    def sdn_list(self, token):
+        result = {'error': True, 'data': ''}
+        headers = {"accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+        _url = "{0}/admin/v1/sdns".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 sdn_delete(self, token, id):
+        result = {'error': True, 'data': ''}
+        headers = {"accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+        _url = "{0}/admin/v1/sdns/{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
+        print r.status_code
+        if r.status_code == requests.codes.accepted:
+            result['error'] = False
+        else:
+            result['data'] = r.text
+        return result
+
+    def sdn_get(self, token, id):
+        result = {'error': True, 'data': ''}
+        headers = {"accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+        _url = "{0}/admin/v1/sdns/{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 sdn_create(self, token, sdn_data):
+        result = {'error': True, 'data': ''}
+        headers = {"Content-Type": "application/json", "accept": "application/json",
+                   'Authorization': 'Bearer {}'.format(token['id'])}
+
+        _url = "{0}/admin/v1/sdns".format(self._base_path)
+
+        try:
+            r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
+        except Exception as e:
+            log.exception(e)
+            result['data'] = str(e)
+            return result
+        print r.status_code
+        if r.status_code == requests.codes.created:
+            result['error'] = False
+        result['data'] = Util.json_loads_byteified(r.text)
+        return result
+
 
     @staticmethod
     def md5(f):
index 0cd6692..f01f8b2 100644 (file)
 
 from django.shortcuts import render, redirect
 from django.contrib.auth.decorators import login_required
-from django.http import JsonResponse
-from lib.osm.osmclient.client import Client
+from django.http import HttpResponse
+import json
 import logging
+#from lib.osm.osmclient.client import Client
+from lib.osm.osmclient.clientv2 import Client
 
 logging.basicConfig(level=logging.DEBUG)
-log = logging.getLogger('view.py')
+log = logging.getLogger('sdnctrlhandler/view.py')
 
 
 @login_required
 def list(request, project_id):
     client = Client()
-    result = client.sdn_list()
+    result = client.sdn_list(request.session['token'])
 
     result = {
         'project_id': project_id,
-        'sdns': result
+        'sdns': result['data'] if result and result['error'] is False else []
     }
     return __response_handler(request, result, 'sdn_list.html')
 
@@ -55,7 +57,7 @@ def create(request, project_id):
         sdn_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_sdn_dict.items()))
         sdn_data['port'] = int(sdn_data['port'])
 
-        result = client.sdn_create(sdn_data)
+        result = client.sdn_create(request.session['token'], sdn_data)
 
         return __response_handler(request, result, 'projects:sdns:list', to_redirect=True, project_id=project_id)
 
@@ -64,7 +66,7 @@ def create(request, project_id):
 def delete(request, project_id, sdn_id=None):
     try:
         client = Client()
-        del_res = client.sdn_delete(sdn_id)
+        del_res = client.sdn_delete(request.session['token'], sdn_id)
     except Exception as e:
         log.exception(e)
     return __response_handler(request, {}, 'projects:sdns:list', to_redirect=True, project_id=project_id)
@@ -73,16 +75,18 @@ def delete(request, project_id, sdn_id=None):
 @login_required
 def show(request, project_id, sdn_id=None):
     client = Client()
-    datacenter = client.sdn_get(sdn_id)
+    result = client.sdn_get(request.session['token'], sdn_id)
+    if isinstance(result, dict) and 'error' in result and result['error']:
+        return render(request, 'error.html')
     return __response_handler(request, {
-        "sdn": datacenter
-    }, project_id=project_id)
+        "sdn": result['data'],
+        "project_id": project_id})
 
 
 def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
     raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
-    if 'application/json' in raw_content_types:
-        return JsonResponse(data_res)
+    if 'application/json' in raw_content_types or url is None:
+        return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
     elif to_redirect:
         return redirect(url, *args, **kwargs)
     else:
index dc96dbe..abaab9c 100644 (file)
@@ -24,7 +24,7 @@ import yaml
 import logging
 
 logging.basicConfig(level=logging.DEBUG)
-log = logging.getLogger('view.py')
+log = logging.getLogger('vimhandler.py')
 
 
 @login_required