Skip to content

Commit 7faf887

Browse files
srbhaakamaiCopilotyec-akamaizliang-akamai
authored
Python SDK for ACLP Alerts (#589)
* DI-26927 Python SDK code for GET Alert Definitions * DI-26927 Python SDK code modified for client code * DI-26927 Added Unit test cases and missing classes * DI-26927 made corrections to keep code consistent * DI-26927 Updated Unit and Integratoion Tests * [dev 51950af] DI-26927 Updated Unit and Integratoion Tests * Revert "DI-26927 Updated Unit and Integratoion Tests" This reverts commit 51950af. * Revert "[dev 51950af] DI-26927 Updated Unit and Integratoion Tests" This reverts commit 6ca6a5a. * DI-26927 Updated Unit and Integratoion Tests * Remove .venv from repo and add to .gitignore * DI-26927 reverted git ignore * DI-26927 reverted conftest,py * DI-26927 added accidentlly deleted file * DI-26927 Corrected Integration and Unit Test cases for Alerting APIs * DI-26927 Reverted conftest.py and check integration without those changes * DI-26927 fixed integration test for firewall and added time for alert update before deletion * DI-26927 fixed changing monitor.py as per review comments * DI-26927 fixed changing monitor.py as per review comments * DI-26927 fixed changing monitor.py as per review comments * DI-26927 Intermediate change to address internal review comments * DI-26927 CLosed review comments * DI-26927 CLosed review comments * DI-27156 closed review comments from Ketan * DI-27156 closed review comments from Ketan * DI-27156 fixed unit test cases post review comments fixes * DI-27156 Updated docstring and make unit test cases changes * DI-27156 Added unit test cases post review comments fixes * DI-27156 Added unit test cases post review comments fixes * tests(monitor): add MonitorAlertDefinitionsTest and update fixtures for alert-definitions * DI-27156 Added unit test cases post review comments fixes * DI-27156 Updated docstring * DI-27156 Updated unit test errors * removed unwanted files * added missing doxcstring * added missing doxcstring * added missing doxcstring * rmoved test files * closed final review comments * fixing integration test issues * Corrected Integration Test Case * DI-26927 Corrected json to object modifications issues * DI-26927 Corrected json to object modifications issues * DI-26927 Unit test corrections for mofied functions name * Delete test/fixtures/monitor/services/dbaas/alert-definitions.json Not Required * Delete test.py Not Required here required for local testing only * Delete test/fixtures/monitor/alert-definitions.json Not required here * DI-26927 reverted fixtures.py as its not necesary to change * DI-26927 reverted fixtures.py as its not necesary to change * DI-26927 reverted fixtures.py as its not necesary to change * DI-26927 fixed linting error * DI-26927 fixed linting error * DI-26927 fixed lint error caught in CI * Update test/unit/groups/monitor_api_test.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/integration/models/monitor/test_monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/integration/models/monitor/test_monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/integration/models/monitor/test_monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update linode_api4/objects/monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * DI-26927 fixed copilot comments * fixed review comments from APIv4 team * fixed review comments from APIv4 team * fixed review comments from APIv4 team * fixed review comments from APIv4 team * DI-26927 fixed review comments from API v4 team * fixed review comments from APIv4 team * fixed lint errors * Update linode_api4/groups/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * Update linode_api4/groups/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * fixed review errors * fixed unittest * fixed unittest * fixed unittest * fixed unittest * fixed unittest * fixed unittest * added update use case to integration test * Update linode_api4/objects/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * Update linode_api4/objects/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * Update linode_api4/objects/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * Update linode_api4/objects/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * fixed further review comments * Updated integration test * Updated integration test with more assert statements * Fix Linode interfaces property (#604) * Fix test for interfaces (#605) * Fix test * changed as per github-advanced-security comments * fixed formatting errors using python black * Migrate test fixtures discovery to be with pathlib (#599) * Migrate test fixtures discovery to be with pathlib * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update linode_api4/objects/monitor.py Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> * Fixed Copilot comments on unit and integration tests * corrected unit test assert statement * Added comment as per copilot suggestion for save() and delete() * resolved comments from copilot commented whereever not applicable * Test: verify automatic GPG signing is working * Incorporated copilot comments * Removed serialisabel file from commit * Fix Lint errors * fixed review comments fo mutable * fixed review comments to keep it in line with SDK guidelines * Addressed review comments on AlertDefinition class * Comprehensive monitor API improvements and code quality fixes - Removed unused imports and optimized import order - Updated test cases for monitor integration - Enhanced monitor objects with proper type annotations - Improved monitor groups with better error handling - Applied code review feedback and best practices * Applied review comments across * Comprehensive monitor API fixes and improvements - Fixed json_object parameter issues in AlertDefinition properties - Corrected list type annotations for AlertChannelEnvelope - Updated integration tests with proper status handling for alert definitions - Applied review comments for better code quality - Enhanced type annotations and import organization - Improved error handling in monitor group methods * change entity id type as per review comments * reverted copilot comments as per the suggestion unit and integration test passed after the changes * removed unused imports * removed unused imports * Refactored integration test * changed _class to alert_class as per review suggestions * changed alert_class to service_class as per review suggestions * Update linode_api4/objects/monitor.py * Update test/integration/models/monitor/test_monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/integration/models/monitor/test_monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update linode_api4/groups/monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/integration/models/monitor/test_monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update linode_api4/groups/monitor.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/fixtures/monitor_alert-definitions.json * Apply suggestion from @zliang-akamai * Apply suggestion from @zliang-akamai --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com> Co-authored-by: Zhiwei Liang <121905282+zliang-akamai@users.noreply.github.com> Co-authored-by: Zhiwei Liang <zliang@akamai.com>
1 parent dc3164c commit 7faf887

File tree

8 files changed

+689
-14
lines changed

8 files changed

+689
-14
lines changed

linode_api4/groups/monitor.py

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
__all__ = [
2-
"MonitorGroup",
3-
]
41
from typing import Any, Optional
52

63
from linode_api4 import PaginatedList
74
from linode_api4.errors import UnexpectedResponseError
85
from linode_api4.groups import Group
96
from linode_api4.objects import (
7+
AlertChannel,
8+
AlertDefinition,
109
MonitorDashboard,
1110
MonitorMetricsDefinition,
1211
MonitorService,
1312
MonitorServiceToken,
1413
)
1514

15+
__all__ = [
16+
"MonitorGroup",
17+
]
18+
1619

1720
class MonitorGroup(Group):
1821
"""
@@ -145,3 +148,139 @@ def create_token(
145148
"Unexpected response when creating token!", json=result
146149
)
147150
return MonitorServiceToken(token=result["token"])
151+
152+
def alert_definitions(
153+
self,
154+
*filters,
155+
service_type: Optional[str] = None,
156+
) -> PaginatedList:
157+
"""
158+
Retrieve alert definitions.
159+
160+
Returns a paginated collection of :class:`AlertDefinition` objects. If you
161+
need to obtain a single :class:`AlertDefinition`, use :meth:`LinodeClient.load`
162+
and supply the `service_type` as the parent identifier, for example:
163+
164+
alerts = client.monitor.alert_definitions()
165+
alerts_by_service = client.monitor.alert_definitions(service_type="dbaas")
166+
.. note:: This endpoint is in beta and requires using the v4beta base URL.
167+
168+
API Documentation:
169+
https://techdocs.akamai.com/linode-api/reference/get-alert-definitions
170+
https://techdocs.akamai.com/linode-api/reference/get-alert-definitions-for-service-type
171+
172+
:param service_type: Optional service type to scope the query (e.g. ``"dbaas"``).
173+
:type service_type: Optional[str]
174+
:param filters: Optional filtering expressions to apply to the returned
175+
collection. See :doc:`Filtering Collections</linode_api4/objects/filtering>`.
176+
177+
:returns: A paginated list of :class:`AlertDefinition` objects.
178+
:rtype: PaginatedList[AlertDefinition]
179+
"""
180+
181+
endpoint = "/monitor/alert-definitions"
182+
if service_type:
183+
endpoint = f"/monitor/services/{service_type}/alert-definitions"
184+
185+
# Requesting a list
186+
return self.client._get_and_filter(
187+
AlertDefinition, *filters, endpoint=endpoint
188+
)
189+
190+
def alert_channels(self, *filters) -> PaginatedList:
191+
"""
192+
List alert channels for the authenticated account.
193+
194+
Returns a paginated collection of :class:`AlertChannel` objects which
195+
describe destinations for alert notifications (for example: email
196+
lists, webhooks, PagerDuty, Slack, etc.). By default this method
197+
returns all channels visible to the authenticated account; you can
198+
supply optional filter expressions to restrict the results.
199+
200+
Examples:
201+
channels = client.monitor.alert_channels()
202+
203+
.. note:: This endpoint is in beta and requires using the v4beta base URL.
204+
205+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-alert-channels
206+
207+
:param filters: Optional filter expressions to apply to the collection.
208+
See :doc:`Filtering Collections</linode_api4/objects/filtering>` for details.
209+
:returns: A paginated list of :class:`AlertChannel` objects.
210+
:rtype: PaginatedList[AlertChannel]
211+
"""
212+
return self.client._get_and_filter(AlertChannel, *filters)
213+
214+
def create_alert_definition(
215+
self,
216+
service_type: str,
217+
label: str,
218+
severity: int,
219+
channel_ids: list[int],
220+
rule_criteria: dict,
221+
trigger_conditions: dict,
222+
entity_ids: Optional[list[str]] = None,
223+
description: Optional[str] = None,
224+
) -> AlertDefinition:
225+
"""
226+
Create a new alert definition for a given service type.
227+
228+
The alert definition configures when alerts are fired and which channels
229+
are notified.
230+
231+
.. note:: This endpoint is in beta and requires using the v4beta base URL.
232+
233+
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-alert-definition-for-service-type
234+
235+
:param service_type: Service type for which to create the alert definition
236+
(e.g. ``"dbaas"``).
237+
:type service_type: str
238+
:param label: Human-readable label for the alert definition.
239+
:type label: str
240+
:param severity: Severity level for the alert (numeric severity used by API).
241+
:type severity: int
242+
:param channel_ids: List of alert channel IDs to notify when the alert fires.
243+
:type channel_ids: list[int]
244+
:param rule_criteria: Rule criteria that determine when the alert
245+
should be evaluated. Structure depends on the service
246+
metric definitions.
247+
:type rule_criteria: dict
248+
:param trigger_conditions: Trigger conditions that define when
249+
the alert should transition state.
250+
:type trigger_conditions: dict
251+
:param entity_ids: (Optional) Restrict the alert to a subset of entity IDs.
252+
:type entity_ids: Optional[list[str]]
253+
:param description: (Optional) Longer description for the alert definition.
254+
:type description: Optional[str]
255+
256+
:returns: The newly created :class:`AlertDefinition`.
257+
:rtype: AlertDefinition
258+
259+
.. note::
260+
For updating an alert definition, use the ``save()`` method on the :class:`AlertDefinition` object.
261+
For deleting an alert definition, use the ``delete()`` method directly on the :class:`AlertDefinition` object.
262+
"""
263+
params = {
264+
"label": label,
265+
"severity": severity,
266+
"channel_ids": channel_ids,
267+
"rule_criteria": rule_criteria,
268+
"trigger_conditions": trigger_conditions,
269+
}
270+
if description is not None:
271+
params["description"] = description
272+
if entity_ids is not None:
273+
params["entity_ids"] = entity_ids
274+
275+
# API will validate service_type and return an error if missing
276+
result = self.client.post(
277+
f"/monitor/services/{service_type}/alert-definitions", data=params
278+
)
279+
280+
if "id" not in result:
281+
raise UnexpectedResponseError(
282+
"Unexpected response when creating alert definition!",
283+
json=result,
284+
)
285+
286+
return AlertDefinition(self.client, result["id"], service_type, result)

0 commit comments

Comments
 (0)