X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=vimhandler%2Fviews.py;h=04c9cded21d03201489e63af7b90b0f1bfced41c;hb=c1e3d672556a859371f2586d6e512bfb708b7561;hp=19b58add438369f3c58c97ee9bc0e8b5233e9ea9;hpb=a03da5e2aab552daa3e7ced70b3e90466c7c8b82;p=osm%2FLW-UI.git diff --git a/vimhandler/views.py b/vimhandler/views.py index 19b58ad..04c9cde 100644 --- a/vimhandler/views.py +++ b/vimhandler/views.py @@ -15,83 +15,108 @@ # from django.shortcuts import render, redirect -from django.contrib.auth.decorators import login_required -from django.http import JsonResponse -from lib.osm.osmclient.client import Client +from sf_t3d.decorators import login_required +from django.http import HttpResponse +import json +from lib.osm.osmclient.clientv2 import Client +import authosm.utils as osmutils import yaml import logging logging.basicConfig(level=logging.DEBUG) -log = logging.getLogger('view.py') +log = logging.getLogger('vimhandler.py') + @login_required def list(request): + user = osmutils.get_user(request) + project_id = user.project_id + result = {'type': 'ns', 'project_id': project_id} + raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') + if 'application/json' not in raw_content_types: + return __response_handler(request, result, 'vim_list.html') client = Client() - result = client.vim_list() - print result - result = { - "datacenters": result - } + result_client = client.vim_list(user.get_token()) + result["datacenters"] = result_client['data'] if result_client and result_client['error'] is False else [] return __response_handler(request, result, 'vim_list.html') + @login_required def create(request): - result = {} + user = osmutils.get_user(request) + project_id = user.project_id + result = {'project_id': project_id} if request.method == 'GET': return __response_handler(request, result, 'vim_create.html') else: - new_vim_dict = request.POST.dict() - client = Client() - keys = ["schema_version", - "schema_type", - "name", - "vim_url", - "vim_type", - "vim_user", - "vim_password", - "vim_tenant_name", - "description"] - vim_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_vim_dict.items())) - vim_data['config']={} - for k,v in new_vim_dict.items(): - if str(k).startswith('config_') and len(v) > 0: - config_key = k[7:] - vim_data['config'][config_key] = v - if 'additional_conf' in new_vim_dict: - try: - additional_conf_dict = yaml.safe_load(new_vim_dict['additional_conf']) - for k,v in additional_conf_dict.items(): - vim_data['config'][k] = v - except Exception as e: - # TODO return error on json.loads exception - print e - result = client.vim_create(vim_data) - # TODO 'vim:show', to_redirect=True, vim_id=vim_id - return __response_handler(request, result, 'vim:list', to_redirect=True) + try: + new_vim_dict = request.POST.dict() + client = Client() + keys = ["schema_version", + "schema_type", + "name", + "vim_url", + "vim_type", + "vim_user", + "vim_password", + "vim_tenant_name", + "description"] + vim_data = dict(filter(lambda i: i[0] in keys and len( + i[1]) > 0, new_vim_dict.items())) + vim_data['config'] = {} + + config_file = request.FILES.get('config_file') + + if config_file is not None: + config = '' + for line in config_file: + config = config + line.decode() + vim_data['config'] = yaml.load(config) + elif 'config' in request.POST and request.POST.get('config') != '': + vim_data['config'] = yaml.load(request.POST.get('config')) + + + except Exception as e: + return __response_handler(request, {'status': 400, 'code': 'BAD_REQUEST', 'detail': e.message}, url=None, status=400) + result = client.vim_create(user.get_token(), vim_data) + + if result['error']: + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) + @login_required def delete(request, vim_id=None): + user = osmutils.get_user(request) try: client = Client() - del_res = client.vim_delete(vim_id) + del_res = client.vim_delete(user.get_token(), vim_id) except Exception as e: log.exception(e) - return __response_handler(request, {}, 'vim:list', to_redirect=True) + return __response_handler(request, del_res, 'vims:list', to_redirect=True) + @login_required def show(request, vim_id=None): + user = osmutils.get_user(request) + project_id = user.project_id client = Client() - datacenter = client.vim_get(vim_id) - print datacenter + result = client.vim_get(user.get_token(), vim_id) + if isinstance(result, dict) and 'error' in result and result['error']: + return render(request, 'error.html') + return __response_handler(request, { - "datacenter": datacenter + "datacenter": result['data'], + "project_id": project_id }, 'vim_show.html') def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') - if 'application/json' in raw_content_types: - return JsonResponse(data_res) + if 'application/json' in raw_content_types or url is None: + return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs) elif to_redirect: return redirect(url, *args, **kwargs) else: