Skip to content

Commit 43867e0

Browse files
author
=
committed
Handle Nested JSON Response Data
1 parent 3e5a5b2 commit 43867e0

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

plugins/module_utils/network/dcnm/dcnm.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,64 @@ def find_dict_in_list_by_key_value(search: list, key: str, value: str):
859859
"""
860860
match = (d for d in search if d[key] == value)
861861
return next(match, None)
862+
863+
864+
def search_nested_json(obj, search_string):
865+
"""
866+
# Summary
867+
868+
Recursively flattens a nested dictionary or list and searches all values
869+
for the given search_string.
870+
871+
## Raises
872+
873+
None
874+
875+
## Parameters
876+
877+
- obj (dict or list): The dictionary or list to flatten.
878+
- search_string (string): string to search in the values.
879+
880+
## Returns
881+
882+
true or false, based on the presence of the search
883+
string in the nested json values.
884+
885+
## Usage
886+
887+
```python
888+
content = {
889+
"key1": "value1",
890+
"key2": {
891+
"subkey1": "subvalue1",
892+
"subkey2": ["item1", "item2", "search_string"],
893+
},
894+
"key3": ["item3", {"subkey3": "search_string"}],
895+
}
896+
search_string = "search_string"
897+
result = search_nested_json(content, search_string)
898+
print(result)
899+
# -> True
900+
901+
search_string = "not_found"
902+
result = search_nested_json(content, search_string)
903+
print(result)
904+
# -> False
905+
```
906+
907+
"""
908+
if isinstance(obj, dict):
909+
for k, v in obj.items():
910+
if isinstance(v, (dict, list)):
911+
return search_nested_json(v,search_string)
912+
else:
913+
if isinstance(v, (str)) and search_string in v.lower():
914+
return True
915+
elif isinstance(obj, list):
916+
for item in obj:
917+
if isinstance(item, (dict, list)):
918+
return search_nested_json(item, search_string)
919+
else:
920+
if isinstance(item, (str)) and search_string in item.lower():
921+
return True
922+
return False

plugins/modules/dcnm_vrf.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@
572572
from ansible_collections.cisco.dcnm.plugins.module_utils.network.dcnm.dcnm import (
573573
dcnm_get_ip_addr_info, dcnm_get_url, dcnm_send, dcnm_version_supported,
574574
get_fabric_details, get_fabric_inventory_details, get_ip_sn_dict,
575-
get_sn_fabric_dict, validate_list_of_dicts)
575+
get_sn_fabric_dict, validate_list_of_dicts, search_nested_json)
576576

577577
from ..module_utils.common.log_v2 import Log
578578

@@ -4136,19 +4136,10 @@ def handle_response(self, res, op):
41364136
fail = True
41374137
changed = False
41384138
if res.get("DATA"):
4139-
if isinstance(res.get("DATA"), list):
4140-
for resp_data_item in res.get("DATA"):
4141-
for resp_string in resp_data_item.values():
4142-
if "fail" in resp_string.lower():
4143-
fail = True
4144-
changed = False
4145-
return fail, changed
4146-
elif isinstance(res.get("DATA"), dict):
4147-
for resp_string in res.get("DATA").values():
4148-
if "fail" in resp_string.lower():
4149-
fail = True
4150-
changed = False
4151-
return fail, changed
4139+
resp_val = search_nested_json(res.get("DATA"), "fail")
4140+
if resp_val:
4141+
fail = True
4142+
changed = False
41524143
if op == "attach" and "is in use already" in str(res.values()):
41534144
fail = True
41544145
changed = False

0 commit comments

Comments
 (0)