From e5df5f1841de13fbda289270353cb2558ac043da Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Tue, 17 Jun 2014 14:47:17 +1200 Subject: [PATCH] Glance, swift use ClientPlugin.ignore_not_found Change-Id: I24a7ebb10f2c9ff85cc2bdd6ce4a925c49b5ec3b --- heat/engine/resources/glance_image.py | 8 ++++---- heat/engine/resources/s3.py | 20 ++++++++------------ heat/engine/resources/swift.py | 23 +++++++++++++---------- heat/tests/test_s3.py | 18 ++++++++++++++++++ heat/tests/test_swift.py | 18 ++++++++++++++++++ 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/heat/engine/resources/glance_image.py b/heat/engine/resources/glance_image.py index 458b5c385..9304353f9 100644 --- a/heat/engine/resources/glance_image.py +++ b/heat/engine/resources/glance_image.py @@ -11,8 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -from glanceclient import exc as glance_exceptions - from heat.engine import constraints from heat.engine import properties from heat.engine import resource @@ -98,6 +96,8 @@ class GlanceImage(resource.Resource): ), } + default_client_name = 'glance' + def handle_create(self): args = dict((k, v) for k, v in self.properties.items() if v is not None) @@ -115,8 +115,8 @@ class GlanceImage(resource.Resource): try: self.glance().images.delete(self.resource_id) - except glance_exceptions.NotFound: - pass + except Exception as ex: + self.client_plugin().ignore_not_found(ex) self.resource_id_set(None) diff --git a/heat/engine/resources/s3.py b/heat/engine/resources/s3.py index f24ec9451..ae2ff90e9 100644 --- a/heat/engine/resources/s3.py +++ b/heat/engine/resources/s3.py @@ -12,15 +12,11 @@ # under the License. from six.moves.urllib import parse as urlparse -from swiftclient import exceptions as swift_exceptions from heat.engine import attributes from heat.engine import constraints from heat.engine import properties from heat.engine import resource -from heat.openstack.common import log as logging - -LOG = logging.getLogger(__name__) class S3Bucket(resource.Resource): @@ -108,6 +104,8 @@ class S3Bucket(resource.Resource): ), } + default_client_name = 'swift' + def tags_to_headers(self): if self.properties[self.TAGS] is None: return {} @@ -119,8 +117,6 @@ class S3Bucket(resource.Resource): """Create a bucket.""" container = self.physical_resource_name() headers = self.tags_to_headers() - LOG.debug('S3Bucket create container %(container)s with headers ' - '%(headers)s' % {'container': container, 'headers': headers}) if self.properties[self.WEBSITE_CONFIGURATION] is not None: sc = self.properties[self.WEBSITE_CONFIGURATION] index_doc = sc[self.WEBSITE_CONFIGURATION_INDEX_DOCUMENT] @@ -150,12 +146,12 @@ class S3Bucket(resource.Resource): def handle_delete(self): """Perform specified delete policy.""" - LOG.debug('S3Bucket delete container %s' % self.resource_id) - if self.resource_id is not None: - try: - self.swift().delete_container(self.resource_id) - except swift_exceptions.ClientException as ex: - LOG.warn(_("Delete container failed: %s") % ex) + if self.resource_id is None: + return + try: + self.swift().delete_container(self.resource_id) + except Exception as ex: + self.client_plugin().ignore_not_found(ex) def FnGetRefId(self): return unicode(self.resource_id) diff --git a/heat/engine/resources/swift.py b/heat/engine/resources/swift.py index 985ce6b09..f4d194521 100644 --- a/heat/engine/resources/swift.py +++ b/heat/engine/resources/swift.py @@ -12,7 +12,6 @@ # under the License. from six.moves.urllib import parse as urlparse -from swiftclient import exceptions as swift_exceptions from heat.engine import attributes from heat.engine import properties @@ -92,6 +91,8 @@ class SwiftContainer(resource.Resource): ), } + default_client_name = 'swift' + def physical_resource_name(self): name = self.properties.get(self.NAME) if name: @@ -141,12 +142,12 @@ class SwiftContainer(resource.Resource): def handle_delete(self): """Perform specified delete policy.""" - LOG.debug('SwiftContainer delete container %s' % self.resource_id) - if self.resource_id is not None: - try: - self.swift().delete_container(self.resource_id) - except swift_exceptions.ClientException as ex: - LOG.warn(_("Delete container failed: %s") % ex) + if self.resource_id is None: + return + try: + self.swift().delete_container(self.resource_id) + except Exception as ex: + self.client_plugin().ignore_not_found(ex) def FnGetRefId(self): return unicode(self.resource_id) @@ -164,9 +165,11 @@ class SwiftContainer(resource.Resource): self.OBJECT_COUNT, self.BYTES_USED, self.HEAD_CONTAINER): try: headers = self.swift().head_container(self.resource_id) - except swift_exceptions.ClientException as ex: - LOG.warn(_("Head container failed: %s") % ex) - return None + except Exception as ex: + if self.client_plugin().is_client_exception(ex): + LOG.warn(_("Head container failed: %s") % ex) + return None + raise else: if key == self.OBJECT_COUNT: return headers['x-container-object-count'] diff --git a/heat/tests/test_s3.py b/heat/tests/test_s3.py index d46b8967b..20fafd8a0 100644 --- a/heat/tests/test_s3.py +++ b/heat/tests/test_s3.py @@ -207,6 +207,24 @@ class s3Test(HeatTestCase): sc.Connection.delete_container(container_name).AndRaise( sc.ClientException('Test delete failure')) + self.m.ReplayAll() + t = template_format.parse(swift_template) + stack = utils.parse_stack(t) + rsrc = self.create_resource(t, stack, 'S3Bucket') + self.assertRaises(exception.ResourceFailure, + scheduler.TaskRunner(rsrc.delete)) + + self.m.VerifyAll() + + def test_delete_not_found(self): + container_name = utils.PhysName('test_stack', 'test_resource') + sc.Connection.put_container( + container_name, + {'X-Container-Write': 'test_tenant:test_username', + 'X-Container-Read': 'test_tenant:test_username'}).AndReturn(None) + sc.Connection.delete_container(container_name).AndRaise( + sc.ClientException('Its gone', http_status=404)) + self.m.ReplayAll() t = template_format.parse(swift_template) stack = utils.parse_stack(t) diff --git a/heat/tests/test_swift.py b/heat/tests/test_swift.py index eb66a55be..f77cc0ac3 100644 --- a/heat/tests/test_swift.py +++ b/heat/tests/test_swift.py @@ -227,6 +227,24 @@ class swiftTest(HeatTestCase): sc.Connection.delete_container(container_name).AndRaise( sc.ClientException('Test delete failure')) + self.m.ReplayAll() + t = template_format.parse(swift_template) + stack = utils.parse_stack(t) + rsrc = self.create_resource(t, stack, 'SwiftContainer') + self.assertRaises(exception.ResourceFailure, + scheduler.TaskRunner(rsrc.delete)) + + self.m.VerifyAll() + + def test_delete_not_found(self): + container_name = utils.PhysName('test_stack', 'test_resource') + sc.Connection.put_container( + container_name, + {}).AndReturn(None) + sc.Connection.delete_container(container_name).AndRaise( + sc.ClientException('Its gone', + http_status=404)) + self.m.ReplayAll() t = template_format.parse(swift_template) stack = utils.parse_stack(t)