diff options
author | xinhuili <lxinhui@vmware.com> | 2017-03-14 00:00:10 -0700 |
---|---|---|
committer | xinhuili <lxinhui@vmware.com> | 2017-03-14 00:18:04 -0700 |
commit | 0464d7a10815def6daef8b3cb96321eb5a40eb1d (patch) | |
tree | bf1c0267b16b0394007605ebefc6e03dba31e47d | |
parent | 9c4b4e6f0aeda63b025a4ff0b0c8acc91f9727fc (diff) |
Fix expection handling
This patch is to fix exception handling.
Change-Id: I215999629b21e78f9e6749aa1f1503109486129c
Issue-ids: MULTIVIM-36
Signed-off-by: xinhuili <lxinhui@vmware.com>
43 files changed, 282 insertions, 93 deletions
diff --git a/vio/vio/pub/vim/const.py b/vio/vio/pub/vim/const.py index a6bd797..47353e7 100644 --- a/vio/vio/pub/vim/const.py +++ b/vio/vio/pub/vim/const.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - VIM_OPENSTACK = "vmware" RES_TYPE_KEY = "res_type" RES_TYPE_NEW = 1 diff --git a/vio/vio/pub/vim/drivers/vimdriver.py b/vio/vio/pub/vim/drivers/vimdriver.py index 807cc98..784e9e7 100644 --- a/vio/vio/pub/vim/drivers/vimdriver.py +++ b/vio/vio/pub/vim/drivers/vimdriver.py @@ -1,14 +1,16 @@ # Copyright (c) 2017 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: +# 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 +# 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. +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. from vio.pub.vim.drivers.vimsdk import image_v2 diff --git a/vio/vio/pub/vim/drivers/vimsdk/compute.py b/vio/vio/pub/vim/drivers/vimsdk/compute.py index d336703..9e774a0 100644 --- a/vio/vio/pub/vim/drivers/vimsdk/compute.py +++ b/vio/vio/pub/vim/drivers/vimsdk/compute.py @@ -10,15 +10,34 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -import base64 import logging +from openstack import resource2 as resource +from openstack.compute import compute_service + from vio.pub.vim.drivers import base from vio.pub.vim.drivers.vimsdk import sdk LOG = logging.getLogger(__name__) +class FlavorExtraSpecs(resource.Resource): + resources_key = 'os-extra_specs' + base_path = '/flavors/%(flavor_id)s/os-extra_specs' + service = compute_service.ComputeService() + + #: The ID for the flavor. + flavor_id = resource.URI('flavor_id') + + # capabilities + allow_create = True + allow_get = True + allow_delete = True + allow_list = True + + extra_specs = resource.Body('extra_specs') + + class ComputeClient(base.DriverBase): """Compute driver.""" @@ -43,6 +62,12 @@ class ComputeClient(base.DriverBase): return server @sdk.translate_exception + def find_server(self, server_id, ignore_missing=False): + server = self.conn.compute.find_server( + server_id, ignore_missing=ignore_missing) + return server + + @sdk.translate_exception def delete_server(self, server_id, **query): self.conn.compute.delete_server(server=server_id) @@ -65,8 +90,9 @@ class ComputeClient(base.DriverBase): return self.conn.compute.get_flavor(flavor=flavor_id) @sdk.translate_exception - def find_flavor(self, flavor_id): - return self.conn.compute.find_flavor(flavor_id, ignore_missing=False) + def find_flavor(self, flavor_id, ignore_missing=False): + return self.conn.compute.find_flavor( + flavor_id, ignore_missing=ignore_missing) @sdk.translate_exception def delete_flavor(self, flavor_id, **query): @@ -74,7 +100,14 @@ class ComputeClient(base.DriverBase): @sdk.translate_exception def get_flavor_extra_specs(self, flavor_id, **query): - return None + return self.conn.compute._get(FlavorExtraSpecs, flavor_id=flavor_id, + requires_id=False) + + @sdk.translate_exception + def create_flavor_extra_specs(self, flavor_id, extra_specs, **query): + return self.conn.compute._create(FlavorExtraSpecs, + flavor_id=flavor_id, + extra_specs=extra_specs) @sdk.translate_exception def find_image(self, image_id, ignore_missing=False): diff --git a/vio/vio/pub/vim/drivers/vimsdk/image_v2.py b/vio/vio/pub/vim/drivers/vimsdk/image_v2.py index ba2db47..88babba 100644 --- a/vio/vio/pub/vim/drivers/vimsdk/image_v2.py +++ b/vio/vio/pub/vim/drivers/vimsdk/image_v2.py @@ -52,8 +52,11 @@ class GlanceClient(base.DriverBase): if not all([container_format, disk_format]): LOG.error( "Both container_format and disk_format are required") - img = self._proxy._create(_image.Image, disk_format=disk_format, + try: + img = self._proxy._create(_image.Image, disk_format=disk_format, container_format=container_format, **data) + except Exception as ex: + pass return img @sdk.translate_exception diff --git a/vio/vio/pub/vim/vimapi/baseclient.py b/vio/vio/pub/vim/vimapi/baseclient.py index 796ce1b..0acddbf 100644 --- a/vio/vio/pub/vim/vimapi/baseclient.py +++ b/vio/vio/pub/vim/vimapi/baseclient.py @@ -10,7 +10,6 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - import logging from vio.pub.vim.drivers import vimdriver as driver_base diff --git a/vio/vio/pub/vim/vimapi/glance/OperateImage.py b/vio/vio/pub/vim/vimapi/glance/OperateImage.py index 74408f3..f93441b 100644 --- a/vio/vio/pub/vim/vimapi/glance/OperateImage.py +++ b/vio/vio/pub/vim/vimapi/glance/OperateImage.py @@ -91,6 +91,8 @@ class OperateImage(baseclient): running_thread_lock.acquire() running_threads[image.id] = image.id running_thread_lock.release() - - upload_image_thread.start() + try: + upload_image_thread.start() + except Exception as ex: + pass return image
\ No newline at end of file diff --git a/vio/vio/pub/vim/vimapi/keystone/OperateTenant.py b/vio/vio/pub/vim/vimapi/keystone/OperateTenant.py index a60cf46..7781be7 100644 --- a/vio/vio/pub/vim/vimapi/keystone/OperateTenant.py +++ b/vio/vio/pub/vim/vimapi/keystone/OperateTenant.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - import logging diff --git a/vio/vio/pub/vim/vimapi/keystone/auth.py b/vio/vio/pub/vim/vimapi/keystone/auth.py index bacfad6..6bf5848 100644 --- a/vio/vio/pub/vim/vimapi/keystone/auth.py +++ b/vio/vio/pub/vim/vimapi/keystone/auth.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - import logging diff --git a/vio/vio/pub/vim/vimapi/network/OperateNetwork.py b/vio/vio/pub/vim/vimapi/network/OperateNetwork.py index 18adf1b..3b67053 100644 --- a/vio/vio/pub/vim/vimapi/network/OperateNetwork.py +++ b/vio/vio/pub/vim/vimapi/network/OperateNetwork.py @@ -89,6 +89,8 @@ class OperateNetwork(BaseNet): vim_info = self.get_vim_info(vimid) network = self.auth(vim_info) net = network.network_get(networkid) + if net is None: + return net vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']} resp = self._convert(net) resp.update(vim_dict) diff --git a/vio/vio/pub/vim/vimapi/network/OperatePort.py b/vio/vio/pub/vim/vimapi/network/OperatePort.py index 5e98623..eb48702 100644 --- a/vio/vio/pub/vim/vimapi/network/OperatePort.py +++ b/vio/vio/pub/vim/vimapi/network/OperatePort.py @@ -59,6 +59,8 @@ class OperatePort(BaseNet): vim_info = self.get_vim_info(vimid) network = self.auth(vim_info) port = network.port_find(portid) + if port is None: + return port vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']} resp = self._convert(port) resp.update(vim_dict) diff --git a/vio/vio/pub/vim/vimapi/network/OperateSubnet.py b/vio/vio/pub/vim/vimapi/network/OperateSubnet.py index f697d2c..a4b0e9d 100644 --- a/vio/vio/pub/vim/vimapi/network/OperateSubnet.py +++ b/vio/vio/pub/vim/vimapi/network/OperateSubnet.py @@ -62,6 +62,8 @@ class OperateSubnet(BaseNet): vim_info = self.get_vim_info(vimid) network = self.auth(vim_info) subnet = network.subnet_get(subnetid) + if subnet is None: + return subnet vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']} resp = self._convert(subnet) resp.update(vim_dict) diff --git a/vio/vio/pub/vim/vimapi/nova/OperateFlavors.py b/vio/vio/pub/vim/vimapi/nova/OperateFlavors.py index 6341845..50303b2 100644 --- a/vio/vio/pub/vim/vimapi/nova/OperateFlavors.py +++ b/vio/vio/pub/vim/vimapi/nova/OperateFlavors.py @@ -3,7 +3,7 @@ # 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 @@ -34,9 +34,18 @@ class OperateFlavors(OperateNova): "swap": create_req.get('swap', 0), "is_public": create_req.get('isPublic', True) } - # TODO: support extraSpecs - return self.request('create_flavor', data, - project_id=project_id, **req), None + + flavor = self.request('create_flavor', data, + project_id=project_id, **req) + extra_specs_spec = {l['keyName']: l['value'] + for l in create_req.get('extraSpecs', [])} + extra_specs = {} + if extra_specs_spec: + extra_specs = self.request('create_flavor_extra_specs', data, + project_id=project_id, + flavor_id=flavor.id, + extra_specs=extra_specs_spec) + return flavor, extra_specs def list_flavors(self, data, project_id): flavors = self.request('list_flavors', data, project_id=project_id) @@ -59,6 +68,17 @@ class OperateFlavors(OperateNova): except exceptions.ResourceNotFound: return None, None + def find_flavor(self, data, project_id, flavor_id): + param = {'username': data['username'], + 'user_domain_name': 'default', + 'project_domain_name': 'default', + 'password': data['password'], + 'auth_url': data['url'], + 'project_id': project_id} + cc = self.compute(param) + flavor = cc.find_flavor(flavor_id, ignore_missing=True) + return flavor + def delete_flavor(self, data, project_id, flavor_id): return self.request('delete_flavor', data, project_id=project_id, flavor_id=flavor_id) diff --git a/vio/vio/pub/vim/vimapi/nova/OperateHypervisor.py b/vio/vio/pub/vim/vimapi/nova/OperateHypervisor.py index f6a1921..8b3de91 100644 --- a/vio/vio/pub/vim/vimapi/nova/OperateHypervisor.py +++ b/vio/vio/pub/vim/vimapi/nova/OperateHypervisor.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/pub/vim/vimapi/nova/OperateLimits.py b/vio/vio/pub/vim/vimapi/nova/OperateLimits.py index 6854f60..34e0584 100644 --- a/vio/vio/pub/vim/vimapi/nova/OperateLimits.py +++ b/vio/vio/pub/vim/vimapi/nova/OperateLimits.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/pub/vim/vimapi/nova/OperateNova.py b/vio/vio/pub/vim/vimapi/nova/OperateNova.py index 0bb6db0..11ce23b 100644 --- a/vio/vio/pub/vim/vimapi/nova/OperateNova.py +++ b/vio/vio/pub/vim/vimapi/nova/OperateNova.py @@ -3,7 +3,7 @@ # 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 @@ -29,7 +29,7 @@ class OperateNova(baseclient): 'project_domain_name': 'default', 'password': data['password'], 'auth_url': data['url']} - project_id = kwargs.get('project_id') + project_id = kwargs.pop('project_id', None) if project_id: param['project_id'] = project_id else: diff --git a/vio/vio/pub/vim/vimapi/nova/OperateServers.py b/vio/vio/pub/vim/vimapi/nova/OperateServers.py index 1a9972b..6fad7a5 100644 --- a/vio/vio/pub/vim/vimapi/nova/OperateServers.py +++ b/vio/vio/pub/vim/vimapi/nova/OperateServers.py @@ -3,7 +3,7 @@ # 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 @@ -98,6 +98,17 @@ class OperateServers(OperateNova): except exceptions.ResourceNotFound: return None + def find_server(self, data, project_id, server_id): + param = {'username': data['username'], + 'user_domain_name': 'default', + 'project_domain_name': 'default', + 'password': data['password'], + 'auth_url': data['url'], + 'project_id': project_id} + server = self.compute(param).find_server( + server_id, ignore_missing=True) + return server + def delete_server(self, data, project_id, server_id): param = {'username': data['username'], 'user_domain_name': 'default', diff --git a/vio/vio/pub/vim/vimapi/nova/OperateService.py b/vio/vio/pub/vim/vimapi/nova/OperateService.py index f1b0156..d29779a 100644 --- a/vio/vio/pub/vim/vimapi/nova/OperateService.py +++ b/vio/vio/pub/vim/vimapi/nova/OperateService.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/pub/vim/vimapi/nova/__init__.py b/vio/vio/pub/vim/vimapi/nova/__init__.py index de022ef..8770c43 100644 --- a/vio/vio/pub/vim/vimapi/nova/__init__.py +++ b/vio/vio/pub/vim/vimapi/nova/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/samples/__init__.py b/vio/vio/samples/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/samples/__init__.py +++ b/vio/vio/samples/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/samples/tests.py b/vio/vio/samples/tests.py index d0fcd3f..a3dbf4b 100644 --- a/vio/vio/samples/tests.py +++ b/vio/vio/samples/tests.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/samples/urls.py b/vio/vio/samples/urls.py index bba5529..0e7ba4c 100644 --- a/vio/vio/samples/urls.py +++ b/vio/vio/samples/urls.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/samples/views.py b/vio/vio/samples/views.py index fff6f39..a2ba070 100644 --- a/vio/vio/samples/views.py +++ b/vio/vio/samples/views.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/__init__.py b/vio/vio/swagger/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/__init__.py +++ b/vio/vio/swagger/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/image_utils.py b/vio/vio/swagger/image_utils.py index f31e76c..48a2771 100644 --- a/vio/vio/swagger/image_utils.py +++ b/vio/vio/swagger/image_utils.py @@ -13,14 +13,19 @@ def image_formatter(image): + image = image.to_dict() + properties = {} + properties['vmware_adaptertype'] = image.get("vmware_adaptertype") + properties['vmware_ostype'] = image.get("vmware_ostype") return { - 'id' : image.id, - 'name' : image.name, - 'imageType' : image.disk_format, - 'status' : image.status, - 'size' : image.size, - 'containerFormat' : image.container_format, - 'visibility' : image.visibility + 'id' : image.get("id"), + 'name' : image.get("name"), + 'imageType' : image.get("disk_format"), + 'status' : image.get("status"), + 'size' : image.get("size"), + 'containerFormat' : image.get("container_format"), + 'visibility' : image.get("visibility"), + 'properties' : properties } @@ -52,4 +57,6 @@ def req_body_formatter(body): param['container_format'] = body.get('containerFormat') param['visibility'] = body.get('visibility') param['imagePath'] = body.get('imagePath') - return param
\ No newline at end of file + param['vmware_adaptertype'] = body.get('properties').get('vmware_adaptertype') + param['vmware_ostype'] = body.get('properties').get('vmware_ostype') + return param diff --git a/vio/vio/swagger/nova_utils.py b/vio/vio/swagger/nova_utils.py index 922a49d..bfc02f3 100644 --- a/vio/vio/swagger/nova_utils.py +++ b/vio/vio/swagger/nova_utils.py @@ -68,8 +68,8 @@ def flavor_formatter(flavor, extra_specs): def extra_specs_formatter(extra_specs): - return { - } + return [{"keyName": k, "value": v} + for k, v in six.iteritems(extra_specs.extra_specs)] def server_limits_formatter(limits): diff --git a/vio/vio/swagger/views/__init__.py b/vio/vio/swagger/views/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/views/__init__.py +++ b/vio/vio/swagger/views/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/flavor/__init__.py b/vio/vio/swagger/views/flavor/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/views/flavor/__init__.py +++ b/vio/vio/swagger/views/flavor/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/flavor/views.py b/vio/vio/swagger/views/flavor/views.py index 0d46dd7..a9ae925 100644 --- a/vio/vio/swagger/views/flavor/views.py +++ b/vio/vio/swagger/views/flavor/views.py @@ -9,7 +9,6 @@ # 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 from rest_framework import status @@ -24,7 +23,11 @@ from vio.swagger import nova_utils class FlavorsView(APIView): def post(self, request, vimid, tenantid): - create_req = json.loads(request.body) + try: + create_req = json.loads(request.body) + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) vim_info = extsys.get_vim_by_id(vimid) data = {'vimid': vim_info['vimId'], @@ -33,15 +36,27 @@ class FlavorsView(APIView): 'password': vim_info['password'], 'url': vim_info['url'], 'project_name': vim_info['tenant']} - - flavors_op = OperateFlavors.OperateFlavors() - flavor, extra_specs = flavors_op.create_flavor(data, tenantid, create_req) - flavor_dict = nova_utils.flavor_formatter(flavor, extra_specs) - rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], - 'tenantId': tenantid, - 'returnCode': 1} + 'tenantId': tenantid} + flavor_name = create_req.get('name', None) + flavor_id = create_req.get('id', None) + flavors_op = OperateFlavors.OperateFlavors() + try: + target = flavor_id or flavor_name + flavor = flavors_op.find_flavor(data, tenantid, target) + if flavor: + flavor, extra_specs = flavors_op.get_flavor( + data, tenantid, flavor.id) + rsp['returnCode'] = 0 + else: + rsp['returnCode'] = 1 + flavor, extra_specs = flavors_op.create_flavor( + data, tenantid, create_req) + flavor_dict = nova_utils.flavor_formatter(flavor, extra_specs) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp.update(flavor_dict) return Response(data=rsp, status=status.HTTP_200_OK) @@ -55,9 +70,13 @@ class FlavorsView(APIView): 'project_name': vim_info['tenant']} flavors_op = OperateFlavors.OperateFlavors() - flavors_result = flavors_op.list_flavors(data, tenantid) - flavors_dict = [nova_utils.flavor_formatter(flavor, extra) - for flavor, extra in flavors_result] + try: + flavors_result = flavors_op.list_flavors(data, tenantid) + flavors_dict = [nova_utils.flavor_formatter(flavor, extra) + for flavor, extra in flavors_result] + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], @@ -78,8 +97,12 @@ class FlavorView(APIView): 'project_name': vim_info['tenant']} flavors_op = OperateFlavors.OperateFlavors() - flavor, extra_specs = flavors_op.get_flavor(data, tenantid, flavorid) - flavor_dict = nova_utils.flavor_formatter(flavor, extra_specs) + try: + flavor, extra_specs = flavors_op.get_flavor(data, tenantid, flavorid) + flavor_dict = nova_utils.flavor_formatter(flavor, extra_specs) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], @@ -96,5 +119,9 @@ class FlavorView(APIView): 'url': vim_info['url'], 'project_name': vim_info['tenant']} flavors_op = OperateFlavors.OperateFlavors() - flavors_op.delete_flavor(data, tenantid, flavorid) + try: + flavors_op.delete_flavor(data, tenantid, flavorid) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/vio/vio/swagger/views/hypervisor/views.py b/vio/vio/swagger/views/hypervisor/views.py index 0df9dc9..7946420 100644 --- a/vio/vio/swagger/views/hypervisor/views.py +++ b/vio/vio/swagger/views/hypervisor/views.py @@ -35,7 +35,11 @@ class HostView(APIView): 'project_name': vim_info['tenant']} hypervisor_op = OperateHypervisor.OperateHypervisor() - hv = hypervisor_op.get_hypervisor(data, hypervisor=hostname) + try: + hv = hypervisor_op.get_hypervisor(data, hypervisor=hostname) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], diff --git a/vio/vio/swagger/views/image/__init__.py b/vio/vio/swagger/views/image/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/views/image/__init__.py +++ b/vio/vio/swagger/views/image/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/image/views.py b/vio/vio/swagger/views/image/views.py index e57b4f2..9f166d9 100644 --- a/vio/vio/swagger/views/image/views.py +++ b/vio/vio/swagger/views/image/views.py @@ -70,7 +70,11 @@ class CreateListImagesView(APIView): def post(self, request, vimid, tenantid): vim_info = extsys.get_vim_by_id(vimid) - req_body = json.loads(request.body) + try: + req_body = json.loads(request.body) + 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) diff --git a/vio/vio/swagger/views/limits/views.py b/vio/vio/swagger/views/limits/views.py index 4e4e607..2b8be05 100644 --- a/vio/vio/swagger/views/limits/views.py +++ b/vio/vio/swagger/views/limits/views.py @@ -32,7 +32,11 @@ class LimitsView(APIView): 'project_name': vim_info['tenant']} servers_op = OperateLimits.OperateLimits() - server_limits = servers_op.get_limits(data, tenantid) + try: + server_limits = servers_op.get_limits(data, tenantid) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], diff --git a/vio/vio/swagger/views/network/views.py b/vio/vio/swagger/views/network/views.py index b15b922..0427160 100644 --- a/vio/vio/swagger/views/network/views.py +++ b/vio/vio/swagger/views/network/views.py @@ -28,9 +28,21 @@ class CreateNetworkView(APIView): logger.info("Enter %s, method is %s, vim_id is %s", syscomm.fun_name(), request.method, vimid) net = OperateNetwork.OperateNetwork() - body = json.loads(request.body) try: - resp = net.create_network(vimid, tenantid, body) + body = json.loads(request.body) + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + try: + network_name = body.get('name') + network_id = body.get('id', None) + target = network_id or network_name + resp = net.list_network(vimid, tenantid, target) + if resp: + resp['returnCode'] = 0 + else: + resp = net.create_network(vimid, tenantid, body) + resp['returnCode'] = 1 return Response(data=resp, status=status.HTTP_202_ACCEPTED) except Exception as e: return Response(data={'error': str(e)}, diff --git a/vio/vio/swagger/views/port/views.py b/vio/vio/swagger/views/port/views.py index 7d9d56e..64b0fe7 100644 --- a/vio/vio/swagger/views/port/views.py +++ b/vio/vio/swagger/views/port/views.py @@ -28,9 +28,21 @@ class CreatePortView(APIView): logger.info("Enter %s, method is %s, vim_id is %s", syscomm.fun_name(), request.method, vimid) port = OperatePort.OperatePort() - body = json.loads(request.body) try: - resp = port.create_port(vimid, tenantid, body) + body = json.loads(request.body) + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + try: + port_name = body.get('name') + port_id = body.get('id', None) + target = port_id or port_name + resp = port.list_port(vimid, tenantid, target) + if resp: + resp['returnCode'] = 0 + else: + resp = port.create_port(vimid, tenantid, body) + resp['returnCode'] = 1 return Response(data=resp, status=status.HTTP_202_ACCEPTED) except Exception as e: return Response(data={'error': str(e)}, diff --git a/vio/vio/swagger/views/server/__init__.py b/vio/vio/swagger/views/server/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/views/server/__init__.py +++ b/vio/vio/swagger/views/server/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/server/views.py b/vio/vio/swagger/views/server/views.py index 785208d..211644b 100644 --- a/vio/vio/swagger/views/server/views.py +++ b/vio/vio/swagger/views/server/views.py @@ -9,7 +9,6 @@ # 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 @@ -27,7 +26,11 @@ logger = logging.getLogger(__name__) class ListServersView(APIView): def post(self, request, vimid, tenantid): - create_req = json.loads(request.body) + try: + create_req = json.loads(request.body) + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) vim_info = extsys.get_vim_by_id(vimid) data = {'vimid': vim_info['vimId'], @@ -36,18 +39,26 @@ class ListServersView(APIView): 'password': vim_info['password'], 'url': vim_info['url'], 'project_name': vim_info['tenant']} - + rsp = {'vimId': vim_info['vimId'], + 'vimName': vim_info['name'], + 'tenantId': tenantid} servers_op = OperateServers.OperateServers() + server_name = create_req.get('name', None) + server_id = create_req.get('id', None) try: - server = servers_op.create_server(data, tenantid, create_req) + target = server_id or server_name + server = servers_op.find_server(data, tenantid, target) + # Find server only returns id and name, fetch all attributes again + if server: + server = servers_op.get_server(data, tenantid, server.id) + rsp['returnCode'] = 0 + else: + rsp['returnCode'] = 1 + server = servers_op.create_server(data, tenantid, create_req) except Exception as ex: return Response(data=str(ex), status=status.HTTP_500_INTERNAL_SERVER_ERROR) server_dict = nova_utils.server_formatter(server) - - rsp = {'vimId': vim_info['vimId'], - 'vimName': vim_info['name'], - 'tenantId': tenantid} rsp.update(server_dict) return Response(data=rsp, status=status.HTTP_200_OK) @@ -61,7 +72,11 @@ class ListServersView(APIView): 'project_name': vim_info['tenant']} servers_op = OperateServers.OperateServers() - servers = servers_op.list_servers(data, tenantid) + try: + servers = servers_op.list_servers(data, tenantid) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) servers_resp = [] for server in servers: @@ -88,9 +103,13 @@ class GetServerView(APIView): 'project_name': vim_info['tenant']} servers_op = OperateServers.OperateServers() - server = servers_op.get_server(data, tenantid, serverid) - intfs = servers_op.list_server_interfaces(data, tenantid, server) - server_dict = nova_utils.server_formatter(server, interfaces=intfs) + try: + server = servers_op.get_server(data, tenantid, serverid) + intfs = servers_op.list_server_interfaces(data, tenantid, server) + server_dict = nova_utils.server_formatter(server, interfaces=intfs) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], @@ -108,5 +127,9 @@ class GetServerView(APIView): 'password': vim_info['password'], 'url': vim_info['url'], 'project_name': vim_info['tenant']} - servers_op.delete_server(data, tenantid, serverid) + try: + servers_op.delete_server(data, tenantid, serverid) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/vio/vio/swagger/views/service/views.py b/vio/vio/swagger/views/service/views.py index cdc8103..35dc4c9 100644 --- a/vio/vio/swagger/views/service/views.py +++ b/vio/vio/swagger/views/service/views.py @@ -34,11 +34,16 @@ class HostsView(APIView): 'project_name': vim_info['tenant']} services_op = OperateService.OperateService() + try: + hosts = [nova_utils.service_formatter(svc) + for svc in services_op.list_services(data, tenantid)] + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {'vimid': vim_info['vimId'], 'vimName': vim_info['name'], 'tenantId': tenantid, - 'hosts': [nova_utils.service_formatter(svc) - for svc in services_op.list_services(data, tenantid)]} + 'hosts': hosts} return Response(data=rsp, status=status.HTTP_200_OK) diff --git a/vio/vio/swagger/views/subnet/views.py b/vio/vio/swagger/views/subnet/views.py index 9678627..feb9512 100644 --- a/vio/vio/swagger/views/subnet/views.py +++ b/vio/vio/swagger/views/subnet/views.py @@ -28,9 +28,21 @@ class CreateSubnetView(APIView): logger.info("Enter %s, method is %s, vim_id is %s", syscomm.fun_name(), request.method, vimid) subnet = OperateSubnet.OperateSubnet() - body = json.loads(request.body) try: - resp = subnet.create_subnet(vimid, tenantid, body) + body = json.loads(request.body) + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + try: + subnet_name = body.get('name') + subnet_id = body.get('id', None) + target = subnet_id or subnet_name + resp = subnet.list_subnet(vimid, tenantid, target) + if resp: + resp['returnCode'] = 0 + else: + resp = subnet.create_subnet(vimid, tenantid, body) + resp['returnCode'] = 1 return Response(data=resp, status=status.HTTP_202_ACCEPTED) except Exception as e: return Response(data={'error': str(e)}, diff --git a/vio/vio/swagger/views/swagger_json.py b/vio/vio/swagger/views/swagger_json.py index 546f95e..cf401b5 100644 --- a/vio/vio/swagger/views/swagger_json.py +++ b/vio/vio/swagger/views/swagger_json.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/tenant/__init__.py b/vio/vio/swagger/views/tenant/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/views/tenant/__init__.py +++ b/vio/vio/swagger/views/tenant/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/tenant/views.py b/vio/vio/swagger/views/tenant/views.py index 44f175d..ba08589 100644 --- a/vio/vio/swagger/views/tenant/views.py +++ b/vio/vio/swagger/views/tenant/views.py @@ -36,7 +36,11 @@ class ListTenantsView(APIView): data['project_name'] = vim_info['tenant'] tenant_instance = OperateTenant.OperateTenant() - projects = tenant_instance.get_projects(data) + try: + projects = tenant_instance.get_projects(data) + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {} rsp['vimid'] = vim_info['vimId'] diff --git a/vio/vio/swagger/views/volume/__init__.py b/vio/vio/swagger/views/volume/__init__.py index 12fa1e8..a8fe66a 100644 --- a/vio/vio/swagger/views/volume/__init__.py +++ b/vio/vio/swagger/views/volume/__init__.py @@ -3,7 +3,7 @@ # 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 diff --git a/vio/vio/swagger/views/volume/views.py b/vio/vio/swagger/views/volume/views.py index 86ed2c7..ce56de7 100644 --- a/vio/vio/swagger/views/volume/views.py +++ b/vio/vio/swagger/views/volume/views.py @@ -81,11 +81,13 @@ class CreateListVolumeView(APIView): def post(self, request, vimid, tenantid): vim_info = extsys.get_vim_by_id(vimid) volume_op = OperateVolume.OperateVolume(vim_info) - try: - volumes_detail = volume_op.get_vim_volumes() json_body = json.loads(request.body) - + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + try: + volumes_detail = volume_op.get_vim_volumes() vim_rsp = volume_utils.vim_formatter(vim_info, tenantid) for volume in volumes_detail: if volume.name == json_body.get('name'): |