1 # Copyright 2018 EveryUP Srl
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from django
.shortcuts
import render
, redirect
16 from sf_t3d
.decorators
import login_required
17 from django
.http
import HttpResponse
19 from lib
.osm
.osmclient
.clientv2
import Client
20 import authosm
.utils
as osmutils
24 logging
.basicConfig(level
=logging
.DEBUG
)
25 log
= logging
.getLogger('wimhandler.py')
30 user
= osmutils
.get_user(request
)
31 project_id
= user
.project_id
32 result
= {'type': 'ns', 'project_id': project_id
}
33 raw_content_types
= request
.META
.get('HTTP_ACCEPT', '*/*').split(',')
34 if 'application/json' not in raw_content_types
:
35 return __response_handler(request
, result
, 'wim_list.html')
37 result_client
= client
.wim_list(user
.get_token())
38 result
["datacenters"] = result_client
['data'] if result_client
and result_client
['error'] is False else []
39 return __response_handler(request
, result
, 'wim_list.html')
44 user
= osmutils
.get_user(request
)
45 project_id
= user
.project_id
46 result
= {'project_id': project_id
}
47 if request
.method
== 'GET':
48 return __response_handler(request
, result
, 'wim_create.html')
50 new_wim_dict
= request
.POST
.dict()
52 keys
= ["schema_version",
62 wim_data
= dict(filter(lambda i
: i
[0] in keys
and len(i
[1]) > 0, new_wim_dict
.items()))
63 wim_data
['config'] = {}
64 for k
, v
in new_wim_dict
.items():
65 if str(k
).startswith('config_') and len(v
) > 0:
67 wim_data
['config'][config_key
] = v
68 if 'additional_conf' in new_wim_dict
:
70 additional_conf_dict
= yaml
.safe_load(new_wim_dict
['additional_conf'])
71 for k
, v
in additional_conf_dict
.items():
72 wim_data
['config'][k
] = v
73 except Exception as e
:
74 # TODO return error on json.loads exception
76 result
= client
.wim_create(user
.get_token(), wim_data
)
77 return __response_handler(request
, result
, 'wims:list', to_redirect
=True, )
80 def delete(request
, wim_id
=None):
81 user
= osmutils
.get_user(request
)
84 del_res
= client
.wim_delete(user
.get_token(), wim_id
)
85 except Exception as e
:
87 return __response_handler(request
, del_res
, 'wims:list', to_redirect
=True, )
90 def show(request
, wim_id
=None):
91 user
= osmutils
.get_user(request
)
92 project_id
= user
.project_id
94 result
= client
.wim_get(user
.get_token(), wim_id
)
95 if isinstance(result
, dict) and 'error' in result
and result
['error']:
96 return render(request
, 'error.html')
98 return __response_handler(request
, {
99 "wim": result
['data'],
100 "project_id": project_id
103 def __response_handler(request
, data_res
, url
=None, to_redirect
=None, *args
, **kwargs
):
104 raw_content_types
= request
.META
.get('HTTP_ACCEPT', '*/*').split(',')
105 if 'application/json' in raw_content_types
or url
is None:
106 return HttpResponse(json
.dumps(data_res
), content_type
="application/json", *args
, **kwargs
)
108 return redirect(url
, *args
, **kwargs
)
110 return render(request
, url
, data_res
)