Merge "Make nova_ipam_lib use Network, FixedIP, and FloatingIP objects"

This commit is contained in:
Jenkins 2014-02-14 21:19:54 +00:00 committed by Gerrit Code Review
commit 43a00e4c04
3 changed files with 47 additions and 26 deletions

View File

@ -639,7 +639,7 @@ class NetworkManager(manager.Manager):
continue
gfipbfa = self.ipam.get_floating_ips_by_fixed_address
floating_ips = gfipbfa(context, fixed_ip['address'])
floating_ips = [network_model.IP(address=ip['address'],
floating_ips = [network_model.IP(address=str(ip['address']),
type='floating')
for ip in floating_ips]
for ip in floating_ips:

View File

@ -13,8 +13,12 @@
# License for the specific language governing permissions and limitations
# under the License.
from nova import db
import netaddr
from nova import ipv6
from nova.objects import fixed_ip as fixed_ip_obj
from nova.objects import floating_ip as floating_ip_obj
from nova.objects import network as network_obj
from nova.objects import virtual_interface as vif_obj
@ -39,29 +43,36 @@ class NeutronNovaIPAMLib(object):
"""Returns information about the IPv4 and IPv6 subnets
associated with a Neutron Network UUID.
"""
n = db.network_get_by_uuid(context.elevated(), net_id)
n = network_obj.Network.get_by_uuid(context.elevated(), net_id)
subnet_v4 = {
'network_id': n['uuid'],
'cidr': n['cidr'],
'gateway': n['gateway'],
'broadcast': n['broadcast'],
'netmask': n['netmask'],
'network_id': n.uuid,
'cidr': n.cidr,
'gateway': n.gateway,
'broadcast': n.broadcast,
'netmask': n.netmask,
'version': 4,
'dns1': n['dns1'],
'dns2': n['dns2']}
'dns1': n.dns1,
'dns2': n.dns2}
#TODO(tr3buchet): I'm noticing we've assumed here that all dns is v4.
# this is probably bad as there is no way to add v6
# dns to nova
subnet_v6 = {
'network_id': n['uuid'],
'cidr': n['cidr_v6'],
'gateway': n['gateway_v6'],
'network_id': n.uuid,
'cidr': n.cidr_v6,
'gateway': n.gateway_v6,
'broadcast': None,
'netmask': n['netmask_v6'],
'netmask': n.netmask_v6,
'version': 6,
'dns1': None,
'dns2': None}
return [subnet_v4, subnet_v6]
def ips_to_strs(net):
for key, value in net.items():
if isinstance(value, netaddr.ip.BaseIP):
net[key] = str(value)
return net
return [ips_to_strs(subnet_v4), ips_to_strs(subnet_v6)]
def get_routes_by_ip_block(self, context, block_id, project_id):
"""Returns the list of routes for the IP block."""
@ -75,23 +86,24 @@ class NeutronNovaIPAMLib(object):
vif_rec = vif_obj.VirtualInterface.get_by_uuid(context, vif_id)
if not vif_rec:
return []
fixed_ips = db.fixed_ips_by_virtual_interface(context,
vif_rec.id)
return [fixed_ip['address'] for fixed_ip in fixed_ips]
fixed_ips = fixed_ip_obj.FixedIPList.get_by_virtual_interface_id(
context, vif_rec.id)
return [str(fixed_ip.address) for fixed_ip in fixed_ips]
def get_v6_ips_by_interface(self, context, net_id, vif_id, project_id):
"""Returns a list containing a single IPv6 address strings
associated with the specified virtual interface.
"""
admin_context = context.elevated()
network = db.network_get_by_uuid(admin_context, net_id)
network = network_obj.Network.get_by_uuid(admin_context, net_id)
vif_rec = vif_obj.VirtualInterface.get_by_uuid(context, vif_id)
if network['cidr_v6'] and vif_rec and vif_rec.address:
ip = ipv6.to_global(network['cidr_v6'],
if network.cidr_v6 and vif_rec and vif_rec.address:
ip = ipv6.to_global(network.cidr_v6,
vif_rec.address,
project_id)
return [ip]
return []
def get_floating_ips_by_fixed_address(self, context, fixed_address):
return db.floating_ip_get_by_fixed_address(context, fixed_address)
return floating_ip_obj.FloatingIPList.get_by_fixed_address(
context, fixed_address)

View File

@ -21,6 +21,8 @@ from nova import context
from nova import db
from nova.network import nova_ipam_lib
from nova import test
from nova.tests.objects import test_fixed_ip
from nova.tests.objects import test_network
fake_vif = {
@ -38,12 +40,15 @@ fake_vif = {
class NeutronNovaIPAMTestCase(test.NoDBTestCase):
def test_get_v4_ips_by_interface(self):
fake_ips = [dict(test_fixed_ip.fake_fixed_ip,
address='192.168.1.101'),
dict(test_fixed_ip.fake_fixed_ip,
address='192.168.1.102')]
with contextlib.nested(
mock.patch.object(db, 'virtual_interface_get_by_uuid',
return_value=fake_vif),
mock.patch.object(db, 'fixed_ips_by_virtual_interface',
return_value=[{'address': '192.168.1.101'},
{'address': '192.168.1.102'}])
return_value=fake_ips)
):
ipam_lib = nova_ipam_lib.NeutronNovaIPAMLib(None)
v4_IPs = ipam_lib.get_v4_ips_by_interface(None,
@ -69,9 +74,11 @@ class NeutronNovaIPAMTestCase(test.NoDBTestCase):
def elevated(self):
pass
net = dict(test_network.fake_network,
cidr_v6='2001:db8::')
with contextlib.nested(
mock.patch.object(db, 'network_get_by_uuid',
return_value={'cidr_v6': '2001:db8::'}),
return_value=net),
mock.patch.object(db, 'virtual_interface_get_by_uuid',
return_value=fake_vif)
):
@ -88,9 +95,11 @@ class NeutronNovaIPAMTestCase(test.NoDBTestCase):
def elevated(self):
pass
net = dict(test_network.fake_network,
cidr_v6='2001:db8::')
with contextlib.nested(
mock.patch.object(db, 'network_get_by_uuid',
return_value={'cidr_v6': '2001:db8::'}),
return_value=net),
mock.patch.object(db, 'virtual_interface_get_by_uuid',
return_value=None)
):