Skip to content

Commit

Permalink
Rework required using standard functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ashemedai committed May 14, 2024
1 parent 736b349 commit f9d8d0c
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions plugins/modules/zabbix_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
type: str
script_type:
description:
- Script type.
- Script type. Required when state is 'present'.
type: str
required: true
required: false
choices: ["script", "ipmi", "ssh", "telnet", "webhook"]
command:
description:
- Command to run.
- Command to run. Required when state is 'present'
type: str
required: true
required: false
scope:
description:
- Script scope.
Expand Down Expand Up @@ -137,9 +137,9 @@
suboptions:
name:
description:
- Parameter name.
- Parameter name. Required when 'parameters' is specified for a 'webhook' script.
type: str
required: true
required: false
value:
description:
- Parameter value. Supports macros.
Expand Down Expand Up @@ -292,31 +292,26 @@ def generate_script_config(self, name, script_type, command, scope, execute_on,
request["confirmation"] = confirmation

if script_type == "ssh":
if authtype is None:
self._module.fail_json(changed=False, msg="authtype must be provided for ssh script type")
request["authtype"] = str(zabbix_utils.helper_to_numeric_value([
"password",
"public_key"], authtype))
if authtype == "public_key":
if publickey is None or privatekey is None:
self._module.fail_json(changed=False, msg="publickey and privatekey must be provided for ssh script type with publickey authtype")
request["publickey"] = publickey
request["privatekey"] = privatekey

if script_type in ["ssh", "telnet"]:
if username is None:
self._module.fail_json(changed=False, msg="username must be provided for 'ssh' and 'telnet' script types")
request["username"] = username
if (script_type == "ssh" and authtype == "password") or script_type == "telnet":
if password is None:
self._module.fail_json(changed=False, msg="password must be provided for telnet script type or ssh script type with password autheype")
request["password"] = password
if port is not None:
request["port"] = port

if script_type == "webhook":
request["timeout"] = script_timeout
if parameters:
for parameter in parameters:
if "name" not in parameter.keys() or parameter["name"] is None:
self._module.fail_json(msg="When providing parameters to a webhook script, the 'name' option is required.")
request["parameters"] = parameters

return request
Expand Down Expand Up @@ -347,56 +342,59 @@ def main():
name=dict(type="str", required=True),
script_type=dict(
type="str",
required=True,
choices=["script", "ipmi", "ssh", "telnet", "webhook"]),
command=dict(type="str", required=True),
command=dict(type="str"),
scope=dict(
type="str",
required=False,
choices=["action_operation", "manual_host_action", "manual_event_action"],
default="action_operation"),
execute_on=dict(
type="str",
required=False,
choices=["zabbix_agent", "zabbix_server", "zabbix_server_proxy"],
default="zabbix_server_proxy"),
menu_path=dict(type="str", required=False),
menu_path=dict(type="str"),
authtype=dict(
type="str",
required=False,
choices=["password", "public_key"]),
username=dict(type="str", required=False),
password=dict(type="str", required=False, no_log=True),
publickey=dict(type="str", required=False),
privatekey=dict(type="str", required=False, no_log=True),
port=dict(type="str", required=False),
host_group=dict(type="str", required=False, default="all"),
user_group=dict(type="str", required=False, default="all"),
username=dict(type="str"),
password=dict(type="str", no_log=True),
publickey=dict(type="str"),
privatekey=dict(type="str", no_log=True),
port=dict(type="str"),
host_group=dict(type="str", default="all"),
user_group=dict(type="str", default="all"),
host_access=dict(
type="str",
required=False,
choices=["read", "write"],
default="read"),
confirmation=dict(type="str", required=False),
script_timeout=dict(type="str", default="30s", required=False),
confirmation=dict(type="str"),
script_timeout=dict(type="str", default="30s"),
parameters=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str", required=True),
value=dict(type="str", required=False, default="")
name=dict(type="str"),
value=dict(type="str", default="")
)
),
description=dict(type="str", required=False),
description=dict(type="str"),
state=dict(
type="str",
required=False,
default="present",
choices=["present", "absent"])
))

required_if = [
("state", "present", ("script_type", "command",)),
("script_type", "ssh", ("authtype", "username",)),
("authtype", "password", ("password",)),
("authtype", "public_key", ("publickey", "privatekey",)),
("script_type", "telnet", ("username", "password")),
]

module = AnsibleModule(
argument_spec=argument_spec,
required_if=required_if,
supports_check_mode=True
)

Expand Down

0 comments on commit f9d8d0c

Please sign in to comment.