Merge lp:~ltrager/maas/prevent_assigning_ip_to_bonded_interface into lp:~maas-committers/maas/trunk

Proposed by Lee Trager
Status: Merged
Approved by: Lee Trager
Approved revision: no longer in the source branch.
Merged at revision: 4472
Proposed branch: lp:~ltrager/maas/prevent_assigning_ip_to_bonded_interface
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 63 lines (+34/-2)
2 files modified
src/maasserver/forms_interface_link.py (+11/-2)
src/maasserver/tests/test_forms_interface_link.py (+23/-0)
To merge this branch: bzr merge lp:~ltrager/maas/prevent_assigning_ip_to_bonded_interface
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+276815@code.launchpad.net

Commit message

Don't allow linking interfaces when they are in a bond

Description of the change

Blake was right this morning. We were properly resetting interfaces when they were added to a new bond but still allowed the user to link an interface which is part of a bond to a subnet. This patch prevents linking an interface if its part of a bond. I wasn't sure if there were other interface sets which we should check for so I'm only checking for bonds right now.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good. Doing it only on bonds is correct all other interface modes will allow this.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/forms_interface_link.py'
2--- src/maasserver/forms_interface_link.py 2015-09-15 19:08:17 +0000
3+++ src/maasserver/forms_interface_link.py 2015-11-05 20:08:52 +0000
4@@ -27,8 +27,11 @@
5 IPADDRESS_TYPE,
6 )
7 from maasserver.fields import CaseInsensitiveChoiceField
8-from maasserver.models.interface import Interface
9-from maasserver.models.staticipaddress import StaticIPAddress
10+from maasserver.models import (
11+ BondInterface,
12+ Interface,
13+ StaticIPAddress,
14+)
15 from maasserver.utils.forms import (
16 compose_invalid_choice_text,
17 set_form_error,
18@@ -65,6 +68,12 @@
19 self.fields['subnet'].queryset = self.instance.vlan.subnet_set.all()
20
21 def clean(self):
22+ for interface_set in self.instance.interface_set.all():
23+ if isinstance(interface_set, BondInterface):
24+ set_form_error(
25+ self, "bond",
26+ ("Cannot link interface(%s) when interface is in a "
27+ "bond(%s)." % (self.instance.name, interface_set.name)))
28 cleaned_data = super(InterfaceLinkForm, self).clean()
29 mode = cleaned_data.get("mode", None)
30 if mode is None:
31
32=== modified file 'src/maasserver/tests/test_forms_interface_link.py'
33--- src/maasserver/tests/test_forms_interface_link.py 2015-09-15 19:08:17 +0000
34+++ src/maasserver/tests/test_forms_interface_link.py 2015-11-05 20:08:52 +0000
35@@ -433,6 +433,29 @@
36 "Cannot use in mode '%s'." % (INTERFACE_LINK_TYPE.LINK_UP)]
37 }, form.errors)
38
39+ def test_linking_when_no_bond_not_allowed(self):
40+ node = factory.make_Node()
41+ eth0 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL, node=node)
42+ eth1 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL, node=node)
43+ bond0 = factory.make_Interface(
44+ INTERFACE_TYPE.BOND, parents=[eth0, eth1], node=node)
45+ subnet = factory.make_Subnet(vlan=eth0.vlan)
46+ nodegroup = factory.make_NodeGroup(status=NODEGROUP_STATUS.ENABLED)
47+ ngi = factory.make_NodeGroupInterface(
48+ nodegroup, management=NODEGROUPINTERFACE_MANAGEMENT.DHCP,
49+ subnet=subnet)
50+ ip_in_static = IPAddress(ngi.get_static_ip_range().first)
51+ form = InterfaceLinkForm(instance=eth0, data={
52+ "mode": INTERFACE_LINK_TYPE.STATIC,
53+ "subnet": subnet.id,
54+ "ip_address": "%s" % ip_in_static,
55+ })
56+ self.assertFalse(form.is_valid())
57+ self.assertEquals({
58+ "bond": [("Cannot link interface(%s) when interface is in a "
59+ "bond(%s)." % (eth0.name, bond0.name))]},
60+ form.errors)
61+
62
63 class TestInterfaceUnlinkForm(MAASServerTestCase):
64