]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add volume_attachment to volume usage notifications
authorMathieu Gagné <mgagne@iweb.com>
Tue, 14 Jul 2015 23:44:16 +0000 (19:44 -0400)
committerMathieu Gagné <mgagne@iweb.com>
Fri, 17 Jul 2015 20:58:27 +0000 (16:58 -0400)
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

cinder/tests/unit/test_volume.py
cinder/tests/unit/test_volume_utils.py
cinder/volume/utils.py

index e47496bae1e558da48c2671d2ea7f87af541e3df..11155c7a97957c723a3a658cde32bfd02756d7e0 100644 (file)
@@ -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]
index 604b5387bc92f91747aa2e6ae2726c4fd193c622..396a0b32d3494c2a6dc76d8ae87b5de08784beb3 100644 (file)
@@ -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)
 
index 29145ec36026c28f9d006fe81038c834495baea0..2581c878f6218ce017a42efd311a2c7ca23703f3 100644 (file)
@@ -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