mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +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:
parent
095fa8a2e0
commit
ea973be048
1 changed files with 20 additions and 1 deletions
|
|
@ -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__}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue