2 # Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from django
.shortcuts
import render
, redirect
18 from django
.contrib
.auth
.decorators
import login_required
, permission_required
19 from django
.http
import HttpResponse
, JsonResponse
20 from lib
.osm
.osmclient
.client
import Client
24 logging
.basicConfig(level
=logging
.DEBUG
)
25 log
= logging
.getLogger('helper.py')
30 result
= client
.vim_list()
35 return __response_handler(request
, result
, 'vim_list.html')
40 if request
.method
== 'GET':
41 return __response_handler(request
, result
, 'vim_create.html')
43 new_vim_dict
= request
.POST
.dict()
45 keys
= ["schema_version",
54 vim_data
= dict(filter(lambda i
: i
[0] in keys
and len(i
[1]) > 0, new_vim_dict
.items()))
56 for k
,v
in new_vim_dict
.items():
57 if str(k
).startswith('config_') and len(v
) > 0:
59 vim_data
['config'][config_key
] = v
60 if 'additional_conf' in new_vim_dict
:
62 additional_conf_dict
= yaml
.safe_load(new_vim_dict
['additional_conf'])
63 for k
,v
in additional_conf_dict
.items():
64 vim_data
['config'][k
] = v
65 except Exception as e
:
66 # TODO return error on json.loads exception
68 result
= client
.vim_create(vim_data
)
69 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
70 return __response_handler(request
, result
, 'vim:list', to_redirect
=True)
73 def delete(request
, vim_id
=None):
76 del_res
= client
.vim_delete(vim_id
)
77 except Exception as e
:
79 return __response_handler(request
, {}, 'vim:list', to_redirect
=True)
82 def show(request
, vim_id
=None):
84 datacenter
= client
.vim_get(vim_id
)
86 return __response_handler(request
, {
87 "datacenter": datacenter
91 def __response_handler(request
, data_res
, url
=None, to_redirect
=None, *args
, **kwargs
):
92 raw_content_types
= request
.META
.get('HTTP_ACCEPT', '*/*').split(',')
93 if 'application/json' in raw_content_types
:
94 return JsonResponse(data_res
)
96 return redirect(url
, *args
, **kwargs
)
98 return render(request
, url
, data_res
)