From 4bf582a849955d863fd0615af834c750397bb10d Mon Sep 17 00:00:00 2001 From: chenke Date: Tue, 7 Jan 2020 15:22:26 +0800 Subject: [PATCH] Use ResourceNotFound replace RP and Image NotFound This is a series of optimization for exception. In fact, we only need to use the ResourceNotFound exception to fit NotFound Exception. This patch also adds dependency package cursive which is used in [1]. [1]. https://github.com/openstack/cyborg/blob/6740c3c0c50280f09ab0e32c9e033240fc65c8d7/cyborg/image/glance.py#L30 Change-Id: I9e80dcfed54147c942f90c696e483fa6db842dde Story: 2007045 Task: 37968 --- cyborg/common/exception.py | 8 -------- cyborg/image/glance.py | 4 +++- cyborg/tests/unit/image/__init__.py | 0 cyborg/tests/unit/image/test_glance.py | 28 ++++++++++++++++++++++++++ requirements.txt | 1 + test-requirements.txt | 1 + 6 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 cyborg/tests/unit/image/__init__.py create mode 100644 cyborg/tests/unit/image/test_glance.py diff --git a/cyborg/common/exception.py b/cyborg/common/exception.py index f4f94f41..cfe57c8f 100644 --- a/cyborg/common/exception.py +++ b/cyborg/common/exception.py @@ -297,10 +297,6 @@ class ResourceProviderUpdateFailed(CyborgException): "%(error)s") -class ResourceProviderNotFound(NotFound): - msg_fmt = _("No such resource provider %(name_or_uuid)s.") - - class ResourceProviderSyncFailed(CyborgException): msg_fmt = _("Failed to synchronize the placement service with resource " "provider information supplied by the compute host.") @@ -389,10 +385,6 @@ class ImageNotAuthorized(CyborgException): msg_fmt = _("Not authorized for image %(image_id)s.") -class ImageNotFound(NotFound): - msg_fmt = _("Image %(image_id)s could not be found.") - - class ImageBadRequest(Invalid): msg_fmt = _("Request of image %(image_id)s got BadRequest response: " "%(response)s") diff --git a/cyborg/image/glance.py b/cyborg/image/glance.py index 9ae64ebc..d08b83c2 100644 --- a/cyborg/image/glance.py +++ b/cyborg/image/glance.py @@ -643,7 +643,9 @@ def _translate_image_exception(image_id, exc_value): glanceclient.exc.Unauthorized)): return exception.ImageNotAuthorized(image_id=image_id) if isinstance(exc_value, glanceclient.exc.NotFound): - return exception.ImageNotFound(image_id=image_id) + return exception.ResourceNotFound( + resource='Image', + msg='with uuid=%s' % image_id) if isinstance(exc_value, glanceclient.exc.BadRequest): return exception.ImageBadRequest(image_id=image_id, response=six.text_type(exc_value)) diff --git a/cyborg/tests/unit/image/__init__.py b/cyborg/tests/unit/image/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cyborg/tests/unit/image/test_glance.py b/cyborg/tests/unit/image/test_glance.py new file mode 100644 index 00000000..da08c62e --- /dev/null +++ b/cyborg/tests/unit/image/test_glance.py @@ -0,0 +1,28 @@ +# Copyright 2020 ZTE Corporation. +# All Rights Reserved. +# +# 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. See the +# License for the specific language governing permissions and limitations +# under the License. + +import glanceclient + +from cyborg.common import exception +from cyborg.image import glance +from cyborg.tests.unit.db import base + + +class TestExceptionTranslations(base.DbTestCase): + + def test_client_notfound_converts_to_imagenotfound(self): + in_exc = glanceclient.exc.NotFound('123') + out_exc = glance._translate_image_exception('123', in_exc) + self.assertIsInstance(out_exc, exception.ResourceNotFound) diff --git a/requirements.txt b/requirements.txt index 01757913..5e9c14f4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,3 +29,4 @@ psutil>=3.2.2 # BSD mock>=2.0.0 # BSD python-glanceclient>=2.3.0 # Apache-2.0 oslo.privsep>=1.32.0 # Apache-2.0 +cursive>=0.2.1 # Apache-2.0 diff --git a/test-requirements.txt b/test-requirements.txt index 61d06529..f592ead7 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -19,3 +19,4 @@ doc8>=0.6.0 # Apache-2.0 pycodestyle>=2.0.0 # MIT License Pygments>=2.2.0 # BSD license os-resource-classes>=0.5.0 # Apache-2.0 +cursive>=0.2.1 # Apache-2.0