Skip to content

Commit

Permalink
Merge "Remove unneeded call to conductor in network interface" into s…
Browse files Browse the repository at this point in the history
…table/havana
  • Loading branch information
Jenkins authored and openstack-gerrit committed Mar 6, 2014
2 parents 6aaf436 + d3c8757 commit 1b790cc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
22 changes: 9 additions & 13 deletions nova/network/neutronv2/api.py
Expand Up @@ -20,6 +20,7 @@

from neutronclient.common import exceptions as neutron_client_exc
from oslo.config import cfg
import six

from nova.compute import flavors
from nova import conductor
Expand Down Expand Up @@ -991,27 +992,22 @@ def _build_network_info_model(self, context, instance, networks=None):
data = client.list_ports(**search_opts)
ports = data.get('ports', [])
nw_info = network_model.NetworkInfo()

# Unfortunately, this is sometimes in unicode and sometimes not
if isinstance(instance['info_cache']['network_info'], six.text_type):
ifaces = jsonutils.loads(instance['info_cache']['network_info'])
else:
ifaces = instance['info_cache']['network_info']

if networks is None:
# retrieve networks from info_cache to get correct nic order
network_cache = self.conductor_api.instance_get_by_uuid(
context, instance['uuid'])['info_cache']['network_info']
network_cache = jsonutils.loads(network_cache)
net_ids = [iface['network']['id'] for iface in network_cache]
net_ids = [iface['network']['id'] for iface in ifaces]
networks = self._get_available_networks(context,
instance['project_id'],
net_ids)

# ensure ports are in preferred network order, and filter out
# those not attached to one of the provided list of networks
else:

# Unfortunately, this is sometimes in unicode and sometimes not
if isinstance(instance['info_cache']['network_info'], unicode):
ifaces = jsonutils.loads(
instance['info_cache']['network_info'])
else:
ifaces = instance['info_cache']['network_info']

# Include existing interfaces so they are not removed from the db.
# Needed when interfaces are added to existing instances.
for iface in ifaces:
Expand Down
39 changes: 17 additions & 22 deletions nova/tests/network/test_neutronv2.py
Expand Up @@ -15,12 +15,14 @@
#
# vim: tabstop=4 shiftwidth=4 softtabstop=4

import copy
import uuid

import mox
from neutronclient.common import exceptions
from neutronclient.v2_0 import client
from oslo.config import cfg
import six

from nova.compute import flavors
from nova.conductor import api as conductor_api
Expand Down Expand Up @@ -401,18 +403,14 @@ def _get_instance_nw_info(self, number):
self.instance['uuid'],
mox.IgnoreArg())
port_data = number == 1 and self.port_data1 or self.port_data2
nets = number == 1 and self.nets1 or self.nets2
self.mox.StubOutWithMock(conductor_api.API,
'instance_get_by_uuid')

net_info_cache = []
for port in port_data:
net_info_cache.append({"network": {"id": port['network_id']}})
info_cache = {'info_cache': {'network_info':
jsonutils.dumps(net_info_cache)}}
instance = copy.copy(self.instance)
# This line here does not wrap net_info_cache in jsonutils.dumps()
# intentionally to test the other code path when it's not unicode.
instance['info_cache'] = {'network_info': net_info_cache}

api.conductor_api.instance_get_by_uuid(
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(info_cache)
self.moxed_client.list_ports(
tenant_id=self.instance['project_id'],
device_id=self.instance['uuid']).AndReturn(
Expand All @@ -439,7 +437,7 @@ def _get_instance_nw_info(self, number):
device_owner='network:dhcp').AndReturn(
{'ports': []})
self.mox.ReplayAll()
nw_inf = api.get_instance_nw_info(self.context, self.instance)
nw_inf = api.get_instance_nw_info(self.context, instance)
for i in xrange(0, number):
self._verify_nw_info(nw_inf, i)

Expand Down Expand Up @@ -579,16 +577,15 @@ def test_get_instance_nw_info_without_subnet(self):
net_info_cache = []
for port in self.port_data3:
net_info_cache.append({"network": {"id": port['network_id']}})
info_cache = {'info_cache': {'network_info':
jsonutils.dumps(net_info_cache)}}

api.conductor_api.instance_get_by_uuid(
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(info_cache)
instance = copy.copy(self.instance)
instance['info_cache'] = {'network_info':
six.text_type(
jsonutils.dumps(net_info_cache))}

self.mox.ReplayAll()

nw_inf = api.get_instance_nw_info(self.context,
self.instance)
instance)

id_suffix = 3
self.assertEquals(0, len(nw_inf.fixed_ips()))
Expand Down Expand Up @@ -890,17 +887,15 @@ def _test_deallocate_port_for_instance(self, number):
port_data = number == 1 and self.port_data1 or self.port_data2
nets = number == 1 and self.nets1 or self.nets2
self.moxed_client.delete_port(port_data[0]['id'])
self.mox.StubOutWithMock(conductor_api.API,
'instance_get_by_uuid')

net_info_cache = []
for port in port_data:
net_info_cache.append({"network": {"id": port['network_id']}})
info_cache = {'info_cache': {'network_info':
jsonutils.dumps(net_info_cache)}}
instance = copy.copy(self.instance)
instance['info_cache'] = {'network_info':
six.text_type(
jsonutils.dumps(net_info_cache))}
api = neutronapi.API()
api.conductor_api.instance_get_by_uuid(
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(info_cache)
neutronv2.get_client(mox.IgnoreArg(), admin=True).AndReturn(
self.moxed_client)
self.moxed_client.list_ports(
Expand All @@ -924,7 +919,7 @@ def _test_deallocate_port_for_instance(self, number):

self.mox.ReplayAll()

nwinfo = api.deallocate_port_for_instance(self.context, self.instance,
nwinfo = api.deallocate_port_for_instance(self.context, instance,
port_data[0]['id'])
self.assertEqual(len(nwinfo), len(port_data[1:]))
if len(port_data) > 1:
Expand Down

0 comments on commit 1b790cc

Please sign in to comment.