From 0b4fb877a77e112fd242ff244be8c9eb613447a7 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Tue, 24 Jul 2018 17:10:48 +0200 Subject: [PATCH] fix Bug 511 Change-Id: I14050b6c4e9ade7de1986b24eace8ec8853c7009 Signed-off-by: lombardofr --- .../template/instance_list_ns.html | 7 ++ .../template/modal/instance_create.html | 2 +- instancehandler/views.py | 72 +++++++++++-------- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/instancehandler/template/instance_list_ns.html b/instancehandler/template/instance_list_ns.html index 48695cd..01bdc10 100644 --- a/instancehandler/template/instance_list_ns.html +++ b/instancehandler/template/instance_list_ns.html @@ -14,6 +14,13 @@
+ {% if alert_error %} +
+ +

Error

+ {{alert_error}} +
+ {% endif %} diff --git a/instancehandler/template/modal/instance_create.html b/instancehandler/template/modal/instance_create.html index a776697..b11249b 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/views.py b/instancehandler/views.py index 1a37f90..212fbbf 100644 --- a/instancehandler/views.py +++ b/instancehandler/views.py @@ -39,47 +39,57 @@ def list(request, type=None): result = {'instances': instance_list['data'] if instance_list and instance_list['error'] is False else [], 'type': type, 'project_id': project_id} + if "OSM_ERROR" in request.session: + result['alert_error'] = request.session["OSM_ERROR"] + del request.session["OSM_ERROR"] + return __response_handler(request, result, 'instance_list.html') @login_required def create(request): result = {} - ns_data = { - "nsName": request.POST.get('nsName', 'WithoutName'), - "nsDescription": request.POST.get('nsDescription', ''), - "nsdId": request.POST.get('nsdId', ''), - "vimAccountId": request.POST.get('vimAccountId', ''), - } - if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '': - ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')] - - if 'config' in request.POST: - ns_config = yaml.load(request.POST.get('config')) - if isinstance(ns_config, dict): - if "vim-network-name" in ns_config: - ns_config["vld"] = ns_config.pop("vim-network-name") - if "vld" in ns_config: - for vld in ns_config["vld"]: - if vld.get("vim-network-name"): - if isinstance(vld["vim-network-name"], dict): - vim_network_name_dict = {} - for vim_account, vim_net in vld["vim-network-name"].items(): - vim_network_name_dict[ns_data["vimAccountId"]] = vim_net - vld["vim-network-name"] = vim_network_name_dict - ns_data["vld"] = ns_config["vld"] - if "vnf" in ns_config: - for vnf in ns_config["vnf"]: - if vnf.get("vim_account"): - vnf["vimAccountId"] = ns_data["vimAccountId"] - - ns_data["vnf"] = ns_config["vnf"] + try: + + ns_data = { + "nsName": request.POST.get('nsName', 'WithoutName'), + "nsDescription": request.POST.get('nsDescription', ''), + "nsdId": request.POST.get('nsdId', ''), + "vimAccountId": request.POST.get('vimAccountId', ''), + } + if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '': + ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')] + + if 'config' in request.POST: + ns_config = yaml.load(request.POST.get('config')) + if isinstance(ns_config, dict): + if "vim-network-name" in ns_config: + ns_config["vld"] = ns_config.pop("vim-network-name") + if "vld" in ns_config: + print ns_config + for vld in ns_config["vld"]: + if vld.get("vim-network-name"): + if isinstance(vld["vim-network-name"], dict): + vim_network_name_dict = {} + for vim_account, vim_net in vld["vim-network-name"].items(): + vim_network_name_dict[ns_data["vimAccountId"]] = vim_net + vld["vim-network-name"] = vim_network_name_dict + ns_data["vld"] = ns_config["vld"] + if "vnf" in ns_config: + for vnf in ns_config["vnf"]: + if vnf.get("vim_account"): + vnf["vimAccountId"] = ns_data["vimAccountId"] + + ns_data["vnf"] = ns_config["vnf"] + except Exception as e: + request.session["OSM_ERROR"] = "Error creating the NS; Invalid parameters provided." + return __response_handler(request, {}, 'instances:list', to_redirect=True, type='ns', ) + print ns_data user = osmutils.get_user(request) client = Client() result = client.ns_create(user.get_token(), ns_data) - return __response_handler(request, result, 'instances:list', to_redirect=True, type='ns', - ) + return __response_handler(request, result, 'instances:list', to_redirect=True, type='ns',) @login_required -- 2.25.1