Add snapshot events to the cinder notification

Added and tested snapshot.create.start & snapshot.create.end
on creation of a new snapshot

Added and tested snapshot.delete.start & snapshot.delete.end
on the deletion of a snapshot

Change-Id: Ic3c1cc8949109149c59cf106a24b4eb0d8ba5580
This commit is contained in:
Michael Kerrin 2013-03-28 13:05:01 +00:00
parent 4d0985ea85
commit 43f5f2e9b8
2 changed files with 76 additions and 0 deletions

View File

@ -350,14 +350,75 @@ class VolumeTestCase(test.TestCase):
def test_create_delete_snapshot(self):
"""Test snapshot can be created and deleted."""
volume = self._create_volume()
self.assertEquals(len(test_notifier.NOTIFICATIONS), 0)
self.volume.create_volume(self.context, volume['id'])
self.assertEquals(len(test_notifier.NOTIFICATIONS), 2)
snapshot_id = self._create_snapshot(volume['id'])['id']
self.volume.create_snapshot(self.context, volume['id'], snapshot_id)
self.assertEqual(snapshot_id,
db.snapshot_get(context.get_admin_context(),
snapshot_id).id)
self.assertEquals(len(test_notifier.NOTIFICATIONS), 4)
msg = test_notifier.NOTIFICATIONS[2]
self.assertEquals(msg['event_type'], 'snapshot.create.start')
expected = {
'created_at': 'DONTCARE',
'deleted': '',
'display_name': None,
'snapshot_id': snapshot_id,
'status': 'creating',
'tenant_id': 'fake',
'user_id': 'fake',
'volume_id': volume['id'],
'volume_size': 0
}
self.assertDictMatch(msg['payload'], expected)
msg = test_notifier.NOTIFICATIONS[3]
self.assertEquals(msg['event_type'], 'snapshot.create.end')
expected = {
'created_at': 'DONTCARE',
'deleted': '',
'display_name': None,
'snapshot_id': snapshot_id,
'status': 'creating',
'tenant_id': 'fake',
'user_id': 'fake',
'volume_id': volume['id'],
'volume_size': 0
}
self.assertDictMatch(msg['payload'], expected)
self.volume.delete_snapshot(self.context, snapshot_id)
self.assertEquals(len(test_notifier.NOTIFICATIONS), 6)
msg = test_notifier.NOTIFICATIONS[4]
self.assertEquals(msg['event_type'], 'snapshot.delete.start')
expected = {
'created_at': 'DONTCARE',
'deleted': '',
'display_name': None,
'snapshot_id': snapshot_id,
'status': 'available',
'tenant_id': 'fake',
'user_id': 'fake',
'volume_id': volume['id'],
'volume_size': 0
}
self.assertDictMatch(msg['payload'], expected)
msg = test_notifier.NOTIFICATIONS[5]
self.assertEquals(msg['event_type'], 'snapshot.delete.end')
expected = {
'created_at': 'DONTCARE',
'deleted': '',
'display_name': None,
'snapshot_id': snapshot_id,
'status': 'available',
'tenant_id': 'fake',
'user_id': 'fake',
'volume_id': volume['id'],
'volume_size': 0
}
self.assertDictMatch(msg['payload'], expected)
snap = db.snapshot_get(context.get_admin_context(read_deleted='yes'),
snapshot_id)
self.assertEquals(snap['status'], 'deleted')

View File

@ -452,6 +452,8 @@ class VolumeManager(manager.SchedulerDependentManager):
context = context.elevated()
snapshot_ref = self.db.snapshot_get(context, snapshot_id)
LOG.info(_("snapshot %s: creating"), snapshot_ref['name'])
self._notify_about_snapshot_usage(
context, snapshot_ref, "create.start")
try:
snap_name = snapshot_ref['name']
@ -474,6 +476,7 @@ class VolumeManager(manager.SchedulerDependentManager):
snapshot_ref['id'],
volume_id)
LOG.info(_("snapshot %s: created successfully"), snapshot_ref['name'])
self._notify_about_snapshot_usage(context, snapshot_ref, "create.end")
return snapshot_id
def delete_snapshot(self, context, snapshot_id):
@ -481,6 +484,8 @@ class VolumeManager(manager.SchedulerDependentManager):
context = context.elevated()
snapshot_ref = self.db.snapshot_get(context, snapshot_id)
LOG.info(_("snapshot %s: deleting"), snapshot_ref['name'])
self._notify_about_snapshot_usage(
context, snapshot_ref, "delete.start")
if context.project_id != snapshot_ref['project_id']:
project_id = snapshot_ref['project_id']
@ -520,6 +525,7 @@ class VolumeManager(manager.SchedulerDependentManager):
self.db.volume_glance_metadata_delete_by_snapshot(context, snapshot_id)
self.db.snapshot_destroy(context, snapshot_id)
LOG.info(_("snapshot %s: deleted successfully"), snapshot_ref['name'])
self._notify_about_snapshot_usage(context, snapshot_ref, "delete.end")
# Commit the reservations
if reservations:
@ -702,3 +708,12 @@ class VolumeManager(manager.SchedulerDependentManager):
volume_utils.notify_about_volume_usage(
context, volume, event_suffix,
extra_usage_info=extra_usage_info, host=self.host)
def _notify_about_snapshot_usage(self,
context,
snapshot,
event_suffix,
extra_usage_info=None):
volume_utils.notify_about_snapshot_usage(
context, snapshot, event_suffix,
extra_usage_info=extra_usage_info, host=self.host)