From d289ff9bc1f738eabcce24cb087ca451f94bb004 Mon Sep 17 00:00:00 2001 From: Ethan Lynn Date: Wed, 12 Sep 2018 15:39:37 +0800 Subject: Add infra_workload API This patch adds infra_workload API for VIO plugin Change-Id: I072b9acd6bff651c155265fcc43a5830021ec7ad Issue-ID: MULTICLOUD-357 Signed-off-by: Ethan Lynn --- vio/vio/pub/vim/vimapi/heat/OperateStack.py | 6 +- vio/vio/swagger/urls.py | 9 +++ vio/vio/swagger/views/workload/__init__.py | 0 vio/vio/swagger/views/workload/views.py | 102 ++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 vio/vio/swagger/views/workload/__init__.py create mode 100644 vio/vio/swagger/views/workload/views.py (limited to 'vio') diff --git a/vio/vio/pub/vim/vimapi/heat/OperateStack.py b/vio/vio/pub/vim/vimapi/heat/OperateStack.py index f6eb80e..49efd30 100644 --- a/vio/vio/pub/vim/vimapi/heat/OperateStack.py +++ b/vio/vio/pub/vim/vimapi/heat/OperateStack.py @@ -22,7 +22,11 @@ def sdk_param_formatter(data): param['username'] = data.get('userName') param['password'] = data.get('password') param['auth_url'] = data.get('url') - param['project_id'] = data.get('tenant') + # param['project_id'] = data.get('tenant') + if len(data.get("tenant", "")) == 32: + param['project_id'] = data.get("tenant") + else: + param['project_name'] = data.get("tenant") param['user_domain_name'] = 'default' param['project_domain_name'] = 'default' return param diff --git a/vio/vio/swagger/urls.py b/vio/vio/swagger/urls.py index 8456d71..bae4d15 100644 --- a/vio/vio/swagger/urls.py +++ b/vio/vio/swagger/urls.py @@ -51,6 +51,8 @@ from vio.swagger.views.subnet.views import CreateSubnetViewV1 from vio.swagger.views.subnet.views import DeleteSubnetViewV1 from vio.swagger.views.port.views import CreatePortViewV1, DeletePortViewV1 from vio.swagger.views.tenant.views import ListTenantsViewV1 +from vio.swagger.views.workload.views import CreateStackViewV1 +from vio.swagger.views.workload.views import GetDelStackViewV1 # proxy from vio.swagger.views.proxyplugin.identity.views import TokenView @@ -275,6 +277,13 @@ urlpatterns = [ r'(?P[0-9a-zA-Z_-]+)/(?P[0-9a-zA-Z\-\_]+)/' r'ports/(?P[0-9a-zA-Z\-\_]+)$', DeletePortViewV1.as_view()), + url(r'^api/multicloud-vio/v1/(?P[0-9a-zA-Z\-\_]+)/' + r'(?P[0-9a-zA-Z_-]+)/infra_workload$', + CreateStackViewV1.as_view()), + url(r'^api/multicloud-vio/v1/(?P[0-9a-zA-Z\-\_]+)/' + r'(?P[0-9a-zA-Z_-]+)/infra_workload/' + r'(?P[0-9a-zA-Z\-\_]+)$', + GetDelStackViewV1.as_view()), # fake urls url(r'^api/multicloud-vio/v[01]/vmware[_/]fake/neutron/networks$', diff --git a/vio/vio/swagger/views/workload/__init__.py b/vio/vio/swagger/views/workload/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vio/vio/swagger/views/workload/views.py b/vio/vio/swagger/views/workload/views.py new file mode 100644 index 0000000..ec53408 --- /dev/null +++ b/vio/vio/swagger/views/workload/views.py @@ -0,0 +1,102 @@ +# Copyright (c) 2018 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + +import json +import logging + +from rest_framework import status +from rest_framework.response import Response +from rest_framework.views import APIView + +from vio.pub.msapi import extsys +from vio.pub.vim.vimapi.heat import OperateStack +from vio.pub.exceptions import VimDriverVioException + +logger = logging.getLogger(__name__) + + +class CreateStackViewV1(APIView): + + def post(self, request, cloud_owner, cloud_region): + try: + vim_info = extsys.get_vim_by_id(cloud_owner + "_" + cloud_region) + except VimDriverVioException as e: + return Response(data={'error': str(e)}, status=e.status_code) + try: + body = json.loads(request.body) + template_type = body['template_type'] + if template_type != "heat": + return Response( + data={ + "error": "invalid template type %s" % template_type + }, + status=status.HTTP_400_BAD_REQUEST) + stack_body = body['template_data'] + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + try: + stack_op = OperateStack.OperateStack(vim_info) + stack = stack_op.create_vim_stack(**stack_body) + rsp = { + "template_type": "heat", + "workload_id": stack.id + } + return Response(data=rsp, status=status.HTTP_201_CREATED) + except Exception as e: + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +class GetDelStackViewV1(APIView): + def get(self, request, cloud_owner, cloud_region, workload_id): + try: + vim_info = extsys.get_vim_by_id(cloud_owner + "_" + cloud_region) + # vim_info['tenant'] = tenantid + except VimDriverVioException as e: + return Response(data={'error': str(e)}, status=e.status_code) + try: + stack_op = OperateStack.OperateStack(vim_info) + stack = stack_op.get_vim_stack(workload_id) + rsp = { + "template_type": "heat", + "workload_id": stack.id, + "workload_status": stack.status, + } + return Response(data=rsp, status=status.HTTP_200_OK) + except Exception as e: + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + def delete(self, request, cloud_owner, cloud_region, workload_id): + try: + vim_info = extsys.get_vim_by_id(cloud_owner + "_" + cloud_region) + except VimDriverVioException as e: + return Response(data={'error': str(e)}, status=e.status_code) + try: + stack_op = OperateStack.OperateStack(vim_info) + stack_op.delete_vim_stack(workload_id) + return Response(status=status.HTTP_202_ACCEPTED) + except Exception as e: + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) -- cgit 1.2.3-korg