Add tenant/ user id to volume usage notifications
Volume usage notifications are generated to enable traffic based billing on volumes. Include tenant id and user id to make these notifications more useful to billing systems. Fixes bug # 1158292 Change-Id: Ic71c10f0fc5d9e8c5a0e2f538de072e7ccca20ee
This commit is contained in:
		@@ -2372,11 +2372,17 @@ class VolumeUsageDBApiTestCase(test.TestCase):
 | 
				
			|||||||
        refreshed_time = now - datetime.timedelta(seconds=5)
 | 
					        refreshed_time = now - datetime.timedelta(seconds=5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        expected_vol_usages = [{'volume_id': u'1',
 | 
					        expected_vol_usages = [{'volume_id': u'1',
 | 
				
			||||||
 | 
					                                'instance_uuid': 'fake-instance-uuid1',
 | 
				
			||||||
 | 
					                                'project_id': 'fake-project-uuid1',
 | 
				
			||||||
 | 
					                                'user_id': 'fake-user-uuid1',
 | 
				
			||||||
                                'curr_reads': 1000,
 | 
					                                'curr_reads': 1000,
 | 
				
			||||||
                                'curr_read_bytes': 2000,
 | 
					                                'curr_read_bytes': 2000,
 | 
				
			||||||
                                'curr_writes': 3000,
 | 
					                                'curr_writes': 3000,
 | 
				
			||||||
                                'curr_write_bytes': 4000},
 | 
					                                'curr_write_bytes': 4000},
 | 
				
			||||||
                               {'volume_id': u'2',
 | 
					                               {'volume_id': u'2',
 | 
				
			||||||
 | 
					                                'instance_uuid': 'fake-instance-uuid2',
 | 
				
			||||||
 | 
					                                'project_id': 'fake-project-uuid2',
 | 
				
			||||||
 | 
					                                'user_id': 'fake-user-uuid2',
 | 
				
			||||||
                                'curr_reads': 100,
 | 
					                                'curr_reads': 100,
 | 
				
			||||||
                                'curr_read_bytes': 200,
 | 
					                                'curr_read_bytes': 200,
 | 
				
			||||||
                                'curr_writes': 300,
 | 
					                                'curr_writes': 300,
 | 
				
			||||||
@@ -2390,13 +2396,20 @@ class VolumeUsageDBApiTestCase(test.TestCase):
 | 
				
			|||||||
        self.assertEqual(len(vol_usages), 0)
 | 
					        self.assertEqual(len(vol_usages), 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=10, rd_bytes=20,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=10, rd_bytes=20,
 | 
				
			||||||
                                        wr_req=30, wr_bytes=40, instance_id=1)
 | 
					                                        wr_req=30, wr_bytes=40,
 | 
				
			||||||
 | 
					                                        instance_id='fake-instance-uuid1',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid1',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid1')
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 2, rd_req=100, rd_bytes=200,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 2, rd_req=100, rd_bytes=200,
 | 
				
			||||||
                                        wr_req=300, wr_bytes=400,
 | 
					                                        wr_req=300, wr_bytes=400,
 | 
				
			||||||
                                        instance_id=1)
 | 
					                                        instance_id='fake-instance-uuid2',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid2',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid2')
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=1000, rd_bytes=2000,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=1000, rd_bytes=2000,
 | 
				
			||||||
                                        wr_req=3000, wr_bytes=4000,
 | 
					                                        wr_req=3000, wr_bytes=4000,
 | 
				
			||||||
                                        instance_id=1,
 | 
					                                        instance_id='fake-instance-uuid1',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid1',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid1',
 | 
				
			||||||
                                        last_refreshed=refreshed_time)
 | 
					                                        last_refreshed=refreshed_time)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
 | 
					        vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
 | 
				
			||||||
@@ -2411,6 +2424,9 @@ class VolumeUsageDBApiTestCase(test.TestCase):
 | 
				
			|||||||
        timeutils.set_time_override(now)
 | 
					        timeutils.set_time_override(now)
 | 
				
			||||||
        start_time = now - datetime.timedelta(seconds=10)
 | 
					        start_time = now - datetime.timedelta(seconds=10)
 | 
				
			||||||
        expected_vol_usages = {'volume_id': u'1',
 | 
					        expected_vol_usages = {'volume_id': u'1',
 | 
				
			||||||
 | 
					                               'project_id': 'fake-project-uuid',
 | 
				
			||||||
 | 
					                               'user_id': 'fake-user-uuid',
 | 
				
			||||||
 | 
					                               'instance_uuid': 'fake-instance-uuid',
 | 
				
			||||||
                               'tot_reads': 600,
 | 
					                               'tot_reads': 600,
 | 
				
			||||||
                               'tot_read_bytes': 800,
 | 
					                               'tot_read_bytes': 800,
 | 
				
			||||||
                               'tot_writes': 1000,
 | 
					                               'tot_writes': 1000,
 | 
				
			||||||
@@ -2422,17 +2438,25 @@ class VolumeUsageDBApiTestCase(test.TestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=100, rd_bytes=200,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=100, rd_bytes=200,
 | 
				
			||||||
                                        wr_req=300, wr_bytes=400,
 | 
					                                        wr_req=300, wr_bytes=400,
 | 
				
			||||||
                                        instance_id=1)
 | 
					                                        instance_id='fake-instance-uuid',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid')
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=200, rd_bytes=300,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=200, rd_bytes=300,
 | 
				
			||||||
                                        wr_req=400, wr_bytes=500,
 | 
					                                        wr_req=400, wr_bytes=500,
 | 
				
			||||||
                                        instance_id=1,
 | 
					                                        instance_id='fake-instance-uuid',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid',
 | 
				
			||||||
                                        update_totals=True)
 | 
					                                        update_totals=True)
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=300, rd_bytes=400,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=300, rd_bytes=400,
 | 
				
			||||||
                                        wr_req=500, wr_bytes=600,
 | 
					                                        wr_req=500, wr_bytes=600,
 | 
				
			||||||
                                        instance_id=1)
 | 
					                                        instance_id='fake-instance-uuid',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid')
 | 
				
			||||||
        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=400, rd_bytes=500,
 | 
					        vol_usage = db.vol_usage_update(ctxt, 1, rd_req=400, rd_bytes=500,
 | 
				
			||||||
                                        wr_req=600, wr_bytes=700,
 | 
					                                        wr_req=600, wr_bytes=700,
 | 
				
			||||||
                                        instance_id=1,
 | 
					                                        instance_id='fake-instance-uuid',
 | 
				
			||||||
 | 
					                                        project_id='fake-project-uuid',
 | 
				
			||||||
 | 
					                                        user_id='fake-user-uuid',
 | 
				
			||||||
                                        update_totals=True)
 | 
					                                        update_totals=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
 | 
					        vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1229,6 +1229,46 @@ class TestNovaMigrations(BaseMigrationTestCase, CommonTestsMixIn):
 | 
				
			|||||||
                          it_projects.insert().execute,
 | 
					                          it_projects.insert().execute,
 | 
				
			||||||
                          dict(instance_type=31, project_id='pr1', deleted=0))
 | 
					                          dict(instance_type=31, project_id='pr1', deleted=0))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # migration 175, Modify volume_usage-cache, Drop column instance_id, add
 | 
				
			||||||
 | 
					    # columns instance_uuid, project_id and user_id
 | 
				
			||||||
 | 
					    def _pre_upgrade_175(self, engine):
 | 
				
			||||||
 | 
					        volume_usage_cache = get_table(engine, 'volume_usage_cache')
 | 
				
			||||||
 | 
					        fake_usage = {'volume_id': 'fake_volume_id',
 | 
				
			||||||
 | 
					                      'instance_id': 10,
 | 
				
			||||||
 | 
					                      'tot_last_refreshed': datetime.datetime.now(),
 | 
				
			||||||
 | 
					                      'tot_reads': 2,
 | 
				
			||||||
 | 
					                      'tot_read_bytes': 3,
 | 
				
			||||||
 | 
					                      'tot_writes': 4,
 | 
				
			||||||
 | 
					                      'tot_write_bytes': 5,
 | 
				
			||||||
 | 
					                      'curr_last_refreshed': datetime.datetime.now(),
 | 
				
			||||||
 | 
					                      'curr_reads': 6,
 | 
				
			||||||
 | 
					                      'curr_read_bytes': 7,
 | 
				
			||||||
 | 
					                      'curr_writes': 8,
 | 
				
			||||||
 | 
					                      'curr_write_bytes': 9}
 | 
				
			||||||
 | 
					        volume_usage_cache.insert().execute(fake_usage)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _check_175(self, engine, data):
 | 
				
			||||||
 | 
					        volume_usage_cache = get_table(engine, 'volume_usage_cache')
 | 
				
			||||||
 | 
					        # Get the record
 | 
				
			||||||
 | 
					        rows = volume_usage_cache.select().execute().fetchall()
 | 
				
			||||||
 | 
					        self.assertEqual(len(rows), 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(rows[0]['instance_uuid'], None)
 | 
				
			||||||
 | 
					        self.assertEqual(rows[0]['project_id'], None)
 | 
				
			||||||
 | 
					        self.assertEqual(rows[0]['user_id'], None)
 | 
				
			||||||
 | 
					        self.assertFalse('instance_id' in rows[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _post_downgrade_175(self, engine):
 | 
				
			||||||
 | 
					        volume_usage_cache = get_table(engine, 'volume_usage_cache')
 | 
				
			||||||
 | 
					        # Get the record
 | 
				
			||||||
 | 
					        rows = volume_usage_cache.select().execute().fetchall()
 | 
				
			||||||
 | 
					        self.assertEqual(len(rows), 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertFalse('instance_uuid' in rows[0])
 | 
				
			||||||
 | 
					        self.assertFalse('project_id' in rows[0])
 | 
				
			||||||
 | 
					        self.assertFalse('user_id' in rows[0])
 | 
				
			||||||
 | 
					        self.assertEqual(rows[0]['instance_id'], None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestBaremetalMigrations(BaseMigrationTestCase, CommonTestsMixIn):
 | 
					class TestBaremetalMigrations(BaseMigrationTestCase, CommonTestsMixIn):
 | 
				
			||||||
    """Test sqlalchemy-migrate migrations."""
 | 
					    """Test sqlalchemy-migrate migrations."""
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user