| lombardofr | 3fcf21a | 2019-03-11 10:26:08 +0100 | [diff] [blame] | 1 | # Copyright 2018 EveryUP Srl |
| 2 | # |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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. |
| 14 | |
| 15 | from django.shortcuts import render, redirect |
| 16 | from sf_t3d.decorators import login_required |
| 17 | from django.http import HttpResponse |
| 18 | import json |
| 19 | from lib.osm.osmclient.clientv2 import Client |
| 20 | import authosm.utils as osmutils |
| 21 | import yaml |
| 22 | import logging |
| 23 | |
| 24 | logging.basicConfig(level=logging.DEBUG) |
| 25 | log = logging.getLogger('wimhandler.py') |
| 26 | |
| 27 | |
| 28 | @login_required |
| 29 | def list(request): |
| 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') |
| 36 | client = Client() |
| 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') |
| 40 | |
| lombardofr | c1e3d67 | 2019-12-17 14:58:24 +0100 | [diff] [blame] | 41 | |
| lombardofr | 3fcf21a | 2019-03-11 10:26:08 +0100 | [diff] [blame] | 42 | @login_required |
| 43 | def create(request): |
| 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') |
| 49 | else: |
| 50 | new_wim_dict = request.POST.dict() |
| 51 | client = Client() |
| 52 | keys = ["schema_version", |
| 53 | "schema_type", |
| 54 | "name", |
| 55 | "description", |
| 56 | "wim_url", |
| 57 | "wim_type", |
| 58 | "user", |
| 59 | "password", |
| 60 | "wim", |
| 61 | "description"] |
| 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: |
| 66 | config_key = k[7:] |
| 67 | wim_data['config'][config_key] = v |
| 68 | if 'additional_conf' in new_wim_dict: |
| 69 | try: |
| 70 | additional_conf_dict = yaml.safe_load(new_wim_dict['additional_conf']) |
| lombardofr | c1e3d67 | 2019-12-17 14:58:24 +0100 | [diff] [blame] | 71 | for k, v in additional_conf_dict.items(): |
| lombardofr | 3fcf21a | 2019-03-11 10:26:08 +0100 | [diff] [blame] | 72 | wim_data['config'][k] = v |
| 73 | except Exception as e: |
| 74 | # TODO return error on json.loads exception |
| 75 | print e |
| 76 | result = client.wim_create(user.get_token(), wim_data) |
| 77 | return __response_handler(request, result, 'wims:list', to_redirect=True, ) |
| 78 | |
| 79 | @login_required |
| 80 | def delete(request, wim_id=None): |
| 81 | user = osmutils.get_user(request) |
| 82 | try: |
| 83 | client = Client() |
| 84 | del_res = client.wim_delete(user.get_token(), wim_id) |
| 85 | except Exception as e: |
| 86 | log.exception(e) |
| 87 | return __response_handler(request, del_res, 'wims:list', to_redirect=True, ) |
| 88 | |
| 89 | @login_required |
| 90 | def show(request, wim_id=None): |
| 91 | user = osmutils.get_user(request) |
| 92 | project_id = user.project_id |
| 93 | client = Client() |
| 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') |
| 97 | |
| 98 | return __response_handler(request, { |
| 99 | "wim": result['data'], |
| 100 | "project_id": project_id |
| 101 | }, 'wim_show.html') |
| 102 | |
| 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) |
| 107 | elif to_redirect: |
| 108 | return redirect(url, *args, **kwargs) |
| 109 | else: |
| 110 | return render(request, url, data_res) |