28c264cf1523c62cd0eadfed244fca12d80ae5a2
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
19 from django
.http
import JsonResponse
20 from lib
.osm
.osmclient
.client
import Client
24 logging
.basicConfig(level
=logging
.DEBUG
)
25 log
= logging
.getLogger('view.py')
29 def list(request
, project_id
):
31 result
= client
.vim_list()
34 "project_id": project_id
,
37 return __response_handler(request
, result
, 'vim_list.html')
41 def create(request
, project_id
):
42 result
= {'project_id': project_id
}
43 if request
.method
== 'GET':
44 return __response_handler(request
, result
, 'vim_create.html')
46 new_vim_dict
= request
.POST
.dict()
48 keys
= ["schema_version",
57 vim_data
= dict(filter(lambda i
: i
[0] in keys
and len(i
[1]) > 0, new_vim_dict
.items()))
59 for k
,v
in new_vim_dict
.items():
60 if str(k
).startswith('config_') and len(v
) > 0:
62 vim_data
['config'][config_key
] = v
63 if 'additional_conf' in new_vim_dict
:
65 additional_conf_dict
= yaml
.safe_load(new_vim_dict
['additional_conf'])
66 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
71 result
= client
.vim_create(vim_data
)
72 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
73 return __response_handler(request
, result
, 'projects:vims:list', to_redirect
=True, project_id
=project_id
)
76 def delete(request
, project_id
, vim_id
=None):
79 del_res
= client
.vim_delete(vim_id
)
80 except Exception as e
:
82 return __response_handler(request
, {}, 'projects:vims:list', to_redirect
=True, project_id
=project_id
)
85 def show(request
, project_id
, vim_id
=None):
87 datacenter
= client
.vim_get(vim_id
)
89 return __response_handler(request
, {
90 "datacenter": datacenter
,
91 "project_id": project_id
95 def __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
)
100 return redirect(url
, *args
, **kwargs
)
102 return render(request
, url
, data_res
)