From: lombardofr Date: Tue, 17 Jul 2018 15:27:36 +0000 (+0200) Subject: several changes on auth flow X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=236e3e3acac85d83d8c5f3d331f905644886a1e5;p=osm%2FLW-UI.git several changes on auth flow Change-Id: I49ddbb074a9bc018b9e5becafbe5956fa5860333 Signed-off-by: lombardofr --- diff --git a/.bowerrc b/.bowerrc index c7fde75..05723b0 100644 --- a/.bowerrc +++ b/.bowerrc @@ -1,3 +1,4 @@ { -"directory": "static/bower_components" + "directory": "static/bower_components", + "registry": "https://registry.bower.io" } diff --git a/authosm/backend.py b/authosm/backend.py index 4bf9e72..6c23e91 100644 --- a/authosm/backend.py +++ b/authosm/backend.py @@ -13,8 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from django.core.exceptions import PermissionDenied + from .models import OsmUser from lib.osm.osmclient.clientv2 import Client +from .exceptions import OSMAuthException class OsmBackend(object): @@ -26,27 +29,31 @@ class OsmBackend(object): if all(k in kwargs for k in ('username', 'password', 'project_id')): username = kwargs['username'] password = kwargs['password'] - project_id = kwargs['project_id'] client = Client() result = client.auth(kwargs) - print "######" - print result - if 'error' in result and result['error'] == True: - return None + if 'error' in result and result['error'] is True: + raise OSMAuthException(result['data']) else: try: user = OsmUser.objects.get(username=username) + user.psw = password + user.token=result['data']['id'] + user.project_id=result['data']['project_id'] + user.token_expires=result['data']['expires'] + user.session = result['data'] + user.save() except OsmUser.DoesNotExist: - # Create a new user. There's no need to set a password - # we will keep just some preferences - user = OsmUser(username=username) - + user = OsmUser(username=username, psw=password, token=result['data']['id'], + project_id=result['data']['project_id'], + token_expires=result['data']['expires']) + user.session = result['data'] user.save() - user.session = result['data'] + + return user return None diff --git a/authosm/exceptions.py b/authosm/exceptions.py new file mode 100644 index 0000000..c0b061d --- /dev/null +++ b/authosm/exceptions.py @@ -0,0 +1,19 @@ +# +# Copyright 2018 EveryUP Srl +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +class OSMAuthException(Exception): + pass diff --git a/authosm/models.py b/authosm/models.py index c48f738..36a7b1b 100644 --- a/authosm/models.py +++ b/authosm/models.py @@ -18,8 +18,10 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User, AbstractBaseUser, BaseUserManager, PermissionsMixin from django.utils import timezone -from lib.osm.osmclient.client import Client -import uuid + +from authosm.exceptions import OSMAuthException +from lib.osm.osmclient.clientv2 import Client +import utils class OsmUserManager(BaseUserManager): @@ -58,24 +60,54 @@ class AbstractOsmUser(AbstractBaseUser, PermissionsMixin): * is_superuser """ - username = models.CharField(_('username'), max_length=255, unique=True, db_index=True) - current_project = models.CharField(_('project_id'), max_length=255) - token_project = models.CharField(_('token'), max_length=36) + username = models.CharField(_('username'), primary_key=True, max_length=255, unique=True, db_index=True) + is_admin = models.BooleanField(_('admin status'), default=False) is_basic_user = models.BooleanField(_('basic_user status'), default=False) + current_project = models.CharField(_('project_id'), max_length=255) + + psw = models.CharField(_('psw'), max_length=36) + token = models.CharField(_('token'), max_length=36) + project_id = models.CharField(_('project_id'), max_length=36) + token_expires = models.FloatField(_('token_expires'), max_length=36) objects = OsmUserManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = [] + @property + def is_authenticated(self): + """Checks for a valid authentication.""" + if self.token is not None and utils.is_token_valid({'expires': self.token_expires}): + return True + else: + return False + + def get_token(self): + if self.is_authenticated: + return {'id': self.token, 'expires': self.token_expires, 'project_id': self.project_id} + return None + + def switch_project(self, project_id): + client = Client() + result = client.switch_project({'project_id': project_id, 'username': self.username, 'password': self.psw}) + if 'error' in result and result['error'] is True: + raise OSMAuthException(result['data']) + else: + self.token = result['data']['id'] + self.project_id = result['data']['project_id'] + self.token_expires = result['data']['expires'] + self.save() + return True + return False + class Meta: verbose_name = _('custom user') verbose_name_plural = _('custom users') abstract = True - class OsmUser(AbstractOsmUser): """ Concrete class of AbstractCustomUser. @@ -87,6 +119,3 @@ class OsmUser(AbstractOsmUser): class Meta(AbstractOsmUser.Meta): swappable = 'AUTH_USER_MODEL' - def get_projects(self): - client = Client() - return [] diff --git a/authosm/utils.py b/authosm/utils.py new file mode 100644 index 0000000..c1a5d50 --- /dev/null +++ b/authosm/utils.py @@ -0,0 +1,33 @@ +# +# Copyright 2018 EveryUP Srl +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import time +import authosm.models + + +def get_user(request): + user = authosm.models.OsmUser.objects.get(username=request.session['_auth_user_id']) + return user + + +def is_token_valid(token): + + expiration = token['expires'] if 'expires' in token \ + and isinstance(token['expires'], (int, long, float, complex)) else None + if expiration is None: + return False + + return expiration > time.time() diff --git a/authosm/views.py b/authosm/views.py index 5ce8765..65b1ed6 100644 --- a/authosm/views.py +++ b/authosm/views.py @@ -30,14 +30,20 @@ def user_login(request): print request.POST.get('password') next_page = request.POST.get('next') next_page = urllib.unquote(next_page).decode('iso-8859-2') - user = authenticate(username=request.POST.get('username'), - password=request.POST.get('password'), - project_id=request.POST.get('project_id')) + try: + user = authenticate(username=request.POST.get('username'), + password=request.POST.get('password'), + project_id=request.POST.get('project_id')) + except Exception as e: + print e + res = HttpResponseRedirect('/auth') + res.set_cookie('logout_reason', '', max_age=10) + return res + if user and user.is_active: - if user.is_authenticated(): + if user.is_authenticated: login(request, user) request.session['token'] = user.session - print request.session['token'] if next_page == "" or next_page is None: return HttpResponseRedirect('/home') else: diff --git a/bower.json b/bower.json index d651b65..20c60cb 100644 --- a/bower.json +++ b/bower.json @@ -21,7 +21,7 @@ "dependencies": { "admin-lte": "^2.4.3", "codemirror": "^5.36.0", - "d3": "^5.1.0", + "d3": "^4", "bootbox.js": "bootbox#^4.4.0", "components-font-awesome": "^5.0.6" } diff --git a/instancehandler/template/instance_list.html b/instancehandler/template/instance_list.html index 9baae4c..5888519 100644 --- a/instancehandler/template/instance_list.html +++ b/instancehandler/template/instance_list.html @@ -25,7 +25,7 @@ {% block breadcrumb_body %} {{ block.super }} -
  • {{ type|upper }} Instances
  • +
  • {{ type|upper }} Instances
  • {% endblock %} {% block content_body %} diff --git a/instancehandler/template/instance_list_ns.html b/instancehandler/template/instance_list_ns.html index dd84d47..2da870d 100644 --- a/instancehandler/template/instance_list_ns.html +++ b/instancehandler/template/instance_list_ns.html @@ -7,7 +7,7 @@ @@ -58,14 +58,20 @@
    + @@ -74,21 +80,21 @@ diff --git a/instancehandler/template/instance_list_vnf.html b/instancehandler/template/instance_list_vnf.html index 4aa084e..410fcb0 100644 --- a/instancehandler/template/instance_list_vnf.html +++ b/instancehandler/template/instance_list_vnf.html @@ -29,7 +29,7 @@ {{ i|get:"_id" }} - {{ i|get:"vnfd-ref" }} + {{ i|get:"vnfd-ref" }} {{ i|get:"member-vnf-index-ref" }} {{ i|get:"nsr-id-ref" }} {{ i|get:"created-time"|get_date }} @@ -37,7 +37,7 @@
    diff --git a/instancehandler/template/instance_operations_list.html b/instancehandler/template/instance_operations_list.html index 4115e0d..e72b56c 100644 --- a/instancehandler/template/instance_operations_list.html +++ b/instancehandler/template/instance_operations_list.html @@ -25,7 +25,7 @@ {% block breadcrumb_body %} {{ block.super }} -
  • Instances
  • +
  • Instances
  • {% endblock %} {% block content_body %} @@ -74,7 +74,7 @@
    diff --git a/instancehandler/template/instance_topology_view.html b/instancehandler/template/instance_topology_view.html new file mode 100644 index 0000000..627770d --- /dev/null +++ b/instancehandler/template/instance_topology_view.html @@ -0,0 +1,79 @@ +{% extends "base.html" %} +{% load get %} +{% load staticfiles %} + + + +{% block head_block %} + {{ block.super }} + + + + + + + +{% endblock %} +{% block title_header_big %} + {{ block.super }} +{% endblock %} +{% block left_sidebar %} + {% include 'osm/osm_project_left_sidebar.html' %} +{% endblock %} + + +{% block breadcrumb_body %} + {{ block.super }} +
  • {{ type|upper }} Instances
  • +
  • {{ type|upper }} Topology
  • +{% endblock %} + +{% block content_body %} + {{ block.super }} + {% csrf_token %} + +
    +
    + {% block topology_toolbar %} + {{ block.super }} + {% include 'topology_toolbar_instance.html' %} + {% endblock %} +
    + +
    +
    +
    + + + +{% endblock %} + +{% block resource_block %} + {{ block.super }} + + + + + + + + + + + + + + + + + + + +{% endblock %} + +{% block footer %} + {% include "footer.html" %} +{% endblock %} diff --git a/instancehandler/template/modal/instance_create.html b/instancehandler/template/modal/instance_create.html index f1c24d9..384e782 100644 --- a/instancehandler/template/modal/instance_create.html +++ b/instancehandler/template/modal/instance_create.html @@ -6,7 +6,7 @@
    -
    {% csrf_token %} diff --git a/instancehandler/urls.py b/instancehandler/urls.py index 054c3ba..d097b60 100644 --- a/instancehandler/urls.py +++ b/instancehandler/urls.py @@ -21,6 +21,7 @@ urlpatterns = [ url(r'^(?P[ns|vnf]+)/list/', views.list, name='list'), url(r'^create/', views.create, name='create'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/delete$', views.delete, name='delete'), + url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/topology', views.show_topology, name='show_topology'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/action$', views.action, name='action'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/operation$', views.ns_operations, name='ns_operations'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/operation/(?P[0-9a-z-]+)', views.ns_operation, name='ns_operation'), diff --git a/instancehandler/views.py b/instancehandler/views.py index 39837d7..2148b7b 100644 --- a/instancehandler/views.py +++ b/instancehandler/views.py @@ -21,18 +21,20 @@ import yaml import json import logging from lib.osm.osmclient.clientv2 import Client +import authosm.utils as osmutils logging.basicConfig(level=logging.DEBUG) log = logging.getLogger('instancehandler/view.py') - @login_required -def list(request, project_id=None, type=None): +def list(request, type=None): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() if type == 'ns': - instance_list = client.ns_list(request.session['token']) + instance_list = client.ns_list(user.get_token()) elif type == 'vnf': - instance_list = client.vnf_list(request.session['token']) + instance_list = client.vnf_list(user.get_token()) result = {'instances': instance_list['data'] if instance_list and instance_list['error'] is False else [], 'type': type, 'project_id': project_id} @@ -41,7 +43,7 @@ def list(request, project_id=None, type=None): @login_required -def create(request, project_id=None): +def create(request): result = {} ns_data = { "nsName": request.POST.get('nsName', 'WithoutName'), @@ -73,32 +75,36 @@ def create(request, project_id=None): ns_data["vnf"] = ns_config["vnf"] print ns_data + user = osmutils.get_user(request) client = Client() - result = client.ns_create(request.session['token'], ns_data) + result = client.ns_create(user.get_token(), ns_data) return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', - project_id=project_id) + ) @login_required -def ns_operations(request, project_id=None, instance_id=None, type=None): +def ns_operations(request, instance_id=None, type=None): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() - op_list = client.ns_op_list(request.session['token'], instance_id) + op_list = client.ns_op_list(user.get_token(), instance_id) return __response_handler(request, {'operations': op_list['data'] if op_list and op_list['error'] is False else [], 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html') @login_required -def ns_operation(request, op_id, project_id=None, instance_id=None, type=None): +def ns_operation(request, op_id, instance_id=None, type=None): + user = osmutils.get_user(request) client = Client() - result = client.ns_op(request.session['token'], op_id) + result = client.ns_op(user.get_token(), op_id) return __response_handler(request, result['data']) @login_required -def action(request, project_id=None, instance_id=None, type=None): +def action(request, instance_id=None, type=None): + user = osmutils.get_user(request) client = Client() - # result = client.ns_action(instance_id, action_payload) primitive_param_keys = request.POST.getlist('primitive_params_name') primitive_param_value = request.POST.getlist('primitive_params_value') @@ -108,7 +114,7 @@ def action(request, project_id=None, instance_id=None, type=None): "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0} } - result = client.ns_action(request.session['token'], instance_id, action_payload) + result = client.ns_action(user.get_token(), instance_id, action_payload) print result if result['error']: return __response_handler(request, result['data'], url=None, @@ -119,32 +125,191 @@ def action(request, project_id=None, instance_id=None, type=None): @login_required -def delete(request, project_id=None, instance_id=None, type=None): +def delete(request, instance_id=None, type=None): force = bool(request.GET.get('force', False)) result = {} + user = osmutils.get_user(request) client = Client() - result = client.ns_delete(request.session['token'], instance_id, force) + result = client.ns_delete(user.get_token(), instance_id, force) print result return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', - project_id=project_id) + ) + + +def show_topology(request, instance_id=None, type=None): + user = osmutils.get_user(request) + project_id = user.project_id + raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') + if 'application/json' in raw_content_types: + result = {'vertices': [ + {"info": {"type": "vnf", "property": {"custom_label": ""}, + "group": []}, "id": "ping"}, + {"info": {"type": "vnf", "property": {"custom_label": ""}, + "group": []}, "id": "pong"}, + {"info": {"type": "vdu", "property": {"custom_label": ""}, + "group": ['pong']}, "id": "pong/ubuntu"}, + {"info": {"type": "vdu", "property": {"custom_label": ""}, + "group": ['ping']}, "id": "ping/ubuntu"}, + {"info": {"type": "cp", "property": {"custom_label": ""}, + "group": ['ping']}, "id": "ping/cp0"}, + {"info": {"type": "cp", "property": {"custom_label": ""}, + "group": ['ping']}, "id": "ping/cp1"}, + {"info": {"type": "cp", "property": {"custom_label": ""}, + "group": ['pong']}, "id": "pong/cp0"}, + {"info": {"type": "cp", "property": {"custom_label": ""}, + "group": ['pong']}, "id": "pong/cp1"}, + {"info": {"type": "ns_vl", "property": {"custom_label": ""}, + "group": []}, "id": "mgmt_vl"}, + ], + 'edges': [ + # {"source": "ping", "group": [], "target": "ping/cp0", "view": "Data"}, + {"source": "pong/ubuntu", "group": ['pong'], "target": "pong/cp0", "view": "vnf"}, + {"source": "ping/ubuntu", "group": ['ping'], "target": "ping/cp0", "view": "vnf"}, + {"source": "pong/ubuntu", "group": ['pong'], "target": "pong/cp1", "view": "vnf"}, + {"source": "ping/ubuntu", "group": ['ping'], "target": "ping/cp1", "view": "vnf"}, + {"source": "pong", "group": [], "target": "mgmt_vl", "view": "ns"}, + {"source": "ping", "group": [], "target": "mgmt_vl", "view": "ns"}, + ], 'graph_parameters': [], + 'model': { + "layer": { + + "ns": { + "nodes": { + "vnf": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + }, + "expands": "vnf" + }, + "ns_vl": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + } + }, + + }, + "allowed_edges": { + "ns_vl": { + "destination": { + "vnf": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + } + } + }, + "vnf": { + "destination": { + "ns_vl": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + }, + + } + } + + } + }, + "vnf": { + "nodes": { + "vdu": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + } + }, + "cp": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + } + }, + + }, + "allowed_edges": { + "vdu": { + "destination": { + "cp": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + } + } + }, + "cp": { + "destination": { + "vdu": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + } + } + } + } + }, + "name": "OSM", + "version": 1, + "nodes": { + "vnf": { + "label": "vnf" + }, + "ns_vl": { + "label": "vl" + }, + "cp": { + "label": "cp" + }, + "vdu": { + "label": "vdu" + } + }, + "description": "osm" + } + }} + return __response_handler(request, result) + else: + result = {'type': type, 'project_id': project_id, 'instance_id': instance_id} + return __response_handler(request, result, 'instance_topology_view.html') @login_required -def show(request, project_id=None, instance_id=None, type=None): +def show(request, instance_id=None, type=None): # result = {} + user = osmutils.get_user(request) + project_id = user.project_id client = Client() if type == 'ns': - result = client.ns_get(request.session['token'], instance_id) + result = client.ns_get(user.get_token(), instance_id) elif type == 'vnf': - result = client.vnf_get(request.session['token'], instance_id) + result = client.vnf_get(user.get_token(), instance_id) print result return __response_handler(request, result) @login_required -def export_metric(request, project_id=None, instance_id=None, type=None): +def export_metric(request, instance_id=None, type=None): metric_data = request.POST.dict() - + user = osmutils.get_user(request) + project_id = user.project_id client = Client() keys = ["collection_period", "vnf_member_index", @@ -154,7 +319,7 @@ def export_metric(request, project_id=None, instance_id=None, type=None): "collection_unit"] metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items())) - result = client.ns_metric_export(request.session['token'], instance_id, metric_data) + result = client.ns_metric_export(user.get_token(), instance_id, metric_data) if result['error']: print result @@ -165,9 +330,11 @@ def export_metric(request, project_id=None, instance_id=None, type=None): @login_required -def create_alarm(request, project_id=None, instance_id=None, type=None): +def create_alarm(request, instance_id=None, type=None): metric_data = request.POST.dict() print metric_data + user = osmutils.get_user(request) + project_id = user.project_id client = Client() keys = ["threshold_value", @@ -181,7 +348,7 @@ def create_alarm(request, project_id=None, instance_id=None, type=None): "severity"] metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items())) - result = client.ns_alarm_create(request.session['token'], instance_id, metric_data) + result = client.ns_alarm_create(user.get_token(), instance_id, metric_data) if result['error']: print result return __response_handler(request, result['data'], url=None, diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index 9acb590..6555071 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -28,7 +28,23 @@ class Client(object): try: r = requests.post(token_url, json=args, verify=False, headers=headers) except Exception as e: - print "saltata" + 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 switch_project(self, args): + result = {'error': True, 'data': ''} + token_url = "{0}/{1}".format(self._base_path, self._token_endpoint) + headers = {"Content-Type": "application/yaml", "accept": "application/json"} + try: + r = requests.post(token_url, json=args, verify=False, headers=headers) + except Exception as e: log.exception(e) result['data'] = str(e) return result diff --git a/projecthandler/middleware.py b/projecthandler/middleware.py new file mode 100644 index 0000000..4880ebe --- /dev/null +++ b/projecthandler/middleware.py @@ -0,0 +1,6 @@ + +class OsmProjectMiddleware(object): + def process_view(self, request, view_func, view_args, view_kwargs): + print "OsmProjectMiddleware", view_func, view_args, view_kwargs + + return None \ No newline at end of file diff --git a/projecthandler/template/project/descriptor/descriptor_view_base.html b/projecthandler/template/project/descriptor/descriptor_view_base.html index 3635d66..e97649d 100644 --- a/projecthandler/template/project/descriptor/descriptor_view_base.html +++ b/projecthandler/template/project/descriptor/descriptor_view_base.html @@ -125,7 +125,7 @@ function goToGraph() { - window.location.href = '/projects/{{project_id}}/graph?type={{descriptor_type}}&id={{descriptor_id}}' + window.location.href = '/projects/graph?type={{descriptor_type}}&id={{descriptor_id}}' } {% endblock %} diff --git a/projecthandler/template/project/osm/descriptor/descriptor_new.html b/projecthandler/template/project/osm/descriptor/descriptor_new.html index 799026b..7711b57 100644 --- a/projecthandler/template/project/osm/descriptor/descriptor_new.html +++ b/projecthandler/template/project/osm/descriptor/descriptor_new.html @@ -14,8 +14,8 @@ Onboard new {{ descriptor_type }} package {% block breadcrumb_body %} {{ block.super }} -
  • {{project_id}}
  • -
  • {{ descriptor_type }} Descriptors
  • +
  • {{project_id}}
  • +
  • {{ descriptor_type }} Descriptors
  • Create Descriptor
  • {% endblock %} @@ -50,9 +50,9 @@ Onboard new {{ descriptor_type }} package {{ block.super }} diff --git a/projecthandler/template/project/osm/osm_project_details.html b/projecthandler/template/project/osm/osm_project_details.html index ed3e0fa..b0a1203 100644 --- a/projecthandler/template/project/osm/osm_project_details.html +++ b/projecthandler/template/project/osm/osm_project_details.html @@ -44,7 +44,7 @@

    {{ project_overview.nsd }}

    NS packages

    - Open list Open list
    @@ -52,7 +52,7 @@

    {{ project_overview.vnfd }}

    VNF packages

    - Open list Open list
    @@ -62,7 +62,7 @@

    {{ project_overview.ns }}

    NS Instances

    - Open list Open list @@ -71,7 +71,7 @@

    {{ project_overview.vnf }}

    VNF Instances

    - Open list Open list diff --git a/projecthandler/template/project/osm/osm_project_left_sidebar.html b/projecthandler/template/project/osm/osm_project_left_sidebar.html index 6399b11..b07c432 100644 --- a/projecthandler/template/project/osm/osm_project_left_sidebar.html +++ b/projecthandler/template/project/osm/osm_project_left_sidebar.html @@ -8,14 +8,14 @@
  • Home
  • PROJECT
  • - {% url "projects:open_project" project_id=project_id as project_url %} + {% url "projects:open_project" as project_url %}
  • - + Overview
  • - {% url "projects:list_descriptors" project_id=project_id descriptor_type="nsd" as nsd_list_url %} - {% url "projects:list_descriptors" project_id=project_id descriptor_type='vnfd' as vnfd_list_url %} + {% url "projects:list_descriptors" descriptor_type="nsd" as nsd_list_url %} + {% url "projects:list_descriptors" descriptor_type='vnfd' as vnfd_list_url %} - {% url "projects:instances:list" project_id=project_id type='ns' as instance_ns_list_url %} - {% url "projects:instances:list" project_id=project_id type='vnf' as instance_vnf_list_url %} + {% url "projects:instances:list" type='ns' as instance_ns_list_url %} + {% url "projects:instances:list" type='vnf' as instance_vnf_list_url %} - {% url "projects:sdns:list" project_id=project_id as sdn_list_url %} + {% url "projects:sdns:list" as sdn_list_url %}
  • - + SDN Controllers
  • - {% url "projects:vims:list" project_id=project_id as vim_list_url %} + {% url "projects:vims:list" as vim_list_url %}
  • - + VIM Accounts
  • diff --git a/projecthandler/template/project/project_delete.html b/projecthandler/template/project/project_delete.html index 432e1ff..4ae063e 100644 --- a/projecthandler/template/project/project_delete.html +++ b/projecthandler/template/project/project_delete.html @@ -14,7 +14,7 @@ {% block breadcrumb_body %} {{ block.super }} {% if project_id %} -
  • {{project_overview_data.name}}
  • +
  • {{project_overview_data.name}}
  • Delete Project
  • {% endif %} {% endblock %} diff --git a/projecthandler/template/project/project_details.html b/projecthandler/template/project/project_details.html index e944e0d..aac2983 100644 --- a/projecthandler/template/project/project_details.html +++ b/projecthandler/template/project/project_details.html @@ -25,7 +25,7 @@ {% block breadcrumb_body %} {{ block.super }}
  • Projects
  • -
  • {{project_overview.name}}
  • +
  • {{project_overview.name}}
  • {% endblock %} {% block content_body %} diff --git a/projecthandler/template/project/project_graph_base.html b/projecthandler/template/project/project_graph_base.html index 7d344da..282afb4 100644 --- a/projecthandler/template/project/project_graph_base.html +++ b/projecthandler/template/project/project_graph_base.html @@ -1,58 +1,81 @@ {% extends "base.html" %} - +{% load get %} {% load staticfiles %} + + {% block head_block %} - {{ block.super }} - - + {{ block.super }} + + + + + + + {% endblock %} {% block title_header_big %} {{ block.super }} {% endblock %} {% block left_sidebar %} - + {% include 'osm/osm_project_left_sidebar.html' %} {% endblock %} {% block breadcrumb_body %} - {{ block.super }} -
  • {{project_overview_data.name}}
  • + {{ block.super }} +
  • Composer
  • {% endblock %} {% block content_body %} - {{ block.super }} - {% csrf_token %} -
    -
    - {% block topology_toolbar %} - - - {% endblock %} -
    - + {{ block.super }} + {% csrf_token %} + {% include 'osm/descriptor/modal/create_link_chooser.html' %} +{% include 'osm/descriptor/modal/choose_node_id.html' %} +{% include 'osm/modal/modal_keyboard_info.html' %} +
    +
    + {% block topology_toolbar %} + {{ block.super }} + {% include 'topology_toolbar.html' %} + {% endblock %} +
    + +
    -
    + {% endblock %} {% block resource_block %} - {{ block.super }} + {{ block.super }} + + + - + + - - - - + + + + + -{% endblock %} + + + -{% block header %} {% endblock %} +{% block footer %} + {% include "footer.html" %} +{% endblock %} diff --git a/projecthandler/template/project/projectlist.html b/projecthandler/template/project/projectlist.html index 6339679..6e3793d 100644 --- a/projecthandler/template/project/projectlist.html +++ b/projecthandler/template/project/projectlist.html @@ -57,13 +57,13 @@ - {{ p.name }} + {{ p.name }} {{ p|get_sub:"_admin,modified"|get_date}} {{ p|get_sub:"_admin,created"|get_date}} - Delete + Delete {% endfor %} diff --git a/projecthandler/template/project/repository/repo_base_left_sidebar.html b/projecthandler/template/project/repository/repo_base_left_sidebar.html deleted file mode 100644 index 3367eeb..0000000 --- a/projecthandler/template/project/repository/repo_base_left_sidebar.html +++ /dev/null @@ -1,12 +0,0 @@ -{% extends "left_sidebar_base.html" %} - -{% block left_sidebar_base %} -
  • REPOSITORY
  • - {% url "repos:repos_list" as repo_list_url %} -
  • Ropository Registered
  • - - {% block li_list %} - - - {% endblock %} -{% endblock %} diff --git a/projecthandler/template/project/repository/repo_create_modal.html b/projecthandler/template/project/repository/repo_create_modal.html deleted file mode 100644 index 6df53b5..0000000 --- a/projecthandler/template/project/repository/repo_create_modal.html +++ /dev/null @@ -1,43 +0,0 @@ - \ No newline at end of file diff --git a/projecthandler/template/project/repository/repo_list.html b/projecthandler/template/project/repository/repo_list.html deleted file mode 100644 index 1f0a8f3..0000000 --- a/projecthandler/template/project/repository/repo_list.html +++ /dev/null @@ -1,120 +0,0 @@ -{% extends "base.html" %} - -{% load staticfiles %} - -{% block head_block %} -{{ block.super }} - - -{% endblock %} - -{% block title_header_big %} -{{ block.super }} -{% endblock %} - -{% block left_sidebar %} - -{% include "repository/repo_base_left_sidebar.html" %} -{% endblock %} - - -{% block breadcrumb_body %} -{{ block.super }} - -{% endblock %} - -{% block content_body %} -{{ block.super }} -{% csrf_token %} -
    - -
    - -
    -
    -

    Registered Repository

    - -
    -
    - - - - - - - - - - - - {% for repo in repos %} - - - - - - - - - {% endfor %} - -
    NameUrlLast UpdateActions
    - {{ repo.name }} - - {{ repo.base_url }} - {{ repo.last_update }} -
    - - - -
    -
    -
    -
    -
    - -
    -{% include 'repository/repo_create_modal.html' %} - -{% endblock %} - -{% block resource_block %} -{{ block.super }} - - - -{% endblock %} \ No newline at end of file diff --git a/projecthandler/urls/project.py b/projecthandler/urls/project.py index 2054656..1e3b24e 100644 --- a/projecthandler/urls/project.py +++ b/projecthandler/urls/project.py @@ -18,24 +18,26 @@ from django.conf.urls import url, include from projecthandler import views urlpatterns = [ + url(r'^$', views.open_project, name='open_project'), url(r'^list/', views.user_projects, name='projects_list'), url(r'^new/', views.create_new_project, name='new_project'), - url(r'^(?P\w+)/$', views.open_project, name='open_project'), - url(r'^(?P\w+)/sdn/', include('sdnctrlhandler.urls', namespace='sdns'), name='sdns_base'), - url(r'^(?P\w+)/vims/', include('vimhandler.urls', namespace='vims'), name='vims_base'), - url(r'^(?P\w+)/instances/', include('instancehandler.urls', namespace='instances'), name='instances_base'), - url(r'^(?P\w+)/delete$', views.delete_project, name='delete_project'), - url(r'^(?P\w+)/descriptors/(?P\w+)/list$', views.show_descriptors, name='list_descriptors'), - url(r'^(?P\w+)/descriptors/(?P\w+)/(?P[-\w]+)(/$)', + url(r'^delete$', views.delete_project, name='delete_project'), + url(r'^switch/(?P\w+)', views.switch_project, name='switch_project'), + url(r'^descriptors/(?P\w+)/list$', views.show_descriptors, name='list_descriptors'), + url(r'^descriptors/(?P\w+)/(?P[-\w]+)(/$)', views.edit_descriptor, name='edit_descriptor'), - url(r'^(?P\w+)/descriptors/(?P\w+)/(?P[-\w]+)/delete$', + url(r'^descriptors/(?P\w+)/(?P[-\w]+)/delete$', views.delete_descriptor, name='delete_descriptor'), - url(r'^(?P\w+)/descriptors/(?P\w+)/(?P[-\w]+)/action/(?P[-\w]+)', + url(r'^descriptors/(?P\w+)/(?P[-\w]+)/action/(?P[-\w]+)', views.custom_action, name='custom_action'), - url(r'^(?P\w+)/descriptors/(?P\w+)/new$', views.new_descriptor, + url(r'^descriptors/(?P\w+)/new$', views.new_descriptor, name='new_descriptor'), - + url(r'^composer$', views.open_composer, name='open_composer'), + url(r'^availablenodes', views.get_available_nodes, name='get_available_nodes'), + url(r'^sdn/', include('sdnctrlhandler.urls', namespace='sdns'), name='sdns_base'), + url(r'^vims/', include('vimhandler.urls', namespace='vims'), name='vims_base'), + url(r'^instances/', include('instancehandler.urls', namespace='instances'), name='instances_base'), ] \ No newline at end of file diff --git a/projecthandler/views.py b/projecthandler/views.py index 0d50e10..144aa5b 100644 --- a/projecthandler/views.py +++ b/projecthandler/views.py @@ -20,11 +20,12 @@ import logging import yaml from django.contrib.auth.decorators import login_required from django.http import HttpResponse, JsonResponse -from django.middleware.csrf import get_token from django.shortcuts import render, redirect + +from authosm.exceptions import OSMAuthException from lib.util import Util -from projecthandler.osm_model import OsmProject from lib.osm.osmclient.clientv2 import Client +import authosm.utils as osmutils logging.basicConfig(level=logging.DEBUG) @@ -39,11 +40,12 @@ def home(request): @login_required def create_new_project(request): if request.method == 'POST': + user = osmutils.get_user(request) client = Client() new_project_dict = request.POST.dict() keys = ["name"] project_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_project_dict.items())) - result = client.project_create(request.session['token'], project_data) + result = client.project_create(user.get_token(), project_data) if isinstance(result, dict) and 'error' in result and result['error']: print result return __response_handler(request, result['data'], url=None, @@ -54,23 +56,208 @@ def create_new_project(request): @login_required def user_projects(request): + user = osmutils.get_user(request) client = Client() - result = client.project_list(request.session['token']) + result = client.project_list(user.get_token()) return render(request, 'projectlist.html', { 'projects': result['data'] if result and result['error'] is False else [], }) +def open_composer(request): + user = osmutils.get_user(request) + project_id = user.project_id + result = {'project_id': project_id, + 'vertices': [ + {"info": {"type": "vnf", "property": {"custom_label": ""}, + "group": []}, "id": "vm"}, + {"info": {"type": "vnf", "property": {"custom_label": ""}, + "group": []}, "id": "vlan"}, + {"info": {"type": "vnf", "property": {"custom_label": ""}, + "group": []}, "id": "firewall"}, + {"info": {"type": "vnf", "property": {"custom_label": ""}, + "group": []}, "id": "ping"}, + + {"info": {"type": "ns_vl", "property": {"custom_label": ""}, + "group": []}, "id": "vl1"}, + {"info": {"type": "ns_vl", "property": {"custom_label": ""}, + "group": []}, "id": "vl2"}, + {"info": {"type": "ns_vl", "property": {"custom_label": ""}, + "group": []}, "id": "vl3"}, + ], + 'edges': [ + {"source": "vm", "group": [], "target": "vl3", "view": "ns"}, + {"source": "vlan", "group": [], "target": "vl3", "view": "ns"}, + {"source": "vlan", "group": [], "target": "vl1", "view": "ns"}, + {"source": "firewall", "group": [], "target": "vl1", "view": "ns"}, + {"source": "firewall", "group": [], "target": "vl2", "view": "ns"}, + {"source": "ping", "group": [], "target": "vl2", "view": "ns"}, + ], + 'model': { + "layer": { + + "ns": { + "nodes": { + "vnf": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + }, + "expands": "vnf" + }, + "ns_vl": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + } + }, + + }, + "allowed_edges": { + "ns_vl": { + "destination": { + "vnf": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + } + } + }, + "vnf": { + "destination": { + "ns_vl": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + }, + + } + } + + } + }, + "vnf": { + "nodes": { + "vdu": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + } + }, + "cp": { + "addable": { + "callback": "addNode" + }, + "removable": { + "callback": "removeNode" + } + }, + + }, + "allowed_edges": { + "vdu": { + "destination": { + "cp": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + } + } + }, + "cp": { + "destination": { + "vdu": { + "callback": "addLink", + "direct_edge": False, + "removable": { + "callback": "removeLink" + } + } + } + } + } + }, + "name": "OSM", + "version": 1, + "nodes": { + "vnf": { + "label": "vnf" + }, + "ns_vl": { + "label": "vl" + }, + "cp": { + "label": "cp" + }, + "vdu": { + "label": "vdu" + } + }, + "description": "osm", + "callback": { + "addNode": { + "file": "osm_controller.js", + "class": "OsmController" + }, + "removeNode": { + "file": "osm_controller.js", + "class": "OsmController" + }, + "addLink": { + "file": "osm_controller.js", + "class": "OsmController" + }, + "removeLink": { + "file": "osm_controller.js", + "class": "OsmController" + } + } + + } + }} + return __response_handler(request, result, 'project_graph_base.html') + + +def get_available_nodes(request): + + params = request.GET.dict() + nodes = { + 'ns': [{"types": [{"name": "Generic", "id": "vnf"}, + {"name": "ping", "id": "vnf"}, + {"name": "pong", "id": "vnf"}, + {"name": "hackfest1-vm", "id": "vnf"}], "category_name": "Vnf"}, + {"types": [{"name": "VL", "id": "ns_vl"}], "category_name": "VirtualLink"}], + 'vnf': [{"types": [{"name": "VDU", "id": "vdu"}], "category_name": "Vdu"}, + {"types": [{"name": "CP", "id": "cp"}], "category_name": "CP"}] + } + + return __response_handler(request, nodes[params['layer']]) + + @login_required -def open_project(request, project_id=None): +def open_project(request): + user = osmutils.get_user(request) + project_id = user.project_id try: - user = request.user + client = Client() ##TODO change with adhoc api call - nsd = client.nsd_list(request.session['token']) - vnfd = client.vnfd_list(request.session['token']) - ns = client.ns_list(request.session['token']) - vnf = client.vnf_list(request.session['token']) + nsd = client.nsd_list(user.get_token()) + vnfd = client.vnfd_list(user.get_token()) + ns = client.ns_list(user.get_token()) + vnf = client.vnf_list(user.get_token()) project_overview = { 'owner': user.username, 'name': project_id, @@ -92,9 +279,11 @@ def open_project(request, project_id=None): @login_required -def delete_project(request, project_id=None): +def delete_project(request): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() - result = client.project_delete(request.session['token'], project_id) + result = client.project_delete(user.get_token(), project_id) if isinstance(result, dict) and 'error' in result and result['error']: print result return __response_handler(request, result['data'], url=None, @@ -104,14 +293,23 @@ def delete_project(request, project_id=None): @login_required -def show_descriptors(request, project_id=None, descriptor_type=None): +def switch_project(request, project_id): + user = osmutils.get_user(request) + user.switch_project(project_id) + return redirect('projects:open_project') + + +@login_required +def show_descriptors(request, descriptor_type=None): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() print request.GET.dict() try: if descriptor_type == 'nsd': - descriptors = client.nsd_list(request.session['token']) + descriptors = client.nsd_list(user.get_token()) elif descriptor_type == 'vnfd': - descriptors = client.vnfd_list(request.session['token']) + descriptors = client.vnfd_list(user.get_token()) except Exception as e: log.exception(e) descriptors = [] @@ -126,14 +324,15 @@ def show_descriptors(request, project_id=None, descriptor_type=None): @login_required -def delete_descriptor(request, project_id=None, descriptor_type=None, descriptor_id=None): - +def delete_descriptor(request, descriptor_type=None, descriptor_id=None): + user = osmutils.get_user(request) + project_id = user.project_id try: client = Client() if descriptor_type == 'nsd': - result = client.nsd_delete(request.session['token'], descriptor_id) + result = client.nsd_delete(user.get_token(), descriptor_id) elif descriptor_type == 'vnfd': - result = client.vnfd_delete(request.session['token'], descriptor_id) + result = client.vnfd_delete(user.get_token(), descriptor_id) except Exception as e: log.exception(e) result = {'error': True, 'data': str(e)} @@ -142,9 +341,9 @@ def delete_descriptor(request, project_id=None, descriptor_type=None, descriptor descriptors = {} try: if descriptor_type == 'nsd': - descriptors = client.nsd_list(request.session['token']) + descriptors = client.nsd_list(user.get_token()) elif descriptor_type == 'vnfd': - descriptors = client.vnfd_list(request.session['token']) + descriptors = client.vnfd_list(user.get_token()) except Exception as e: log.exception(e) @@ -161,8 +360,9 @@ def delete_descriptor(request, project_id=None, descriptor_type=None, descriptor @login_required -def new_descriptor(request, project_id=None, descriptor_type=None): - +def new_descriptor(request, descriptor_type=None): + user = osmutils.get_user(request) + project_id = user.project_id page = 'osm/descriptor/descriptor_new.html' if request.method == 'GET': request_id = request.GET.get('id', '') @@ -181,9 +381,9 @@ def new_descriptor(request, project_id=None, descriptor_type=None): try: client = Client() if descriptor_type == 'nsd': - result = client.nsd_onboard(request.session['token'], file_uploaded) + result = client.nsd_onboard(user.get_token(), file_uploaded) elif descriptor_type == 'vnfd': - result = client.vnfd_onboard(request.session['token'], file_uploaded) + result = client.vnfd_onboard(user.get_token(), file_uploaded) else: log.debug('Create descriptor: Unknown data type') result = {'error': True, 'data': 'Create descriptor: Unknown data type'} @@ -202,7 +402,9 @@ def new_descriptor(request, project_id=None, descriptor_type=None): @login_required -def edit_descriptor(request, project_id=None, descriptor_id=None, descriptor_type=None): +def edit_descriptor(request, descriptor_id=None, descriptor_type=None): + user = osmutils.get_user(request) + project_id = user.project_id if request.method == 'POST': print "edit_descriptor" new_data = request.POST.get('text'), @@ -216,14 +418,14 @@ def edit_descriptor(request, project_id=None, descriptor_id=None, descriptor_typ elif data_type == 'json': new_data = json.loads(request.POST.get('text')) print new_data - result = client.nsd_update(request.session['token'], descriptor_id, new_data) + result = client.nsd_update(user.get_token(), descriptor_id, new_data) elif descriptor_type == 'vnfd': if data_type == 'yaml': new_data = yaml.load(request.POST.get('text')) elif data_type == 'json': new_data = json.loads(request.POST.get('text')) print new_data - result = client.vnfd_update(request.session['token'], descriptor_id, new_data) + result = client.vnfd_update(user.get_token(), descriptor_id, new_data) else: log.debug('Update descriptor: Unknown data type') @@ -244,10 +446,10 @@ def edit_descriptor(request, project_id=None, descriptor_id=None, descriptor_typ try: client = Client() if descriptor_type == 'nsd': - result = client.nsd_get(request.session['token'], descriptor_id) + result = client.nsd_get(user.get_token(), descriptor_id) print result elif descriptor_type == 'vnfd': - result = client.vnfd_get(request.session['token'], descriptor_id) + result = client.vnfd_get(user.get_token(), descriptor_id) print result except Exception as e: @@ -269,14 +471,14 @@ def edit_descriptor(request, project_id=None, descriptor_id=None, descriptor_typ @login_required -def get_package_files_list(request, project_id, descriptor_id, descriptor_type): - files_list = [] +def get_package_files_list(request, descriptor_id, descriptor_type): + user = osmutils.get_user(request) try: client = Client() if descriptor_type == 'nsd': - artifacts_res = client.nsd_artifacts(request.session['token'], descriptor_id) + artifacts_res = client.nsd_artifacts(user.get_token(), descriptor_id) elif descriptor_type == 'vnfd': - artifacts_res = client.vnf_packages_artifacts(request.session['token'], descriptor_id) + artifacts_res = client.vnf_packages_artifacts(user.get_token(), descriptor_id) else: return False @@ -289,15 +491,16 @@ def get_package_files_list(request, project_id, descriptor_id, descriptor_type): return __response_handler(request, result) @login_required -def download_pkg(request, project_id, descriptor_id, descriptor_type): +def download_pkg(request, descriptor_id, descriptor_type): + user = osmutils.get_user(request) file_name = "osm_export.tar.gz" tar_pkg = None try: client = Client() if descriptor_type == 'nsd': - tar_pkg = client.get_nsd_pkg(request.session['token'], descriptor_id) + tar_pkg = client.get_nsd_pkg(user.get_token(), descriptor_id) elif descriptor_type == 'vnfd': - tar_pkg = client.get_vnfd_pkg(request.session['token'], descriptor_id) + tar_pkg = client.get_vnfd_pkg(user.get_token(), descriptor_id) except Exception as e: log.exception(e) @@ -309,10 +512,10 @@ def download_pkg(request, project_id, descriptor_id, descriptor_type): @login_required -def custom_action(request, project_id=None, descriptor_id=None, descriptor_type=None, action_name=None): +def custom_action(request, descriptor_id=None, descriptor_type=None, action_name=None): if request.method == 'GET': print "Custom action: " + action_name - return globals()[action_name](request, project_id, descriptor_id, descriptor_type) + return globals()[action_name](request, descriptor_id, descriptor_type) def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): diff --git a/sdnctrlhandler/template/sdn_create.html b/sdnctrlhandler/template/sdn_create.html index 60249a3..75d010d 100644 --- a/sdnctrlhandler/template/sdn_create.html +++ b/sdnctrlhandler/template/sdn_create.html @@ -19,7 +19,7 @@ {% block breadcrumb_body %} {{ block.super }} -
  • SDN Controllers
  • +
  • SDN Controllers
  • {% endblock %} {% block content_body %} @@ -27,7 +27,7 @@
    -
    + {% csrf_token %}
    @@ -120,7 +120,7 @@
    {% block boxes_toolbar %} diff --git a/template/topology_toolbar_instance.html b/template/topology_toolbar_instance.html new file mode 100644 index 0000000..be42390 --- /dev/null +++ b/template/topology_toolbar_instance.html @@ -0,0 +1,21 @@ +
    + + + + + + + + {% block topology_toolbar_buttons %} + + {% endblock %} + + +
    +{% block boxes_toolbar %} + +{% endblock %} + diff --git a/vimhandler/template/vim_create.html b/vimhandler/template/vim_create.html index 4368735..187dc87 100644 --- a/vimhandler/template/vim_create.html +++ b/vimhandler/template/vim_create.html @@ -19,7 +19,7 @@ {% block breadcrumb_body %} {{ block.super }} -
  • VIMS
  • +
  • VIMS
  • {% endblock %} {% block content_body %} @@ -27,7 +27,7 @@
    - + {% csrf_token %}
    @@ -175,7 +175,7 @@
    diff --git a/vimhandler/views.py b/vimhandler/views.py index abaab9c..bf88c11 100644 --- a/vimhandler/views.py +++ b/vimhandler/views.py @@ -18,8 +18,8 @@ from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.http import HttpResponse import json -#from lib.osm.osmclient.client import Client from lib.osm.osmclient.clientv2 import Client +import authosm.utils as osmutils import yaml import logging @@ -28,9 +28,11 @@ log = logging.getLogger('vimhandler.py') @login_required -def list(request, project_id): +def list(request): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() - result = client.vim_list(request.session['token']) + result = client.vim_list(user.get_token()) print result result = { "project_id": project_id, @@ -40,7 +42,9 @@ def list(request, project_id): @login_required -def create(request, project_id): +def create(request): + user = osmutils.get_user(request) + project_id = user.project_id result = {'project_id': project_id} if request.method == 'GET': return __response_handler(request, result, 'vim_create.html') @@ -70,23 +74,26 @@ def create(request, project_id): except Exception as e: # TODO return error on json.loads exception print e - result = client.vim_create(request.session['token'], vim_data) + result = client.vim_create(user.get_token(), vim_data) # TODO 'vim:show', to_redirect=True, vim_id=vim_id - return __response_handler(request, result, 'projects:vims:list', to_redirect=True, project_id=project_id) + return __response_handler(request, result, 'projects:vims:list', to_redirect=True, ) @login_required -def delete(request, project_id, vim_id=None): +def delete(request, vim_id=None): + user = osmutils.get_user(request) try: client = Client() - del_res = client.vim_delete(request.session['token'], vim_id) + del_res = client.vim_delete(user.get_token(), vim_id) except Exception as e: log.exception(e) - return __response_handler(request, {}, 'projects:vims:list', to_redirect=True, project_id=project_id) + return __response_handler(request, {}, 'projects:vims:list', to_redirect=True, ) @login_required -def show(request, project_id, vim_id=None): +def show(request, vim_id=None): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() - result = client.vim_get(request.session['token'], vim_id) + result = client.vim_get(user.get_token(), vim_id) print result if isinstance(result, dict) and 'error' in result and result['error']: return render(request, 'error.html')