1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-03 23:51:48 +00:00

chore: allow checking if a param was defined by the user (#762)

##### SUMMARY

Use `self.module.param_is_defined("key")` to check if the parameter was
defined by the user, useful when null values have a meaning.
This commit is contained in:
Jonas L. 2025-12-10 12:28:19 +01:00 committed by GitHub
parent 095fa8a2e0
commit ea973be048
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
@ -26,9 +27,25 @@ from .vendor.hcloud.actions import ActionException
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 +53,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__}")