From 2a916819f392c3f1a0fa90e9a5f96c8b312d789d Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Fri, 17 Jan 2014 08:44:24 -0800 Subject: [PATCH] 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 --- nova/network/manager.py | 2 +- nova/network/nova_ipam_lib.py | 54 ++++++++++++++++++----------- nova/tests/network/test_ipam_lib.py | 17 ++++++--- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/nova/network/manager.py b/nova/network/manager.py index ff2e10323678..1d657bf918ce 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -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: diff --git a/nova/network/nova_ipam_lib.py b/nova/network/nova_ipam_lib.py index 58af85b68e52..cb7046a0cb29 100644 --- a/nova/network/nova_ipam_lib.py +++ b/nova/network/nova_ipam_lib.py @@ -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) diff --git a/nova/tests/network/test_ipam_lib.py b/nova/tests/network/test_ipam_lib.py index c58203643e96..abc939f77c14 100644 --- a/nova/tests/network/test_ipam_lib.py +++ b/nova/tests/network/test_ipam_lib.py @@ -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) ):