launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #09888
[Merge] lp:~rvb/maas/maas-dns2 into lp:maas
Raphaël Badin has proposed merging lp:~rvb/maas/maas-dns2 into lp:maas.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~rvb/maas/maas-dns2/+merge/114707
This branch adds a new method get_hostname_ip_mapping to DHCPLease.objects. This method returns a mapping hostname -> ip for all the nodes in the given node group by considering the valid leases (from the table maasserver_dhcplease) for the first interface of each node.
This method will be used to populate the DNS zone files.
= Pre-imp =
This was discussed on IRC with Gavin and Jeroen.
= Notes =
I don't think it's possible to avoid the subquery given the fact that we have to consider only the first interface for all the nodes... but I'm happy to be proven wrong.
--
https://code.launchpad.net/~rvb/maas/maas-dns2/+merge/114707
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-dns2 into lp:maas.
=== modified file 'src/maasserver/models/dhcplease.py'
--- src/maasserver/models/dhcplease.py 2012-07-11 04:14:08 +0000
+++ src/maasserver/models/dhcplease.py 2012-07-12 19:15:39 +0000
@@ -96,6 +96,37 @@
self._delete_obsolete_leases(nodegroup, leases)
self._add_missing_leases(nodegroup, leases)
+ def get_hostname_ip_mapping(self, nodegroup):
+ """Return a mapping {hostnames -> ips} for the currently leased
+ IP addresses for the nodes in `nodegroup`.
+
+ This will consider only the first interface (i.e. the first
+ MAC Address) associated with each node withing the given
+ `nodegroup`.
+ """
+ cursor = connection.cursor()
+ # The subquery fetches the IDs of the first MAC Address for
+ # all the nodes in this nodegroup.
+ # Then the main query returns the hostname -> ip mapping for
+ # these MAC Addresses.
+ cursor.execute("""
+ SELECT hostname, ip
+ FROM maasserver_macaddress as mac,
+ maasserver_node as node,
+ maasserver_dhcplease as lease
+ WHERE mac.id IN (
+ SELECT DISTINCT ON (node_id) mac.id
+ FROM maasserver_macaddress as mac,
+ maasserver_node as node
+ WHERE node.nodegroup_id = %s AND mac.node_id = node.id
+ ORDER BY node_id, mac.created
+ )
+ AND mac.node_id = node.id
+ AND mac.mac_address = lease.mac
+ AND lease.nodegroup_id = %s
+ """ % (nodegroup.id, nodegroup.id))
+ return dict(cursor.fetchall())
+
class DHCPLease(CleanSave, Model):
"""A known mapping of an IP address to a MAC address.
=== modified file 'src/maasserver/tests/test_dhcplease.py'
--- src/maasserver/tests/test_dhcplease.py 2012-07-10 17:28:42 +0000
+++ src/maasserver/tests/test_dhcplease.py 2012-07-12 19:15:39 +0000
@@ -134,3 +134,41 @@
new_ip: mac1,
},
map_leases(nodegroup))
+
+ def test_get_hostname_ip_mapping_returns_mapping(self):
+ nodegroup = factory.make_node_group()
+ expected_mapping = {}
+ for i in range(3):
+ node = factory.make_node(
+ nodegroup=nodegroup, set_hostname=True)
+ mac = factory.make_mac_address(node=node)
+ factory.make_mac_address(node=node)
+ lease = factory.make_dhcp_lease(
+ nodegroup=nodegroup, mac=mac.mac_address)
+ expected_mapping[node.hostname] = lease.ip
+ mapping = DHCPLease.objects.get_hostname_ip_mapping(nodegroup)
+ self.assertEqual(expected_mapping, mapping)
+
+ def test_get_hostname_ip_mapping_considers_only_first_mac(self):
+ nodegroup = factory.make_node_group()
+ node = factory.make_node(
+ nodegroup=nodegroup, set_hostname=True)
+ factory.make_mac_address(node=node)
+ second_mac = factory.make_mac_address(node=node)
+ # Create a lease for the second MAC Address.
+ factory.make_dhcp_lease(
+ nodegroup=nodegroup, mac=second_mac.mac_address)
+ mapping = DHCPLease.objects.get_hostname_ip_mapping(nodegroup)
+ self.assertEqual({}, mapping)
+
+ def test_get_hostname_ip_mapping_considers_given_nodegroup(self):
+ nodegroup = factory.make_node_group()
+ node = factory.make_node(
+ nodegroup=nodegroup, set_hostname=True)
+ mac = factory.make_mac_address(node=node)
+ factory.make_dhcp_lease(
+ nodegroup=nodegroup, mac=mac.mac_address)
+ another_nodegroup = factory.make_node_group()
+ mapping = DHCPLease.objects.get_hostname_ip_mapping(
+ another_nodegroup)
+ self.assertEqual({}, mapping)