From bee9efee53a4359ba40bc6054508865a5cd8b6e8 Mon Sep 17 00:00:00 2001 From: Randall Burt Date: Mon, 22 Dec 2014 13:37:37 -0600 Subject: [PATCH] Handle 404 on Zaqar Queue delete Change-Id: Iacff8adadc59693e9f8e4132f369bf08956affe3 Closes-Bug: 1404375 --- contrib/heat_zaqar/heat_zaqar/client.py | 7 +++++ .../heat_zaqar/heat_zaqar/resources/queue.py | 9 ++++--- .../heat_zaqar/heat_zaqar/tests/test_queue.py | 26 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/contrib/heat_zaqar/heat_zaqar/client.py b/contrib/heat_zaqar/heat_zaqar/client.py index 2a94d57e75..fcd3af01c4 100644 --- a/contrib/heat_zaqar/heat_zaqar/client.py +++ b/contrib/heat_zaqar/heat_zaqar/client.py @@ -18,14 +18,18 @@ LOG = logging.getLogger(__name__) try: from zaqarclient.queues.v1 import client as zaqarclient + from zaqarclient.transport import errors as zaqar_errors except ImportError: zaqarclient = None + zaqar_errors = None from heat.engine.clients import client_plugin class ZaqarClientPlugin(client_plugin.ClientPlugin): + exception_module = zaqar_errors + def _create(self): con = self.context @@ -47,3 +51,6 @@ class ZaqarClientPlugin(client_plugin.ClientPlugin): client = zaqarclient.Client(url=endpoint, conf=conf) return client + + def is_not_found(self, ex): + return isinstance(ex, zaqar_errors.ResourceNotFound) diff --git a/contrib/heat_zaqar/heat_zaqar/resources/queue.py b/contrib/heat_zaqar/heat_zaqar/resources/queue.py index f2c1c9c650..a2635c8191 100644 --- a/contrib/heat_zaqar/heat_zaqar/resources/queue.py +++ b/contrib/heat_zaqar/heat_zaqar/resources/queue.py @@ -104,9 +104,12 @@ class ZaqarQueue(resource.Resource): """Delete a zaqar message queue.""" if not self.resource_id: return - - queue = self.client().queue(self.resource_id, auto_create=False) - queue.delete() + try: + queue = self.client().queue(self.resource_id, auto_create=False) + except Exception as exc: + self.client_plugin().ignore_not_found(exc) + else: + queue.delete() def href(self): api_endpoint = self.client().api_url diff --git a/contrib/heat_zaqar/heat_zaqar/tests/test_queue.py b/contrib/heat_zaqar/heat_zaqar/tests/test_queue.py index e6890f4a90..f23a6c7b47 100644 --- a/contrib/heat_zaqar/heat_zaqar/tests/test_queue.py +++ b/contrib/heat_zaqar/heat_zaqar/tests/test_queue.py @@ -11,12 +11,14 @@ # License for the specific language governing permissions and limitations # under the License. +import mock import six from heat.common import exception from heat.common import template_format from heat.engine import parser from heat.engine import resource +from heat.engine import rsrc_defn from heat.engine import scheduler from heat.engine import template from heat.tests import common @@ -24,6 +26,11 @@ from heat.tests import utils from ..resources import queue # noqa +try: + from zaqarclient.transport.errors import ResourceNotFound # noqa +except ImportError: + ResourceNotFound = Exception + wp_template = ''' { "AWSTemplateFormatVersion" : "2010-09-09", @@ -183,6 +190,25 @@ class ZaqarMessageQueueTest(common.HeatTestCase): scheduler.TaskRunner(queue.delete)() self.m.VerifyAll() + @mock.patch.object(queue.ZaqarQueue, "client") + @mock.patch.object(queue.ZaqarQueue, "client_plugin") + def test_delete_not_found(self, mockplugin, mockclient): + + mock_def = mock.Mock(spec=rsrc_defn.ResourceDefinition) + mock_stack = mock.Mock() + mock_stack.db_resource_get.return_value = None + + mockclient.return_value.queue.side_effect = ResourceNotFound + mockplugin.return_value.ignore_not_found.return_value = None + zplugin = queue.ZaqarQueue("test_delete_not_found", mock_def, + mock_stack) + zplugin.resource_id = "test_delete_not_found" + zplugin.handle_delete() + mockclient.return_value.queue.assert_called_once_with( + "test_delete_not_found", auto_create=False) + mockplugin.return_value.ignore_not_found.assert_called_once_with( + mock.ANY) + def test_update_in_place(self): t = template_format.parse(wp_template) self.parse_stack(t)