blob: 28c264cf1523c62cd0eadfed244fca12d80ae5a2 [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
19from django.http import JsonResponse
lombardoffb37bca2018-05-03 16:20:04 +020020from lib.osm.osmclient.client import Client
lombardof7ad15232018-05-25 17:48:59 +020021import yaml
lombardoffb37bca2018-05-03 16:20:04 +020022import logging
23
24logging.basicConfig(level=logging.DEBUG)
lombardofa03da5e2018-06-02 18:36:44 +020025log = logging.getLogger('view.py')
lombardoffb37bca2018-05-03 16:20:04 +020026
lombardof911c9e42018-06-03 16:52:12 +020027
lombardoffb37bca2018-05-03 16:20:04 +020028@login_required
lombardof911c9e42018-06-03 16:52:12 +020029def list(request, project_id):
lombardoffb37bca2018-05-03 16:20:04 +020030 client = Client()
31 result = client.vim_list()
32 print result
33 result = {
lombardof911c9e42018-06-03 16:52:12 +020034 "project_id": project_id,
lombardoffb37bca2018-05-03 16:20:04 +020035 "datacenters": result
36 }
37 return __response_handler(request, result, 'vim_list.html')
38
lombardof911c9e42018-06-03 16:52:12 +020039
lombardoffb37bca2018-05-03 16:20:04 +020040@login_required
lombardof911c9e42018-06-03 16:52:12 +020041def create(request, project_id):
42 result = {'project_id': project_id}
lombardoffb37bca2018-05-03 16:20:04 +020043 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
lombardof1a6af282018-05-10 11:49:32 +020063 if 'additional_conf' in new_vim_dict:
lombardof04c0dc72018-05-10 19:00:49 +020064 try:
lombardof7ad15232018-05-25 17:48:59 +020065 additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf'])
lombardof04c0dc72018-05-10 19:00:49 +020066 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
lombardoffb37bca2018-05-03 16:20:04 +020071 result = client.vim_create(vim_data)
72 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
lombardof911c9e42018-06-03 16:52:12 +020073 return __response_handler(request, result, 'projects:vims:list', to_redirect=True, project_id=project_id)
lombardoffb37bca2018-05-03 16:20:04 +020074
75@login_required
lombardof911c9e42018-06-03 16:52:12 +020076def delete(request, project_id, vim_id=None):
lombardoffb37bca2018-05-03 16:20:04 +020077 try:
78 client = Client()
79 del_res = client.vim_delete(vim_id)
80 except Exception as e:
81 log.exception(e)
lombardof911c9e42018-06-03 16:52:12 +020082 return __response_handler(request, {}, 'projects:vims:list', to_redirect=True, project_id=project_id)
lombardoffb37bca2018-05-03 16:20:04 +020083
84@login_required
lombardof911c9e42018-06-03 16:52:12 +020085def show(request, project_id, vim_id=None):
lombardoffb37bca2018-05-03 16:20:04 +020086 client = Client()
87 datacenter = client.vim_get(vim_id)
88 print datacenter
89 return __response_handler(request, {
lombardof911c9e42018-06-03 16:52:12 +020090 "datacenter": datacenter,
91 "project_id": project_id
lombardoffb37bca2018-05-03 16:20:04 +020092 }, 'vim_show.html')
93
94
95def __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)