Glance, swift use ClientPlugin.ignore_not_found

Change-Id: I24a7ebb10f2c9ff85cc2bdd6ce4a925c49b5ec3b
This commit is contained in:
Steve Baker 2014-06-17 14:47:17 +12:00 committed by Randall Burt
parent 93c8dc9b54
commit e5df5f1841
5 changed files with 61 additions and 26 deletions

View File

@ -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)

View File

@ -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:
if self.resource_id is None:
return
try:
self.swift().delete_container(self.resource_id)
except swift_exceptions.ClientException as ex:
LOG.warn(_("Delete container failed: %s") % ex)
except Exception as ex:
self.client_plugin().ignore_not_found(ex)
def FnGetRefId(self):
return unicode(self.resource_id)

View File

@ -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:
if self.resource_id is None:
return
try:
self.swift().delete_container(self.resource_id)
except swift_exceptions.ClientException as ex:
LOG.warn(_("Delete container failed: %s") % ex)
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:
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']

View File

@ -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)

View File

@ -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)