Browse Source

Don't use oslo-incubator in glance client plugin

There is plan to remove all oslo incubator code from the
clients. This removes the usage of deprecated
openstack.common.apiclient.exceptions in glance client
plugin.

The available glance HTTPNotFound exception is not usable
for validations(no message) and there is no UniqueMatch
exception.

This adds a new custom client_exception module with two
exceptions to ensure that the error messages remain the
same as before.

Change-Id: I5b32eef047a1fe38382f649532c8bb8a910315e5
tags/8.0.0.0b2
rabi 2 years ago
parent
commit
e1237f48ef

+ 27
- 0
heat/engine/clients/client_exception.py View File

@@ -0,0 +1,27 @@
1
+#
2
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
3
+#    not use this file except in compliance with the License. You may obtain
4
+#    a copy of the License at
5
+#
6
+#         http://www.apache.org/licenses/LICENSE-2.0
7
+#
8
+#    Unless required by applicable law or agreed to in writing, software
9
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
+#    License for the specific language governing permissions and limitations
12
+#    under the License.
13
+
14
+from heat.common import exception
15
+from heat.common.i18n import _
16
+
17
+
18
+class EntityMatchNotFound(exception.HeatException):
19
+    msg_fmt = _("No %(entity)s matching %(args)s.")
20
+
21
+    def __init__(self, entity=None, args=None, **kwargs):
22
+        super(EntityMatchNotFound, self).__init__(entity=entity, args=args,
23
+                                                  **kwargs)
24
+
25
+
26
+class EntityUniqueMatchNotFound(EntityMatchNotFound):
27
+    msg_fmt = _("No %(entity)s unique match found for %(args)s.")

+ 11
- 16
heat/engine/clients/os/glance.py View File

@@ -13,9 +13,8 @@
13 13
 
14 14
 from glanceclient import client as gc
15 15
 from glanceclient import exc
16
-from glanceclient.openstack.common.apiclient import exceptions
17 16
 
18
-from heat.common.i18n import _
17
+from heat.engine.clients import client_exception
19 18
 from heat.engine.clients import client_plugin
20 19
 from heat.engine.clients import os as os_client
21 20
 from heat.engine import constraints
@@ -25,7 +24,7 @@ CLIENT_NAME = 'glance'
25 24
 
26 25
 class GlanceClientPlugin(client_plugin.ClientPlugin):
27 26
 
28
-    exceptions_module = [exceptions, exc]
27
+    exceptions_module = [client_exception, exc]
29 28
 
30 29
     service_types = [IMAGE] = ['image']
31 30
 
@@ -47,17 +46,11 @@ class GlanceClientPlugin(client_plugin.ClientPlugin):
47 46
         matches = list(self._findall_with_attr(entity, **kwargs))
48 47
         num_matches = len(matches)
49 48
         if num_matches == 0:
50
-            msg = _("No %(name)s matching %(args)s.") % {
51
-                'name': entity,
52
-                'args': kwargs
53
-            }
54
-            raise exceptions.NotFound(msg)
49
+            raise client_exception.EntityMatchNotFound(entity=entity,
50
+                                                       args=kwargs)
55 51
         elif num_matches > 1:
56
-            msg = _("No %(name)s unique match found for %(args)s.") % {
57
-                'name': entity,
58
-                'args': kwargs
59
-            }
60
-            raise exceptions.NoUniqueMatch(msg)
52
+            raise client_exception.EntityUniqueMatchNotFound(entity=entity,
53
+                                                             args=kwargs)
61 54
         else:
62 55
             return matches[0]
63 56
 
@@ -68,13 +61,14 @@ class GlanceClientPlugin(client_plugin.ClientPlugin):
68 61
         return func.list(**filters)
69 62
 
70 63
     def is_not_found(self, ex):
71
-        return isinstance(ex, (exceptions.NotFound, exc.HTTPNotFound))
64
+        return isinstance(ex, (client_exception.EntityMatchNotFound,
65
+                               exc.HTTPNotFound))
72 66
 
73 67
     def is_over_limit(self, ex):
74 68
         return isinstance(ex, exc.HTTPOverLimit)
75 69
 
76 70
     def is_conflict(self, ex):
77
-        return isinstance(ex, (exceptions.Conflict, exc.Conflict))
71
+        return isinstance(ex, exc.Conflict)
78 72
 
79 73
     def find_image_by_name_or_id(self, image_identifier):
80 74
         """Return the ID for the specified image name or identifier.
@@ -104,7 +98,8 @@ class GlanceClientPlugin(client_plugin.ClientPlugin):
104 98
 
105 99
 
106 100
 class ImageConstraint(constraints.BaseCustomConstraint):
107
-    expected_exceptions = (exceptions.NotFound, exceptions.NoUniqueMatch)
101
+    expected_exceptions = (client_exception.EntityMatchNotFound,
102
+                           client_exception.EntityUniqueMatchNotFound)
108 103
 
109 104
     resource_client_name = CLIENT_NAME
110 105
     resource_getter_name = 'find_image_by_name_or_id'

+ 13
- 9
heat/tests/aws/test_instance.py View File

@@ -422,9 +422,10 @@ class InstancesTest(common.HeatTestCase):
422 422
         resource_defns = tmpl.resource_definitions(stack)
423 423
         instance = instances.Instance('instance_create_image_err',
424 424
                                       resource_defns['WebServer'], stack)
425
-
426
-        self._mock_get_image_id_fail('Slackware',
427
-                                     glance.exceptions.NotFound())
425
+        self._mock_get_image_id_fail(
426
+            'Slackware',
427
+            glance.client_exception.EntityMatchNotFound(
428
+                entity='image', args='Slackware'))
428 429
         self.stub_VolumeConstraint_validate()
429 430
         self.stub_FlavorConstraint_validate()
430 431
         self.stub_KeypairConstraint_validate()
@@ -436,7 +437,7 @@ class InstancesTest(common.HeatTestCase):
436 437
         self.assertEqual(
437 438
             "StackValidationFailed: resources.instance_create_image_err: "
438 439
             "Property error: WebServer.Properties.ImageId: "
439
-            "Error validating value 'Slackware': Not Found (HTTP 404)",
440
+            "Error validating value 'Slackware': No image matching Slackware.",
440 441
             six.text_type(error))
441 442
 
442 443
         self.m.VerifyAll()
@@ -452,9 +453,10 @@ class InstancesTest(common.HeatTestCase):
452 453
         instance = instances.Instance('instance_create_image_err',
453 454
                                       resource_defns['WebServer'], stack)
454 455
 
455
-        msg = 'No image unique match found for CentOS 5.2.'
456
-        self._mock_get_image_id_fail('CentOS 5.2',
457
-                                     glance.exceptions.NoUniqueMatch(msg))
456
+        self._mock_get_image_id_fail(
457
+            'CentOS 5.2',
458
+            glance.client_exception.EntityUniqueMatchNotFound(
459
+                entity='image', args='CentOS 5.2'))
458 460
 
459 461
         self.stub_KeypairConstraint_validate()
460 462
         self.stub_SnapshotConstraint_validate()
@@ -483,7 +485,9 @@ class InstancesTest(common.HeatTestCase):
483 485
         instance = instances.Instance('instance_create_image_err',
484 486
                                       resource_defns['WebServer'], stack)
485 487
 
486
-        self._mock_get_image_id_fail('1', glance.exceptions.NotFound())
488
+        self._mock_get_image_id_fail(
489
+            '1', glance.client_exception.EntityMatchNotFound(entity='image',
490
+                                                             args='1'))
487 491
 
488 492
         self.stub_VolumeConstraint_validate()
489 493
         self.stub_FlavorConstraint_validate()
@@ -496,7 +500,7 @@ class InstancesTest(common.HeatTestCase):
496 500
         self.assertEqual(
497 501
             "StackValidationFailed: resources.instance_create_image_err: "
498 502
             "Property error: WebServer.Properties.ImageId: "
499
-            "Error validating value '1': Not Found (HTTP 404)",
503
+            "Error validating value '1': No image matching 1.",
500 504
             six.text_type(error))
501 505
 
502 506
         self.m.VerifyAll()

+ 2
- 10
heat/tests/clients/test_clients.py View File

@@ -16,7 +16,6 @@ from ceilometerclient import exc as ceil_exc
16 16
 from ceilometerclient.openstack.common.apiclient import exceptions as c_a_exc
17 17
 from cinderclient import exceptions as cinder_exc
18 18
 from glanceclient import exc as glance_exc
19
-from glanceclient.openstack.common.apiclient import exceptions as g_a_exc
20 19
 from heatclient import client as heatclient
21 20
 from heatclient import exc as heat_exc
22 21
 from keystoneauth1 import exceptions as keystone_exc
@@ -34,6 +33,7 @@ from zaqarclient.transport import errors as zaqar_exc
34 33
 
35 34
 from heat.common import exception
36 35
 from heat.engine import clients
36
+from heat.engine.clients import client_exception
37 37
 from heat.engine.clients import client_plugin
38 38
 from heat.tests import common
39 39
 from heat.tests import fakes
@@ -490,7 +490,7 @@ class TestIsNotFound(common.HeatTestCase):
490 490
             is_client_exception=True,
491 491
             is_conflict=False,
492 492
             plugin='glance',
493
-            exception=lambda: g_a_exc.NotFound(),
493
+            exception=lambda: client_exception.EntityMatchNotFound(),
494 494
         )),
495 495
         ('glance_not_found_2', dict(
496 496
             is_not_found=True,
@@ -516,14 +516,6 @@ class TestIsNotFound(common.HeatTestCase):
516 516
             plugin='glance',
517 517
             exception=lambda: glance_exc.HTTPOverLimit(details='over'),
518 518
         )),
519
-        ('glance_conflict_1', dict(
520
-            is_not_found=False,
521
-            is_over_limit=False,
522
-            is_client_exception=True,
523
-            is_conflict=True,
524
-            plugin='glance',
525
-            exception=lambda: g_a_exc.Conflict(),
526
-        )),
527 519
         ('glance_conflict_1', dict(
528 520
             is_not_found=False,
529 521
             is_over_limit=False,

+ 6
- 6
heat/tests/clients/test_glance_client.py View File

@@ -14,9 +14,9 @@
14 14
 import uuid
15 15
 
16 16
 from glanceclient import exc
17
-from glanceclient.openstack.common.apiclient import exceptions
18 17
 import mock
19 18
 
19
+from heat.engine.clients import client_exception as exception
20 20
 from heat.engine.clients.os import glance
21 21
 from heat.tests import common
22 22
 from heat.tests import utils
@@ -53,10 +53,10 @@ class GlanceUtilsTest(common.HeatTestCase):
53 53
                          self.glance_plugin.find_image_by_name_or_id(img_id))
54 54
         self.assertEqual(img_id,
55 55
                          self.glance_plugin.find_image_by_name_or_id(img_name))
56
-        self.assertRaises(exceptions.NotFound,
56
+        self.assertRaises(exception.EntityMatchNotFound,
57 57
                           self.glance_plugin.find_image_by_name_or_id,
58 58
                           'noimage')
59
-        self.assertRaises(exceptions.NoUniqueMatch,
59
+        self.assertRaises(exception.EntityUniqueMatchNotFound,
60 60
                           self.glance_plugin.find_image_by_name_or_id,
61 61
                           'myfakeimage')
62 62
 
@@ -72,9 +72,9 @@ class ImageConstraintTest(common.HeatTestCase):
72 72
         self.constraint = glance.ImageConstraint()
73 73
 
74 74
     def test_validation(self):
75
-        self.mock_find_image.side_effect = ["id1",
76
-                                            exceptions.NotFound(),
77
-                                            exceptions.NoUniqueMatch()]
75
+        self.mock_find_image.side_effect = [
76
+            "id1", exception.EntityMatchNotFound(),
77
+            exception.EntityUniqueMatchNotFound()]
78 78
         self.assertTrue(self.constraint.validate("foo", self.ctx))
79 79
         self.assertFalse(self.constraint.validate("bar", self.ctx))
80 80
         self.assertFalse(self.constraint.validate("baz", self.ctx))

+ 19
- 13
heat/tests/openstack/nova/test_server.py View File

@@ -546,17 +546,18 @@ class ServersTest(common.HeatTestCase):
546 546
         (tmpl, stack) = self._setup_test_stack(stack_name)
547 547
         mock_image = self.patchobject(glance.GlanceClientPlugin,
548 548
                                       'find_image_by_name_or_id')
549
-        mock_image.side_effect = [glance.exceptions.NotFound(
550
-            'Image Slackware Not Found')]
549
+        mock_image.side_effect = [
550
+            glance.client_exception.EntityMatchNotFound(
551
+                entity='image', args={'name': 'Slackware'})]
551 552
         # Init a server with non exist image name
552 553
         tmpl['Resources']['WebServer']['Properties']['image'] = 'Slackware'
553 554
         resource_defns = tmpl.resource_definitions(stack)
554 555
         server = servers.Server('WebServer',
555 556
                                 resource_defns['WebServer'], stack)
556 557
 
557
-        error = self.assertRaises(glance.exceptions.NotFound,
558
+        error = self.assertRaises(glance.client_exception.EntityMatchNotFound,
558 559
                                   scheduler.TaskRunner(server.create))
559
-        self.assertEqual("Image Slackware Not Found (HTTP 404)",
560
+        self.assertEqual("No image matching {'name': 'Slackware'}.",
560 561
                          six.text_type(error))
561 562
 
562 563
     def test_server_duplicate_image_name_err(self):
@@ -564,15 +565,17 @@ class ServersTest(common.HeatTestCase):
564 565
         (tmpl, stack) = self._setup_test_stack(stack_name)
565 566
         mock_image = self.patchobject(glance.GlanceClientPlugin,
566 567
                                       'find_image_by_name_or_id')
567
-        mock_image.side_effect = [glance.exceptions.NoUniqueMatch(
568
-            'No image unique match found for CentOS 5.2.')]
568
+        mock_image.side_effect = [
569
+            glance.client_exception.EntityUniqueMatchNotFound(
570
+                entity='image', args='CentOS 5.2')]
569 571
         tmpl['Resources']['WebServer']['Properties']['image'] = 'CentOS 5.2'
570 572
         resource_defns = tmpl.resource_definitions(stack)
571 573
         server = servers.Server('WebServer',
572 574
                                 resource_defns['WebServer'], stack)
573 575
 
574
-        error = self.assertRaises(glance.exceptions.NoUniqueMatch,
575
-                                  scheduler.TaskRunner(server.create))
576
+        error = self.assertRaises(
577
+            glance.client_exception.EntityUniqueMatchNotFound,
578
+            scheduler.TaskRunner(server.create))
576 579
         self.assertEqual('No image unique match found for CentOS 5.2.',
577 580
                          six.text_type(error))
578 581
 
@@ -2655,8 +2658,9 @@ class ServersTest(common.HeatTestCase):
2655 2658
         server = servers.Server('image_not_found',
2656 2659
                                 resource_defns['WebServer'], stack)
2657 2660
         self.patchobject(glance.GlanceClientPlugin, 'get_image',
2658
-                         side_effect=[glance.exceptions.NotFound(),
2659
-                                      self.mock_image])
2661
+                         side_effect=[
2662
+                             glance.client_exception.EntityMatchNotFound,
2663
+                             self.mock_image])
2660 2664
         self.patchobject(nova.NovaClientPlugin, 'get_flavor',
2661 2665
                          side_effect=nova.exceptions.NotFound(''))
2662 2666
         self.assertIsNone(server.validate())
@@ -3570,7 +3574,8 @@ class ServersTest(common.HeatTestCase):
3570 3574
         server = self._create_test_server(return_server,
3571 3575
                                           'my_server')
3572 3576
 
3573
-        ex = glance.exceptions.NotFound()
3577
+        ex = glance.client_exception.EntityMatchNotFound(entity='image',
3578
+                                                         args='Update Image')
3574 3579
         self.patchobject(glance.GlanceClientPlugin,
3575 3580
                          'find_image_by_name_or_id',
3576 3581
                          side_effect=[1, ex])
@@ -3580,8 +3585,9 @@ class ServersTest(common.HeatTestCase):
3580 3585
 
3581 3586
         # update
3582 3587
         updater = scheduler.TaskRunner(server.update, update_template)
3583
-        err = self.assertRaises(glance.exceptions.NotFound, updater)
3584
-        self.assertEqual('Not Found (HTTP 404)',
3588
+        err = self.assertRaises(glance.client_exception.EntityMatchNotFound,
3589
+                                updater)
3590
+        self.assertEqual('No image matching Update Image.',
3585 3591
                          six.text_type(err))
3586 3592
 
3587 3593
     def test_server_snapshot(self):

Loading…
Cancel
Save