Skip to content

Conversation

srbhaakamai
Copy link

@srbhaakamai srbhaakamai commented Sep 1, 2025

📝 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

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

Clone the repository
Prepare environment (zsh / macOS)

✔️create & activate venv (recommended)

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

install deps
python -m pip install --upgrade pip

Install runtime dependencies:
pip3 install requests polling deprecated

Install dev/test extras
pip3 install -e '.[dev,test]'

test deps
pip3 install pytest mock httpretty pytest-rerunfailures

✔️How do I run the relevant tests?

cd <PROJECT_DIRECTORY>/linode_api4-python
Unit tests: python -m pytest test/unit -q
output: 347 passed, 18 warnings in 0.72s

Unit test for all monitor: python -m pytest test/unit/groups/monitor_api_test.py -q -s
output: 6 passed, 1 warning in 0.09s

Single unit: python -m pytest test/unit/groups/monitor_api_test.py::MonitorAlertDefinitionsTest -q -s
output: 5 passed, 1 warning in 0.08s

What are the steps to reproduce the issue or verify the changes?
Its a new feature, so end to end integration should suffice

✔️How do I run the relevant unit/integration tests?

Integration tests (E2E):
Note:
#if you have PAT token with write access only to Monitor and read for rest of the services for integration
export SKIP_E2E_FIREWALL=1 # optional: skip firewall autouse fixture

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

@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
Copy link

@ktnvaish22 ktnvaish22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving with respect to ACLP Alerting API specifications, logic, test cases and integration testing.
Requesting API / Cloud Firewall team to review https://github.com/linode/linode_api4-python/pull/589/files#diff-70a344c4146b66b96b63999ccef7d16b311cceba86cfc2ac987a5edf3cffcdc3R82-R87

Thanks!

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive Monitor alerting SDK support to the Python API client, including CRUD operations for alert definitions, and improves test infrastructure to support both unit and integration testing scenarios.

  • Adds full SDK support for Monitor alert definitions (create/get/update/delete operations)
  • Fixes Enum serialization in JSONObject to prevent json.dumps errors
  • Enhances test infrastructure with new fixtures and integration test resilience

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
test/unit/groups/monitor_api_test.py Adds comprehensive unit tests for alert definition CRUD operations
test/unit/base.py Adds missing mock helper methods (GET, PUT, DELETE) for MonitorClientBaseCase
test/integration/models/monitor/test_monitor.py Adds E2E integration test for alert definition lifecycle
test/integration/conftest.py Makes firewall fixture skippable for local development
test/fixtures/*.json Adds JSON fixtures for alert definition mocking
linode_api4/objects/serializable.py Fixes Enum serialization to prevent JSON encoding errors
linode_api4/objects/monitor.py Adds comprehensive data classes for alert definitions and channels
linode_api4/linode_client.py Exposes MonitorGroup on MonitorClient for alert operations
linode_api4/groups/monitor.py Implements alert definition CRUD methods

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@srbhaakamai
Copy link
Author

srbhaakamai commented Sep 12, 2025

FIxed issues with CI and comments from Copilot AI

@yec-akamai
Copy link
Contributor

yec-akamai commented Sep 16, 2025

To fix the lint issue, you can run make isort and make lint locally

Copy link
Contributor

@yec-akamai yec-akamai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! You probably need to make changes around the implementation of AlertDefinition

@srbhaakamai
Copy link
Author

thanks for all the comments and observations, i incorporated the suggestions and also verified commits as well.

@srbhaakamai
Copy link
Author

ced that not all commits are signed.

all commits are signed now and will ensure they are signed going forward.

and is used by the SDK to list, load, and inspect channels.
"""

api_endpoint = "/monitor/alert-channels/"
Copy link
Contributor

@yec-akamai yec-akamai Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, but the api_endpoint here refers to the GET single entity endpoint not the listing one. Our code have handled parsing the correct listing URL, so I don't think it hurts adding /id to the path. By just adding it, once the other endpoints available, user can directly have access to them without code change (except Create op).

You can add a note in the docstring saying single entity CRUD is not available now.

srbhaakamai and others added 6 commits September 29, 2025 07:46
Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com>
Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com>
Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com>
Co-authored-by: Ye Chen <127243817+yec-akamai@users.noreply.github.com>
@srbhaakamai
Copy link
Author

@yec-akamai , fixed all the comments. thanks for the detailed review,

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.

4 participants