Skip to content
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
6 changes: 3 additions & 3 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ Get a list of audit events.
**Usage**:

```console
$ pyaxm-cli audit-events [OPTIONS] START_TIMESTAMP END_TIMESTAMP
$ pyaxm-cli audit-events [OPTIONS] START_DATE END_DATE
```

**Arguments**:

* `START_TIMESTAMP`: [required]
* `END_TIMESTAMP`: [required]
* `START_DATE`: [required] Start date in `YYYY-MM-DD` or ISO 8601 format.
* `END_DATE`: [required] End date in `YYYY-MM-DD` or ISO 8601 format.

**Options**:

Expand Down
42 changes: 40 additions & 2 deletions pyaxm/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sys
from datetime import datetime, timezone

import pandas as pd
import typer
import yaml
Expand All @@ -21,6 +23,28 @@ def _output(data, format: str):
yaml.dump(data, sys.stdout, default_flow_style=False)


def _parse_audit_timestamp(value: str, *, end_of_day: bool = False) -> str:
"""Parse an audit-event date/timestamp argument.

Accepts YYYY-MM-DD or full ISO 8601 datetimes.
Date-only values are converted to UTC start/end of day.
"""
try:
parsed = datetime.fromisoformat(value)
except ValueError:
raise typer.BadParameter(
"Invalid date/time format. Use YYYY-MM-DD or ISO 8601 like 2025-12-31 or 2025-12-31T23:59:59Z."
)

if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=timezone.utc)

if end_of_day and len(value) == 10:
parsed = parsed.replace(hour=23, minute=59, second=59, microsecond=0)

return parsed.isoformat().replace("+00:00", "Z")


# ── device commands ─────────────────────────────────────────────────


Expand Down Expand Up @@ -158,8 +182,20 @@ def unassign_device(

@app.command()
def audit_events(
start_timestamp: Annotated[str, typer.Argument()],
end_timestamp: Annotated[str, typer.Argument()],
start_timestamp: Annotated[
str,
typer.Argument(
...,
help="Start date in YYYY-MM-DD or ISO 8601 format. Date-only values are treated as midnight UTC.",
),
],
end_timestamp: Annotated[
str,
typer.Argument(
...,
help="End date in YYYY-MM-DD or ISO 8601 format. Date-only values are treated as end of day UTC.",
),
],
actor_id: Annotated[Optional[str], typer.Option("--actor-id", "-a")] = None,
subject_id: Annotated[Optional[str], typer.Option("--subject-id", "-s")] = None,
event_type: Annotated[Optional[str], typer.Option("--event-type", "-e")] = None,
Expand All @@ -169,6 +205,8 @@ def audit_events(
format: Annotated[str, typer.Option("--format", help="Output format")] = "yaml",
):
"""Get a list of audit events."""
start_timestamp = _parse_audit_timestamp(start_timestamp, end_of_day=False)
end_timestamp = _parse_audit_timestamp(end_timestamp, end_of_day=True)
client = Client()
events = client.get_audit_events(
start_timestamp=start_timestamp,
Expand Down
Loading