From b398ca0911e4add9f54dd90ef7d5ed1a4268d29e Mon Sep 17 00:00:00 2001 From: Peder Hovdan Andresen <107681714+pederhan@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:48:35 +0100 Subject: [PATCH] Add `update_host` command (#242) --- CHANGELOG | 1 + zabbix_cli/commands/host.py | 42 +++++++++++++++++++++++++++++++++++ zabbix_cli/output/console.py | 3 +-- zabbix_cli/pyzabbix/client.py | 19 ++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b28a9c48..24452637 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 ` 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 diff --git a/zabbix_cli/commands/host.py b/zabbix_cli/commands/host.py index 609b223f..6f2f35a9 100644 --- a/zabbix_cli/commands/host.py +++ b/zabbix_cli/commands/host.py @@ -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, diff --git a/zabbix_cli/output/console.py b/zabbix_cli/output/console.py index ee102ebe..90ecc361 100644 --- a/zabbix_cli/output/console.py +++ b/zabbix_cli/output/console.py @@ -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: diff --git a/zabbix_cli/pyzabbix/client.py b/zabbix_cli/pyzabbix/client.py index c16f9c16..2d24233c 100644 --- a/zabbix_cli/pyzabbix/client.py +++ b/zabbix_cli/pyzabbix/client.py @@ -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: