Skip to content

Commit

Permalink
Stop network.api import on network import
Browse files Browse the repository at this point in the history
The nova.network.__init__.py was importing the network.api
automatically at import time. This was leading to some silly
workarounds (for example in nova/virt/firewall.py) involving
late importing nova.network to stop circular imports.

It is also causing issues with https://review.openstack.org/#/c/13007/
because quantum api needs to import a flag from network manager
which is causing a circular import.

This fixes the issue by moving to use the code from volume_api
which only imports the api class when the method is called. It cleans
up the tests that were stubbing out network.API and also removes
the unneeded workarounds.

Change-Id: I4c9b372d078ab4398e797335a8de5e9e1077e31f
  • Loading branch information
vishvananda authored and Gary Kotton committed Nov 17, 2012
1 parent b874d21 commit 197398f
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions nova/api/openstack/compute/contrib/networks.py
Expand Up @@ -23,7 +23,7 @@
from nova.api.openstack import extensions
from nova import exception
from nova import flags
import nova.network.api
from nova import network
from nova.openstack.common import log as logging


Expand Down Expand Up @@ -58,7 +58,7 @@ def network_dict(context, network):
class NetworkController(object):

def __init__(self, network_api=None):
self.network_api = network_api or nova.network.api.API()
self.network_api = network_api or network.API()

def action(self, req, id, body):
_actions = {
Expand Down
8 changes: 5 additions & 3 deletions nova/network/__init__.py
Expand Up @@ -20,7 +20,9 @@
# collisions with use of 'from nova.network import <foo>' elsewhere.
import nova.flags
import nova.openstack.common.importutils
import nova.utils

API = nova.openstack.common.importutils.import_class(
nova.flags.FLAGS.network_api_class)

def API():
importutils = nova.openstack.common.importutils
cls = importutils.import_class(nova.flags.FLAGS.network_api_class)
return cls()
5 changes: 3 additions & 2 deletions nova/tests/api/openstack/fakes.py
Expand Up @@ -38,6 +38,7 @@
from nova.db.sqlalchemy import models
from nova import exception as exc
import nova.image.glance
from nova.network import api as network_api
from nova.openstack.common import jsonutils
from nova.openstack.common import timeutils
from nova import quota
Expand Down Expand Up @@ -189,7 +190,7 @@ def get_floating_ips_by_fixed_address(self, context, fixed_ip):

if func is None:
func = get_floating_ips_by_fixed_address
stubs.Set(nova.network.API, 'get_floating_ips_by_fixed_address', func)
stubs.Set(network_api.API, 'get_floating_ips_by_fixed_address', func)


def stub_out_nw_api(stubs, cls=None, private=None, publics=None):
Expand All @@ -207,7 +208,7 @@ def get_floating_ips_by_fixed_address(*args, **kwargs):

if cls is None:
cls = Fake
stubs.Set(nova.network, 'API', cls)
stubs.Set(network_api, 'API', cls)
fake_network.stub_out_nw_api_get_instance_nw_info(stubs, spectacular=True)


Expand Down
11 changes: 6 additions & 5 deletions nova/tests/compute/test_compute.py
Expand Up @@ -41,6 +41,7 @@
from nova import db
from nova import exception
from nova import flags
from nova.network import api as network_api
from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging
Expand Down Expand Up @@ -195,9 +196,9 @@ def fake_get_nw_info(cls, ctxt, instance, *args, **kwargs):
spectacular=True)

super(ComputeTestCase, self).setUp()
self.stubs.Set(nova.network.API, 'get_instance_nw_info',
self.stubs.Set(network_api.API, 'get_instance_nw_info',
fake_get_nw_info)
self.stubs.Set(nova.network.API, 'allocate_for_instance',
self.stubs.Set(network_api.API, 'allocate_for_instance',
fake_get_nw_info)
self.compute_api = compute.API()

Expand Down Expand Up @@ -1193,7 +1194,7 @@ def test_add_fixed_ip_usage_notification(self):
def dummy(*args, **kwargs):
pass

self.stubs.Set(nova.network.API, 'add_fixed_ip_to_instance',
self.stubs.Set(network_api.API, 'add_fixed_ip_to_instance',
dummy)
self.stubs.Set(nova.compute.manager.ComputeManager,
'inject_network_info', dummy)
Expand All @@ -1214,7 +1215,7 @@ def test_remove_fixed_ip_usage_notification(self):
def dummy(*args, **kwargs):
pass

self.stubs.Set(nova.network.API, 'remove_fixed_ip_from_instance',
self.stubs.Set(network_api.API, 'remove_fixed_ip_from_instance',
dummy)
self.stubs.Set(nova.compute.manager.ComputeManager,
'inject_network_info', dummy)
Expand Down Expand Up @@ -2653,7 +2654,7 @@ def fake_get_nw_info(cls, ctxt, instance):
spectacular=True)

super(ComputeAPITestCase, self).setUp()
self.stubs.Set(nova.network.API, 'get_instance_nw_info',
self.stubs.Set(network_api.API, 'get_instance_nw_info',
fake_get_nw_info)
self.security_group_api = compute.api.SecurityGroupAPI()
self.compute_api = compute.API(
Expand Down
3 changes: 2 additions & 1 deletion nova/tests/compute/test_compute_utils.py
Expand Up @@ -25,6 +25,7 @@
from nova import db
from nova import exception
from nova import flags
from nova.network import api as network_api
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova.openstack.common.notifier import api as notifier_api
Expand Down Expand Up @@ -194,7 +195,7 @@ def fake_get_nw_info(cls, ctxt, instance):
spectacular=True)

super(UsageInfoTestCase, self).setUp()
self.stubs.Set(nova.network.API, 'get_instance_nw_info',
self.stubs.Set(network_api.API, 'get_instance_nw_info',
fake_get_nw_info)

self.flags(compute_driver='nova.virt.fake.FakeDriver',
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/fake_network.py
Expand Up @@ -21,6 +21,7 @@
from nova import db
from nova import exception
from nova import flags
from nova.network import api as network_api
from nova.network import manager as network_manager
from nova.network import model as network_model
from nova.network import nova_ipam_lib
Expand Down Expand Up @@ -362,7 +363,6 @@ def stub_out_nw_api_get_instance_nw_info(stubs, func=None,
ips_per_vif=1,
floating_ips_per_fixed_ip=0,
spectacular=False):
import nova.network

def get_instance_nw_info(self, context, instance):
return fake_get_instance_nw_info(stubs, num_networks=num_networks,
Expand All @@ -372,7 +372,7 @@ def get_instance_nw_info(self, context, instance):

if func is None:
func = get_instance_nw_info
stubs.Set(nova.network.API, 'get_instance_nw_info', func)
stubs.Set(network_api.API, 'get_instance_nw_info', func)


_real_functions = {}
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/test_metadata.py
Expand Up @@ -32,7 +32,7 @@
from nova.db.sqlalchemy import api
from nova import exception
from nova import flags
from nova import network
from nova.network import api as network_api
from nova import test
from nova.tests import fake_network

Expand Down Expand Up @@ -362,7 +362,7 @@ def test_version_root(self):
self.assertEqual(response.status_int, 404)

def test_user_data_non_existing_fixed_address(self):
self.stubs.Set(network.API, 'get_fixed_ip_by_address',
self.stubs.Set(network_api.API, 'get_fixed_ip_by_address',
return_non_existing_address)
response = fake_request(None, self.mdinst, "/2009-04-04/user-data",
"127.1.1.1")
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/test_notifications.py
Expand Up @@ -25,7 +25,7 @@
from nova import context
from nova import db
from nova import flags
import nova.network
from nova.network import api as network_api
from nova import notifications
from nova.openstack.common import log as logging
from nova.openstack.common.notifier import api as notifier_api
Expand All @@ -49,7 +49,7 @@ def fake_get_nw_info(cls, ctxt, instance):
self.assertTrue(ctxt.is_admin)
return self.net_info

self.stubs.Set(nova.network.API, 'get_instance_nw_info',
self.stubs.Set(network_api.API, 'get_instance_nw_info',
fake_get_nw_info)
fake_network.set_stub_network_methods(self.stubs)

Expand Down
6 changes: 3 additions & 3 deletions nova/virt/firewall.py
Expand Up @@ -20,6 +20,8 @@
from nova import context
from nova import db
from nova import flags
from nova import network
from nova.network import linux_net
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
Expand Down Expand Up @@ -138,7 +140,6 @@ class IptablesFirewallDriver(FirewallDriver):
"""Driver which enforces security groups through iptables rules."""

def __init__(self, **kwargs):
from nova.network import linux_net
self.iptables = linux_net.iptables_manager
self.instances = {}
self.network_infos = {}
Expand Down Expand Up @@ -393,8 +394,7 @@ def instance_rules(self, instance, network_info):
# has access to a nw_api handle,
# and should be the only one making
# making rpc calls.
import nova.network
nw_api = nova.network.API()
nw_api = network.API()
for instance in rule['grantee_group']['instances']:
nw_info = nw_api.get_instance_nw_info(ctxt,
instance)
Expand Down

0 comments on commit 197398f

Please sign in to comment.