VIM accounts part of the project view
[osm/LW-UI.git] / vimhandler / 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 JsonResponse
20 from lib.osm.osmclient.client import Client
21 import yaml
22 import logging
23
24 logging.basicConfig(level=logging.DEBUG)
25 log = logging.getLogger('view.py')
26
27
28 @login_required
29 def list(request, project_id):
30 client = Client()
31 result = client.vim_list()
32 print result
33 result = {
34 "project_id": project_id,
35 "datacenters": result
36 }
37 return __response_handler(request, result, 'vim_list.html')
38
39
40 @login_required
41 def create(request, project_id):
42 result = {'project_id': project_id}
43 if request.method == 'GET':
44 return __response_handler(request, result, 'vim_create.html')
45 else:
46 new_vim_dict = request.POST.dict()
47 client = Client()
48 keys = ["schema_version",
49 "schema_type",
50 "name",
51 "vim_url",
52 "vim_type",
53 "vim_user",
54 "vim_password",
55 "vim_tenant_name",
56 "description"]
57 vim_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_vim_dict.items()))
58 vim_data['config']={}
59 for k,v in new_vim_dict.items():
60 if str(k).startswith('config_') and len(v) > 0:
61 config_key = k[7:]
62 vim_data['config'][config_key] = v
63 if 'additional_conf' in new_vim_dict:
64 try:
65 additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf'])
66 for k,v in additional_conf_dict.items():
67 vim_data['config'][k] = v
68 except Exception as e:
69 # TODO return error on json.loads exception
70 print e
71 result = client.vim_create(vim_data)
72 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
73 return __response_handler(request, result, 'projects:vims:list', to_redirect=True, project_id=project_id)
74
75 @login_required
76 def delete(request, project_id, vim_id=None):
77 try:
78 client = Client()
79 del_res = client.vim_delete(vim_id)
80 except Exception as e:
81 log.exception(e)
82 return __response_handler(request, {}, 'projects:vims:list', to_redirect=True, project_id=project_id)
83
84 @login_required
85 def show(request, project_id, vim_id=None):
86 client = Client()
87 datacenter = client.vim_get(vim_id)
88 print datacenter
89 return __response_handler(request, {
90 "datacenter": datacenter,
91 "project_id": project_id
92 }, 'vim_show.html')
93
94
95 def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
96 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
97 if 'application/json' in raw_content_types:
98 return JsonResponse(data_res)
99 elif to_redirect:
100 return redirect(url, *args, **kwargs)
101 else:
102 return render(request, url, data_res)