Skip to content

Commit

Permalink
Add update_host command (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan authored Nov 4, 2024
1 parent 6b36b1b commit b398ca0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Application now automatically assigns deprecated config options to their new equivalents internally.
- New command `update_config` to update an outdated configuration file with new options, as well as any currently applied overrides.
- `show_config --secrets <hide|mask|plain>` option for controlling the display mode of sensitive information in the configuration file. Defaults to `mask`.
- New command `update_host` to update basic information about a host.

### Changed

Expand Down
42 changes: 42 additions & 0 deletions zabbix_cli/commands/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,48 @@ def create_host(
render_result(Result(message=f"Created host {host_name!r} ({host_id})"))


@app.command(name="update_host", rich_help_panel=HELP_PANEL)
def update_host(
ctx: typer.Context,
hostname_or_ip: str = typer.Argument(
help="Hostname or IP",
show_default=False,
),
name: Optional[str] = typer.Option(
None,
"--name",
help="Visible name of the host.",
),
description: Optional[str] = typer.Option(
None,
"--description",
help="Description of the host.",
),
) -> None:
"""Update basic information about a host.
Other notable commands to update a host:
- [command]update_host_inventory[/]
- [command]create_host_interface[/]
- [command]update_host_interface[/]
- [command]monitor_host[/]
- [command]add_host_to_hostgroup[/]
- [command]remove_host_from_hostgroup[/]
"""
from zabbix_cli.models import Result

check_at_least_one_option_set(ctx)

host = app.state.client.get_host(hostname_or_ip)
app.state.client.update_host(
host,
name=name,
description=description,
)
render_result(Result(message=f"Updated host {host}."))


@app.command(
name="create_host_interface",
rich_help_panel=HELP_PANEL,
Expand Down
3 changes: 1 addition & 2 deletions zabbix_cli/output/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def error(


def print_help(ctx: typer.Context) -> None:
console.print(ctx.command.get_help(ctx))
raise SystemExit(1)
console.print(ctx.get_help())


def exit_ok(message: Optional[str] = None, code: int = 0, **kwargs: Any) -> NoReturn:
Expand Down
19 changes: 19 additions & 0 deletions zabbix_cli/pyzabbix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,25 @@ def create_host(
)
return str(resp["hostids"][0])

def update_host(
self,
host: Host,
name: Optional[str] = None,
description: Optional[str] = None,
) -> None:
"""Updates basic information about a host."""
params: ParamsType = {
"hostid": host.hostid,
}
if name:
params["host"] = name
if description:
params["description"] = description
try:
self.host.update(**params)
except ZabbixAPIException as e:
raise ZabbixAPICallError(f"Failed to update host {host.host!r}") from e

def delete_host(self, host_id: str) -> None:
"""Deletes a host."""
try:
Expand Down

0 comments on commit b398ca0

Please sign in to comment.