From: Mathieu Gagné Date: Tue, 14 Jul 2015 23:44:16 +0000 (-0400) Subject: Add volume_attachment to volume usage notifications X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=4eb70bcf6ae6a4bb3631018da1923b6cfaae59dd;p=openstack-build%2Fcinder-build.git Add volume_attachment to volume usage notifications This change adds volume_attachment to volume usage notifications sent by Cinder. This information is useful for billing and audit purposes. Closes-bug: #1474596 Change-Id: I0698a7d90e61df9e0cb46cee23519ac334b36def --- diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py index e47496bae..11155c7a9 100644 --- a/cinder/tests/unit/test_volume.py +++ b/cinder/tests/unit/test_volume.py @@ -562,6 +562,7 @@ class VolumeTestCase(BaseVolumeTestCase): 'replication_extended_status': None, 'replication_driver_data': None, 'metadata': [], + 'volume_attachment': [], } self.assertDictMatch(expected, msg['payload']) msg = self.notifier.notifications[1] diff --git a/cinder/tests/unit/test_volume_utils.py b/cinder/tests/unit/test_volume_utils.py index 604b5387b..396a0b32d 100644 --- a/cinder/tests/unit/test_volume_utils.py +++ b/cinder/tests/unit/test_volume_utils.py @@ -226,8 +226,10 @@ class NotifyUsageTestCase(test.TestCase): self.assertEqual(expected_snapshot, usage_info) @mock.patch('cinder.db.volume_glance_metadata_get') - def test_usage_from_volume(self, mock_image_metadata): + @mock.patch('cinder.db.volume_attachment_get_used_by_volume_id') + def test_usage_from_volume(self, mock_attachment, mock_image_metadata): mock_image_metadata.return_value = {'image_id': 'fake_image_id'} + mock_attachment.return_value = [{'instance_uuid': 'fake_instance_id'}] raw_volume = { 'project_id': '12b0330ec2584a', 'user_id': '158cba1b8c2bb6008e', @@ -267,6 +269,7 @@ class NotifyUsageTestCase(test.TestCase): 'status': 'available', 'metadata': {'fake_metadata_key': 'fake_metadata_value'}, 'glance_metadata': {'image_id': 'fake_image_id'}, + 'volume_attachment': [{'instance_uuid': 'fake_instance_id'}], } self.assertEqual(expected_volume, usage_info) diff --git a/cinder/volume/utils.py b/cinder/volume/utils.py index 29145ec36..2581c878f 100644 --- a/cinder/volume/utils.py +++ b/cinder/volume/utils.py @@ -68,6 +68,10 @@ def _usage_from_volume(context, volume_ref, **kw): usage_info.update(kw) try: + attachments = db.volume_attachment_get_used_by_volume_id( + context, volume_ref['id']) + usage_info['volume_attachment'] = attachments + glance_meta = db.volume_glance_metadata_get(context, volume_ref['id']) if glance_meta: usage_info['glance_metadata'] = glance_meta @@ -75,6 +79,7 @@ def _usage_from_volume(context, volume_ref, **kw): pass except exception.VolumeNotFound: LOG.debug("Can not find volume %s at notify usage", volume_ref['id']) + return usage_info