Skip to content

Commit 89be246

Browse files
committed
Added ability to trigger user alerts
1 parent de4e0e6 commit 89be246

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "simvue-cli"
3-
version = "1.0.1"
3+
version = "1.0.2"
44
description = "Command Line Interface for interaction with a Simvue v3 server"
55
authors = [
66
{name = "Simvue Development Team", email = "[email protected]"}

src/simvue_cli/actions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,31 @@ def create_user_alert(
436436
return _alert
437437

438438

439+
def trigger_user_alert(
440+
run_id: str, alert_id: str, status: typing.Literal["ok", "critical"]
441+
) -> None:
442+
"""Trigger a manually defined user alert.
443+
444+
Parameters
445+
----------
446+
run_id: str
447+
the unique identifier for the run to alert on
448+
alert_id : str
449+
the unique identifier for the alert
450+
status: Literal['ok', 'critical']
451+
the state to set the alert to
452+
453+
"""
454+
_alert = get_alert(alert_id=alert_id)
455+
456+
if not isinstance(_alert, UserAlert):
457+
raise ValueError(f"Alert '{alert_id}' is not a user alert.")
458+
459+
_alert.read_only(False)
460+
_alert.set_status(run_id=run_id, status=status)
461+
_alert.commit()
462+
463+
439464
def create_simvue_user(
440465
username: str,
441466
email: str,

src/simvue_cli/cli/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,31 @@ def simvue_alert(ctx) -> None:
504504
pass
505505

506506

507+
@simvue_alert.command("trigger")
508+
@click.pass_context
509+
@click.argument("run_id")
510+
@click.argument("alert_id")
511+
@click.option(
512+
"--ok",
513+
"is_ok",
514+
is_flag=True,
515+
help="Set alert to status 'ok' as opposed to critical.",
516+
show_default=True,
517+
)
518+
def trigger_alert(ctx, is_ok: bool, **kwargs) -> None:
519+
"""Trigger a user alert"""
520+
try:
521+
simvue_cli.actions.trigger_user_alert(
522+
status="ok" if is_ok else "critical", **kwargs
523+
)
524+
except ValueError as e:
525+
if ctx.obj["plain"]:
526+
print(e.args[0])
527+
else:
528+
click.secho(e.args[0], fg="red", bold=True)
529+
sys.exit(1)
530+
531+
507532
@simvue_alert.command("list")
508533
@click.pass_context
509534
@click.option(

tests/test_command_line_interface.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from logging import critical
12
import random
23
import string
4+
import typing
35
from uuid import uuid4
46
from _pytest.compat import LEGACY_PATH
57
from simvue.api.objects import Alert, Storage, Tenant, User
@@ -252,6 +254,31 @@ def test_user_alert() -> None:
252254
Alert(identifier=_alert)
253255

254256

257+
@pytest.mark.parametrize("status", ("ok", "critical"))
258+
def test_user_alert_trigger(create_plain_run: tuple[simvue.Run, dict], status: typing.Literal["ok", "critical"]) -> None:
259+
run, _ = create_plain_run
260+
_alert_id = run.create_user_alert(
261+
name="test_user_alert_triggered_alert_cli",
262+
description="Test alert for CLI triggering",
263+
trigger_abort=True
264+
)
265+
_command = [
266+
"alert",
267+
"trigger",
268+
run.id,
269+
_alert_id
270+
]
271+
runner = click.testing.CliRunner()
272+
result = runner.invoke(
273+
sv_cli.simvue,
274+
_command + ([] if status == "critical" else ["--ok"])
275+
)
276+
assert result.exit_code == 0, result.output
277+
_alert: UserAlert = Alert(_alert_id)
278+
assert _alert.get_status(run.id) == status
279+
280+
281+
255282
def test_server_ping() -> None:
256283
runner = click.testing.CliRunner()
257284
result = runner.invoke(

tests/unit/test_cli_actions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
import time
33
import re
44
import pathlib
5+
import typing
56
import pytest
67
import uuid
78
import os
89
import simvue
910
from simvue.api.objects import Alert, Run, Events, Storage, Tenant, User
1011
from simvue.exception import ObjectNotFoundError
12+
from simvue.run import UserAlert
1113
import simvue_cli.actions
1214

1315

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

164166

167+
@pytest.mark.parametrize(
168+
"status", ("ok", "critical")
169+
)
170+
def test_user_alert_triggered(create_plain_run: tuple[simvue.Run, dict], status: typing.Literal["ok", "critical"]) -> None:
171+
run, _ = create_plain_run
172+
_alert_id = run.create_user_alert(
173+
name="test_user_alert_triggered_alert",
174+
description="Test alert for CLI triggering",
175+
trigger_abort=True
176+
)
177+
simvue_cli.actions.trigger_user_alert(
178+
run.id,
179+
_alert_id,
180+
status
181+
)
182+
_alert: UserAlert = Alert(_alert_id)
183+
assert _alert.get_status(run.id) == status

0 commit comments

Comments
 (0)