ttlama-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ttlama_cli/__init__.py +0 -0
- ttlama_cli/__main__.py +6 -0
- ttlama_cli/api_client.py +221 -0
- ttlama_cli/cli.py +71 -0
- ttlama_cli/commands/__init__.py +256 -0
- ttlama_cli/commands/admin/__init__.py +22 -0
- ttlama_cli/commands/admin/devices.py +300 -0
- ttlama_cli/commands/admin/leases.py +244 -0
- ttlama_cli/commands/admin/profiles.py +328 -0
- ttlama_cli/commands/admin/users.py +460 -0
- ttlama_cli/commands/auth.py +469 -0
- ttlama_cli/commands/backend/__init__.py +18 -0
- ttlama_cli/commands/backend/openvpn/__init__.py +0 -0
- ttlama_cli/commands/backend/openvpn/peers.py +0 -0
- ttlama_cli/commands/backend/wireguard/__init__.py +18 -0
- ttlama_cli/commands/backend/wireguard/peers.py +282 -0
- ttlama_cli/commands/config.py +350 -0
- ttlama_cli/commands/user/__init__.py +29 -0
- ttlama_cli/commands/user/devices.py +214 -0
- ttlama_cli/commands/user/get.py +95 -0
- ttlama_cli/commands/user/leases.py +223 -0
- ttlama_cli/commands/user/profiles.py +84 -0
- ttlama_cli/config_store.py +109 -0
- ttlama_cli/drivers/__init__.py +19 -0
- ttlama_cli/drivers/_common.py +116 -0
- ttlama_cli/drivers/openvpn.py +59 -0
- ttlama_cli/drivers/wireguard.py +165 -0
- ttlama_cli/exc.py +78 -0
- ttlama_cli/objects/__init__.py +18 -0
- ttlama_cli/objects/requests.py +65 -0
- ttlama_cli/token_store.py +85 -0
- ttlama_cli/utils.py +30 -0
- ttlama_cli-0.1.0.dist-info/METADATA +310 -0
- ttlama_cli-0.1.0.dist-info/RECORD +38 -0
- ttlama_cli-0.1.0.dist-info/WHEEL +5 -0
- ttlama_cli-0.1.0.dist-info/entry_points.txt +2 -0
- ttlama_cli-0.1.0.dist-info/licenses/LICENSE +71 -0
- ttlama_cli-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""User device command group, reserved for device operations.
|
|
2
|
+
|
|
3
|
+
.. prefix:: `ttlama user devices`
|
|
4
|
+
|
|
5
|
+
.. CLI::
|
|
6
|
+
- `list` : List the user's devices.
|
|
7
|
+
- `get` : Display details of a specific device.
|
|
8
|
+
- [--id] : Specify the device ID.
|
|
9
|
+
- `add` : Add a new device.
|
|
10
|
+
- [--file] : Specify the file containing device details.
|
|
11
|
+
- `delete` : Remove a specific device.
|
|
12
|
+
- [--id] : Specify the device ID.
|
|
13
|
+
|
|
14
|
+
.. module_author:: Rémi Verschuur <rverschuur@audit-io.fr>
|
|
15
|
+
.. version:: 1.0.0"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
import typer
|
|
21
|
+
from rich.console import Console
|
|
22
|
+
from rich.table import Table
|
|
23
|
+
|
|
24
|
+
from .. import (
|
|
25
|
+
display_structured_output,
|
|
26
|
+
load_data_file,
|
|
27
|
+
request_configured_api,
|
|
28
|
+
resource_object,
|
|
29
|
+
resource_rows,
|
|
30
|
+
validate_output_options,
|
|
31
|
+
)
|
|
32
|
+
from ...exc import APIError, AuthenticationError, AuthorizationError, handle_api_error
|
|
33
|
+
from ...objects import HTTPMethod, SpecificRequest
|
|
34
|
+
|
|
35
|
+
app = typer.Typer(
|
|
36
|
+
no_args_is_help=True,
|
|
37
|
+
help="Inspect devices owned by the authenticated user.",
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _display_devices(
|
|
42
|
+
devices: list[dict[str, object]],
|
|
43
|
+
*,
|
|
44
|
+
json_output: bool,
|
|
45
|
+
yaml_output: bool,
|
|
46
|
+
) -> None:
|
|
47
|
+
"""Display devices as JSON, YAML, or a Rich table.
|
|
48
|
+
|
|
49
|
+
:param devices: The list of devices to display.
|
|
50
|
+
:type devices: list[dict[str, object]]
|
|
51
|
+
:param json_output: Whether to output as JSON.
|
|
52
|
+
:type json_output: bool
|
|
53
|
+
:param yaml_output: Whether to output as YAML.
|
|
54
|
+
:type yaml_output: bool
|
|
55
|
+
"""
|
|
56
|
+
if display_structured_output(
|
|
57
|
+
devices,
|
|
58
|
+
json_output=json_output,
|
|
59
|
+
yaml_output=yaml_output,
|
|
60
|
+
yaml_root="devices",
|
|
61
|
+
):
|
|
62
|
+
return
|
|
63
|
+
|
|
64
|
+
table = Table(title="Devices", expand=True)
|
|
65
|
+
table.add_column("ID", style="cyan", no_wrap=True)
|
|
66
|
+
table.add_column("Name", style="green", overflow="ellipsis")
|
|
67
|
+
table.add_column("Created")
|
|
68
|
+
table.add_column("Status")
|
|
69
|
+
for device in devices:
|
|
70
|
+
table.add_row(
|
|
71
|
+
str(device.get("id", "")),
|
|
72
|
+
str(device.get("name", "")),
|
|
73
|
+
str(device.get("created_at") or "-"),
|
|
74
|
+
"revoked" if device.get("revoked_at") is not None else "active",
|
|
75
|
+
)
|
|
76
|
+
Console().print(table)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _display_device(
|
|
80
|
+
device: dict[str, object],
|
|
81
|
+
*,
|
|
82
|
+
json_output: bool,
|
|
83
|
+
yaml_output: bool,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Display one device as JSON, YAML, or a Rich table.
|
|
86
|
+
|
|
87
|
+
:param device: The device to display.
|
|
88
|
+
:type device: dict[str, object]
|
|
89
|
+
:param json_output: Whether to output as JSON.
|
|
90
|
+
:type json_output: bool
|
|
91
|
+
:param yaml_output: Whether to output as YAML.
|
|
92
|
+
:type yaml_output: bool
|
|
93
|
+
"""
|
|
94
|
+
if display_structured_output(
|
|
95
|
+
device,
|
|
96
|
+
json_output=json_output,
|
|
97
|
+
yaml_output=yaml_output,
|
|
98
|
+
):
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
table = Table(title=f"Device {device.get('id', '')}", expand=True)
|
|
102
|
+
table.add_column("Field", style="cyan")
|
|
103
|
+
table.add_column("Value", style="green", overflow="ellipsis")
|
|
104
|
+
for key, value in device.items():
|
|
105
|
+
table.add_row(key, str(value if value is not None else "-"))
|
|
106
|
+
Console().print(table)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@app.command("ls")
|
|
110
|
+
def list_devices(
|
|
111
|
+
json_output: bool = typer.Option(False, "--json", help="Output devices as JSON."),
|
|
112
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output devices as YAML."),
|
|
113
|
+
) -> None:
|
|
114
|
+
"""List devices owned by the authenticated user.
|
|
115
|
+
|
|
116
|
+
:param json_output: Whether to output as JSON.
|
|
117
|
+
:type json_output: bool
|
|
118
|
+
:param yaml_output: Whether to output as YAML.
|
|
119
|
+
:type yaml_output: bool
|
|
120
|
+
"""
|
|
121
|
+
validate_output_options(json_output, yaml_output)
|
|
122
|
+
try:
|
|
123
|
+
devices = resource_rows(
|
|
124
|
+
request_configured_api(SpecificRequest(
|
|
125
|
+
method=HTTPMethod.GET,
|
|
126
|
+
endpoint="users/me/devices",
|
|
127
|
+
)),
|
|
128
|
+
"device",
|
|
129
|
+
)
|
|
130
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
131
|
+
handle_api_error(error)
|
|
132
|
+
return
|
|
133
|
+
|
|
134
|
+
if not devices and not (json_output or yaml_output):
|
|
135
|
+
typer.echo("No devices found.")
|
|
136
|
+
return
|
|
137
|
+
_display_devices(devices, json_output=json_output, yaml_output=yaml_output)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@app.command("get")
|
|
141
|
+
def get_device(
|
|
142
|
+
device_id: int = typer.Option(..., "--id", help="Device ID to retrieve."),
|
|
143
|
+
json_output: bool = typer.Option(False, "--json", help="Output the device as JSON."),
|
|
144
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output the device as YAML."),
|
|
145
|
+
) -> None:
|
|
146
|
+
"""Retrieve an owned device by ID.
|
|
147
|
+
|
|
148
|
+
:param device_id: The ID of the device to retrieve.
|
|
149
|
+
:type device_id: int
|
|
150
|
+
:param json_output: Whether to output as JSON.
|
|
151
|
+
:type json_output: bool
|
|
152
|
+
:param yaml_output: Whether to output as YAML.
|
|
153
|
+
:type yaml_output: bool
|
|
154
|
+
"""
|
|
155
|
+
validate_output_options(json_output, yaml_output)
|
|
156
|
+
try:
|
|
157
|
+
device = resource_object(
|
|
158
|
+
request_configured_api(SpecificRequest(
|
|
159
|
+
method=HTTPMethod.GET,
|
|
160
|
+
endpoint=f"users/me/devices/{device_id}",
|
|
161
|
+
)),
|
|
162
|
+
"device",
|
|
163
|
+
)
|
|
164
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
165
|
+
handle_api_error(error)
|
|
166
|
+
return
|
|
167
|
+
_display_device(device, json_output=json_output, yaml_output=yaml_output)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@app.command("add")
|
|
171
|
+
def add_device(
|
|
172
|
+
file_path: Path = typer.Option(..., "--file", help="JSON or YAML device data file."),
|
|
173
|
+
) -> None:
|
|
174
|
+
"""Add a device from a JSON or YAML file.
|
|
175
|
+
|
|
176
|
+
:param file_path: The path to the JSON or YAML device data file.
|
|
177
|
+
:type file_path: Path
|
|
178
|
+
"""
|
|
179
|
+
device_data = load_data_file(file_path, "device")
|
|
180
|
+
try:
|
|
181
|
+
device = resource_object(
|
|
182
|
+
request_configured_api(SpecificRequest(
|
|
183
|
+
method=HTTPMethod.POST,
|
|
184
|
+
endpoint="users/me/devices",
|
|
185
|
+
json_body=device_data,
|
|
186
|
+
)),
|
|
187
|
+
"device",
|
|
188
|
+
)
|
|
189
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
190
|
+
handle_api_error(error)
|
|
191
|
+
return
|
|
192
|
+
typer.echo(f"Device {device.get('id', '')} added successfully.")
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@app.command("delete")
|
|
196
|
+
def delete_device(
|
|
197
|
+
device_id: int = typer.Option(..., "--id", help="Device ID to revoke."),
|
|
198
|
+
) -> None:
|
|
199
|
+
"""Revoke an owned device and its active leases.
|
|
200
|
+
|
|
201
|
+
:param device_id: The ID of the device to revoke.
|
|
202
|
+
:type device_id: int
|
|
203
|
+
"""
|
|
204
|
+
if not typer.confirm(f"Revoke device {device_id} and its leases?"):
|
|
205
|
+
raise typer.Exit(code=1)
|
|
206
|
+
try:
|
|
207
|
+
request_configured_api(SpecificRequest(
|
|
208
|
+
method=HTTPMethod.DELETE,
|
|
209
|
+
endpoint=f"users/me/devices/{device_id}",
|
|
210
|
+
))
|
|
211
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
212
|
+
handle_api_error(error)
|
|
213
|
+
return
|
|
214
|
+
typer.echo(f"Device {device_id} revoked successfully.")
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Display the authenticated user's overview.
|
|
2
|
+
|
|
3
|
+
.. prefix:: `ttlama user`
|
|
4
|
+
|
|
5
|
+
.. CLI::
|
|
6
|
+
- `get` : Display the authenticated user's overview.
|
|
7
|
+
|
|
8
|
+
.. module_author:: Rémi Verschuur <rverschuur@audit-io.fr>
|
|
9
|
+
.. version:: 1.0.0
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from typing import cast
|
|
14
|
+
|
|
15
|
+
import typer
|
|
16
|
+
from rich.console import Console
|
|
17
|
+
from rich.table import Table
|
|
18
|
+
|
|
19
|
+
from .. import (
|
|
20
|
+
display_profiles,
|
|
21
|
+
display_structured_output,
|
|
22
|
+
get_server_and_token,
|
|
23
|
+
profile_rows,
|
|
24
|
+
request_user_overview,
|
|
25
|
+
validate_output_options,
|
|
26
|
+
)
|
|
27
|
+
from ...exc import APIError, AuthenticationError, AuthorizationError, handle_api_error
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_user( # pylint: disable=R0914
|
|
31
|
+
json_output: bool = typer.Option(False, "--json", help="Output as JSON."),
|
|
32
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output as YAML."),
|
|
33
|
+
) -> None:
|
|
34
|
+
"""Display the current user, devices, leases, and assigned profiles."""
|
|
35
|
+
validate_output_options(json_output, yaml_output)
|
|
36
|
+
server_url, server_port, token = get_server_and_token()
|
|
37
|
+
try:
|
|
38
|
+
overview = request_user_overview(server_url, server_port, token)
|
|
39
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
40
|
+
handle_api_error(error)
|
|
41
|
+
return
|
|
42
|
+
|
|
43
|
+
if display_structured_output(
|
|
44
|
+
overview,
|
|
45
|
+
json_output=json_output,
|
|
46
|
+
yaml_output=yaml_output,
|
|
47
|
+
):
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
user = cast(dict[str, object], overview["user"])
|
|
51
|
+
user_table = Table(title="User", expand=True)
|
|
52
|
+
user_table.add_column("Field", style="cyan")
|
|
53
|
+
user_table.add_column("Value", style="green", overflow="ellipsis")
|
|
54
|
+
for key in ("id", "username", "email"):
|
|
55
|
+
user_table.add_row(key, str(user.get(key) or "-"))
|
|
56
|
+
user_table.add_row("Administrator", "yes" if user.get("is_admin") else "no")
|
|
57
|
+
Console().print(user_table)
|
|
58
|
+
|
|
59
|
+
devices = cast(list[dict[str, object]], overview["devices"])
|
|
60
|
+
device_table = Table(title="Devices", expand=True)
|
|
61
|
+
device_table.add_column("ID", style="cyan", no_wrap=True)
|
|
62
|
+
device_table.add_column("Name", style="green", overflow="ellipsis")
|
|
63
|
+
device_table.add_column("Created")
|
|
64
|
+
device_table.add_column("Status")
|
|
65
|
+
for device in devices:
|
|
66
|
+
device_table.add_row(
|
|
67
|
+
str(device.get("id", "")),
|
|
68
|
+
str(device.get("name", "")),
|
|
69
|
+
str(device.get("created_at") or "-"),
|
|
70
|
+
"revoked" if device.get("revoked_at") is not None else "active",
|
|
71
|
+
)
|
|
72
|
+
Console().print(device_table)
|
|
73
|
+
|
|
74
|
+
leases = cast(list[dict[str, object]], overview["leases"])
|
|
75
|
+
lease_table = Table(title="Leases", expand=True)
|
|
76
|
+
lease_table.add_column("ID", style="cyan", no_wrap=True)
|
|
77
|
+
lease_table.add_column("Device ID")
|
|
78
|
+
lease_table.add_column("Profile ID")
|
|
79
|
+
lease_table.add_column("Ends")
|
|
80
|
+
lease_table.add_column("Status")
|
|
81
|
+
for lease in leases:
|
|
82
|
+
lease_table.add_row(
|
|
83
|
+
str(lease.get("id", "")),
|
|
84
|
+
str(lease.get("device_id", "")),
|
|
85
|
+
str(lease.get("profile_id", "")),
|
|
86
|
+
str(lease.get("ended_at") or "-"),
|
|
87
|
+
str(lease.get("status", "")),
|
|
88
|
+
)
|
|
89
|
+
Console().print(lease_table)
|
|
90
|
+
|
|
91
|
+
display_profiles(
|
|
92
|
+
profile_rows(overview["profiles"]),
|
|
93
|
+
json_output=False,
|
|
94
|
+
yaml_output=False,
|
|
95
|
+
)
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""User lease command group, reserved for lease operations.
|
|
2
|
+
|
|
3
|
+
.. prefix:: `ttlama user leases`
|
|
4
|
+
|
|
5
|
+
.. CLI::
|
|
6
|
+
- `list` : List the user's leases.
|
|
7
|
+
- `get` : Display details of a specific lease.
|
|
8
|
+
- [--id] : Specify the lease ID.
|
|
9
|
+
- `create` : Create a new lease.
|
|
10
|
+
- [--file] : Specify the file containing lease details.
|
|
11
|
+
- `revoke` : Revoke a specific lease.
|
|
12
|
+
- [--id] : Specify the lease ID.
|
|
13
|
+
|
|
14
|
+
.. module_author:: Rémi Verschuur <rverschuur@audit-io.fr>
|
|
15
|
+
.. version:: 1.0.0
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
import typer
|
|
22
|
+
from rich.console import Console
|
|
23
|
+
from rich.table import Table
|
|
24
|
+
|
|
25
|
+
from .. import (
|
|
26
|
+
display_structured_output,
|
|
27
|
+
load_data_file,
|
|
28
|
+
request_configured_api,
|
|
29
|
+
resource_object,
|
|
30
|
+
resource_rows,
|
|
31
|
+
validate_output_options,
|
|
32
|
+
)
|
|
33
|
+
from ...exc import APIError, AuthenticationError, AuthorizationError, handle_api_error
|
|
34
|
+
from ...objects import HTTPMethod, SpecificRequest
|
|
35
|
+
|
|
36
|
+
PROVISIONING_REQUEST_TIMEOUT_SECONDS = 60.0
|
|
37
|
+
|
|
38
|
+
app = typer.Typer(
|
|
39
|
+
no_args_is_help=True,
|
|
40
|
+
help="Inspect leases owned by the authenticated user.",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _display_leases(
|
|
45
|
+
leases: list[dict[str, object]],
|
|
46
|
+
*,
|
|
47
|
+
json_output: bool,
|
|
48
|
+
yaml_output: bool,
|
|
49
|
+
) -> None:
|
|
50
|
+
"""Display leases as JSON, YAML, or a Rich table.
|
|
51
|
+
|
|
52
|
+
:param leases: The list of leases to display.
|
|
53
|
+
:type leases: list[dict[str, object]]
|
|
54
|
+
:param json_output: Whether to output as JSON.
|
|
55
|
+
:type json_output: bool
|
|
56
|
+
:param yaml_output: Whether to output as YAML.
|
|
57
|
+
:type yaml_output: bool
|
|
58
|
+
"""
|
|
59
|
+
if display_structured_output(
|
|
60
|
+
leases,
|
|
61
|
+
json_output=json_output,
|
|
62
|
+
yaml_output=yaml_output,
|
|
63
|
+
yaml_root="leases",
|
|
64
|
+
):
|
|
65
|
+
return
|
|
66
|
+
|
|
67
|
+
table = Table(title="Leases", expand=True)
|
|
68
|
+
table.add_column("ID", style="cyan", no_wrap=True)
|
|
69
|
+
table.add_column("Device ID")
|
|
70
|
+
table.add_column("Profile ID")
|
|
71
|
+
table.add_column("Created")
|
|
72
|
+
table.add_column("Ends")
|
|
73
|
+
table.add_column("Status")
|
|
74
|
+
for lease in leases:
|
|
75
|
+
table.add_row(
|
|
76
|
+
str(lease.get("id", "")),
|
|
77
|
+
str(lease.get("device_id", "")),
|
|
78
|
+
str(lease.get("profile_id", "")),
|
|
79
|
+
str(lease.get("created_at") or "-"),
|
|
80
|
+
str(lease.get("ended_at") or "-"),
|
|
81
|
+
str(lease.get("status", "")),
|
|
82
|
+
)
|
|
83
|
+
Console().print(table)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _display_lease(
|
|
87
|
+
lease: dict[str, object],
|
|
88
|
+
*,
|
|
89
|
+
json_output: bool,
|
|
90
|
+
yaml_output: bool,
|
|
91
|
+
) -> None:
|
|
92
|
+
"""Display one lease as JSON, YAML, or a Rich table.
|
|
93
|
+
|
|
94
|
+
:param lease: The lease to display.
|
|
95
|
+
:type lease: dict[str, object]
|
|
96
|
+
:param json_output: Whether to output as JSON.
|
|
97
|
+
:type json_output: bool
|
|
98
|
+
:param yaml_output: Whether to output as YAML.
|
|
99
|
+
:type yaml_output: bool
|
|
100
|
+
"""
|
|
101
|
+
if display_structured_output(
|
|
102
|
+
lease,
|
|
103
|
+
json_output=json_output,
|
|
104
|
+
yaml_output=yaml_output,
|
|
105
|
+
):
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
table = Table(title=f"Lease {lease.get('id', '')}", expand=True)
|
|
109
|
+
table.add_column("Field", style="cyan")
|
|
110
|
+
table.add_column("Value", style="green", overflow="ellipsis")
|
|
111
|
+
for key, value in lease.items():
|
|
112
|
+
table.add_row(key, str(value if value is not None else "-"))
|
|
113
|
+
Console().print(table)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@app.command("ls")
|
|
117
|
+
def list_leases(
|
|
118
|
+
json_output: bool = typer.Option(False, "--json", help="Output leases as JSON."),
|
|
119
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output leases as YAML."),
|
|
120
|
+
) -> None:
|
|
121
|
+
"""List leases owned by the authenticated user.
|
|
122
|
+
|
|
123
|
+
:param json_output: Whether to output as JSON.
|
|
124
|
+
:type json_output: bool
|
|
125
|
+
:param yaml_output: Whether to output as YAML.
|
|
126
|
+
:type yaml_output: bool
|
|
127
|
+
"""
|
|
128
|
+
validate_output_options(json_output, yaml_output)
|
|
129
|
+
try:
|
|
130
|
+
leases = resource_rows(
|
|
131
|
+
request_configured_api(SpecificRequest(
|
|
132
|
+
method=HTTPMethod.GET,
|
|
133
|
+
endpoint="users/me/leases",
|
|
134
|
+
)),
|
|
135
|
+
"lease",
|
|
136
|
+
)
|
|
137
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
138
|
+
handle_api_error(error)
|
|
139
|
+
return
|
|
140
|
+
|
|
141
|
+
if not leases and not (json_output or yaml_output):
|
|
142
|
+
typer.echo("No leases found.")
|
|
143
|
+
return
|
|
144
|
+
_display_leases(leases, json_output=json_output, yaml_output=yaml_output)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@app.command("get")
|
|
148
|
+
def get_lease(
|
|
149
|
+
lease_id: int = typer.Option(..., "--id", help="Lease ID to retrieve."),
|
|
150
|
+
json_output: bool = typer.Option(False, "--json", help="Output the lease as JSON."),
|
|
151
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output the lease as YAML."),
|
|
152
|
+
) -> None:
|
|
153
|
+
"""Retrieve an owned lease by ID.
|
|
154
|
+
|
|
155
|
+
:param lease_id: The ID of the lease to retrieve.
|
|
156
|
+
:type lease_id: int
|
|
157
|
+
:param json_output: Whether to output as JSON.
|
|
158
|
+
:type json_output: bool
|
|
159
|
+
:param yaml_output: Whether to output as YAML.
|
|
160
|
+
:type yaml_output: bool
|
|
161
|
+
"""
|
|
162
|
+
validate_output_options(json_output, yaml_output)
|
|
163
|
+
try:
|
|
164
|
+
lease = resource_object(
|
|
165
|
+
request_configured_api(SpecificRequest(
|
|
166
|
+
method=HTTPMethod.GET,
|
|
167
|
+
endpoint=f"users/me/leases/{lease_id}",
|
|
168
|
+
)),
|
|
169
|
+
"lease",
|
|
170
|
+
)
|
|
171
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
172
|
+
handle_api_error(error)
|
|
173
|
+
return
|
|
174
|
+
_display_lease(lease, json_output=json_output, yaml_output=yaml_output)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
@app.command("create")
|
|
178
|
+
def create_lease(
|
|
179
|
+
file_path: Path = typer.Option(..., "--file", help="JSON or YAML lease data file."),
|
|
180
|
+
) -> None:
|
|
181
|
+
"""Create a lease from a JSON or YAML file.
|
|
182
|
+
|
|
183
|
+
:param file_path: Path to the JSON or YAML lease data file.
|
|
184
|
+
:type file_path: Path
|
|
185
|
+
"""
|
|
186
|
+
lease_data = load_data_file(file_path, "lease")
|
|
187
|
+
try:
|
|
188
|
+
lease = resource_object(
|
|
189
|
+
request_configured_api(SpecificRequest(
|
|
190
|
+
method=HTTPMethod.POST,
|
|
191
|
+
endpoint="users/me/leases",
|
|
192
|
+
json_body=lease_data,
|
|
193
|
+
timeout=PROVISIONING_REQUEST_TIMEOUT_SECONDS,
|
|
194
|
+
)),
|
|
195
|
+
"lease",
|
|
196
|
+
)
|
|
197
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
198
|
+
handle_api_error(error)
|
|
199
|
+
return
|
|
200
|
+
typer.echo(f"Lease {lease.get('id', '')} created successfully.")
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@app.command("revoke")
|
|
204
|
+
def revoke_lease(
|
|
205
|
+
lease_id: int = typer.Option(..., "--id", help="Lease ID to revoke."),
|
|
206
|
+
) -> None:
|
|
207
|
+
"""Revoke an owned lease without deleting its history.
|
|
208
|
+
|
|
209
|
+
:param lease_id: The ID of the lease to revoke.
|
|
210
|
+
:type lease_id: int
|
|
211
|
+
"""
|
|
212
|
+
if not typer.confirm(f"Revoke lease {lease_id}?"):
|
|
213
|
+
raise typer.Exit(code=1)
|
|
214
|
+
try:
|
|
215
|
+
request_configured_api(SpecificRequest(
|
|
216
|
+
method=HTTPMethod.POST,
|
|
217
|
+
endpoint=f"users/me/leases/{lease_id}/revoke",
|
|
218
|
+
timeout=PROVISIONING_REQUEST_TIMEOUT_SECONDS,
|
|
219
|
+
))
|
|
220
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
221
|
+
handle_api_error(error)
|
|
222
|
+
return
|
|
223
|
+
typer.echo(f"Lease {lease_id} revoked successfully.")
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""List and inspect profiles assigned to the authenticated user.
|
|
2
|
+
|
|
3
|
+
.. prefix:: `ttlama user profiles`
|
|
4
|
+
|
|
5
|
+
.. CLI::
|
|
6
|
+
- `list` : List the user's profiles.
|
|
7
|
+
- `get` : Display details of a specific profile.
|
|
8
|
+
- [--id] : Specify the profile ID.
|
|
9
|
+
|
|
10
|
+
.. module_author:: Rémi Verschuur <rverschuur@audit-io.fr>
|
|
11
|
+
.. version:: 1.0.0
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import typer
|
|
16
|
+
|
|
17
|
+
from .. import (
|
|
18
|
+
display_profiles,
|
|
19
|
+
display_structured_output,
|
|
20
|
+
get_server_and_token,
|
|
21
|
+
profile_rows,
|
|
22
|
+
request_user_overview,
|
|
23
|
+
validate_output_options,
|
|
24
|
+
)
|
|
25
|
+
from ...exc import APIError, AuthenticationError, AuthorizationError, handle_api_error
|
|
26
|
+
|
|
27
|
+
app = typer.Typer(
|
|
28
|
+
no_args_is_help=True,
|
|
29
|
+
help="Inspect profiles assigned to the authenticated user.",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_profiles() -> list[dict[str, object]]:
|
|
34
|
+
"""Retrieve the profiles in the authenticated user's overview."""
|
|
35
|
+
server_url, server_port, token = get_server_and_token()
|
|
36
|
+
overview = request_user_overview(server_url, server_port, token)
|
|
37
|
+
return profile_rows(overview["profiles"])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@app.command("ls")
|
|
41
|
+
def list_profiles(
|
|
42
|
+
json_output: bool = typer.Option(False, "--json", help="Output profiles as JSON."),
|
|
43
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output profiles as YAML."),
|
|
44
|
+
) -> None:
|
|
45
|
+
"""List profiles actively assigned to the current user."""
|
|
46
|
+
validate_output_options(json_output, yaml_output)
|
|
47
|
+
try:
|
|
48
|
+
profiles = _get_profiles()
|
|
49
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
50
|
+
handle_api_error(error)
|
|
51
|
+
return
|
|
52
|
+
|
|
53
|
+
if not profiles:
|
|
54
|
+
typer.echo("No profiles assigned.")
|
|
55
|
+
return
|
|
56
|
+
display_profiles(profiles, json_output=json_output, yaml_output=yaml_output)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@app.command("get")
|
|
60
|
+
def get_profile(
|
|
61
|
+
profile_id: int = typer.Option(..., "--id", help="Profile ID to retrieve."),
|
|
62
|
+
json_output: bool = typer.Option(False, "--json", help="Output the profile as JSON."),
|
|
63
|
+
yaml_output: bool = typer.Option(False, "--yaml", help="Output the profile as YAML."),
|
|
64
|
+
) -> None:
|
|
65
|
+
"""Retrieve an assigned profile by ID."""
|
|
66
|
+
validate_output_options(json_output, yaml_output)
|
|
67
|
+
try:
|
|
68
|
+
profiles = _get_profiles()
|
|
69
|
+
except (AuthorizationError, AuthenticationError, APIError) as error:
|
|
70
|
+
handle_api_error(error)
|
|
71
|
+
return
|
|
72
|
+
|
|
73
|
+
profile = next((row for row in profiles if row.get("id") == profile_id), None)
|
|
74
|
+
if profile is None:
|
|
75
|
+
typer.echo(f"Profile {profile_id} is not assigned to this user.", err=True)
|
|
76
|
+
raise typer.Exit(code=1)
|
|
77
|
+
|
|
78
|
+
if display_structured_output(
|
|
79
|
+
profile,
|
|
80
|
+
json_output=json_output,
|
|
81
|
+
yaml_output=yaml_output,
|
|
82
|
+
):
|
|
83
|
+
return
|
|
84
|
+
display_profiles([profile], json_output=False, yaml_output=False)
|