Add cinder snapshot custom constraint

Add cinder snapshot custom constraint to validate
the existent of the snapshot not wait creation.

Change-Id: I4140dfc212eeb4830632c8450e8c8faad3d165f6
Implements: blueprint cinder-custom-constraints
This commit is contained in:
huangtianhua 2014-11-21 16:54:18 +08:00
parent bc5d34bf5d
commit 9d146f489d
4 changed files with 55 additions and 0 deletions

View File

@ -240,6 +240,10 @@ class VolumeNotFound(HeatException):
msg_fmt = _("The Volume (%(volume)s) could not be found.")
class VolumeSnapshotNotFound(HeatException):
msg_fmt = _("The VolumeSnapshot (%(snapshot)s) could not be found.")
class PhysicalResourceNameAmbiguity(HeatException):
msg_fmt = _(
"Multiple physical resources were found with name (%(name)s).")

View File

@ -94,6 +94,14 @@ class CinderClientPlugin(client_plugin.ClientPlugin):
{'volume': volume, 'ex': ex})
raise exception.VolumeNotFound(volume=volume)
def get_volume_snapshot(self, snapshot):
try:
return self.client().volume_snapshots.get(snapshot)
except exceptions.NotFound as ex:
LOG.info(_LI('VolumeSnapshot (%(snapshot)s) not found: %(ex)s'),
{'snapshot': snapshot, 'ex': ex})
raise exception.VolumeSnapshotNotFound(snapshot=snapshot)
def is_not_found(self, ex):
return isinstance(ex, exceptions.NotFound)
@ -111,3 +119,11 @@ class VolumeConstraint(constraints.BaseCustomConstraint):
def validate_with_client(self, client, volume):
client.client_plugin('cinder').get_volume(volume)
class VolumeSnapshotConstraint(constraints.BaseCustomConstraint):
expected_exceptions = (exception.VolumeSnapshotNotFound,)
def validate_with_client(self, client, snapshot):
client.client_plugin('cinder').get_volume_snapshot(snapshot)

View File

@ -47,6 +47,20 @@ class CinderClientPluginTests(common.HeatTestCase):
self.m.VerifyAll()
def test_get_snapshot(self):
"""Tests the get_volume_snapshot function."""
snapshot_id = str(uuid.uuid4())
my_snapshot = self.m.CreateMockAnything()
self.cinder_client.volume_snapshots = self.m.CreateMockAnything()
self.cinder_client.volume_snapshots.get(snapshot_id).MultipleTimes().\
AndReturn(my_snapshot)
self.m.ReplayAll()
self.assertEqual(my_snapshot,
self.cinder_plugin.get_volume_snapshot(snapshot_id))
self.m.VerifyAll()
class VolumeConstraintTest(common.HeatTestCase):
@ -66,3 +80,23 @@ class VolumeConstraintTest(common.HeatTestCase):
self.mock_get_volume.side_effect = exception.VolumeNotFound(
volume='bar')
self.assertFalse(self.constraint.validate("bar", self.ctx))
class VolumeSnapshotConstraintTest(common.HeatTestCase):
def setUp(self):
super(VolumeSnapshotConstraintTest, self).setUp()
self.ctx = utils.dummy_context()
self.mock_get_snapshot = mock.Mock()
self.ctx.clients.client_plugin(
'cinder').get_volume_snapshot = self.mock_get_snapshot
self.constraint = cinder.VolumeSnapshotConstraint()
def test_validation(self):
self.mock_get_snapshot.return_value = 'snapshot'
self.assertTrue(self.constraint.validate("foo", self.ctx))
def test_validation_error(self):
self.mock_get_snapshot.side_effect = exception.VolumeSnapshotNotFound(
snapshot='bar')
self.assertFalse(self.constraint.validate("bar", self.ctx))

View File

@ -59,6 +59,7 @@ heat.constraints =
nova.server = heat.engine.clients.os.nova:ServerConstraint
nova.keypair = heat.engine.clients.os.nova:KeypairConstraint
cinder.volume = heat.engine.clients.os.cinder:VolumeConstraint
cinder.snapshot = heat.engine.clients.os.cinder:VolumeSnapshotConstraint
heat.stack_lifecycle_plugins =