blob: 04c9cded21d03201489e63af7b90b0f1bfced41c [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:
lombardofrc1e3d672019-12-17 14:58:24 +010052 try:
53 new_vim_dict = request.POST.dict()
54 client = Client()
55 keys = ["schema_version",
56 "schema_type",
57 "name",
58 "vim_url",
59 "vim_type",
60 "vim_user",
61 "vim_password",
62 "vim_tenant_name",
63 "description"]
64 vim_data = dict(filter(lambda i: i[0] in keys and len(
65 i[1]) > 0, new_vim_dict.items()))
66 vim_data['config'] = {}
67
68 config_file = request.FILES.get('config_file')
69
70 if config_file is not None:
71 config = ''
72 for line in config_file:
73 config = config + line.decode()
74 vim_data['config'] = yaml.load(config)
75 elif 'config' in request.POST and request.POST.get('config') != '':
76 vim_data['config'] = yaml.load(request.POST.get('config'))
77
78
79 except Exception as e:
80 return __response_handler(request, {'status': 400, 'code': 'BAD_REQUEST', 'detail': e.message}, url=None, status=400)
lombardofr99f922f2018-07-17 17:27:36 +020081 result = client.vim_create(user.get_token(), vim_data)
lombardofrc1e3d672019-12-17 14:58:24 +010082
83 if result['error']:
84 return __response_handler(request, result['data'], url=None,
85 status=result['data']['status'] if 'status' in result['data'] else 500)
86 else:
87 return __response_handler(request, {}, url=None, status=200)
lombardoffb37bca2018-05-03 16:20:04 +020088
lombardofr3c7234a2019-12-03 11:23:17 +010089
lombardoffb37bca2018-05-03 16:20:04 +020090@login_required
lombardofr99f922f2018-07-17 17:27:36 +020091def delete(request, vim_id=None):
92 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +020093 try:
94 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +020095 del_res = client.vim_delete(user.get_token(), vim_id)
lombardoffb37bca2018-05-03 16:20:04 +020096 except Exception as e:
97 log.exception(e)
lombardofrc1e3d672019-12-17 14:58:24 +010098 return __response_handler(request, del_res, 'vims:list', to_redirect=True)
lombardoffb37bca2018-05-03 16:20:04 +020099
lombardofrc1e3d672019-12-17 14:58:24 +0100100
lombardoffb37bca2018-05-03 16:20:04 +0200101@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200102def show(request, vim_id=None):
103 user = osmutils.get_user(request)
104 project_id = user.project_id
lombardoffb37bca2018-05-03 16:20:04 +0200105 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200106 result = client.vim_get(user.get_token(), vim_id)
lombardofr5a31a722018-06-26 19:14:14 +0200107 if isinstance(result, dict) and 'error' in result and result['error']:
108 return render(request, 'error.html')
109
lombardoffb37bca2018-05-03 16:20:04 +0200110 return __response_handler(request, {
lombardofr5a31a722018-06-26 19:14:14 +0200111 "datacenter": result['data'],
lombardof911c9e42018-06-03 16:52:12 +0200112 "project_id": project_id
lombardoffb37bca2018-05-03 16:20:04 +0200113 }, 'vim_show.html')
114
115
116def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
117 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
lombardofr5a31a722018-06-26 19:14:14 +0200118 if 'application/json' in raw_content_types or url is None:
119 return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
lombardoffb37bca2018-05-03 16:20:04 +0200120 elif to_redirect:
121 return redirect(url, *args, **kwargs)
122 else:
123 return render(request, url, data_res)