From 3fcf21a3589d2e9f94b2de318d3dc0404f4be94b Mon Sep 17 00:00:00 2001 From: lombardofr Date: Mon, 11 Mar 2019 10:26:08 +0100 Subject: [PATCH] WIM handler Change-Id: I4e9f120d9dc8485db850461bb5eb2d0f66bea3d3 Signed-off-by: lombardofr --- .gitignore | 4 + build-debpkg.sh | 2 +- lib/osm/osmclient/clientv2.py | 70 ++++++ projecthandler/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../commands/delete_project_type.py | 14 -- .../management/commands/new_project_type.py | 14 -- .../project/osm/osm_project_left_sidebar.html | 7 + requirements.txt | 3 +- sf_t3d/settings.py | 4 +- sf_t3d/urls.py | 1 + vimhandler/tests.py => wimhandler/__init__.py | 10 +- vimhandler/models.py => wimhandler/apps.py | 10 +- wimhandler/template/modal/wim_create.html | 74 ++++++ wimhandler/template/modal/wim_details.html | 51 ++++ wimhandler/template/wim_list.html | 231 ++++++++++++++++++ vimhandler/admin.py => wimhandler/urls.py | 15 +- wimhandler/views.py | 109 +++++++++ 18 files changed, 569 insertions(+), 50 deletions(-) delete mode 100644 projecthandler/management/__init__.py delete mode 100644 projecthandler/management/commands/__init__.py delete mode 100644 projecthandler/management/commands/delete_project_type.py delete mode 100644 projecthandler/management/commands/new_project_type.py rename vimhandler/tests.py => wimhandler/__init__.py (73%) rename vimhandler/models.py => wimhandler/apps.py (80%) create mode 100644 wimhandler/template/modal/wim_create.html create mode 100644 wimhandler/template/modal/wim_details.html create mode 100644 wimhandler/template/wim_list.html rename vimhandler/admin.py => wimhandler/urls.py (61%) create mode 100644 wimhandler/views.py diff --git a/.gitignore b/.gitignore index 5af1e6d..5e05014 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,10 @@ instancehandler/migrations sdnctrlhandler/migrations authosm/migrations userhandler/migrations +packagehandler/migrations +netslicehandler/migrations +vimhandler/migrations +wimhandler/migrations #Deb package deb_dist/ diff --git a/build-debpkg.sh b/build-debpkg.sh index d1becda..6f05b94 100755 --- a/build-debpkg.sh +++ b/build-debpkg.sh @@ -15,7 +15,7 @@ # under the License. -PKG_DIRECTORIES="authosm descriptorhandler instancehandler lib projecthandler sdnctrlhandler sf_t3d static template userhandler vimhandler packagehandler netslicehandler" +PKG_DIRECTORIES="authosm descriptorhandler instancehandler lib projecthandler sdnctrlhandler sf_t3d static template userhandler vimhandler packagehandler netslicehandler wimhandler" PKG_FILES="bower.json django.ini LICENSE manage.py nginx-app.conf README.md requirements.txt supervisor-app.conf .bowerrc" MDG_NAME=lightui DEB_INSTALL=debian/osm-${MDG_NAME}.install diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index f82bf4d..0914fb3 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -1210,6 +1210,23 @@ class Client(object): result['data'] = r.text return result + def wim_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/wim_accounts".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 vim_list(self, token): result = {'error': True, 'data': ''} headers = {"Content-Type": "application/yaml", "accept": "application/json", @@ -1227,6 +1244,23 @@ class Client(object): return result + def wim_delete(self, token, id): + result = {'error': True, 'data': ''} + headers = {"accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/wim_accounts/{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.status_code == requests.codes.accepted: + result['error'] = False + else: + result['data'] = r.text + return result + def vim_delete(self, token, id): result = {'error': True, 'data': ''} headers = {"accept": "application/json", @@ -1244,6 +1278,24 @@ class Client(object): result['data'] = r.text return result + def wim_get(self, token, id): + + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/wim_accounts/{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 vim_get(self, token, id): result = {'error': True, 'data': ''} @@ -1262,6 +1314,24 @@ class Client(object): result['data'] = Util.json_loads_byteified(r.text) return result + def wim_create(self, token, wim_data): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/admin/v1/wim_accounts".format(self._base_path) + + try: + r = requests.post(_url, json=wim_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 vim_create(self, token, vim_data): result = {'error': True, 'data': ''} diff --git a/projecthandler/management/__init__.py b/projecthandler/management/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/projecthandler/management/commands/__init__.py b/projecthandler/management/commands/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/projecthandler/management/commands/delete_project_type.py b/projecthandler/management/commands/delete_project_type.py deleted file mode 100644 index d750f9f..0000000 --- a/projecthandler/management/commands/delete_project_type.py +++ /dev/null @@ -1,14 +0,0 @@ -from django.core.management.base import BaseCommand, CommandError - - -class Command(BaseCommand): - help = 'Delete a project type' - - def handle(self, *args, **options): - - try: - print 'delete project type' - except Exception: - raise CommandError('Error unable to delete a new project type') - - self.stdout.write(self.style.SUCCESS('Project type successfully deleted')) diff --git a/projecthandler/management/commands/new_project_type.py b/projecthandler/management/commands/new_project_type.py deleted file mode 100644 index 7c9dce9..0000000 --- a/projecthandler/management/commands/new_project_type.py +++ /dev/null @@ -1,14 +0,0 @@ -from django.core.management.base import BaseCommand, CommandError - - -class Command(BaseCommand): - help = 'Create a new project type' - - def handle(self, *args, **options): - - try: - print 'new project type' - except Exception: - raise CommandError('Error unable to create a new project type') - - self.stdout.write(self.style.SUCCESS('New project type successfully created')) diff --git a/projecthandler/template/project/osm/osm_project_left_sidebar.html b/projecthandler/template/project/osm/osm_project_left_sidebar.html index d39a0c8..7067ca7 100644 --- a/projecthandler/template/project/osm/osm_project_left_sidebar.html +++ b/projecthandler/template/project/osm/osm_project_left_sidebar.html @@ -98,6 +98,13 @@ VIM Accounts + + {% url "wims:list" as wim_list_url %} +
  • + + WIM Accounts + +
  • {% if user.is_admin %}
  • ADMIN
  • {% url "users:list" as user_list_url %} diff --git a/requirements.txt b/requirements.txt index 99d35b5..d8f43b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,11 @@ decorator==4.0.10 -Django==1.10.1 +Django==1.11.18 django-model-utils==2.6 functools32==3.2.3.post2 jsonfield==1.0.3 jsonschema==2.5.1 pbr==1.10.0 pyaml==15.8.2 -pymongo==3.4.0 python-dateutil==2.6.0 PyYAML==3.12 requests==2.12.4 diff --git a/sf_t3d/settings.py b/sf_t3d/settings.py index 21563af..d000e65 100644 --- a/sf_t3d/settings.py +++ b/sf_t3d/settings.py @@ -29,7 +29,7 @@ if os.getenv('DJANGO_ENV') == 'prod': DEBUG = False else: DEBUG = True - print DEBUG + ALLOWED_HOSTS = ['*'] AUTH_USER_MODEL = "authosm.OsmUser" @@ -56,6 +56,7 @@ INSTALLED_APPS = [ 'packagehandler', 'descriptorhandler', 'vimhandler', + 'wimhandler', 'instancehandler', 'sdnctrlhandler', 'userhandler', @@ -95,6 +96,7 @@ TEMPLATES = [ os.path.join(BASE_DIR, 'packagehandler', 'template'), os.path.join(BASE_DIR, 'descriptorhandler', 'template'), os.path.join(BASE_DIR, 'vimhandler', 'template'), + os.path.join(BASE_DIR, 'wimhandler', 'template'), os.path.join(BASE_DIR, 'instancehandler', 'template'), os.path.join(BASE_DIR, 'sdnctrlhandler', 'template'), os.path.join(BASE_DIR, 'userhandler', 'templates'), diff --git a/sf_t3d/urls.py b/sf_t3d/urls.py index 8333b5d..d13bd0f 100644 --- a/sf_t3d/urls.py +++ b/sf_t3d/urls.py @@ -26,6 +26,7 @@ urlpatterns = [ url(r'^projects/', include('projecthandler.urls.project', namespace='projects'), name='projects_base'), url(r'^sdn/', include('sdnctrlhandler.urls', namespace='sdns'), name='sdns_base'), url(r'^vims/', include('vimhandler.urls', namespace='vims'), name='vims_base'), + url(r'^wims/', include('wimhandler.urls', namespace='wims'), name='wims_base'), url(r'^packages/', include('packagehandler.urls', namespace='packages'), name='packages_base'), url(r'^instances/', include('instancehandler.urls', namespace='instances'), name='instances_base'), url(r'^netslices/', include('netslicehandler.urls', namespace='netslices'), name='netslices_base'), diff --git a/vimhandler/tests.py b/wimhandler/__init__.py similarity index 73% rename from vimhandler/tests.py rename to wimhandler/__init__.py index 79947e6..00de7ab 100644 --- a/vimhandler/tests.py +++ b/wimhandler/__init__.py @@ -1,5 +1,4 @@ -# -# Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni +# 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. @@ -11,9 +10,4 @@ # 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. -# - -from django.test import TestCase - -# Create your tests here. +# limitations under the License. \ No newline at end of file diff --git a/vimhandler/models.py b/wimhandler/apps.py similarity index 80% rename from vimhandler/models.py rename to wimhandler/apps.py index 21d5735..8d17b0a 100644 --- a/vimhandler/models.py +++ b/wimhandler/apps.py @@ -1,5 +1,4 @@ -# -# Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni +# 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. @@ -12,10 +11,11 @@ # 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. -# from __future__ import unicode_literals -from django.db import models +from django.apps import AppConfig + -# Create your models here. +class WimhandlerConfig(AppConfig): + name = 'wimhandler' diff --git a/wimhandler/template/modal/wim_create.html b/wimhandler/template/modal/wim_create.html new file mode 100644 index 0000000..e7eb6a2 --- /dev/null +++ b/wimhandler/template/modal/wim_create.html @@ -0,0 +1,74 @@ + + + + diff --git a/wimhandler/template/modal/wim_details.html b/wimhandler/template/modal/wim_details.html new file mode 100644 index 0000000..3e788ad --- /dev/null +++ b/wimhandler/template/modal/wim_details.html @@ -0,0 +1,51 @@ + \ No newline at end of file diff --git a/wimhandler/template/wim_list.html b/wimhandler/template/wim_list.html new file mode 100644 index 0000000..9a07c0f --- /dev/null +++ b/wimhandler/template/wim_list.html @@ -0,0 +1,231 @@ +{% 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 }} +
  • VIMS
  • +{% endblock %} + +{% block content_body %} + {{ block.super }} + {% include 'modal/wim_details.html' %} + {% include 'modal/wim_create.html' %} + {% csrf_token %} +
    +
    + +
    +
    +

    Registered WIM

    +
    + +
    +
    +
    + + + + + + + + + + + + + + +
    NameIdentifierTypeOperational StateDescriptionActions
    +
    +
    +
    + +
    +{% endblock %} + +{% block resource_block %} + {{ block.super }} + + + + +{% endblock %} diff --git a/vimhandler/admin.py b/wimhandler/urls.py similarity index 61% rename from vimhandler/admin.py rename to wimhandler/urls.py index 2e9690a..94b9645 100644 --- a/vimhandler/admin.py +++ b/wimhandler/urls.py @@ -1,5 +1,4 @@ -# -# Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni +# 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. @@ -12,8 +11,14 @@ # 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. -# -from django.contrib import admin +from django.conf.urls import url +from wimhandler import views + +urlpatterns = [ + url(r'^list/', views.list, name='list'), + url(r'^create/', views.create, name='create'), + url(r'^(?P[0-9a-z-]+)/delete$', views.delete, name='delete'), + url(r'^(?P[0-9a-z-]+)', views.show, name='show'), -# Register your models here. +] \ No newline at end of file diff --git a/wimhandler/views.py b/wimhandler/views.py new file mode 100644 index 0000000..c885b60 --- /dev/null +++ b/wimhandler/views.py @@ -0,0 +1,109 @@ +# 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. + +from django.shortcuts import render, redirect +from sf_t3d.decorators import login_required +from django.http import HttpResponse +import json +from lib.osm.osmclient.clientv2 import Client +import authosm.utils as osmutils +import yaml +import logging + +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger('wimhandler.py') + + +@login_required +def list(request): + user = osmutils.get_user(request) + project_id = user.project_id + result = {'type': 'ns', 'project_id': project_id} + raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') + if 'application/json' not in raw_content_types: + return __response_handler(request, result, 'wim_list.html') + client = Client() + result_client = client.wim_list(user.get_token()) + result["datacenters"] = result_client['data'] if result_client and result_client['error'] is False else [] + return __response_handler(request, result, 'wim_list.html') + +@login_required +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, 'wim_create.html') + else: + new_wim_dict = request.POST.dict() + client = Client() + keys = ["schema_version", + "schema_type", + "name", + "description", + "wim_url", + "wim_type", + "user", + "password", + "wim", + "description"] + wim_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_wim_dict.items())) + wim_data['config'] = {} + for k, v in new_wim_dict.items(): + if str(k).startswith('config_') and len(v) > 0: + config_key = k[7:] + wim_data['config'][config_key] = v + if 'additional_conf' in new_wim_dict: + try: + additional_conf_dict = yaml.safe_load(new_wim_dict['additional_conf']) + for k,v in additional_conf_dict.items(): + wim_data['config'][k] = v + except Exception as e: + # TODO return error on json.loads exception + print e + result = client.wim_create(user.get_token(), wim_data) + return __response_handler(request, result, 'wims:list', to_redirect=True, ) + +@login_required +def delete(request, wim_id=None): + user = osmutils.get_user(request) + try: + client = Client() + del_res = client.wim_delete(user.get_token(), wim_id) + except Exception as e: + log.exception(e) + return __response_handler(request, del_res, 'wims:list', to_redirect=True, ) + +@login_required +def show(request, wim_id=None): + user = osmutils.get_user(request) + project_id = user.project_id + client = Client() + result = client.wim_get(user.get_token(), wim_id) + if isinstance(result, dict) and 'error' in result and result['error']: + return render(request, 'error.html') + + return __response_handler(request, { + "wim": result['data'], + "project_id": project_id + }, 'wim_show.html') + +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 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: + return render(request, url, data_res) \ No newline at end of file -- 2.17.1