blob: d16fb7ed7cf2937e1e4f7bc5e44e9fa81710ef35 [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
lombardofr4908f382018-09-10 11:36:06 +020018from sf_t3d.decorators import login_required
lombardofr5a31a722018-06-26 19:14:14 +020019from django.http import HttpResponse
20import json
lombardofr5a31a722018-06-26 19:14:14 +020021from lib.osm.osmclient.clientv2 import Client
lombardofr99f922f2018-07-17 17:27:36 +020022import authosm.utils as osmutils
lombardof7ad15232018-05-25 17:48:59 +020023import yaml
lombardoffb37bca2018-05-03 16:20:04 +020024import logging
25
26logging.basicConfig(level=logging.DEBUG)
lombardofrbeb8a4d2018-06-26 20:17:07 +020027log = logging.getLogger('vimhandler.py')
lombardoffb37bca2018-05-03 16:20:04 +020028
lombardof911c9e42018-06-03 16:52:12 +020029
lombardoffb37bca2018-05-03 16:20:04 +020030@login_required
lombardofr99f922f2018-07-17 17:27:36 +020031def list(request):
32 user = osmutils.get_user(request)
33 project_id = user.project_id
lombardofr4908f382018-09-10 11:36:06 +020034 result = {'type': 'ns', 'project_id': project_id}
35 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
36 if 'application/json' not in raw_content_types:
37 return __response_handler(request, result, 'vim_list.html')
lombardoffb37bca2018-05-03 16:20:04 +020038 client = Client()
lombardofr4908f382018-09-10 11:36:06 +020039 result_client = client.vim_list(user.get_token())
40 result["datacenters"] = result_client['data'] if result_client and result_client['error'] is False else []
lombardoffb37bca2018-05-03 16:20:04 +020041 return __response_handler(request, result, 'vim_list.html')
42
lombardof911c9e42018-06-03 16:52:12 +020043
lombardoffb37bca2018-05-03 16:20:04 +020044@login_required
lombardofr99f922f2018-07-17 17:27:36 +020045def create(request):
46 user = osmutils.get_user(request)
47 project_id = user.project_id
lombardof911c9e42018-06-03 16:52:12 +020048 result = {'project_id': project_id}
lombardoffb37bca2018-05-03 16:20:04 +020049 if request.method == 'GET':
50 return __response_handler(request, result, 'vim_create.html')
51 else:
52 new_vim_dict = request.POST.dict()
53 client = Client()
54 keys = ["schema_version",
55 "schema_type",
56 "name",
57 "vim_url",
58 "vim_type",
59 "vim_user",
60 "vim_password",
61 "vim_tenant_name",
62 "description"]
63 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 +020064 vim_data['config'] = {}
65 for k, v in new_vim_dict.items():
lombardoffb37bca2018-05-03 16:20:04 +020066 if str(k).startswith('config_') and len(v) > 0:
67 config_key = k[7:]
68 vim_data['config'][config_key] = v
lombardof1a6af282018-05-10 11:49:32 +020069 if 'additional_conf' in new_vim_dict:
lombardof04c0dc72018-05-10 19:00:49 +020070 try:
lombardof7ad15232018-05-25 17:48:59 +020071 additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf'])
lombardof04c0dc72018-05-10 19:00:49 +020072 for k,v in additional_conf_dict.items():
73 vim_data['config'][k] = v
74 except Exception as e:
75 # TODO return error on json.loads exception
76 print e
lombardofr99f922f2018-07-17 17:27:36 +020077 result = client.vim_create(user.get_token(), vim_data)
lombardoffb37bca2018-05-03 16:20:04 +020078 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
lombardofr2ad37de2018-07-18 09:47:28 +020079 return __response_handler(request, result, 'vims:list', to_redirect=True, )
lombardoffb37bca2018-05-03 16:20:04 +020080
81@login_required
lombardofr99f922f2018-07-17 17:27:36 +020082def delete(request, vim_id=None):
83 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +020084 try:
85 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +020086 del_res = client.vim_delete(user.get_token(), vim_id)
lombardoffb37bca2018-05-03 16:20:04 +020087 except Exception as e:
88 log.exception(e)
lombardofrc9488202018-07-24 14:38:16 +020089 return __response_handler(request, del_res, 'vims:list', to_redirect=True, )
lombardoffb37bca2018-05-03 16:20:04 +020090
91@login_required
lombardofr99f922f2018-07-17 17:27:36 +020092def show(request, vim_id=None):
93 user = osmutils.get_user(request)
94 project_id = user.project_id
lombardoffb37bca2018-05-03 16:20:04 +020095 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +020096 result = client.vim_get(user.get_token(), vim_id)
lombardofr5a31a722018-06-26 19:14:14 +020097 print result
98 if isinstance(result, dict) and 'error' in result and result['error']:
99 return render(request, 'error.html')
100
lombardoffb37bca2018-05-03 16:20:04 +0200101 return __response_handler(request, {
lombardofr5a31a722018-06-26 19:14:14 +0200102 "datacenter": result['data'],
lombardof911c9e42018-06-03 16:52:12 +0200103 "project_id": project_id
lombardoffb37bca2018-05-03 16:20:04 +0200104 }, 'vim_show.html')
105
106
107def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
108 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
lombardofr5a31a722018-06-26 19:14:14 +0200109 if 'application/json' in raw_content_types or url is None:
110 return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
lombardoffb37bca2018-05-03 16:20:04 +0200111 elif to_redirect:
112 return redirect(url, *args, **kwargs)
113 else:
114 return render(request, url, data_res)