|
1 | | -__all__ = [ |
2 | | - "MonitorGroup", |
3 | | -] |
4 | 1 | from typing import Any, Optional |
5 | 2 |
|
6 | 3 | from linode_api4 import PaginatedList |
7 | 4 | from linode_api4.errors import UnexpectedResponseError |
8 | 5 | from linode_api4.groups import Group |
9 | 6 | from linode_api4.objects import ( |
| 7 | + AlertChannel, |
| 8 | + AlertDefinition, |
10 | 9 | MonitorDashboard, |
11 | 10 | MonitorMetricsDefinition, |
12 | 11 | MonitorService, |
13 | 12 | MonitorServiceToken, |
14 | 13 | ) |
15 | 14 |
|
| 15 | +__all__ = [ |
| 16 | + "MonitorGroup", |
| 17 | +] |
| 18 | + |
16 | 19 |
|
17 | 20 | class MonitorGroup(Group): |
18 | 21 | """ |
@@ -145,3 +148,139 @@ def create_token( |
145 | 148 | "Unexpected response when creating token!", json=result |
146 | 149 | ) |
147 | 150 | 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