summaryrefslogtreecommitdiff
path: root/plugins/module_utils/vyos.py
diff options
context:
space:
mode:
authoromnom62 <omnom62@outlook.com>2026-08-23 08:39:18 +1000
committeromnom62 <omnom62@outlook.com>2026-08-23 08:39:18 +1000
commitef3ded9ace9d7c650e15104698acdf629c57f797 (patch)
tree0c06e3e6d565f0295b2a3bb7c0778c8da5a27889 /plugins/module_utils/vyos.py
parentff5224728484eb4f3bbf6ae52fe0b79f64959660 (diff)
downloadrest.vyos-ef3ded9ace9d7c650e15104698acdf629c57f797.tar.gz
rest.vyos-ef3ded9ace9d7c650e15104698acdf629c57f797.zip
T8989: vyos_hostname rework
Diffstat (limited to 'plugins/module_utils/vyos.py')
-rw-r--r--plugins/module_utils/vyos.py102
1 files changed, 22 insertions, 80 deletions
diff --git a/plugins/module_utils/vyos.py b/plugins/module_utils/vyos.py
index a37a0c2..bdbc2c4 100644
--- a/plugins/module_utils/vyos.py
+++ b/plugins/module_utils/vyos.py
@@ -19,85 +19,6 @@ from ansible_collections.vyos.rest.plugins.module_utils.vyos_rest import (
# ---------------------------------------------------------------------------
-# Legacy dynamic config utilities (used by Wave 1-3 modules)
-# ---------------------------------------------------------------------------
-
-
-def _kebab_to_snake(s):
- """Convert kebab-case string to snake_case."""
- return s.replace("-", "_")
-
-
-def _snake_to_kebab(s):
- """Convert snake_case string to kebab-case."""
- return s.replace("_", "-")
-
-
-def normalize(raw):
- """Recursively normalize an API response dict to snake_case keys."""
- if isinstance(raw, dict):
- return {_kebab_to_snake(k): normalize(v) for k, v in raw.items()}
- if isinstance(raw, list):
- return [normalize(v) for v in raw]
- return raw
-
-
-def denormalize_path(path):
- """Convert a snake_case path list to kebab-case for the API."""
- return [_snake_to_kebab(p) for p in path]
-
-
-def _diff_value(want_val, have_val, path, cmds, delete_missing):
- if isinstance(want_val, dict):
- if not want_val:
- if have_val is None:
- cmds.append(("set", denormalize_path(path)))
- else:
- have_dict = have_val if isinstance(have_val, dict) else {}
- _diff_dict(want_val, have_dict, path, cmds, delete_missing)
- elif isinstance(want_val, list):
- have_set = set(have_val) if isinstance(have_val, list) else set()
- for item in want_val:
- if item not in have_set:
- cmds.append(("set", denormalize_path(path + [str(item)])))
- if delete_missing:
- want_set = set(str(i) for i in want_val)
- for item in have_val or []:
- if str(item) not in want_set:
- cmds.append(("delete", denormalize_path(path + [str(item)])))
- else:
- if want_val != have_val:
- cmds.append(("set", denormalize_path(path + [str(want_val)])))
-
-
-def _diff_dict(want, have, path, cmds, delete_missing):
- for key, want_val in want.items():
- _diff_value(want_val, have.get(key), path + [key], cmds, delete_missing)
- if delete_missing:
- for key in have:
- if key not in want:
- cmds.append(("delete", denormalize_path(path + [key])))
-
-
-def diff_configs(want, have, base_path, delete_missing=False):
- """Diff two normalized config dicts and return API command tuples.
-
- Args:
- want (dict): Desired configuration (snake_case keys).
- have (dict): Current configuration (snake_case keys).
- base_path (list): Base API path for commands.
- delete_missing (bool): Generate delete commands for keys in
- ``have`` absent from ``want``.
-
- Returns:
- list: Tuples of ``("set", path)`` or ``("delete", path)``.
- """
- cmds = []
- _diff_dict(want, have, base_path, cmds, delete_missing)
- return cmds
-
-
-# ---------------------------------------------------------------------------
# Generic dict diff engine (used by Wave 4+ modules)
#
# Design principles:
@@ -229,7 +150,10 @@ def cast_by_spec(entry, options):
continue
spec_type = spec.get("type")
if spec_type == "int":
- entry[key] = int(entry[key])
+ val = entry[key]
+ if isinstance(val, list):
+ val = val[0] if val else None
+ entry[key] = int(val) if val is not None else None
elif spec_type == "dict":
cast_by_spec(entry[key], spec.get("options"))
elif spec_type == "list":
@@ -380,6 +304,24 @@ class VyOSModule:
except VyOSRestError:
return {}
+ def get_value(self, path):
+ """Retrieve a single scalar leaf value at *path*.
+
+ Uses VyOS's dedicated "returnValue" retrieve operation --
+ genuinely distinct from get_config's "showConfig" operation,
+ which returns a config subtree rather than a single value.
+ Appropriate for a plain leafNode (e.g. "system host-name"),
+ not a container.
+
+ Errors genuinely propagate rather than being swallowed into a
+ misleading "absent" result: a transient failure here must not
+ be indistinguishable from the value legitimately being unset,
+ since a module could otherwise decide to overwrite a value
+ that's actually already correct.
+ """
+ result = self._client.retrieve_return_value(path)
+ return result.get("data") or ""
+
def apply_commands(self, commands):
if not commands:
return []