Skip to content

Commit d33a714

Browse files
committed
adding delete and update channel changes
1 parent f4ab4af commit d33a714

5 files changed

Lines changed: 110 additions & 71 deletions

File tree

‎linode_api4/groups/monitor.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ def channel_create(
378378
379379
:returns: The newly created :class:`AlertChannel`.
380380
:rtype: AlertChannel
381+
382+
.. note::
383+
For updating an alert channel, use the ``save()`` method on the :class:`AlertChannel` object.
384+
For deleting an alert channel, use the ``delete()`` method directly on the :class:`AlertChannel` object.
381385
"""
382386
params = {
383387
"label": label,

‎linode_api4/objects/monitor.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,10 @@ class AlertChannel(Base):
504504

505505
properties = {
506506
"id": Property(identifier=True),
507-
"label": Property(),
507+
"label": Property(mutable=True),
508508
"type": Property(),
509509
"channel_type": Property(),
510-
"details": Property(mutable=False, json_object=ChannelDetails),
510+
"details": Property(mutable=True, json_object=ChannelDetails),
511511
"alerts": Property(mutable=False, json_object=AlertInfo),
512512
"created": Property(is_datetime=True),
513513
"updated": Property(is_datetime=True),

‎test/integration/models/monitor/test_monitor.py‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
MonitorService,
1919
MonitorServiceToken,
2020
)
21-
from linode_api4.objects.monitor import AlertStatus, ChannelDetails, EmailDetails
21+
from linode_api4.objects.monitor import (
22+
AlertStatus,
23+
ChannelDetails,
24+
EmailDetails,
25+
)
2226

2327

2428
# List all dashboards
@@ -314,12 +318,12 @@ def test_alert_definition_entities(test_linode_client):
314318
assert entity._type == service_type
315319

316320

317-
def test_integration_create_get_delete_alert_channel(test_linode_client):
318-
"""E2E: create an alert channel, fetch it, then delete it.
321+
def test_integration_create_get_update_delete_alert_channel(test_linode_client):
322+
"""E2E: create an alert channel, fetch it, update it, then delete it.
319323
320324
This test creates an alert channel with email details, retrieves it,
321-
and then deletes it. It ensures the feature is working end-to-end
322-
against the actual API.
325+
updates it, and then deletes it. It ensures the full CRUD feature is
326+
working end-to-end against the actual API.
323327
"""
324328
client = test_linode_client
325329
label = get_test_label() + "-e2e-channel"
@@ -362,11 +366,25 @@ def test_integration_create_get_delete_alert_channel(test_linode_client):
362366
assert found_channel.label == label
363367
assert found_channel.channel_type == "email"
364368

369+
# Update the channel label
370+
updated_label = f"{label}-updated"
371+
created_channel.label = updated_label
372+
result = created_channel.save()
373+
assert result is True, "Failed to update channel"
374+
375+
# Fetch the updated channel to verify the change
376+
reloaded_channel = client.load(AlertChannel, created_channel.id)
377+
assert (
378+
reloaded_channel.label == updated_label
379+
), "Channel label was not updated"
380+
365381
finally:
366382
if created_channel:
367383
# Clean up: delete the created channel
368384
try:
369385
created_channel.delete()
370386
except Exception as e:
371387
# Log but don't fail if cleanup fails
372-
print(f"Warning: Failed to delete channel {created_channel.id}: {e}")
388+
print(
389+
f"Warning: Failed to delete channel {created_channel.id}: {e}"
390+
)

‎test/unit/groups/monitor_api_test.py‎

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,54 +183,71 @@ def test_alert_definition_entities(self):
183183
assert entities[2].url == "/v4/databases/mysql/instances/3"
184184
assert entities[2]._type == "dbaas"
185185

186-
def test_create_channel(self):
187-
url = "/monitor/alert-channels"
188-
result = {
189-
"id": 123,
190-
"label": "email channel for api change",
186+
def test_create_update_delete_alert_channel(self):
187+
"""
188+
E2E test for alert channel CRUD: create, update, and delete.
189+
Verifies the full lifecycle of an alert channel.
190+
"""
191+
create_url = "/monitor/alert-channels"
192+
channel_id = 789
193+
channel_url = f"{create_url}/{channel_id}"
194+
195+
# Create channel
196+
create_response = {
197+
"id": channel_id,
198+
"label": "Test Channel",
191199
"type": "user",
192200
"channel_type": "email",
193201
"details": {
194202
"email": {
195-
"usernames": ["mawasthy_tenant02_admin"],
203+
"usernames": ["test_user"],
196204
"recipient_type": "user",
197205
}
198206
},
199207
"alerts": {
200-
"url": "/monitor/alert-channels/123/alerts",
208+
"url": f"{channel_url}/alerts",
201209
"type": "alerts-definitions",
202210
"alert_count": 0,
203211
},
204212
"created": "2024-01-01T00:00:00",
205213
"updated": "2024-01-01T00:00:00",
206-
"created_by": "mawasthy_tenant02_admin",
207-
"updated_by": "mawasthy_tenant02_admin",
214+
"created_by": "test_user",
215+
"updated_by": "test_user",
208216
}
209217

210-
with self.mock_post(result) as mock_post:
218+
with self.mock_post(create_response) as mock_post:
211219
channel = self.client.monitor.channel_create(
212-
label="email channel for api change",
220+
label="Test Channel",
213221
channel_type="email",
214222
details=ChannelDetails(
215223
email=EmailDetails(
216224
recipient_type="user",
217-
usernames=["mawasthy_tenant02_admin"],
225+
usernames=["test_user"],
218226
)
219227
),
220228
)
221229

222-
assert mock_post.call_url == url
223-
# payload should include the provided fields
224-
assert mock_post.call_data["label"] == "email channel for api change"
225-
assert mock_post.call_data["channel_type"] == "email"
226-
assert "details" in mock_post.call_data
227-
230+
assert mock_post.call_url == create_url
228231
assert isinstance(channel, AlertChannel)
229-
assert channel.id == 123
230-
assert channel.label == "email channel for api change"
231-
assert channel.channel_type == "email"
232+
assert channel.id == channel_id
233+
assert channel.label == "Test Channel"
232234

233-
# fetch the same response from the client and assert
234-
resp = self.client.post(url, data={})
235-
assert resp["label"] == "email channel for api change"
236-
assert resp["channel_type"] == "email"
235+
# Update channel
236+
updated_response = create_response.copy()
237+
updated_response["label"] = "Test Channel Updated"
238+
updated_response["updated"] = "2024-01-02T00:00:00"
239+
240+
with self.mock_put(updated_response) as mock_put:
241+
channel.label = "Test Channel Updated"
242+
result = channel.save()
243+
244+
assert mock_put.call_url == channel_url
245+
assert result is True
246+
assert channel.label == "Test Channel Updated"
247+
248+
# Delete channel
249+
with self.mock_delete() as mock_delete:
250+
result = channel.delete()
251+
252+
assert mock_delete.call_url == channel_url
253+
assert result is True

‎test/unit/objects/monitor_test.py‎

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from test.unit.base import ClientBaseCase
33

44
from linode_api4.objects import AlertChannel, MonitorDashboard, MonitorService
5-
from linode_api4.objects.monitor import ChannelDetails, EmailDetails
65

76

87
class MonitorTest(ClientBaseCase):
@@ -171,58 +170,59 @@ def test_alert_channels(self):
171170
)
172171
self.assertEqual(channels[0].alerts.alert_count, 0)
173172

174-
def test_create_channel(self):
175-
173+
def test_create_update_delete_channel(self):
174+
"""
175+
Test CRUD operations for AlertChannel: create, update, and delete.
176+
Verifies the full lifecycle of an alert channel object.
177+
"""
178+
channel_id = 999
179+
url = f"/monitor/alert-channels/{channel_id}"
180+
181+
# Create the channel
176182
create_response = {
177-
"id": 456,
178-
"label": "Email channel for api change",
183+
"id": channel_id,
184+
"label": "CRUD Test Channel",
179185
"type": "user",
180186
"channel_type": "email",
181187
"details": {
182188
"email": {
189+
"usernames": ["crud_user"],
183190
"recipient_type": "user",
184-
"usernames": ["mawasthy_tenant02_admin"],
185191
}
186192
},
187193
"alerts": {
188-
"url": "/monitor/alert-channels/456/alerts",
194+
"url": f"{url}/alerts",
189195
"type": "alerts-definitions",
190196
"alert_count": 0,
191197
},
192198
"created": "2024-01-01T00:00:00",
193199
"updated": "2024-01-01T00:00:00",
194-
"created_by": "mawasthy_tenant02_admin",
195-
"updated_by": "mawasthy_tenant02_admin",
200+
"created_by": "crud_user",
201+
"updated_by": "crud_user",
196202
}
197203

198-
with self.mock_post(create_response) as m:
199-
result = self.client.monitor.channel_create(
200-
label="Email channel for api change",
201-
channel_type="email",
202-
details=ChannelDetails(
203-
email=EmailDetails(
204-
recipient_type="user",
205-
usernames=["mawasthy_tenant02_admin"],
206-
)
207-
),
208-
)
204+
with self.mock_get(create_response) as m_get:
205+
channel = self.client.load(AlertChannel, channel_id)
206+
self.assertIsInstance(channel, AlertChannel)
207+
self.assertEqual(channel.id, channel_id)
208+
self.assertEqual(channel.label, "CRUD Test Channel")
209209

210-
self.assertEqual(m.call_url, "/monitor/alert-channels")
211-
self.assertEqual(m.call_data["label"], "Email channel for api change")
212-
self.assertEqual(m.call_data["channel_type"], "email")
213-
self.assertEqual(
214-
m.call_data["details"]["email"]["recipient_type"], "user"
215-
)
216-
self.assertEqual(
217-
m.call_data["details"]["email"]["usernames"], ["mawasthy_tenant02_admin"]
218-
)
210+
# Update the channel
211+
updated_response = create_response.copy()
212+
updated_response["label"] = "CRUD Test Channel Updated"
213+
updated_response["updated"] = "2024-01-02T00:00:00"
214+
215+
with self.mock_put(updated_response) as m_put:
216+
channel.label = "CRUD Test Channel Updated"
217+
result = channel.save()
218+
219+
self.assertEqual(m_put.call_url, url)
220+
self.assertTrue(result)
221+
self.assertEqual(channel.label, "CRUD Test Channel Updated")
222+
223+
# Delete the channel
224+
with self.mock_delete() as m_delete:
225+
result = channel.delete()
219226

220-
self.assertIsInstance(result, AlertChannel)
221-
self.assertEqual(result.id, 456)
222-
self.assertEqual(result.label, "Email channel for api change")
223-
self.assertEqual(result.type, "user")
224-
self.assertEqual(result.channel_type, "email")
225-
self.assertIsNotNone(result.details)
226-
self.assertIsNotNone(result.details.email)
227-
self.assertEqual(result.details.email.recipient_type, "user")
228-
self.assertEqual(result.details.email.usernames, ["mawasthy_tenant02_admin"])
227+
self.assertEqual(m_delete.call_url, url)
228+
self.assertTrue(result)

0 commit comments

Comments
 (0)