Skip to content

Conversation

srbhaakamai
Copy link

@srbhaakamai srbhaakamai commented Sep 1, 2025

@yec-akamai @jriddle-linode Is you see this line, internal reviews are not yet complete. Will notify/inform once the internal reviews are complete.

📝 Description

Add Monitor alerting SDK + fix/harden monitor unit & integration tests

What does this PR do and why is this change necessary?

  1. Adds full SDK support for Monitor alert definitions (create / get / update / delete) used by tests and examples.
  2. Fixes serialization so JSONObject payloads containing Enum values serialize to primitives (prevents json.dumps errors).
  3. Adds and updates unit fixtures and monitor unit tests; adds integration test coverage for create → update → delete alert definitions.
  4. Makes integration fixtures resilient to tokens lacking certain scopes (skips firewall / DB provisioning when unauthorized) to allow running partial E2E locally.

This change is necessary because:

  1. Unit tests were failing due to missing Enum serialization and missing mock helpers.
  2. Integration tests would fail the whole session on tokens without cloud networking / DB scopes. Making fixtures skip when unauthorized lets developers iterate locally without privileged tokens.
  3. Adding SDK monitor methods enables first-class programmatic control over alert definitions as documented in the API (POST /monitor/services/{service_type}/alert-definitions).

SDK additions (high level)

  1. MonitorGroup methods (exposed on client.monitor):
  2. alert_definitions(service_type, alert_id=None) — list or get
  3. create_alert_definition(service_type, label, severity, type, description, channel_ids, trigger_conditions, rule_criteria, notification_groups, conditions)
  4. update_alert_definition(service_type, alert_id, **fields) or client.monitor.update_alert_definition(...)
  5. delete_alert_definition(service_type, alert_id)
  6. alert_channels() — list alert channels
  7. These map to the Monitor Alert Definition endpoints per docs: https://techdocs.akamai.com/linode-api/reference/post-alert-definition-for-service-type

Payload:
{
"label": "sdk-e2e-1234",
"severity": 3,
"type": "threshold",
"description": "Temporary alert created by SDK e2e test",
"channel_ids": [10000],
"trigger_conditions": {
"criteria_condition": "ALL",
"evaluation_period_seconds": 300,
"polling_interval_seconds": 300,
"trigger_occurrences": 1
},
"rule_criteria": {
"rules": [
{
"metric": "cpu_usage",
"operator": "gt",
"threshold": 90,
"aggregate_function": "avg",
"period": 300,
"unit": "percent",
"dimension_filters": []
}
]
},
"conditions": [],
"notification_groups": []
}

Note:

  1. severity must be integer.
  2. trigger_conditions.polling_interval_seconds and evaluation_period_seconds must be API-allowed values (for these services 300s in our tests).
  3. label/description must begin and end with an alphanumeric character.

✔️ How to Test

  1. Prepare environment (zsh / macOS)

create & activate venv (recommended)

python3 -m venv .venv
source .venv/bin/activate

install deps

python -m pip install --upgrade pip
python -m pip install -r requirements-dev.txt

Files changed (summary)
Modified (key):
linode_api4/groups/monitor.py — MonitorGroup CRUD methods
linode_api4/linode_client.py — client.monitor init
linode_api4/objects/monitor.py — objects for AlertDefinition, Metric, etc.
linode_api4/objects/serializable.py — Enum serialization fix
test/unit/base.py — added mock_get/_helpers
test/unit/groups/monitor_api_test.py — updated/added tests
test/integration/conftest.py — fixtures: skip behavior for insufficient token perms
test/integration/models/monitor/test_monitor.py — E2E create/update/delete test

How do I run the relevant tests?
Unit tests: python -m pytest test/unit -q
Single unit: python -m pytest test/unit/groups/monitor_api_test.py::MonitorAlertDefinitionsTest::test_alert_definitions_for_service_calls_endpoint -q -s
Single integration: python -m pytest test/integration/models/monitor/test_monitor.py::test_integration_create_get_update_delete_alert_definition -q -s
What are the steps to reproduce the issue or verify the changes?

How do I run the relevant unit/integration tests?
Unit tests (fast)

Run all monitor unit tests

python -m pytest test/unit/groups/monitor_api_test.py -q -s

Run a single unit test

python -m pytest test/unit/groups/monitor_api_test.py::MonitorAlertDefinitionsTest::test_alert_definitions_for_service_calls_endpoint -q -s

Integration tests (E2E)

Provide a Linode token with required scopes. For full E2E you need monitor:write and other service-specific scopes (networking:write / databases:write if fixtures create firewalls or DBs).
If you lack networking or DB scopes and want to run alert integration only, set SKIP_E2E_FIREWALL=1 (the test fixtures will skip firewall provisioning).

export LINODE_TOKEN="YOUR_REAL_TOKEN" # required for integration
export SKIP_E2E_FIREWALL=1 # optional: skip firewall autouse fixture
python -m pytest test/integration/models/monitor/test_monitor.py::test_integration_create_get_update_delete_alert_definition -q -s

📷 Preview

@srbhaakamai srbhaakamai requested a review from a team as a code owner September 1, 2025 10:42
@srbhaakamai srbhaakamai requested review from jriddle-linode and yec-akamai and removed request for a team September 1, 2025 10:42
@srbhaakamai srbhaakamai changed the title <WIP> Python SDK for ACLP Alerts Python SDK for ACLP Alerts Sep 2, 2025
@srbhaakamai srbhaakamai marked this pull request as draft September 3, 2025 04:56
@srbhaakamai srbhaakamai marked this pull request as ready for review September 3, 2025 07:58
@srbhaakamai
Copy link
Author

Request @ketan Vaish to review this as internal reviwer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant