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 sf_t3d
.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
34 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')
39 result_client
= client
.vim_list(user
.get_token())
40 result
["datacenters"] = result_client
['data'] if result_client
and result_client
['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')
53 new_vim_dict
= request
.POST
.dict()
55 keys
= ["schema_version",
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'] = {}
68 config_file
= request
.FILES
.get('config_file')
70 if config_file
is not None:
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'))
79 except Exception as e
:
80 return __response_handler(request
, {'status': 400, 'code': 'BAD_REQUEST', 'detail': e
.message
}, url
=None, status
=400)
81 result
= client
.vim_create(user
.get_token(), vim_data
)
84 return __response_handler(request
, result
['data'], url
=None,
85 status
=result
['data']['status'] if 'status' in result
['data'] else 500)
87 return __response_handler(request
, {}, url
=None, status
=200)
91 def delete(request
, vim_id
=None):
92 user
= osmutils
.get_user(request
)
95 del_res
= client
.vim_delete(user
.get_token(), vim_id
)
96 except Exception as e
:
98 return __response_handler(request
, del_res
, 'vims:list', to_redirect
=True)
102 def show(request
, vim_id
=None):
103 user
= osmutils
.get_user(request
)
104 project_id
= user
.project_id
106 result
= client
.vim_get(user
.get_token(), vim_id
)
107 if isinstance(result
, dict) and 'error' in result
and result
['error']:
108 return render(request
, 'error.html')
110 return __response_handler(request
, {
111 "datacenter": result
['data'],
112 "project_id": project_id
116 def __response_handler(request
, data_res
, url
=None, to_redirect
=None, *args
, **kwargs
):
117 raw_content_types
= request
.META
.get('HTTP_ACCEPT', '*/*').split(',')
118 if 'application/json' in raw_content_types
or url
is None:
119 return HttpResponse(json
.dumps(data_res
), content_type
="application/json", *args
, **kwargs
)
121 return redirect(url
, *args
, **kwargs
)
123 return render(request
, url
, data_res
)