From 244a5e8d72e89f9f0e6ded73038109f1e4ebbada Mon Sep 17 00:00:00 2001 From: Alessio Ababilov Date: Fri, 10 Aug 2012 11:06:56 +0300 Subject: Revert "Remove unused add_network_to_project() method" This reverts commit d8e39cb775a872c46d067bee8febc40dee799369. Change-Id: I22f0692814ff029b4960ab32cbee63bbf4922106 --- etc/nova/policy.json | 1 + nova/db/sqlalchemy/api.py | 21 ++++++++++++++++----- nova/network/api.py | 6 ++++++ nova/network/manager.py | 5 +++++ nova/network/quantumv2/api.py | 4 ++++ nova/tests/policy.json | 1 + 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/etc/nova/policy.json b/etc/nova/policy.json index b0a20ee94..72390c75e 100644 --- a/etc/nova/policy.json +++ b/etc/nova/policy.json @@ -98,6 +98,7 @@ "network:get_fixed_ip": [], "network:add_fixed_ip_to_instance": [], "network:remove_fixed_ip_from_instance": [], + "network:add_network_to_project": [], "network:get_instance_nw_info": [], "network:get_dns_domains": [], diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 2993aaaf3..a55e7ab80 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1995,12 +1995,20 @@ def key_pair_count_by_user(context, user_id): @require_admin_context -def network_associate(context, project_id): +def network_associate(context, project_id, force=False): """Associate a project with a network. called by project_get_networks under certain conditions + and network manager add_network_to_project() only associate if the project doesn't already have a network + or if force is True + + force solves race condition where a fresh project has multiple instance + builds simultaneously picked up by multiple network hosts which attempt + to associate the project with multiple networks + force should only be used as a direct consequence of user request + all automated requests should not use force """ session = get_session() with session.begin(): @@ -2012,10 +2020,13 @@ def network_associate(context, project_id): with_lockmode('update').\ first() - # find out if project has a network - network_ref = network_query(project_id) - if not network_ref: - # project doesn't have a network so associate with a new network + if not force: + # find out if project has a network + network_ref = network_query(project_id) + + if force or not network_ref: + # in force mode or project doesn't have a network so associate + # with a new network # get new network network_ref = network_query(None) diff --git a/nova/network/api.py b/nova/network/api.py index d2c311f95..ae230455f 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -278,6 +278,12 @@ class API(base.Base): {'method': 'remove_fixed_ip_from_instance', 'args': args}) + def add_network_to_project(self, context, project_id): + """Force adds another network to a project.""" + rpc.call(context, FLAGS.network_topic, + {'method': 'add_network_to_project', + 'args': {'project_id': project_id}}) + @refresh_cache def get_instance_nw_info(self, context, instance): """Returns all network info related to an instance.""" diff --git a/nova/network/manager.py b/nova/network/manager.py index f81004fed..116413007 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -1906,6 +1906,11 @@ class VlanManager(RPCAllocateFixedIP, FloatingIP, NetworkManager): self._setup_network_on_host(context, network) return address + @wrap_check_policy + def add_network_to_project(self, context, project_id): + """Force adds another network to a project.""" + self.db.network_associate(context, project_id, force=True) + def _get_networks_for_instance(self, context, instance_id, project_id, requested_networks=None): """Determine which networks an instance should connect to.""" diff --git a/nova/network/quantumv2/api.py b/nova/network/quantumv2/api.py index 16dba96fa..50042278b 100644 --- a/nova/network/quantumv2/api.py +++ b/nova/network/quantumv2/api.py @@ -244,6 +244,10 @@ class API(base.Base): it is associated with.""" raise NotImplementedError() + def add_network_to_project(self, context, project_id): + """Force add a network to the project.""" + raise NotImplementedError() + def _build_network_info_model(self, context, instance, networks=None): search_opts = {'tenant_id': instance['project_id'], 'device_id': instance['uuid'], } diff --git a/nova/tests/policy.json b/nova/tests/policy.json index 4836a33b4..c2388d411 100644 --- a/nova/tests/policy.json +++ b/nova/tests/policy.json @@ -170,6 +170,7 @@ "network:get_fixed_ip": [], "network:add_fixed_ip_to_instance": [], "network:remove_fixed_ip_from_instance": [], + "network:add_network_to_project": [], "network:get_instance_nw_info": [], "network:get_dns_domains": [], -- cgit