abaab9c2bda254d3021d45c67f0e621a23179371
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 HttpResponse
21 #from lib.osm.osmclient.client import Client
22 from lib
.osm
.osmclient
.clientv2
import Client
26 logging
.basicConfig(level
=logging
.DEBUG
)
27 log
= logging
.getLogger('vimhandler.py')
31 def list(request
, project_id
):
33 result
= client
.vim_list(request
.session
['token'])
36 "project_id": project_id
,
37 "datacenters": result
['data'] if result
and result
['error'] is False else []
39 return __response_handler(request
, result
, 'vim_list.html')
43 def create(request
, project_id
):
44 result
= {'project_id': project_id
}
45 if request
.method
== 'GET':
46 return __response_handler(request
, result
, 'vim_create.html')
48 new_vim_dict
= request
.POST
.dict()
50 keys
= ["schema_version",
59 vim_data
= dict(filter(lambda i
: i
[0] in keys
and len(i
[1]) > 0, new_vim_dict
.items()))
60 vim_data
['config'] = {}
61 for k
, v
in new_vim_dict
.items():
62 if str(k
).startswith('config_') and len(v
) > 0:
64 vim_data
['config'][config_key
] = v
65 if 'additional_conf' in new_vim_dict
:
67 additional_conf_dict
= yaml
.safe_load(new_vim_dict
['additional_conf'])
68 for k
,v
in additional_conf_dict
.items():
69 vim_data
['config'][k
] = v
70 except Exception as e
:
71 # TODO return error on json.loads exception
73 result
= client
.vim_create(request
.session
['token'], vim_data
)
74 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
75 return __response_handler(request
, result
, 'projects:vims:list', to_redirect
=True, project_id
=project_id
)
78 def delete(request
, project_id
, vim_id
=None):
81 del_res
= client
.vim_delete(request
.session
['token'], vim_id
)
82 except Exception as e
:
84 return __response_handler(request
, {}, 'projects:vims:list', to_redirect
=True, project_id
=project_id
)
87 def show(request
, project_id
, vim_id
=None):
89 result
= client
.vim_get(request
.session
['token'], vim_id
)
91 if isinstance(result
, dict) and 'error' in result
and result
['error']:
92 return render(request
, 'error.html')
94 return __response_handler(request
, {
95 "datacenter": result
['data'],
96 "project_id": project_id
100 def __response_handler(request
, data_res
, url
=None, to_redirect
=None, *args
, **kwargs
):
101 raw_content_types
= request
.META
.get('HTTP_ACCEPT', '*/*').split(',')
102 if 'application/json' in raw_content_types
or url
is None:
103 return HttpResponse(json
.dumps(data_res
), content_type
="application/json", *args
, **kwargs
)
105 return redirect(url
, *args
, **kwargs
)
107 return render(request
, url
, data_res
)