diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | vio/vio/api_v2/api_definition/subnets.yaml | 88 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/controller_builder.py | 9 | ||||
-rw-r--r-- | vio/vio/api_v2/service.py | 4 | ||||
-rw-r--r-- | vio/vio/pub/config/config.py | 14 | ||||
-rwxr-xr-x | vio/vio/pub/utils/share_lock.py | 82 | ||||
-rw-r--r-- | vio/vio/pub/vim/vimapi/glance/OperateImage.py | 41 | ||||
-rw-r--r-- | vio/vio/swagger/views/capacity/views.py | 2 | ||||
-rw-r--r-- | vio/vio/swagger/views/image/views.py | 7 | ||||
-rw-r--r-- | vio/vio/tests/test_capacity_view.py | 7 |
10 files changed, 151 insertions, 104 deletions
@@ -1,5 +1,6 @@ .project .classpath +.vscode .settings/ .checkstyle target/ diff --git a/vio/vio/api_v2/api_definition/subnets.yaml b/vio/vio/api_v2/api_definition/subnets.yaml new file mode 100644 index 0000000..bbe35c0 --- /dev/null +++ b/vio/vio/api_v2/api_definition/subnets.yaml @@ -0,0 +1,88 @@ +--- + info: + version: "1.0.0" + title: "Multi Cloud Subnet" + description: "Definition of Subnet API" + termsOfService: "http://swagger.io/terms/" + schemes: + - "http" + produces: + - "application/json" + paths: + /{vimid}/{tenantid}/subnets/{subnetid}: + parameters: + - type: string + name: vimid + - type: string + format: uuid + name: tenantid + - type: string + name: subnetid + in: path + required: true + get: + produces: + - "application/json" + responses: + "200": + schema: + $ref: "#/definitions/subnet" + get_all: + produces: + - "application/json" + responses: + "200": + schema: + type: "array" + items: + $ref: "#/definitions/subnet" + post: + produces: + - "application/json" + responses: + "200": + schema: + $ref: "#/definitions/subnet" + delete: + responses: "204" + vim_path: "/network/v2.0/subnets" + definitions: + subnet: + plural_vim_resource: "subnets" + vim_resource: "subnet" + plural: "subnets" + properties: + name: + type: string + required: true + source: subnet.name + id: + type: string + source: subnet.id + status: + type: string + source: subnet.status + networkId: + type: string + source: subnet.network_id + required: true + allocationPools: + source: subnet.allocation_pools + gatewayIp: + type: string + source: subnet.gateway_ip + default: None + tenantId: + type: string + source: subnet.tenant_id + enableDhcp: + type: boolean + source: subnet.enable_dhcp + ipVersion: + source: subnet.ip_version + dnsNameServers: + source: subnet.dns_nameservers + cidr: + source: subnet.cidr + hostRoutes: + source: subnet.host_routes diff --git a/vio/vio/api_v2/api_router/controller_builder.py b/vio/vio/api_v2/api_router/controller_builder.py index 4e6d39d..9c01301 100644 --- a/vio/vio/api_v2/api_router/controller_builder.py +++ b/vio/vio/api_v2/api_router/controller_builder.py @@ -141,10 +141,11 @@ def _build_api_controller(api_meta): resp = session.get(full_url, endpoint_filter=service) mc_res = _convert_vim_res_to_mc_res(resp.json(), resource_properties) - return {"vimName": vim_id, - name: mc_res, - "tenantId": tenant_id, - "vimid": vim_id} + mc_res.update({"vimName": vim_id, + "vimId": vim_id, + "tenantId": tenant_id, + "returnCode": 0}) + return mc_res controller_meta["get"] = _get diff --git a/vio/vio/api_v2/service.py b/vio/vio/api_v2/service.py index a9473c3..60fb991 100644 --- a/vio/vio/api_v2/service.py +++ b/vio/vio/api_v2/service.py @@ -16,6 +16,7 @@ from oslo_service import service from oslo_service import wsgi from vio.api_v2 import app +from vio.pub.config import config as mc_cfg CONF = cfg.CONF @@ -33,9 +34,8 @@ class WSGIService(service.ServiceBase): CONF, "vio", self.app, - # TODO(xiaohhui): these should be configurable. host="0.0.0.0", - port="9004", + port=mc_cfg.API_SERVER_PORT, use_ssl=False ) diff --git a/vio/vio/pub/config/config.py b/vio/vio/pub/config/config.py index 8f18ad5..ad5bb48 100644 --- a/vio/vio/pub/config/config.py +++ b/vio/vio/pub/config/config.py @@ -28,18 +28,10 @@ AAI_SCHEMA_VERSION = "v13" AAI_USERNAME = 'AAI' AAI_PASSWORD = 'AAI' -# [REDIS] -REDIS_HOST = '127.0.0.1' -REDIS_PORT = '6379' -REDIS_PASSWD = '' - # [MDC] SERVICE_NAME = "multicloud-vio" FORWARDED_FOR_FIELDS = ["HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED_HOST", "HTTP_X_FORWARDED_SERVER"] -# [mysql] -# DB_IP = '127.0.0.1' -# DB_PORT = 3306 -# DB_NAME = 'multivimvio' -# DB_USER = 'root' -# DB_PASSWD = 'password' + +# [Local Config] +API_SERVER_PORT = 9004 diff --git a/vio/vio/pub/utils/share_lock.py b/vio/vio/pub/utils/share_lock.py deleted file mode 100755 index 0e015c5..0000000 --- a/vio/vio/pub/utils/share_lock.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# Copyright (c) 2017-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 time - -import redis - -from vio.pub.config.config import REDIS_HOST, REDIS_PORT, REDIS_PASSWD - - -class SharedLock: - def __init__(self, lock_key, host=REDIS_HOST, port=REDIS_PORT, - password=REDIS_PASSWD, db=9, lock_timeout=5 * 60): - self.lock_key = lock_key - self.lock_timeout = lock_timeout - self.redis = redis.Redis(host=host, port=port, - db=db, password=password) - self.acquire_time = -1 - - def acquire(self): - begin = now = int(time.time()) - while (now - begin) < self.lock_timeout: - - result = self.redis.setnx( - self.lock_key, now + self.lock_timeout + 1) - if result == 1 or result is True: - self.acquire_time = now - return True - - current_lock_timestamp = self.redis.get(self.lock_key) - if not current_lock_timestamp: - time.sleep(1) - continue - - current_lock_timestamp = int(current_lock_timestamp) - - if now > current_lock_timestamp: - next_lock_timestamp = self.redis.getset( - self.lock_key, now + self.lock_timeout + 1) - if not next_lock_timestamp: - time.sleep(1) - continue - next_lock_timestamp = int(next_lock_timestamp) - - if next_lock_timestamp == current_lock_timestamp: - self.acquire_time = now - return True - else: - time.sleep(1) - continue - return False - - def release(self): - now = int(time.time()) - if now > self.acquire_time + self.lock_timeout: - # key expired, do nothing and let other clients handle it - return - self.acquire_time = None - self.redis.delete(self.lock_key) - - -def do_biz_with_share_lock(lock_name, callback): - lock = SharedLock(lock_name) - try: - if not lock.acquire(): - raise Exception(lock_name + " timeout") - callback() - except Exception as e: - raise e - finally: - lock.release() diff --git a/vio/vio/pub/vim/vimapi/glance/OperateImage.py b/vio/vio/pub/vim/vimapi/glance/OperateImage.py index b26a184..ddb7ba2 100644 --- a/vio/vio/pub/vim/vimapi/glance/OperateImage.py +++ b/vio/vio/pub/vim/vimapi/glance/OperateImage.py @@ -15,14 +15,12 @@ import logging import threading import urllib2 - from vio.pub.msapi import extsys from vio.pub.vim.vimapi.baseclient import baseclient from vio.swagger import image_utils logger = logging.getLogger(__name__) - running_threads = {} running_thread_lock = threading.Lock() @@ -108,10 +106,47 @@ class OperateImage(baseclient): image = self.glance(self.param).create_image_file(file_name, image_type) - self.glance(self.param).upload_image(open(file_dest), image) + upload_image_file_thread = imagefileThread(vimid, tenantid, + image, file_dest) + logger.debug("launch thread to upload image: %s" % image.id) + running_thread_lock.acquire() + running_threads[image.id] = image.id + running_thread_lock.release() + try: + upload_image_file_thread.start() + except Exception: + pass return image def download_vim_image(self, image): image_data = self.glance(self.param).download_image(image) return image_data + + +class imagefileThread(threading.Thread): + def __init__(self, vimid, tenantid, image, file_dest): + + threading.Thread.__init__(self) + self.imageid = image.id + self.vimid = vimid + self.tenantid = tenantid + self.image = image + self.file_dest = file_dest + + def run(self): + + logger.debug("start imagethread") + self.transfer_image_file(self.vimid, self.tenantid, + self.image, self.file_dest) + running_thread_lock.acquire() + running_threads.pop(self.imageid) + running_thread_lock.release() + + def transfer_image_file(self, vimid, tenantid, image, file_dest): + logger.debug("Image----transfer_image") + vim_info = extsys.get_vim_by_id(vimid) + vim_info['tenant'] = tenantid + param = image_utils.sdk_param_formatter(vim_info) + client = baseclient() + client.glance(param).upload_image(open(file_dest), image) diff --git a/vio/vio/swagger/views/capacity/views.py b/vio/vio/swagger/views/capacity/views.py index ce5d854..1fdc481 100644 --- a/vio/vio/swagger/views/capacity/views.py +++ b/vio/vio/swagger/views/capacity/views.py @@ -72,7 +72,7 @@ class CapacityCheck(APIView): def post(self, request, vimid): try: requirement = json.loads(request.body) - except json.JSONDecodeError as ex: + except ValueError as ex: return Response(data={'error': str(ex)}, status=status.HTTP_400_BAD_REQUEST) try: diff --git a/vio/vio/swagger/views/image/views.py b/vio/vio/swagger/views/image/views.py index 52d14ef..62770c7 100644 --- a/vio/vio/swagger/views/image/views.py +++ b/vio/vio/swagger/views/image/views.py @@ -209,6 +209,7 @@ class GetImageFileView(APIView): except Exception as e: return Response(data={'error': 'Fail to decode request body.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + vim_rsp = image_utils.vim_formatter(vim_info, tenantid) image_instance = OperateImage.OperateImage(vim_info) try: image = image_instance.find_vim_image(imageid) @@ -229,7 +230,11 @@ class GetImageFileView(APIView): image_file.write(chunk) image_file.close() - return Response(data={'status': 'donwload OK'}, + rsp = image_utils.image_formatter(image) + rsp.update(vim_rsp) + rsp['returnCode'] = '1' + + return Response(data={'status': rsp}, status=status.HTTP_200_OK) except Exception as e: diff --git a/vio/vio/tests/test_capacity_view.py b/vio/vio/tests/test_capacity_view.py index 174d73e..d27efba 100644 --- a/vio/vio/tests/test_capacity_view.py +++ b/vio/vio/tests/test_capacity_view.py @@ -102,3 +102,10 @@ class CapacityCheckTest(unittest.TestCase): resp = self.view.post(req, "openstack_regionone") self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual({"result": False}, resp.data) + + def test_check_capacity_invalid_input(self): + req = mock.Mock() + req.body = "hello world" + + resp = self.view.post(req, "openstack_regionone") + self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code) |