c40e4e935dc86b23fd7e38698264a637eff04f95
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
.clientv2
import Client
22 import authosm
.utils
as osmutils
26 logging
.basicConfig(level
=logging
.DEBUG
)
27 log
= logging
.getLogger('vimhandler.py')
32 user
= osmutils
.get_user(request
)
33 project_id
= user
.project_id
35 result
= client
.vim_list(user
.get_token())
38 "project_id": project_id
,
39 "datacenters": result
['data'] if result
and result
['error'] is False else []
41 return __response_handler(request
, result
, 'vim_list.html')
46 user
= osmutils
.get_user(request
)
47 project_id
= user
.project_id
48 result
= {'project_id': project_id
}
49 if request
.method
== 'GET':
50 return __response_handler(request
, result
, 'vim_create.html')
52 new_vim_dict
= request
.POST
.dict()
54 keys
= ["schema_version",
63 vim_data
= dict(filter(lambda i
: i
[0] in keys
and len(i
[1]) > 0, new_vim_dict
.items()))
64 vim_data
['config'] = {}
65 for k
, v
in new_vim_dict
.items():
66 if str(k
).startswith('config_') and len(v
) > 0:
68 vim_data
['config'][config_key
] = v
69 if 'additional_conf' in new_vim_dict
:
71 additional_conf_dict
= yaml
.safe_load(new_vim_dict
['additional_conf'])
72 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
77 result
= client
.vim_create(user
.get_token(), vim_data
)
78 # TODO 'vim:show', to_redirect=True, vim_id=vim_id
79 return __response_handler(request
, result
, 'vims:list', to_redirect
=True, )
82 def delete(request
, vim_id
=None):
83 user
= osmutils
.get_user(request
)
86 del_res
= client
.vim_delete(user
.get_token(), vim_id
)
87 except Exception as e
:
89 return __response_handler(request
, del_res
, 'vims:list', to_redirect
=True, )
92 def show(request
, vim_id
=None):
93 user
= osmutils
.get_user(request
)
94 project_id
= user
.project_id
96 result
= client
.vim_get(user
.get_token(), vim_id
)
98 if isinstance(result
, dict) and 'error' in result
and result
['error']:
99 return render(request
, 'error.html')
101 return __response_handler(request
, {
102 "datacenter": result
['data'],
103 "project_id": project_id
107 def __response_handler(request
, data_res
, url
=None, to_redirect
=None, *args
, **kwargs
):
108 raw_content_types
= request
.META
.get('HTTP_ACCEPT', '*/*').split(',')
109 if 'application/json' in raw_content_types
or url
is None:
110 return HttpResponse(json
.dumps(data_res
), content_type
="application/json", *args
, **kwargs
)
112 return redirect(url
, *args
, **kwargs
)
114 return render(request
, url
, data_res
)