|
| 1 | +"""hyp incident subcommands.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Annotated |
| 6 | + |
| 7 | +import typer |
| 8 | + |
| 9 | +from hyperping.cli._config import get_client |
| 10 | +from hyperping.cli._output import print_detail, print_error, print_success, print_table |
| 11 | +from hyperping.exceptions import HyperpingAPIError |
| 12 | +from hyperping.models import Incident, IncidentCreate, IncidentType, LocalizedText |
| 13 | + |
| 14 | +incident_app = typer.Typer(name="incident", help="Manage incidents.") |
| 15 | + |
| 16 | + |
| 17 | +def _incident_row(i: Incident) -> list[object]: |
| 18 | + return [i.uuid, i.title_en, i.type, str(i.is_resolved), i.date or ""] |
| 19 | + |
| 20 | + |
| 21 | +@incident_app.command("list") |
| 22 | +def incident_list( |
| 23 | + ctx: typer.Context, |
| 24 | + status: Annotated[str | None, typer.Option("--status", help="Filter by status")] = None, |
| 25 | +) -> None: |
| 26 | + """List incidents.""" |
| 27 | + api_key: str | None = ctx.obj.get("api_key") |
| 28 | + json_mode: bool = ctx.obj.get("json", False) |
| 29 | + client = get_client(api_key) |
| 30 | + incidents = client.list_incidents(status=status) |
| 31 | + columns = ["uuid", "title", "type", "resolved", "date"] |
| 32 | + rows = [_incident_row(i) for i in incidents] |
| 33 | + print_table("Incidents", columns, rows, json_mode) |
| 34 | + |
| 35 | + |
| 36 | +@incident_app.command("create") |
| 37 | +def incident_create( |
| 38 | + ctx: typer.Context, |
| 39 | + title: Annotated[str, typer.Option("--title", help="Incident title (English)")], |
| 40 | + text: Annotated[str, typer.Option("--text", help="Incident message (English)")], |
| 41 | + statuspage: Annotated[str, typer.Option("--statuspage", help="Status page UUID")], |
| 42 | + incident_type: Annotated[ |
| 43 | + str | None, typer.Option("--type", help="Incident type: incident or outage") |
| 44 | + ] = None, |
| 45 | +) -> None: |
| 46 | + """Create a new incident.""" |
| 47 | + api_key: str | None = ctx.obj.get("api_key") |
| 48 | + json_mode: bool = ctx.obj.get("json", False) |
| 49 | + client = get_client(api_key) |
| 50 | + payload = IncidentCreate( |
| 51 | + title=LocalizedText.from_string(title), |
| 52 | + text=LocalizedText.from_string(text), |
| 53 | + type=IncidentType(incident_type) if incident_type else IncidentType.INCIDENT, |
| 54 | + statuspages=[statuspage], |
| 55 | + ) |
| 56 | + try: |
| 57 | + incident = client.create_incident(payload) |
| 58 | + except HyperpingAPIError as exc: |
| 59 | + print_error(str(exc)) |
| 60 | + raise typer.Exit(code=1) from exc |
| 61 | + fields = { |
| 62 | + "uuid": incident.uuid, |
| 63 | + "title": incident.title_en, |
| 64 | + "type": incident.type, |
| 65 | + "resolved": incident.is_resolved, |
| 66 | + "date": incident.date or "", |
| 67 | + } |
| 68 | + print_detail("Incident", fields, json_mode) |
| 69 | + |
| 70 | + |
| 71 | +@incident_app.command("resolve") |
| 72 | +def incident_resolve( |
| 73 | + ctx: typer.Context, |
| 74 | + incident_id: Annotated[str, typer.Argument(help="Incident UUID")], |
| 75 | + message: Annotated[ |
| 76 | + str | None, typer.Option("--message", help="Resolution message") |
| 77 | + ] = None, |
| 78 | +) -> None: |
| 79 | + """Resolve an incident.""" |
| 80 | + api_key: str | None = ctx.obj.get("api_key") |
| 81 | + client = get_client(api_key) |
| 82 | + try: |
| 83 | + incident = client.resolve_incident(incident_id, message=message) |
| 84 | + except HyperpingAPIError as exc: |
| 85 | + print_error(str(exc)) |
| 86 | + raise typer.Exit(code=1) from exc |
| 87 | + print_success(f"Incident {incident.uuid} resolved.") |
0 commit comments