| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni |
| 3 | # |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 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. |
| 15 | # |
| 16 | |
| 17 | from django.shortcuts import render, redirect |
| lombardof | a03da5e | 2018-06-02 18:36:44 +0200 | [diff] [blame^] | 18 | from django.contrib.auth.decorators import login_required |
| 19 | from django.http import JsonResponse |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 20 | from lib.osm.osmclient.client import Client |
| lombardof | 7ad1523 | 2018-05-25 17:48:59 +0200 | [diff] [blame] | 21 | import yaml |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 22 | import logging |
| 23 | |
| 24 | logging.basicConfig(level=logging.DEBUG) |
| lombardof | a03da5e | 2018-06-02 18:36:44 +0200 | [diff] [blame^] | 25 | log = logging.getLogger('view.py') |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 26 | |
| 27 | @login_required |
| 28 | def list(request): |
| 29 | client = Client() |
| 30 | result = client.vim_list() |
| 31 | print result |
| 32 | result = { |
| 33 | "datacenters": result |
| 34 | } |
| 35 | return __response_handler(request, result, 'vim_list.html') |
| 36 | |
| 37 | @login_required |
| 38 | def create(request): |
| 39 | result = {} |
| 40 | if request.method == 'GET': |
| 41 | return __response_handler(request, result, 'vim_create.html') |
| 42 | else: |
| 43 | new_vim_dict = request.POST.dict() |
| 44 | client = Client() |
| 45 | keys = ["schema_version", |
| 46 | "schema_type", |
| 47 | "name", |
| 48 | "vim_url", |
| 49 | "vim_type", |
| 50 | "vim_user", |
| 51 | "vim_password", |
| 52 | "vim_tenant_name", |
| 53 | "description"] |
| 54 | vim_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_vim_dict.items())) |
| 55 | vim_data['config']={} |
| 56 | for k,v in new_vim_dict.items(): |
| 57 | if str(k).startswith('config_') and len(v) > 0: |
| 58 | config_key = k[7:] |
| 59 | vim_data['config'][config_key] = v |
| lombardof | 1a6af28 | 2018-05-10 11:49:32 +0200 | [diff] [blame] | 60 | if 'additional_conf' in new_vim_dict: |
| lombardof | 04c0dc7 | 2018-05-10 19:00:49 +0200 | [diff] [blame] | 61 | try: |
| lombardof | 7ad1523 | 2018-05-25 17:48:59 +0200 | [diff] [blame] | 62 | additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf']) |
| lombardof | 04c0dc7 | 2018-05-10 19:00:49 +0200 | [diff] [blame] | 63 | for k,v in additional_conf_dict.items(): |
| 64 | vim_data['config'][k] = v |
| 65 | except Exception as e: |
| 66 | # TODO return error on json.loads exception |
| 67 | print e |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 68 | result = client.vim_create(vim_data) |
| 69 | # TODO 'vim:show', to_redirect=True, vim_id=vim_id |
| 70 | return __response_handler(request, result, 'vim:list', to_redirect=True) |
| 71 | |
| 72 | @login_required |
| 73 | def delete(request, vim_id=None): |
| 74 | try: |
| 75 | client = Client() |
| 76 | del_res = client.vim_delete(vim_id) |
| 77 | except Exception as e: |
| 78 | log.exception(e) |
| 79 | return __response_handler(request, {}, 'vim:list', to_redirect=True) |
| 80 | |
| 81 | @login_required |
| 82 | def show(request, vim_id=None): |
| 83 | client = Client() |
| 84 | datacenter = client.vim_get(vim_id) |
| 85 | print datacenter |
| 86 | return __response_handler(request, { |
| 87 | "datacenter": datacenter |
| 88 | }, 'vim_show.html') |
| 89 | |
| 90 | |
| 91 | def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): |
| 92 | raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') |
| 93 | if 'application/json' in raw_content_types: |
| 94 | return JsonResponse(data_res) |
| 95 | elif to_redirect: |
| 96 | return redirect(url, *args, **kwargs) |
| 97 | else: |
| 98 | return render(request, url, data_res) |