From e449bea695112ccb304c7ec2b1c86194db3de9a3 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 10 Dec 2025 12:21:32 +0100 Subject: [PATCH] chore: allow checking if a param was defined by the user Use `self.module.param_is_defined("key")` to check if the parameter was defined by the user, useful when null values have a meaning. --- plugins/module_utils/hcloud.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/plugins/module_utils/hcloud.py b/plugins/module_utils/hcloud.py index 9332cb0..d205eb8 100644 --- a/plugins/module_utils/hcloud.py +++ b/plugins/module_utils/hcloud.py @@ -6,6 +6,7 @@ from __future__ import annotations import traceback +from copy import deepcopy from typing import Any, NoReturn from ansible.module_utils.basic import AnsibleModule as AnsibleModuleBase, env_fallback @@ -29,6 +30,23 @@ from .version import version # Provide typing definitions to the AnsibleModule class class AnsibleModule(AnsibleModuleBase): params: dict + params_raw: dict + + def _load_params(self): + """ + Copy the params before validation, to keep track whether a value was defined by the user. + + Validation will modify the params dict by adding missing keys. + """ + # https://github.com/ansible/ansible/blob/7b4d4ed672415f31689e7f25bc0b40c0697c0c88/lib/ansible/module_utils/basic.py#L1244-L1251 + super()._load_params() + self.params_raw = deepcopy(self.params) + + def param_is_defined(self, key: str): + """ + Check if a parameter was defined by the user. + """ + return key in self.params_raw class AnsibleHCloud: @@ -36,6 +54,8 @@ class AnsibleHCloud: module: AnsibleModule + client: Client + def __init__(self, module: AnsibleModule): if not self.represent: raise NotImplementedError(f"represent property is not defined for {self.__class__.__name__}")