Merge ~ltrager/maas:lp1816651_2.5 into maas:2.5

Proposed by Lee Trager
Status: Merged
Approved by: Lee Trager
Approved revision: 54e66d6b13c645d9ebbaaad296467ab4e5225ef4
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ltrager/maas:lp1816651_2.5
Merge into: maas:2.5
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
Lee Trager (community) Approve
Review via email: mp+363524@code.launchpad.net

Commit message

Backport of 1a502e4 LP: #1816651 - Handle duplicate IP addresses when creating a BMC object.

To post a comment you must log in.
Revision history for this message
Lee Trager (ltrager) wrote :
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