From 6bf299bf7f8a0f4c69be9523263f6d897e2e87b9 Mon Sep 17 00:00:00 2001 From: Victor Sergeyev Date: Fri, 17 May 2013 18:28:06 +0300 Subject: [PATCH] Removed session from fixed_ip_*() functions. Removed session objects from public fixed_ip_*() functions in nova/db/sqlalchemy/api module. Minor code refactiring. blueprint db-session-cleanup Change-Id: Ic2a96270745799496e365aee0380ac079e1edeff --- nova/db/sqlalchemy/api.py | 69 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index a35e8154d3e1..2205ac7fae10 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1067,11 +1067,8 @@ def fixed_ip_bulk_create(context, ips): def fixed_ip_disassociate(context, address): session = get_session() with session.begin(): - fixed_ip_ref = fixed_ip_get_by_address(context, - address, - session=session) - fixed_ip_ref['instance_uuid'] = None - fixed_ip_ref.save(session=session) + _fixed_ip_get_by_address(context, address, session=session).\ + update({'instance_uuid': None}) @require_admin_context @@ -1129,10 +1126,8 @@ def fixed_ip_get(context, id, get_network=False): @require_admin_context -def fixed_ip_get_all(context, session=None): - result = model_query(context, models.FixedIp, session=session, - read_deleted="yes").\ - all() +def fixed_ip_get_all(context): + result = model_query(context, models.FixedIp, read_deleted="yes").all() if not result: raise exception.NoFixedIpsDefined() @@ -1140,35 +1135,44 @@ def fixed_ip_get_all(context, session=None): @require_context -def fixed_ip_get_by_address(context, address, session=None): - result = model_query(context, models.FixedIp, session=session).\ - filter_by(address=address).\ - first() - if not result: - raise exception.FixedIpNotFoundForAddress(address=address) +def fixed_ip_get_by_address(context, address): + return _fixed_ip_get_by_address(context, address) - # NOTE(sirp): shouldn't we just use project_only here to restrict the - # results? - if (nova.context.is_user_context(context) and - result['instance_uuid'] is not None): - instance = _instance_get_by_uuid(context.elevated(read_deleted='yes'), - result['instance_uuid'], - session) - nova.context.authorize_project_context(context, instance.project_id) + +@require_context +def _fixed_ip_get_by_address(context, address, session=None): + if session is None: + session = get_session() + + with session.begin(subtransactions=True): + result = model_query(context, models.FixedIp, session=session).\ + filter_by(address=address).\ + first() + if not result: + raise exception.FixedIpNotFoundForAddress(address=address) + + # NOTE(sirp): shouldn't we just use project_only here to restrict the + # results? + if (nova.context.is_user_context(context) and + result['instance_uuid'] is not None): + instance = _instance_get_by_uuid( + context.elevated(read_deleted='yes'), + result['instance_uuid'], + session + ) + nova.context.authorize_project_context(context, + instance.project_id) return result @require_admin_context -def fixed_ip_get_by_address_detailed(context, address, session=None): +def fixed_ip_get_by_address_detailed(context, address): """ :returns: a tuple of (models.FixedIp, models.Network, models.Instance) """ - if not session: - session = get_session() - - result = model_query(context, models.FixedIp, models.Network, - models.Instance, session=session).\ + result = model_query(context, models.FixedIp, + models.Network, models.Instance).\ filter_by(address=address).\ outerjoin((models.Network, models.Network.id == @@ -1250,11 +1254,8 @@ def fixed_ips_by_virtual_interface(context, vif_id): def fixed_ip_update(context, address, values): session = get_session() with session.begin(): - fixed_ip_ref = fixed_ip_get_by_address(context, - address, - session=session) - fixed_ip_ref.update(values) - fixed_ip_ref.save(session=session) + _fixed_ip_get_by_address(context, address, session=session).\ + update(values) @require_context