Make nova_ipam_lib use Network, FixedIP, and FloatingIP objects

This makes nova_ipam_lib use objects for almost everything except
for VIF-related functions, which is coming soon.

Related to blueprint nova-network-objects

Change-Id: Ib39bbc86921eeb849e715a205dccaab3aad6d1c6
This commit is contained in:
Dan Smith 2014-01-17 08:44:24 -08:00
parent 9776175fc3
commit 2a916819f3
3 changed files with 47 additions and 26 deletions

View File

@ -639,7 +639,7 @@ class NetworkManager(manager.Manager):
continue continue
gfipbfa = self.ipam.get_floating_ips_by_fixed_address gfipbfa = self.ipam.get_floating_ips_by_fixed_address
floating_ips = gfipbfa(context, fixed_ip['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') type='floating')
for ip in floating_ips] for ip in floating_ips]
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 # License for the specific language governing permissions and limitations
# under the License. # under the License.
from nova import db import netaddr
from nova import ipv6 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 from nova.objects import virtual_interface as vif_obj
@ -39,29 +43,36 @@ class NeutronNovaIPAMLib(object):
"""Returns information about the IPv4 and IPv6 subnets """Returns information about the IPv4 and IPv6 subnets
associated with a Neutron Network UUID. 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 = { subnet_v4 = {
'network_id': n['uuid'], 'network_id': n.uuid,
'cidr': n['cidr'], 'cidr': n.cidr,
'gateway': n['gateway'], 'gateway': n.gateway,
'broadcast': n['broadcast'], 'broadcast': n.broadcast,
'netmask': n['netmask'], 'netmask': n.netmask,
'version': 4, 'version': 4,
'dns1': n['dns1'], 'dns1': n.dns1,
'dns2': n['dns2']} 'dns2': n.dns2}
#TODO(tr3buchet): I'm noticing we've assumed here that all dns is v4. #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 # this is probably bad as there is no way to add v6
# dns to nova # dns to nova
subnet_v6 = { subnet_v6 = {
'network_id': n['uuid'], 'network_id': n.uuid,
'cidr': n['cidr_v6'], 'cidr': n.cidr_v6,
'gateway': n['gateway_v6'], 'gateway': n.gateway_v6,
'broadcast': None, 'broadcast': None,
'netmask': n['netmask_v6'], 'netmask': n.netmask_v6,
'version': 6, 'version': 6,
'dns1': None, 'dns1': None,
'dns2': 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): def get_routes_by_ip_block(self, context, block_id, project_id):
"""Returns the list of routes for the IP block.""" """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) vif_rec = vif_obj.VirtualInterface.get_by_uuid(context, vif_id)
if not vif_rec: if not vif_rec:
return [] return []
fixed_ips = db.fixed_ips_by_virtual_interface(context, fixed_ips = fixed_ip_obj.FixedIPList.get_by_virtual_interface_id(
vif_rec.id) context, vif_rec.id)
return [fixed_ip['address'] for fixed_ip in fixed_ips] 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): def get_v6_ips_by_interface(self, context, net_id, vif_id, project_id):
"""Returns a list containing a single IPv6 address strings """Returns a list containing a single IPv6 address strings
associated with the specified virtual interface. associated with the specified virtual interface.
""" """
admin_context = context.elevated() 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) vif_rec = vif_obj.VirtualInterface.get_by_uuid(context, vif_id)
if network['cidr_v6'] and vif_rec and vif_rec.address: if network.cidr_v6 and vif_rec and vif_rec.address:
ip = ipv6.to_global(network['cidr_v6'], ip = ipv6.to_global(network.cidr_v6,
vif_rec.address, vif_rec.address,
project_id) project_id)
return [ip] return [ip]
return [] return []
def get_floating_ips_by_fixed_address(self, context, fixed_address): 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 import db
from nova.network import nova_ipam_lib from nova.network import nova_ipam_lib
from nova import test from nova import test
from nova.tests.objects import test_fixed_ip
from nova.tests.objects import test_network
fake_vif = { fake_vif = {
@ -38,12 +40,15 @@ fake_vif = {
class NeutronNovaIPAMTestCase(test.NoDBTestCase): class NeutronNovaIPAMTestCase(test.NoDBTestCase):
def test_get_v4_ips_by_interface(self): 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( with contextlib.nested(
mock.patch.object(db, 'virtual_interface_get_by_uuid', mock.patch.object(db, 'virtual_interface_get_by_uuid',
return_value=fake_vif), return_value=fake_vif),
mock.patch.object(db, 'fixed_ips_by_virtual_interface', mock.patch.object(db, 'fixed_ips_by_virtual_interface',
return_value=[{'address': '192.168.1.101'}, return_value=fake_ips)
{'address': '192.168.1.102'}])
): ):
ipam_lib = nova_ipam_lib.NeutronNovaIPAMLib(None) ipam_lib = nova_ipam_lib.NeutronNovaIPAMLib(None)
v4_IPs = ipam_lib.get_v4_ips_by_interface(None, v4_IPs = ipam_lib.get_v4_ips_by_interface(None,
@ -69,9 +74,11 @@ class NeutronNovaIPAMTestCase(test.NoDBTestCase):
def elevated(self): def elevated(self):
pass pass
net = dict(test_network.fake_network,
cidr_v6='2001:db8::')
with contextlib.nested( with contextlib.nested(
mock.patch.object(db, 'network_get_by_uuid', 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', mock.patch.object(db, 'virtual_interface_get_by_uuid',
return_value=fake_vif) return_value=fake_vif)
): ):
@ -88,9 +95,11 @@ class NeutronNovaIPAMTestCase(test.NoDBTestCase):
def elevated(self): def elevated(self):
pass pass
net = dict(test_network.fake_network,
cidr_v6='2001:db8::')
with contextlib.nested( with contextlib.nested(
mock.patch.object(db, 'network_get_by_uuid', 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', mock.patch.object(db, 'virtual_interface_get_by_uuid',
return_value=None) return_value=None)
): ):