ssh-key and config in NS create modal
[osm/LW-UI.git] / instancehandler / views.py
1 #
2 # Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 from django.shortcuts import render, redirect
18 from django.contrib.auth.decorators import login_required
19 from django.http import HttpResponse, JsonResponse
20 import yaml
21 import logging
22 from lib.osm.osmclient.client import Client
23
24
25 @login_required
26 def list(request, project_id=None, type=None):
27 client = Client()
28 if type == 'ns':
29 result = client.ns_list()
30
31 return __response_handler(request, {'instances': result, 'type': 'ns', 'project_id': project_id}, 'instance_list.html')
32
33
34 @login_required
35 def create(request, project_id=None):
36 result = {}
37 ns_data = {
38 "nsName": request.POST.get('nsName', 'WithoutName'),
39 "nsDescription": request.POST.get('nsDescription', ''),
40 "nsdId": request.POST.get('nsdId', ''),
41 "vimAccountId": request.POST.get('vimAccountId', ''),
42 }
43 if 'ssh_key' in request.POST:
44 ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')]
45
46 if 'config' in request.POST:
47 ns_config = yaml.load(request.POST.get('config'))
48 if "vim-network-name" in ns_config:
49 ns_config["vld"] = ns_config.pop("vim-network-name")
50 if "vld" in ns_config:
51 for vld in ns_config["vld"]:
52 if vld.get("vim-network-name"):
53 if isinstance(vld["vim-network-name"], dict):
54 vim_network_name_dict = {}
55 for vim_account, vim_net in vld["vim-network-name"].items():
56 vim_network_name_dict[ns_data["vimAccountId"]] = vim_net
57 vld["vim-network-name"] = vim_network_name_dict
58 ns_data["vld"] = ns_config["vld"]
59 if "vnf" in ns_config:
60 for vnf in ns_config["vnf"]:
61 if vnf.get("vim_account"):
62 vnf["vimAccountId"] = ns_data["vimAccountId"]
63
64 ns_data["vnf"] = ns_config["vnf"]
65 print ns_data
66 client = Client()
67 result = client.ns_create(ns_data)
68 return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id)
69
70
71 @login_required
72 def action(request, project_id=None, instance_id=None, type=None):
73
74 client = Client()
75
76 # result = client.ns_action(instance_id, action_payload)
77 primitive_param_keys = request.POST.getlist('primitive_params_name')
78 primitive_param_value = request.POST.getlist('primitive_params_value')
79 action_payload = {
80 "vnf_member_index": request.POST.get('vnf_member_index'),
81 "primitive": request.POST.get('primitive'),
82 "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0}
83 }
84
85 result = client.ns_action(instance_id, action_payload)
86 return __response_handler(request, result, None, to_redirect=False, status=result['status'] )
87
88
89 @login_required
90 def delete(request, project_id=None, instance_id=None, type=None):
91 result = {}
92 client = Client()
93 result = client.ns_delete(instance_id)
94 print result
95 return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id)
96
97
98 @login_required
99 def show(request, project_id=None, instance_id=None, type=None):
100 # result = {}
101 client = Client()
102 result = client.ns_get(instance_id)
103 print result
104 return __response_handler(request, result)
105
106
107 def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
108 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
109 if 'application/json' in raw_content_types or url is None:
110 return JsonResponse(data_res, *args, **kwargs)
111 elif to_redirect:
112 return redirect(url, *args, **kwargs)
113 else:
114 return render(request, url, data_res)