blob: dc96dbe8e8fb14efb9717b01848d3759f2df7c11 [file] [log] [blame]
lombardoffb37bca2018-05-03 16:20:04 +02001#
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
17from django.shortcuts import render, redirect
lombardofa03da5e2018-06-02 18:36:44 +020018from django.contrib.auth.decorators import login_required
lombardofr5a31a722018-06-26 19:14:14 +020019from django.http import HttpResponse
20import json
21#from lib.osm.osmclient.client import Client
22from lib.osm.osmclient.clientv2 import Client
lombardof7ad15232018-05-25 17:48:59 +020023import yaml
lombardoffb37bca2018-05-03 16:20:04 +020024import logging
25
26logging.basicConfig(level=logging.DEBUG)
lombardofa03da5e2018-06-02 18:36:44 +020027log = logging.getLogger('view.py')
lombardoffb37bca2018-05-03 16:20:04 +020028
lombardof911c9e42018-06-03 16:52:12 +020029
lombardoffb37bca2018-05-03 16:20:04 +020030@login_required
lombardof911c9e42018-06-03 16:52:12 +020031def list(request, project_id):
lombardoffb37bca2018-05-03 16:20:04 +020032 client = Client()
lombardofr5a31a722018-06-26 19:14:14 +020033 result = client.vim_list(request.session['token'])
lombardoffb37bca2018-05-03 16:20:04 +020034 print result
35 result = {
lombardof911c9e42018-06-03 16:52:12 +020036 "project_id": project_id,
lombardofr5a31a722018-06-26 19:14:14 +020037 "datacenters": result['data'] if result and result['error'] is False else []
lombardoffb37bca2018-05-03 16:20:04 +020038 }
39 return __response_handler(request, result, 'vim_list.html')
40
lombardof911c9e42018-06-03 16:52:12 +020041
lombardoffb37bca2018-05-03 16:20:04 +020042@login_required
lombardof911c9e42018-06-03 16:52:12 +020043def create(request, project_id):
44 result = {'project_id': project_id}
lombardoffb37bca2018-05-03 16:20:04 +020045 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()))
lombardofr5a31a722018-06-26 19:14:14 +020060 vim_data['config'] = {}
61 for k, v in new_vim_dict.items():
lombardoffb37bca2018-05-03 16:20:04 +020062 if str(k).startswith('config_') and len(v) > 0:
63 config_key = k[7:]
64 vim_data['config'][config_key] = v
lombardof1a6af282018-05-10 11:49:32 +020065 if 'additional_conf' in new_vim_dict:
lombardof04c0dc72018-05-10 19:00:49 +020066 try:
lombardof7ad15232018-05-25 17:48:59 +020067 additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf'])
lombardof04c0dc72018-05-10 19:00:49 +020068 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
lombardofr5a31a722018-06-26 19:14:14 +020073 result = client.vim_create(request.session['token'], vim_data)
lombardoffb37bca2018-05-03 16:20:04 +020074 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
lombardof911c9e42018-06-03 16:52:12 +020075 return __response_handler(request, result, 'projects:vims:list', to_redirect=True, project_id=project_id)
lombardoffb37bca2018-05-03 16:20:04 +020076
77@login_required
lombardof911c9e42018-06-03 16:52:12 +020078def delete(request, project_id, vim_id=None):
lombardoffb37bca2018-05-03 16:20:04 +020079 try:
80 client = Client()
lombardofr5a31a722018-06-26 19:14:14 +020081 del_res = client.vim_delete(request.session['token'], vim_id)
lombardoffb37bca2018-05-03 16:20:04 +020082 except Exception as e:
83 log.exception(e)
lombardof911c9e42018-06-03 16:52:12 +020084 return __response_handler(request, {}, 'projects:vims:list', to_redirect=True, project_id=project_id)
lombardoffb37bca2018-05-03 16:20:04 +020085
86@login_required
lombardof911c9e42018-06-03 16:52:12 +020087def show(request, project_id, vim_id=None):
lombardoffb37bca2018-05-03 16:20:04 +020088 client = Client()
lombardofr5a31a722018-06-26 19:14:14 +020089 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
lombardoffb37bca2018-05-03 16:20:04 +020094 return __response_handler(request, {
lombardofr5a31a722018-06-26 19:14:14 +020095 "datacenter": result['data'],
lombardof911c9e42018-06-03 16:52:12 +020096 "project_id": project_id
lombardoffb37bca2018-05-03 16:20:04 +020097 }, 'vim_show.html')
98
99
100def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
101 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
lombardofr5a31a722018-06-26 19:14:14 +0200102 if 'application/json' in raw_content_types or url is None:
103 return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
lombardoffb37bca2018-05-03 16:20:04 +0200104 elif to_redirect:
105 return redirect(url, *args, **kwargs)
106 else:
107 return render(request, url, data_res)