Merge lp:~julian-edwards/maas/auto-link-mac-to-network into lp:~maas-committers/maas/trunk

Proposed by Julian Edwards
Status: Merged
Approved by: Julian Edwards
Approved revision: no longer in the source branch.
Merged at revision: 2695
Proposed branch: lp:~julian-edwards/maas/auto-link-mac-to-network
Merge into: lp:~maas-committers/maas/trunk
Prerequisite: lp:~julian-edwards/maas/cluster-interfaces-as-networks-2
Diff against target: 79 lines (+34/-2)
2 files modified
src/maasserver/api.py (+10/-0)
src/maasserver/tests/test_api_nodegroup.py (+24/-2)
To merge this branch: bzr merge lp:~julian-edwards/maas/auto-link-mac-to-network
Reviewer Review Type Date Requested Status
Gavin Panella (community) Approve
Review via email: mp+230422@code.launchpad.net

This proposal supersedes a proposal from 2014-08-12.

Commit message

Auto-populate the connected Network for a MAC at the same time the MAC's cluster_interface is set (at lease upload time).

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) :
review: Approve
Revision history for this message
Julian Edwards (julian-edwards) wrote :

Thanks for the review.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/api.py'
2--- src/maasserver/api.py 2014-08-11 21:49:55 +0000
3+++ src/maasserver/api.py 2014-08-12 03:08:32 +0000
4@@ -1728,6 +1728,16 @@
5 if netaddr.IPAddress(ip) in ip_range:
6 mac_address.cluster_interface = interface
7 mac_address.save()
8+
9+ # Locate the Network to which this MAC belongs.
10+ ipnetwork = interface.network
11+ if ipnetwork is not None:
12+ try:
13+ network = Network.objects.get(ip=ipnetwork.ip.format())
14+ network.macaddress_set.add(mac_address)
15+ except Network.DoesNotExist:
16+ pass
17+
18 # Cheap optimisation. No other interfaces will match, so
19 # break out of the loop.
20 break
21
22=== modified file 'src/maasserver/tests/test_api_nodegroup.py'
23--- src/maasserver/tests/test_api_nodegroup.py 2014-08-11 21:49:55 +0000
24+++ src/maasserver/tests/test_api_nodegroup.py 2014-08-12 03:08:32 +0000
25@@ -32,6 +32,7 @@
26 NODEGROUP_STATUS_CHOICES,
27 NODEGROUPINTERFACE_MANAGEMENT,
28 )
29+from maasserver.forms import create_Network_from_NodeGroupInterface
30 from maasserver.models import (
31 Config,
32 DHCPLease,
33@@ -833,7 +834,7 @@
34 class TestUpdateMacClusterInterfaces(MAASServerTestCase):
35 """Tests for `update_mac_cluster_interfaces`()."""
36
37- def test_updates_mac_cluster_interfaces(self):
38+ def make_cluster_with_macs_and_leases(self):
39 cluster = factory.make_node_group()
40 mac_addresses = {
41 factory.make_mac_address(): factory.make_node_group_interface(
42@@ -843,8 +844,13 @@
43 leases = {
44 get_random_ip_from_interface_range(interface): (
45 mac_address.mac_address)
46- for mac_address, interface in mac_addresses.items()
47+ for mac_address, interface in mac_addresses.viewitems()
48 }
49+ return cluster, mac_addresses, leases
50+
51+ def test_updates_mac_cluster_interfaces(self):
52+ cluster, mac_addresses, leases = (
53+ self.make_cluster_with_macs_and_leases())
54 update_mac_cluster_interfaces(leases, cluster)
55 results = {
56 mac_address: mac_address.cluster_interface
57@@ -853,6 +859,22 @@
58 }
59 self.assertEqual(mac_addresses, results)
60
61+ def test_updates_network_relations(self):
62+ # update_mac_cluster_interfaces should also associate the mac
63+ # with the network on which it resides.
64+ cluster, mac_addresses, leases = (
65+ self.make_cluster_with_macs_and_leases())
66+ expected_relations = []
67+ for mac, interface in mac_addresses.viewitems():
68+ net = create_Network_from_NodeGroupInterface(interface)
69+ expected_relations.append((net, mac))
70+ update_mac_cluster_interfaces(leases, cluster)
71+ # Doing a single giant comparison here would be unintuitive and
72+ # fugly, so I'm iterating.
73+ for net, mac in expected_relations:
74+ [observed_macddress] = net.macaddress_set.all()
75+ self.assertEqual(mac, observed_macddress)
76+
77 def test_ignores_mac_not_attached_to_cluster(self):
78 cluster = factory.make_node_group()
79 mac_address = factory.make_mac_address()