From a993b2b969bad0785aad02dc2a6f04ac0c675f8d Mon Sep 17 00:00:00 2001 From: Ollie Leahy Date: Thu, 21 Mar 2013 13:16:05 +0000 Subject: 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 --- nova/db/api.py | 5 +- nova/db/sqlalchemy/api.py | 15 ++++-- ...75_add_project_user_id_to_volume_usage_cache.py | 53 ++++++++++++++++++++++ nova/db/sqlalchemy/models.py | 4 +- 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/175_add_project_user_id_to_volume_usage_cache.py (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index ba3957be6..bae086829 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1466,11 +1466,12 @@ def vol_get_usage_by_time(context, begin): def vol_usage_update(context, id, rd_req, rd_bytes, wr_req, wr_bytes, - instance_id, last_refreshed=None, update_totals=False): + instance_id, project_id, user_id, + last_refreshed=None, update_totals=False): """Update cached volume usage for a volume Creates new record if needed.""" return IMPL.vol_usage_update(context, id, rd_req, rd_bytes, wr_req, - wr_bytes, instance_id, + wr_bytes, instance_id, project_id, user_id, last_refreshed=last_refreshed, update_totals=update_totals) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 354341998..65d762a7f 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -4188,8 +4188,8 @@ def vol_get_usage_by_time(context, begin): @require_context def vol_usage_update(context, id, rd_req, rd_bytes, wr_req, wr_bytes, - instance_id, last_refreshed=None, update_totals=False, - session=None): + instance_id, project_id, user_id, last_refreshed=None, + update_totals=False, session=None): if not session: session = get_session() @@ -4206,7 +4206,9 @@ def vol_usage_update(context, id, rd_req, rd_bytes, wr_req, wr_bytes, 'curr_read_bytes': rd_bytes, 'curr_writes': wr_req, 'curr_write_bytes': wr_bytes, - 'instance_id': instance_id} + 'instance_uuid': instance_id, + 'project_id': project_id, + 'user_id': user_id} else: values = {'tot_last_refreshed': last_refreshed, 'tot_reads': models.VolumeUsage.tot_reads + rd_req, @@ -4219,7 +4221,9 @@ def vol_usage_update(context, id, rd_req, rd_bytes, wr_req, wr_bytes, 'curr_read_bytes': 0, 'curr_writes': 0, 'curr_write_bytes': 0, - 'instance_id': instance_id} + 'instance_uuid': instance_id, + 'project_id': project_id, + 'user_id': user_id} rows = model_query(context, models.VolumeUsage, session=session, read_deleted="yes").\ @@ -4233,6 +4237,9 @@ def vol_usage_update(context, id, rd_req, rd_bytes, wr_req, wr_bytes, vol_usage.tot_last_refreshed = timeutils.utcnow() vol_usage.curr_last_refreshed = timeutils.utcnow() vol_usage.volume_id = id + vol_usage.instance_uuid = instance_id + vol_usage.project_id = project_id + vol_usage.user_id = user_id if not update_totals: vol_usage.curr_reads = rd_req diff --git a/nova/db/sqlalchemy/migrate_repo/versions/175_add_project_user_id_to_volume_usage_cache.py b/nova/db/sqlalchemy/migrate_repo/versions/175_add_project_user_id_to_volume_usage_cache.py new file mode 100644 index 000000000..73b21304a --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/175_add_project_user_id_to_volume_usage_cache.py @@ -0,0 +1,53 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 OpenStack Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from sqlalchemy import MetaData, Integer, String, Table, Column + +from nova.openstack.common import log as logging + +LOG = logging.getLogger(__name__) + + +def upgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + # Allow the instance_id to be stored as a uuid, add columns for project_id + # and tenant_id. + volume_usage_cache = Table('volume_usage_cache', meta, autoload=True) + volume_usage_cache.drop_column('instance_id') + + instance_id = Column('instance_uuid', String(36)) + project_id = Column('project_id', String(36)) + user_id = Column('user_id', String(36)) + + volume_usage_cache.create_column(instance_id) + volume_usage_cache.create_column(project_id) + volume_usage_cache.create_column(user_id) + + +def downgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + volume_usage_cache = Table('volume_usage_cache', meta, autoload=True) + + volume_usage_cache.drop_column('instance_uuid') + volume_usage_cache.drop_column('user_id') + volume_usage_cache.drop_column('project_id') + + instance_id = Column('instance_id', Integer) + volume_usage_cache.create_column(instance_id) diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index ce5f84578..016563c37 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -888,7 +888,9 @@ class VolumeUsage(BASE, NovaBase): __tablename__ = 'volume_usage_cache' id = Column(Integer, primary_key=True, nullable=False) volume_id = Column(String(36), nullable=False) - instance_id = Column(Integer) + instance_uuid = Column(String(36)) + project_id = Column(String(36)) + user_id = Column(String(36)) tot_last_refreshed = Column(DateTime) tot_reads = Column(BigInteger, default=0) tot_read_bytes = Column(BigInteger, default=0) -- cgit