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