Merge ~ltrager/maas:lp1816651 into maas:master

Proposed by Lee Trager
Status: Merged
Approved by: Lee Trager
Approved revision: 326d99566d56686a4e0bc103be14582e9679e620
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ltrager/maas:lp1816651
Merge into: maas:master
Diff against target: 67 lines (+29/-8)
2 files modified
src/maasserver/models/bmc.py (+9/-7)
src/maasserver/models/tests/test_bmc.py (+20/-1)
Reviewer Review Type Date Requested Status
Mike Pontillo (community) Approve
MAAS Lander Approve
Review via email: mp+363520@code.launchpad.net

Commit message

LP: #1816651 - Handle duplicate IP addresses when creating a BMC object.

To post a comment you must log in.
~ltrager/maas:lp1816651 updated
e172ae2... by Lee Trager

Copyright++

Revision history for this message
Mike Pontillo (mpontillo) wrote :

Can you see if this slightly simpler change does the trick?

https://paste.ubuntu.com/p/cxqZqmzZHC/

I don't think this change will work as intended, since the BMC IP might be an AUTO IP, not strictly a STICKY. For example, if I deploy a machine and use it for a KVM pod, it might get an AUTO address (or even DHCP), rather than STICKY.

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp1816651 lp:~ltrager/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: e172ae26e4215c5ab6ea158069e67f69e2a1d236

review: Approve
~ltrager/maas:lp1816651 updated
951c852... by Lee Trager

mpontillo suggestion

326d995... by Lee Trager

Use mpontillos patch

Revision history for this message
Mike Pontillo (mpontillo) wrote :

Looks good; thanks for the fix!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/models/bmc.py b/src/maasserver/models/bmc.py
2index 1e3b865..711e756 100644
3--- a/src/maasserver/models/bmc.py
4+++ b/src/maasserver/models/bmc.py
5@@ -1,4 +1,4 @@
6-# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
7+# Copyright 2015-2019 Canonical Ltd. This software is licensed under the
8 # GNU Affero General Public License version 3 (see the file LICENSE).
9
10 """BMC objects."""
11@@ -337,12 +337,14 @@ class BMC(CleanSave, TimestampedModel):
12 with transaction.atomic():
13 subnet = Subnet.objects.get_best_subnet_for_ip(new_ip)
14 (self.ip_address,
15- _) = StaticIPAddress.objects.get_or_create(
16- ip=new_ip,
17- defaults={
18- 'alloc_type': IPADDRESS_TYPE.STICKY,
19- 'subnet': subnet,
20- })
21+ _) = StaticIPAddress.objects.exclude(
22+ alloc_type=IPADDRESS_TYPE.DISCOVERED
23+ ).get_or_create(
24+ ip=new_ip,
25+ defaults={
26+ 'alloc_type': IPADDRESS_TYPE.STICKY,
27+ 'subnet': subnet,
28+ })
29 except Exception as error:
30 maaslog.info(
31 "BMC could not save extracted IP "
32diff --git a/src/maasserver/models/tests/test_bmc.py b/src/maasserver/models/tests/test_bmc.py
33index e806ff0..41ff534 100644
34--- a/src/maasserver/models/tests/test_bmc.py
35+++ b/src/maasserver/models/tests/test_bmc.py
36@@ -1,4 +1,4 @@
37-# Copyright 2016-2018 Canonical Ltd. This software is licensed under the
38+# Copyright 2016-2019 Canonical Ltd. This software is licensed under the
39 # GNU Affero General Public License version 3 (see the file LICENSE).
40
41 """Test maasserver models."""
42@@ -146,6 +146,25 @@ class TestBMC(MAASServerTestCase):
43 self.assertNotEqual(machine_ip_addr.id, bmc.ip_address.id)
44 return machine, bmc, machine_ip
45
46+ def test_make_machine_and_bmc_discovered_ip(self):
47+ # Regression test for LP:1816651
48+ subnet = factory.make_Subnet()
49+ discovered_ip = factory.make_StaticIPAddress(
50+ subnet=subnet, alloc_type=IPADDRESS_TYPE.DISCOVERED)
51+ sticky_ip = factory.make_StaticIPAddress(
52+ ip=discovered_ip.ip, subnet=subnet,
53+ alloc_type=IPADDRESS_TYPE.STICKY)
54+ bmc = factory.make_BMC(
55+ power_type='virsh',
56+ power_parameters={
57+ 'power_address':
58+ "protocol://%s:8080/path/to/thing#tag" % (
59+ factory.ip_to_url_format(discovered_ip.ip))})
60+ self.assertItemsEqual(
61+ [discovered_ip, sticky_ip],
62+ StaticIPAddress.objects.filter(ip=discovered_ip.ip))
63+ self.assertEqual(sticky_ip, bmc.ip_address)
64+
65 def test_bmc_save_extracts_ip_address(self):
66 subnet = factory.make_Subnet()
67 sticky_ip = factory.make_StaticIPAddress(

Subscribers

People subscribed via source and target branches