Skip to content

Commit 6e66bbf

Browse files
authored
feat: Add create_notification_user --dryrun (#294)
1 parent a0ee14b commit 6e66bbf

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

CHANGELOG

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
<!-- released start -->
99

10-
<!-- ## [Unreleased] -->
10+
## [Unreleased]
11+
12+
### Added
13+
14+
- `create_notification_user --dryrun` option to preview changes without making them.
15+
16+
### Changed
17+
18+
- `create_notification_user` now always shows a table of the user that has been created, along with its media types.
1119

1220
## [3.5.1](https://github.com/unioslo/zabbix-cli/releases/tag/3.5.1) - 2025-02-03
1321

zabbix_cli/commands/results/user.py

+35
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,38 @@
44
# most of the result rendering was implemented in the Pyzabbix models
55
# themselves instead of as result classes. That is why this module is
66
# (mostly) empty.
7+
from __future__ import annotations
8+
9+
from zabbix_cli.models import AggregateResult
10+
from zabbix_cli.models import ColsRowsType
11+
from zabbix_cli.models import ColsType
12+
from zabbix_cli.models import RowsType
13+
from zabbix_cli.models import TableRenderable
14+
from zabbix_cli.pyzabbix.types import Usergroup
15+
from zabbix_cli.pyzabbix.types import UserMedia
16+
17+
18+
class CreateNotificationUserResult(TableRenderable):
19+
"""Result type for creating a notification user."""
20+
21+
username: str
22+
userid: str
23+
media: list[UserMedia]
24+
usergroups: list[Usergroup]
25+
26+
def __cols_rows__(self) -> ColsRowsType:
27+
cols: ColsType = [
28+
"User ID",
29+
"Username",
30+
"Media",
31+
"Usergroups",
32+
]
33+
rows: RowsType = [
34+
[
35+
self.userid,
36+
self.username,
37+
AggregateResult(result=self.media).as_table(),
38+
"\n".join([ug.name for ug in self.usergroups]),
39+
]
40+
]
41+
return cols, rows

zabbix_cli/commands/user.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from zabbix_cli.app import app
1515
from zabbix_cli.exceptions import ZabbixNotFoundError
1616
from zabbix_cli.output.console import exit_err
17+
from zabbix_cli.output.console import info
18+
from zabbix_cli.output.console import success
1719
from zabbix_cli.output.console import warning
1820
from zabbix_cli.output.prompts import str_prompt
1921
from zabbix_cli.output.render import render_result
@@ -169,6 +171,11 @@ def create_notification_user(
169171
"--usergroups",
170172
help="Comma-separated list of usergroups to add the user to. Overrides user groups in config file.",
171173
),
174+
dryrun: bool = typer.Option(
175+
False,
176+
"--dryrun",
177+
help="Do not create the user, just show what would be done.",
178+
),
172179
# Legacy V2 args
173180
args: Optional[list[str]] = ARGS_POSITIONAL,
174181
) -> None:
@@ -191,8 +198,7 @@ def create_notification_user(
191198
Adds the user to the default user group and default notification user group
192199
unless [option]--usergroups[/option] is specified.
193200
"""
194-
from zabbix_cli.models import Result
195-
from zabbix_cli.pyzabbix.types import User
201+
from zabbix_cli.commands.results.user import CreateNotificationUserResult
196202
from zabbix_cli.pyzabbix.types import UserMedia
197203

198204
if args:
@@ -233,6 +239,7 @@ def create_notification_user(
233239
ug_list: list[str] = []
234240
ug_list.extend(app.state.config.app.default_notification_users_usergroups)
235241
ug_list.extend(app.state.config.app.default_create_user_usergroups)
242+
236243
with app.status("Fetching user group(s)..."):
237244
ugroups = [app.state.client.get_usergroup(ug) for ug in set(ug_list)]
238245

@@ -246,6 +253,14 @@ def create_notification_user(
246253
)
247254
]
248255

256+
result = CreateNotificationUserResult(
257+
username=username, media=user_media, userid="", usergroups=ugroups
258+
)
259+
if dryrun:
260+
info("Would create:")
261+
render_result(result)
262+
return
263+
249264
with app.status("Creating user..."):
250265
userid = app.state.client.create_user(
251266
username=username,
@@ -257,12 +272,10 @@ def create_notification_user(
257272
media=user_media,
258273
)
259274

260-
render_result(
261-
Result(
262-
message=f"Created notification user {username!r} ({userid}).",
263-
result=User(userid=userid, username=username),
264-
),
265-
)
275+
result.userid = userid
276+
277+
success(f"Created notification user {username!r} ({userid}).")
278+
render_result(result)
266279

267280

268281
@app.command("remove_user", rich_help_panel=HELP_PANEL)

0 commit comments

Comments
 (0)