Skip to content

Added ability to trigger user alerts #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "simvue-cli"
version = "1.0.1"
version = "1.0.2"
description = "Command Line Interface for interaction with a Simvue v3 server"
authors = [
{name = "Simvue Development Team", email = "[email protected]"}
Expand Down
25 changes: 25 additions & 0 deletions src/simvue_cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,31 @@ def create_user_alert(
return _alert


def trigger_user_alert(
run_id: str, alert_id: str, status: typing.Literal["ok", "critical"]
) -> None:
"""Trigger a manually defined user alert.

Parameters
----------
run_id: str
the unique identifier for the run to alert on
alert_id : str
the unique identifier for the alert
status: Literal['ok', 'critical']
the state to set the alert to

"""
_alert = get_alert(alert_id=alert_id)

if not isinstance(_alert, UserAlert):
raise ValueError(f"Alert '{alert_id}' is not a user alert.")

_alert.read_only(False)
_alert.set_status(run_id=run_id, status=status)
_alert.commit()


def create_simvue_user(
username: str,
email: str,
Expand Down
25 changes: 25 additions & 0 deletions src/simvue_cli/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,31 @@ def simvue_alert(ctx) -> None:
pass


@simvue_alert.command("trigger")
@click.pass_context
@click.argument("run_id")
@click.argument("alert_id")
@click.option(
"--ok",
"is_ok",
is_flag=True,
help="Set alert to status 'ok' as opposed to critical.",
show_default=True,
)
def trigger_alert(ctx, is_ok: bool, **kwargs) -> None:
"""Trigger a user alert"""
try:
simvue_cli.actions.trigger_user_alert(
status="ok" if is_ok else "critical", **kwargs
)
except ValueError as e:
if ctx.obj["plain"]:
print(e.args[0])
else:
click.secho(e.args[0], fg="red", bold=True)
sys.exit(1)


@simvue_alert.command("list")
@click.pass_context
@click.option(
Expand Down
27 changes: 27 additions & 0 deletions tests/test_command_line_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from logging import critical
import random
import string
import typing
from uuid import uuid4
from _pytest.compat import LEGACY_PATH
from simvue.api.objects import Alert, Storage, Tenant, User
Expand Down Expand Up @@ -252,6 +254,31 @@ def test_user_alert() -> None:
Alert(identifier=_alert)


@pytest.mark.parametrize("status", ("ok", "critical"))
def test_user_alert_trigger(create_plain_run: tuple[simvue.Run, dict], status: typing.Literal["ok", "critical"]) -> None:
run, _ = create_plain_run
_alert_id = run.create_user_alert(
name="test_user_alert_triggered_alert_cli",
description="Test alert for CLI triggering",
trigger_abort=True
)
_command = [
"alert",
"trigger",
run.id,
_alert_id
]
runner = click.testing.CliRunner()
result = runner.invoke(
sv_cli.simvue,
_command + ([] if status == "critical" else ["--ok"])
)
assert result.exit_code == 0, result.output
_alert: UserAlert = Alert(_alert_id)
assert _alert.get_status(run.id) == status



def test_server_ping() -> None:
runner = click.testing.CliRunner()
result = runner.invoke(
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_cli_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import time
import re
import pathlib
import typing
import pytest
import uuid
import os
import simvue
from simvue.api.objects import Alert, Run, Events, Storage, Tenant, User
from simvue.exception import ObjectNotFoundError
from simvue.run import UserAlert
import simvue_cli.actions


Expand Down Expand Up @@ -162,3 +164,20 @@ def test_run_abort(create_test_run, monkeypatch) -> None:
assert _run._status == "terminated"


@pytest.mark.parametrize(
"status", ("ok", "critical")
)
def test_user_alert_triggered(create_plain_run: tuple[simvue.Run, dict], status: typing.Literal["ok", "critical"]) -> None:
run, _ = create_plain_run
_alert_id = run.create_user_alert(
name="test_user_alert_triggered_alert",
description="Test alert for CLI triggering",
trigger_abort=True
)
simvue_cli.actions.trigger_user_alert(
run.id,
_alert_id,
status
)
_alert: UserAlert = Alert(_alert_id)
assert _alert.get_status(run.id) == status
Loading