Merge ~mpontillo/maas:subnet-ip-listing-trigger--bug-1761281 into maas:master

Proposed by Mike Pontillo on 2018-04-25
Status: Merged
Approved by: Andres Rodriguez on 2018-04-27
Approved revision: 29375308ee460df55f757d78392ceafcf6b907a3
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~mpontillo/maas:subnet-ip-listing-trigger--bug-1761281
Merge into: maas:master
Diff against target: 150 lines (+107/-3)
3 files modified
src/maasserver/triggers/tests/test_init.py (+2/-0)
src/maasserver/triggers/tests/test_websocket_listener.py (+90/-0)
src/maasserver/triggers/websocket.py (+15/-3)
Reviewer Review Type Date Requested Status
Lee Trager 2018-04-25 Approve on 2018-04-25
MAAS Lander Approve on 2018-04-25
Review via email: mp+344212@code.launchpad.net

Commit message

LP: #1761281 - Add triggers to update subnet websocket for IP address insert and delete.

To post a comment you must log in.
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b subnet-ip-listing-trigger--bug-1761281 lp:~mpontillo/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci-jenkins.internal:8080/job/maas/job/branch-tester/2608/console
COMMIT: 11a634aa02a589047c15ec00a2499a9765b87a57

review: Needs Fixing
2937530... by Mike Pontillo on 2018-04-25

Fix lint.

MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b subnet-ip-listing-trigger--bug-1761281 lp:~mpontillo/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 29375308ee460df55f757d78392ceafcf6b907a3

review: Approve
Lee Trager (ltrager) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/triggers/tests/test_init.py b/src/maasserver/triggers/tests/test_init.py
2index 1079ef6..66b1c9a 100644
3--- a/src/maasserver/triggers/tests/test_init.py
4+++ b/src/maasserver/triggers/tests/test_init.py
5@@ -238,6 +238,8 @@ class TestTriggersUsed(MAASServerTestCase):
6 "staticipaddress_ipaddress_domain_insert_notify",
7 "staticipaddress_ipaddress_domain_update_notify",
8 "staticipaddress_ipaddress_machine_update_notify",
9+ "staticipaddress_ipaddress_subnet_delete_notify",
10+ "staticipaddress_ipaddress_subnet_insert_notify",
11 "staticipaddress_ipaddress_subnet_update_notify",
12 "staticroute_staticroute_create_notify",
13 "staticroute_staticroute_delete_notify",
14diff --git a/src/maasserver/triggers/tests/test_websocket_listener.py b/src/maasserver/triggers/tests/test_websocket_listener.py
15index 5e69c2d..f7961a2 100644
16--- a/src/maasserver/triggers/tests/test_websocket_listener.py
17+++ b/src/maasserver/triggers/tests/test_websocket_listener.py
18@@ -2220,6 +2220,96 @@ class TestSubnetListener(
19 finally:
20 yield listener.stopService()
21
22+ @wait_for_reactor
23+ @inlineCallbacks
24+ def test__calls_handler_with_update_on_ip_address_insert(self):
25+ yield deferToDatabase(register_websocket_triggers)
26+ node = yield deferToDatabase(
27+ self.create_node,
28+ {'node_type': NODE_TYPE.MACHINE, 'interface': True})
29+ interface = yield deferToDatabase(
30+ self.get_node_boot_interface, node.system_id)
31+ subnet = yield deferToDatabase(self.create_subnet)
32+
33+ listener = PostgresListenerService()
34+ dv = DeferredValue()
35+ listener.register('subnet', lambda *args: dv.set(args))
36+ yield listener.startService()
37+ try:
38+ yield deferToDatabase(
39+ self.create_staticipaddress, {
40+ "alloc_type": IPADDRESS_TYPE.AUTO,
41+ "interface": interface,
42+ "subnet": subnet,
43+ "ip": "",
44+ })
45+ yield dv.get(timeout=2)
46+ self.assertEqual(('update', '%s' % subnet.id), dv.value)
47+ finally:
48+ yield listener.stopService()
49+
50+ @wait_for_reactor
51+ @inlineCallbacks
52+ def test__calls_handler_with_update_on_ip_address_update(self):
53+ yield deferToDatabase(register_websocket_triggers)
54+ node = yield deferToDatabase(
55+ self.create_node,
56+ {'node_type': NODE_TYPE.MACHINE, 'interface': True})
57+ interface = yield deferToDatabase(
58+ self.get_node_boot_interface, node.system_id)
59+ subnet = yield deferToDatabase(self.create_subnet)
60+ selected_ip = factory.pick_ip_in_network(subnet.get_ipnetwork())
61+ ipaddress = yield deferToDatabase(
62+ self.create_staticipaddress, {
63+ "alloc_type": IPADDRESS_TYPE.AUTO,
64+ "interface": interface,
65+ "subnet": subnet,
66+ "ip": "",
67+ })
68+
69+ listener = PostgresListenerService()
70+ dv = DeferredValue()
71+ listener.register('subnet', lambda *args: dv.set(args))
72+ yield listener.startService()
73+ try:
74+ yield deferToDatabase(
75+ self.update_staticipaddress, ipaddress.id,
76+ {"ip": selected_ip})
77+ yield dv.get(timeout=2)
78+ self.assertEqual(('update', '%s' % subnet.id), dv.value)
79+ finally:
80+ yield listener.stopService()
81+
82+ @wait_for_reactor
83+ @inlineCallbacks
84+ def test__calls_handler_with_update_on_ip_address_delete(self):
85+ yield deferToDatabase(register_websocket_triggers)
86+ node = yield deferToDatabase(
87+ self.create_node,
88+ {'node_type': NODE_TYPE.MACHINE, 'interface': True})
89+ interface = yield deferToDatabase(
90+ self.get_node_boot_interface, node.system_id)
91+ subnet = yield deferToDatabase(self.create_subnet)
92+ ipaddress = yield deferToDatabase(
93+ self.create_staticipaddress, {
94+ "alloc_type": IPADDRESS_TYPE.AUTO,
95+ "interface": interface,
96+ "subnet": subnet,
97+ "ip": "",
98+ })
99+
100+ listener = PostgresListenerService()
101+ dv = DeferredValue()
102+ listener.register('subnet', lambda *args: dv.set(args))
103+ yield listener.startService()
104+ try:
105+ yield deferToDatabase(
106+ self.delete_staticipaddress, ipaddress.id)
107+ yield dv.get(timeout=2)
108+ self.assertEqual(('update', '%s' % subnet.id), dv.value)
109+ finally:
110+ yield listener.stopService()
111+
112
113 class TestSpaceListener(
114 MAASTransactionServerTestCase, TransactionalHelpersMixin):
115diff --git a/src/maasserver/triggers/websocket.py b/src/maasserver/triggers/websocket.py
116index ec87ae9..e489202 100644
117--- a/src/maasserver/triggers/websocket.py
118+++ b/src/maasserver/triggers/websocket.py
119@@ -709,6 +709,14 @@ STATIC_IP_ADDRESS_NODE_NOTIFY = dedent("""\
120 STATIC_IP_ADDRESS_SUBNET_NOTIFY = dedent("""\
121 CREATE OR REPLACE FUNCTION %s() RETURNS trigger AS $$
122 BEGIN
123+ IF TG_OP = 'INSERT' THEN
124+ PERFORM pg_notify('subnet_update',CAST(NEW.subnet_id AS text));
125+ RETURN NEW;
126+ END IF;
127+ IF TG_OP = 'DELETE' THEN
128+ PERFORM pg_notify('subnet_update',CAST(OLD.subnet_id AS text));
129+ RETURN OLD;
130+ END IF;
131 IF OLD.subnet_id != NEW.subnet_id THEN
132 IF OLD.subnet_id IS NOT NULL THEN
133 PERFORM pg_notify('subnet_update',CAST(OLD.subnet_id AS text));
134@@ -1416,9 +1424,13 @@ def register_websocket_triggers():
135 # IP address subnet notifications
136 register_procedure(
137 STATIC_IP_ADDRESS_SUBNET_NOTIFY % 'ipaddress_subnet_update_notify')
138- register_trigger(
139- "maasserver_staticipaddress",
140- "ipaddress_subnet_update_notify", "update")
141+ register_procedure(
142+ STATIC_IP_ADDRESS_SUBNET_NOTIFY % 'ipaddress_subnet_insert_notify')
143+ register_procedure(
144+ STATIC_IP_ADDRESS_SUBNET_NOTIFY % 'ipaddress_subnet_delete_notify')
145+ register_triggers(
146+ "maasserver_staticipaddress", "ipaddress_subnet",
147+ events=EVENTS_IUD)
148
149 # IP address domain notifications
150 register_procedure(

Subscribers

People subscribed via source and target branches

to all changes: