From c1e3d672556a859371f2586d6e512bfb708b7561 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Tue, 17 Dec 2019 14:58:24 +0100 Subject: [PATCH] fix bug 923; fix bug 940 Change-Id: I2b0175af9a8ff80ff4d20316739646b69e8dcba1 Signed-off-by: lombardofr --- static/src/vimhandler/vim_list.js | 158 +++++++++++++++ vimhandler/template/modal/vim_create.html | 108 ++++++++++ vimhandler/template/vim_create.html | 231 ---------------------- vimhandler/template/vim_list.html | 116 +---------- vimhandler/views.py | 66 ++++--- wimhandler/views.py | 3 +- 6 files changed, 314 insertions(+), 368 deletions(-) create mode 100644 static/src/vimhandler/vim_list.js create mode 100644 vimhandler/template/modal/vim_create.html delete mode 100644 vimhandler/template/vim_create.html diff --git a/static/src/vimhandler/vim_list.js b/static/src/vimhandler/vim_list.js new file mode 100644 index 0000000..ed86310 --- /dev/null +++ b/static/src/vimhandler/vim_list.js @@ -0,0 +1,158 @@ +/* + Copyright 2019 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. +*/ + +$(document).ready(function () { + table = $('#vims_table').DataTable({ + responsive: true, + "ajax": { + "url": "/vims/list/", + "dataSrc": function (json) { + return json['datacenters']; + }, + statusCode: { + 401: function () { + console.log("no auth"); + moveToLogin(window.location.pathname); + } + }, + "error": function (hxr, error, thrown) { + console.log(hxr) + console.log(thrown) + console.log(error); + } + + }, + "columns": [ + { + "render": function (data, type, row) { + return row["name"]; + }, + "targets": 0 + }, + { + "render": function (data, type, row) { + return row['_id']; + }, + "targets": 1 + }, + { + "render": function (data, type, row) { + return row["vim_type"]; + }, + "targets": 2 + }, + { + "render": function (data, type, row) { + return row["_admin"]['operationalState']; + }, + "targets": 3 + }, + { + "render": function (data, type, row) { + return row["_admin"]['description'] || ''; + }, + "targets": 4 + }, + { + "render": function (data, type, row) { + return '
' + + '
'; + }, + "targets": 5, + "orderable": false + } + ] + }); + + setInterval(function () { + table.ajax.reload(); + }, 10000); + + $("#formCreateVIM").submit(function (event) { + event.preventDefault(); //prevent default action + var post_url = $(this).attr("action"); //get form action url + var request_method = $(this).attr("method"); //get form GET/POST method + var form_data = new FormData(this); //Encode form elements for submission + $.ajax({ + url: post_url, + type: request_method, + data: form_data, + headers: { + "Accept": 'application/json' + }, + contentType: false, + processData: false + }).done(function (response, textStatus, jqXHR) { + table.ajax.reload(); + $('#modal_new_vim').modal('hide'); + }).fail(function (result) { + var data = result.responseJSON; + var title = "Error " + (data.code ? data.code : 'unknown'); + var message = data.detail ? data.detail : 'No detail available.'; + bootbox.alert({ + title: title, + message: message + }); + }); + }); +}); + +function openModalCreateVIM() { + $('#modal_new_vim').modal('show'); +} + +function deleteVim(vim_id, vim_name) { + var url = "/vims/" + vim_id + "/delete"; + bootbox.confirm("Are you sure want to delete " + vim_name + "?", function (result) { + if (result) { + var dialog = bootbox.dialog({ + message: '
Loading...
', + closeButton: true + }); + $.ajax({ + url: url, + type: 'GET', + dataType: "json", + contentType: "application/json;charset=utf-8", + success: function (result) { + if (result['error'] == true) { + dialog.modal('hide'); + bootbox.alert("An error occurred."); + } + else { + dialog.modal('hide'); + table.ajax.reload(); + } + }, + error: function (error) { + dialog.modal('hide'); + var data = result.responseJSON; + var title = "Error " + (data && data.code ? data.code : 'unknown'); + var message = data && data.detail ? data.detail : 'No detail available.'; + bootbox.alert({ + title: title, + message: message + }); + } + }); + } + }) +} \ No newline at end of file diff --git a/vimhandler/template/modal/vim_create.html b/vimhandler/template/modal/vim_create.html new file mode 100644 index 0000000..bc76721 --- /dev/null +++ b/vimhandler/template/modal/vim_create.html @@ -0,0 +1,108 @@ + + + + + diff --git a/vimhandler/template/vim_create.html b/vimhandler/template/vim_create.html deleted file mode 100644 index caf32b7..0000000 --- a/vimhandler/template/vim_create.html +++ /dev/null @@ -1,231 +0,0 @@ - - -{% 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 }} - -
    -
    -
    - {% csrf_token %} -
    -
    -

    NEW VIM ACCOUNT

    -
    - -
    -
    -
    - -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    - - - - -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    - -
    -
    -
    -
    - - -
    - -
    - - - - -
    - {% include 'config/openvim.html' %} - {% include 'config/openstack.html' %} - {% include 'config/vmware.html' %} - {% include 'config/aws.html' %} - {% include 'config/opennebula.html' %} - -
    -
    -
    -
    - -
    - -
    -
    -
    -
    -
    - -
    -
    - - -
    - -
    -
    -
    - -
    -{% endblock %} - -{% block resource_block %} - {{ block.super }} - - - - -{% endblock %} - -{% block footer %} - {% include "footer.html" %} -{% endblock %} \ No newline at end of file diff --git a/vimhandler/template/vim_list.html b/vimhandler/template/vim_list.html index b7bc224..79e0e1d 100644 --- a/vimhandler/template/vim_list.html +++ b/vimhandler/template/vim_list.html @@ -40,6 +40,7 @@ limitations under the License. {% block content_body %} {{ block.super }} + {% include 'modal/vim_create.html' %} {% include 'modal/vim_details.html' %} {% csrf_token %}
    @@ -49,8 +50,11 @@ limitations under the License.

    Registered VIM

    - New VIM +
    @@ -80,113 +84,11 @@ limitations under the License. {{ block.super }} + - {% endblock %} diff --git a/vimhandler/views.py b/vimhandler/views.py index ff1f2ae..04c9cde 100644 --- a/vimhandler/views.py +++ b/vimhandler/views.py @@ -49,34 +49,42 @@ def create(request): if request.method == 'GET': return __response_handler(request, result, 'vim_create.html') else: - new_vim_dict = request.POST.dict() - client = Client() - keys = ["schema_version", - "schema_type", - "name", - "vim_url", - "vim_type", - "vim_user", - "vim_password", - "vim_tenant_name", - "description"] - vim_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_vim_dict.items())) - vim_data['config'] = {} - for k, v in new_vim_dict.items(): - if str(k).startswith('config_') and len(v) > 0: - config_key = k[7:] - vim_data['config'][config_key] = v - if 'additional_conf' in new_vim_dict: - try: - additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf']) - for k,v in additional_conf_dict.items(): - vim_data['config'][k] = v - except Exception as e: - # TODO return error on json.loads exception - print e + try: + new_vim_dict = request.POST.dict() + client = Client() + keys = ["schema_version", + "schema_type", + "name", + "vim_url", + "vim_type", + "vim_user", + "vim_password", + "vim_tenant_name", + "description"] + vim_data = dict(filter(lambda i: i[0] in keys and len( + i[1]) > 0, new_vim_dict.items())) + vim_data['config'] = {} + + config_file = request.FILES.get('config_file') + + if config_file is not None: + config = '' + for line in config_file: + config = config + line.decode() + vim_data['config'] = yaml.load(config) + elif 'config' in request.POST and request.POST.get('config') != '': + vim_data['config'] = yaml.load(request.POST.get('config')) + + + except Exception as e: + return __response_handler(request, {'status': 400, 'code': 'BAD_REQUEST', 'detail': e.message}, url=None, status=400) result = client.vim_create(user.get_token(), vim_data) - # TODO 'vim:show', to_redirect=True, vim_id=vim_id - return __response_handler(request, result, 'vims:list', to_redirect=True, ) + + if result['error']: + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) @login_required @@ -87,9 +95,9 @@ def delete(request, vim_id=None): del_res = client.vim_delete(user.get_token(), vim_id) except Exception as e: log.exception(e) - return __response_handler(request, del_res, 'vims:list', to_redirect=True, ) + return __response_handler(request, del_res, 'vims:list', to_redirect=True) + - @login_required def show(request, vim_id=None): user = osmutils.get_user(request) diff --git a/wimhandler/views.py b/wimhandler/views.py index c885b60..93df223 100644 --- a/wimhandler/views.py +++ b/wimhandler/views.py @@ -38,6 +38,7 @@ def list(request): 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) @@ -67,7 +68,7 @@ def create(request): 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(): + for k, v in additional_conf_dict.items(): wim_data['config'][k] = v except Exception as e: # TODO return error on json.loads exception -- 2.25.1