Merge ~ack/maas:deprecation-notifications into maas:master

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: a34ff7e0828bf7800e14f0e199975e758e6320b8
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ack/maas:deprecation-notifications
Merge into: maas:master
Diff against target: 165 lines (+102/-1)
4 files modified
src/maasserver/deprecations.py (+34/-0)
src/maasserver/start_up.py (+4/-0)
src/maasserver/tests/test_deprecations.py (+45/-1)
src/maasserver/tests/test_start_up.py (+19/-0)
Reviewer Review Type Date Requested Status
Dougal Matthews (community) Approve
MAAS Lander Approve
Review via email: mp+382901@code.launchpad.net

Commit message

LP #1874439 - create notifications for active deprecation notices

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b deprecation-notifications lp:~ack/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: a34ff7e0828bf7800e14f0e199975e758e6320b8

review: Approve
Revision history for this message
Dougal Matthews (d0ugal) :
Revision history for this message
Alberto Donato (ack) :
Revision history for this message
Dougal Matthews (d0ugal) wrote :

Thanks for the clarifications!

review: Approve

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/deprecations.py b/src/maasserver/deprecations.py
2index dbeefae..e87fad3 100644
3--- a/src/maasserver/deprecations.py
4+++ b/src/maasserver/deprecations.py
5@@ -46,3 +46,37 @@ def log_deprecations(logger=None):
6
7 for d in get_deprecations():
8 logger.msg("Deprecation {id} ({url}): {description}".format(**d))
9+
10+
11+def sync_deprecation_notifications():
12+ from maasserver.models import Notification
13+
14+ notifications = set(
15+ Notification.objects.filter(
16+ ident__startswith="deprecation_"
17+ ).values_list("ident", flat=True)
18+ )
19+ for deprecation in get_deprecations():
20+ dep_id = deprecation["id"]
21+ for kind in ("users", "admins"):
22+ dep_ident = f"deprecation_{dep_id}_{kind}"
23+ if dep_ident in notifications:
24+ notifications.remove(dep_ident)
25+ continue
26+ message = f"{deprecation['description']}"
27+ if kind == "users":
28+ message += "<br>Please contact your MAAS administrator."
29+ message += (
30+ f"<br><a class='p-link--external' href='{deprecation['url']}'>"
31+ f"{deprecation['link-text']}...</a>"
32+ )
33+ Notification(
34+ ident=dep_ident,
35+ category="warning",
36+ message=message,
37+ **{kind: True},
38+ ).save()
39+
40+ # delete other deprecation notifications
41+ if notifications:
42+ Notification.objects.filter(ident__in=notifications).delete()
43diff --git a/src/maasserver/start_up.py b/src/maasserver/start_up.py
44index c104ef3..52235f8 100644
45--- a/src/maasserver/start_up.py
46+++ b/src/maasserver/start_up.py
47@@ -13,6 +13,7 @@ from twisted.internet import reactor
48 from twisted.internet.defer import inlineCallbacks
49
50 from maasserver import locks, security
51+from maasserver.deprecations import sync_deprecation_notifications
52 from maasserver.fields import register_mac_type
53 from maasserver.models.config import Config
54 from maasserver.models.domain import dns_kms_setting_changed
55@@ -140,6 +141,9 @@ def inner_start_up(master=False):
56 ident="commissioning_release_deprecated",
57 )
58
59+ # Update deprecation notifications if needed
60+ sync_deprecation_notifications()
61+
62 # Refresh soon after this transaction is in.
63 post_commit_do(reactor.callLater, 0, refreshRegion, region)
64
65diff --git a/src/maasserver/tests/test_deprecations.py b/src/maasserver/tests/test_deprecations.py
66index 7725162..2fdc6df 100644
67--- a/src/maasserver/tests/test_deprecations.py
68+++ b/src/maasserver/tests/test_deprecations.py
69@@ -2,7 +2,13 @@ from pathlib import Path
70
71 from fixtures import EnvironmentVariable
72
73-from maasserver.deprecations import get_deprecations, log_deprecations
74+from maasserver.deprecations import (
75+ get_deprecations,
76+ log_deprecations,
77+ sync_deprecation_notifications,
78+)
79+from maasserver.models import Notification
80+from maasserver.testing.testcase import MAASServerTestCase
81 from maastesting.testcase import MAASTestCase
82 from provisioningserver.logger import LegacyLogger
83
84@@ -54,3 +60,41 @@ class TestLogDeprecations(MAASTestCase):
85 "The setup for this MAAS is deprecated and not suitable for production "
86 "environments, as the database is running inside the snap.",
87 )
88+
89+
90+class TestSyncDeprecationNotifications(MAASServerTestCase):
91+ def test_create_notifications(self):
92+ self.useFixture(EnvironmentVariable("SNAP", "/snap/maas/current"))
93+ snap_common_path = Path(self.make_dir())
94+ self.useFixture(
95+ EnvironmentVariable("SNAP_COMMON", str(snap_common_path))
96+ )
97+ snap_common_path.joinpath("snap_mode").write_text("all", "utf-8")
98+
99+ sync_deprecation_notifications()
100+ notification1, notification2 = Notification.objects.order_by("ident")
101+ self.assertEqual(notification1.ident, "deprecation_MD1_admins")
102+ self.assertEqual(notification1.category, "warning")
103+ self.assertTrue(notification1.admins)
104+ self.assertFalse(notification1.users)
105+ self.assertIn(
106+ "https://maas.io/deprecations/MD1", notification1.message
107+ )
108+ self.assertNotIn(
109+ "Please contact your MAAS administrator.", notification1.message
110+ )
111+ self.assertEqual(notification2.ident, "deprecation_MD1_users")
112+ self.assertEqual(notification2.category, "warning")
113+ self.assertFalse(notification2.admins)
114+ self.assertTrue(notification2.users)
115+ self.assertIn(
116+ "Please contact your MAAS administrator.", notification2.message
117+ )
118+
119+ def test_remove_deprecations(self):
120+ Notification(
121+ ident="deprecation_MD1_admins", message="some text"
122+ ).save()
123+ sync_deprecation_notifications()
124+ # the notification is removed since there is no active deprecation
125+ self.assertFalse(Notification.objects.exists())
126diff --git a/src/maasserver/tests/test_start_up.py b/src/maasserver/tests/test_start_up.py
127index 71ceec7..4f10d31 100644
128--- a/src/maasserver/tests/test_start_up.py
129+++ b/src/maasserver/tests/test_start_up.py
130@@ -5,9 +5,11 @@
131
132 __all__ = []
133
134+from pathlib import Path
135 import random
136 from unittest.mock import call
137
138+from fixtures import EnvironmentVariable
139 from testtools.matchers import HasLength, Is, IsInstance, Not
140 from twisted.internet import reactor
141 from twisted.internet.defer import Deferred
142@@ -196,6 +198,23 @@ class TestInnerStartUp(MAASServerTestCase):
143 uuid = Config.objects.get_config("uuid")
144 self.assertThat(uuid, Not(Is(None)))
145
146+ def test__syncs_deprecation_notifications(self):
147+ self.useFixture(EnvironmentVariable("SNAP", "/snap/maas/current"))
148+ snap_common_path = Path(self.make_dir())
149+ self.useFixture(
150+ EnvironmentVariable("SNAP_COMMON", str(snap_common_path))
151+ )
152+ snap_common_path.joinpath("snap_mode").write_text("all", "utf-8")
153+
154+ with post_commit_hooks:
155+ start_up.inner_start_up(master=True)
156+ self.assertEqual(
157+ Notification.objects.filter(
158+ ident__startswith="deprecation_"
159+ ).count(),
160+ 2,
161+ )
162+
163
164 class TestFunctions(MAASServerTestCase):
165 """Tests for other functions in the `start_up` module."""

Subscribers

People subscribed via source and target branches