summaryrefslogtreecommitdiff
path: root/plugins/module_utils/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/module_utils/utils.py')
-rw-r--r--plugins/module_utils/utils.py64
1 files changed, 41 insertions, 23 deletions
diff --git a/plugins/module_utils/utils.py b/plugins/module_utils/utils.py
index f8a738f..b54bd95 100644
--- a/plugins/module_utils/utils.py
+++ b/plugins/module_utils/utils.py
@@ -1,40 +1,58 @@
+"""
+Shared utility functions for the vyos.rest collection.
+"""
+
+from __future__ import absolute_import, division, print_function
+
+
+__metaclass__ = type
+
+
def normalize_to_list(value):
- """
- Normalize VyOS REST return values to list.
+ """Coerce any VyOS config value into a flat Python list.
- Handles:
- dict -> keys
- list -> same
- str -> [value]
- None -> []
- """
- if isinstance(value, dict):
- return list(value.keys())
+ VyOS returns config data in inconsistent shapes depending on how many
+ values are present:
+ - ``None`` -> no config exists -> ``[]``
+ - ``{}`` -> empty dict (node exists) -> ``[]``
+ - ``"10.0.0.1"`` -> single string value -> ``["10.0.0.1"]``
+ - ``["a", "b"]`` -> already a list -> ``["a", "b"]``
+ - ``{"a": {}, "b": {}}`` -> dict of leaf nodes -> ``["a", "b"]``
- if isinstance(value, list):
- return value
+ Args:
+ value: Raw value from a VyOS ``showConfig`` response.
+ Returns:
+ list: Flat list of string values.
+ """
+ if value is None:
+ return []
+ if isinstance(value, list):
+ return [str(v) for v in value]
if isinstance(value, str):
return [value]
-
- return []
+ if isinstance(value, dict):
+ # Keys are the values; dict values are sub-config (ignored here)
+ return [str(k) for k in value.keys()]
+ # Fallback: try to convert
+ return [str(value)]
def normalize_to_dict(value):
- """
- Normalize VyOS REST return values to dict.
+ """Coerce a VyOS multi-value node into a dict keyed by value.
- list -> {item: {}}
- str -> {value: {}}
- dict -> same
+ Args:
+ value: Raw value from showConfig (None, str, list, or dict).
+
+ Returns:
+ dict: ``{value: sub_config}`` pairs.
"""
+ if not value:
+ return {}
if isinstance(value, dict):
return value
-
if isinstance(value, list):
- return {v: {} for v in value}
-
+ return {str(v): {} for v in value}
if isinstance(value, str):
return {value: {}}
-
return {}