From adbb27239809cb7b85777371b4f9d1599a56661f Mon Sep 17 00:00:00 2001 From: jenny-shieh Date: Wed, 22 Jan 2014 12:20:07 -0800 Subject: [PATCH] Add notifier events to cinder volume rename, reset-state Implements: notifiers for cinder volume rename, reset-state Fixes: bug #1271692 Change-Id: Ie82a4c062904a4f86c9f6364649b8c2b9f71ea6e --- cinder/api/contrib/admin_actions.py | 12 ++++++++++++ cinder/api/v1/volumes.py | 6 ++++++ cinder/api/v2/volumes.py | 6 ++++++ cinder/tests/api/contrib/test_scheduler_hints.py | 2 ++ cinder/tests/api/v1/test_volumes.py | 16 ++++++++++++++++ cinder/tests/api/v2/stubs.py | 1 + cinder/tests/api/v2/test_volumes.py | 16 ++++++++++++++++ 7 files changed, 59 insertions(+) diff --git a/cinder/api/contrib/admin_actions.py b/cinder/api/contrib/admin_actions.py index 800e7053d..2a4edc853 100644 --- a/cinder/api/contrib/admin_actions.py +++ b/cinder/api/contrib/admin_actions.py @@ -20,6 +20,7 @@ from cinder.api.openstack import wsgi from cinder import db from cinder import exception from cinder.openstack.common import log as logging +from cinder.openstack.common.notifier import api as notifier_api from cinder.openstack.common import strutils from cinder import volume @@ -81,10 +82,21 @@ class AdminController(wsgi.Controller): msg = _("Updating %(resource)s '%(id)s' with '%(update)r'") LOG.debug(msg, {'resource': self.resource_name, 'id': id, 'update': update}) + + notifier_info = dict(id=id, update=update) + notifier_api.notify(context, 'volumeStatusUpdate', + self.collection + '.reset_status.start', + notifier_api.INFO, notifier_info) + try: self._update(context, id, update) except exception.NotFound as e: raise exc.HTTPNotFound(e) + + notifier_api.notify(context, 'volumeStatusUpdate', + self.collection + '.reset_status.end', + notifier_api.INFO, notifier_info) + return webob.Response(status_int=202) @wsgi.action('os-force_delete') diff --git a/cinder/api/v1/volumes.py b/cinder/api/v1/volumes.py index 8364deb03..708152196 100644 --- a/cinder/api/v1/volumes.py +++ b/cinder/api/v1/volumes.py @@ -27,6 +27,7 @@ from cinder.openstack.common import log as logging from cinder.openstack.common import uuidutils from cinder import utils from cinder import volume as cinder_volume +from cinder.volume import utils as volume_utils from cinder.volume import volume_types @@ -469,6 +470,8 @@ class VolumeController(wsgi.Controller): try: volume = self.volume_api.get(context, id) + volume_utils.notify_about_volume_usage(context, volume, + 'update.start') self.volume_api.update(context, volume, update_dict) except exception.NotFound: raise exc.HTTPNotFound() @@ -477,6 +480,9 @@ class VolumeController(wsgi.Controller): self._add_visible_admin_metadata(context, volume) + volume_utils.notify_about_volume_usage(context, volume, + 'update.end') + return {'volume': _translate_volume_detail_view(context, volume)} diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index 1267cd0b1..72325cfc9 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -29,6 +29,7 @@ from cinder.openstack.common import log as logging from cinder.openstack.common import uuidutils from cinder import utils from cinder import volume as cinder_volume +from cinder.volume import utils as volume_utils from cinder.volume import volume_types @@ -444,6 +445,8 @@ class VolumeController(wsgi.Controller): try: volume = self.volume_api.get(context, id) + volume_utils.notify_about_volume_usage(context, volume, + 'update.start') self.volume_api.update(context, volume, update_dict) except exception.NotFound: msg = _("Volume could not be found") @@ -453,6 +456,9 @@ class VolumeController(wsgi.Controller): self._add_visible_admin_metadata(context, volume) + volume_utils.notify_about_volume_usage(context, volume, + 'update.end') + return self._view_builder.detail(req, volume) diff --git a/cinder/tests/api/contrib/test_scheduler_hints.py b/cinder/tests/api/contrib/test_scheduler_hints.py index 5f8728ba9..ae5c17abc 100644 --- a/cinder/tests/api/contrib/test_scheduler_hints.py +++ b/cinder/tests/api/contrib/test_scheduler_hints.py @@ -32,6 +32,8 @@ class SchedulerHintsTestCase(test.TestCase): self.fake_instance = stubs.stub_volume(1, uuid=UUID) self.fake_instance['created_at'] =\ datetime.datetime(2013, 1, 1, 1, 1, 1) + self.fake_instance['launched_at'] =\ + datetime.datetime(2013, 1, 1, 1, 1, 1) self.flags( osapi_volume_extension=[ 'cinder.api.contrib.select_extensions'], diff --git a/cinder/tests/api/v1/test_volumes.py b/cinder/tests/api/v1/test_volumes.py index 57c4d82b0..2bcf3ee23 100644 --- a/cinder/tests/api/v1/test_volumes.py +++ b/cinder/tests/api/v1/test_volumes.py @@ -25,6 +25,8 @@ from cinder.api.v1 import volumes from cinder import context from cinder import db from cinder import exception +from cinder.openstack.common.notifier import api as notifier_api +from cinder.openstack.common.notifier import test_notifier from cinder import test from cinder.tests.api import fakes from cinder.tests.api.v2 import stubs @@ -60,11 +62,19 @@ class VolumeApiTest(test.TestCase): fake_image.stub_out_image_service(self.stubs) self.controller = volumes.VolumeController(self.ext_mgr) + self.flags(host='fake', + notification_driver=[test_notifier.__name__]) + test_notifier.NOTIFICATIONS = [] + self.stubs.Set(db, 'volume_get_all', stubs.stub_volume_get_all) self.stubs.Set(db, 'service_get_all_by_topic', stubs.stub_service_get_all_by_topic) self.stubs.Set(volume_api.API, 'delete', stubs.stub_volume_delete) + def tearDown(self): + notifier_api._reset_drivers() + super(VolumeApiTest, self).tearDown() + def test_volume_create(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) @@ -230,6 +240,7 @@ class VolumeApiTest(test.TestCase): } body = {"volume": updates} req = fakes.HTTPRequest.blank('/v1/volumes/1') + self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res_dict = self.controller.update(req, '1', body) expected = {'volume': { 'status': 'fakestatus', @@ -253,6 +264,7 @@ class VolumeApiTest(test.TestCase): 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'size': 1}} self.assertEqual(res_dict, expected) + self.assertEqual(len(test_notifier.NOTIFICATIONS), 2) def test_volume_update_metadata(self): self.stubs.Set(db, 'volume_get', stubs.stub_volume_get_db) @@ -263,6 +275,7 @@ class VolumeApiTest(test.TestCase): } body = {"volume": updates} req = fakes.HTTPRequest.blank('/v1/volumes/1') + self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res_dict = self.controller.update(req, '1', body) expected = {'volume': { 'status': 'fakestatus', @@ -288,6 +301,7 @@ class VolumeApiTest(test.TestCase): 'size': 1 }} self.assertEqual(res_dict, expected) + self.assertEqual(len(test_notifier.NOTIFICATIONS), 2) def test_volume_update_with_admin_metadata(self): self.stubs.Set(volume_api.API, "update", stubs.stub_volume_update) @@ -308,6 +322,7 @@ class VolumeApiTest(test.TestCase): } body = {"volume": updates} req = fakes.HTTPRequest.blank('/v1/volumes/1') + self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) admin_ctx = context.RequestContext('admin', 'fakeproject', True) req.environ['cinder.context'] = admin_ctx res_dict = self.controller.update(req, '1', body) @@ -333,6 +348,7 @@ class VolumeApiTest(test.TestCase): 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'size': 1}} self.assertEqual(res_dict, expected) + self.assertEqual(len(test_notifier.NOTIFICATIONS), 2) def test_update_empty_body(self): body = {} diff --git a/cinder/tests/api/v2/stubs.py b/cinder/tests/api/v2/stubs.py index a940db19f..23384c286 100644 --- a/cinder/tests/api/v2/stubs.py +++ b/cinder/tests/api/v2/stubs.py @@ -46,6 +46,7 @@ def stub_volume(id, **kwargs): 'volume_admin_metadata': [{'key': 'attached_mode', 'value': 'rw'}, {'key': 'readonly', 'value': 'False'}], 'bootable': False, + 'launched_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'volume_type': {'name': 'vol_type_name'}} volume.update(kwargs) diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index da6ba5dc9..9fe16c8e7 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -26,6 +26,8 @@ from cinder.api.v2 import volumes from cinder import context from cinder import db from cinder import exception +from cinder.openstack.common.notifier import api as notifier_api +from cinder.openstack.common.notifier import test_notifier from cinder import test from cinder.tests.api import fakes from cinder.tests.api.v2 import stubs @@ -63,12 +65,20 @@ class VolumeApiTest(test.TestCase): fake_image.stub_out_image_service(self.stubs) self.controller = volumes.VolumeController(self.ext_mgr) + self.flags(host='fake', + notification_driver=[test_notifier.__name__]) + test_notifier.NOTIFICATIONS = [] + self.stubs.Set(db, 'volume_get_all', stubs.stub_volume_get_all) self.stubs.Set(volume_api.API, 'delete', stubs.stub_volume_delete) self.stubs.Set(db, 'service_get_all_by_topic', stubs.stub_service_get_all_by_topic) self.maxDiff = None + def tearDown(self): + notifier_api._reset_drivers() + super(VolumeApiTest, self).tearDown() + def test_volume_create(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) @@ -257,6 +267,7 @@ class VolumeApiTest(test.TestCase): } body = {"volume": updates} req = fakes.HTTPRequest.blank('/v2/volumes/1') + self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res_dict = self.controller.update(req, '1', body) expected = { 'volume': { @@ -295,6 +306,7 @@ class VolumeApiTest(test.TestCase): } } self.assertEqual(res_dict, expected) + self.assertEqual(len(test_notifier.NOTIFICATIONS), 2) def test_volume_update_metadata(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) @@ -305,6 +317,7 @@ class VolumeApiTest(test.TestCase): } body = {"volume": updates} req = fakes.HTTPRequest.blank('/v2/volumes/1') + self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res_dict = self.controller.update(req, '1', body) expected = {'volume': { 'status': 'fakestatus', @@ -341,6 +354,7 @@ class VolumeApiTest(test.TestCase): ], }} self.assertEqual(res_dict, expected) + self.assertEqual(len(test_notifier.NOTIFICATIONS), 2) def test_volume_update_with_admin_metadata(self): self.stubs.Set(volume_api.API, "update", stubs.stub_volume_update) @@ -361,6 +375,7 @@ class VolumeApiTest(test.TestCase): } body = {"volume": updates} req = fakes.HTTPRequest.blank('/v2/volumes/1') + self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) admin_ctx = context.RequestContext('admin', 'fake', True) req.environ['cinder.context'] = admin_ctx res_dict = self.controller.update(req, '1', body) @@ -398,6 +413,7 @@ class VolumeApiTest(test.TestCase): ], }} self.assertEqual(res_dict, expected) + self.assertEqual(len(test_notifier.NOTIFICATIONS), 2) def test_update_empty_body(self): body = {} -- 2.45.2