Skip to content

Commit 416fa1a

Browse files
authored
dcnm_vrf: Handling "Fail" messages in Response Data from NDFC : Fix Issue #324 (#457)
* Handling "Fail" messages in Response Data from NDFC * Change Quotes from Single to Double * Handle Dict and Lists in Reponse Data * Handle Nested JSON Response Data * Fix Pep8 Error * Fix Recursive Return Conditionals * Fix Pep8 + Pylint Error --------- Co-authored-by: = <=>
1 parent dd6da07 commit 416fa1a

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

plugins/module_utils/network/dcnm/dcnm.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,3 +886,69 @@ def find_dict_in_list_by_key_value(search: list, key: str, value: str):
886886
"""
887887
match = (d for d in search if d[key] == value)
888888
return next(match, None)
889+
890+
891+
def search_nested_json(obj, search_string):
892+
"""
893+
# Summary
894+
895+
Recursively flattens a nested dictionary or list and searches all values
896+
for the given search_string.
897+
898+
## Raises
899+
900+
None
901+
902+
## Parameters
903+
904+
- obj (dict or list): The dictionary or list to flatten.
905+
- search_string (string): string to search in the values.
906+
907+
## Returns
908+
909+
true or false, based on the presence of the search
910+
string in the nested json values.
911+
912+
## Usage
913+
914+
```python
915+
content = {
916+
"key1": "value1",
917+
"key2": {
918+
"subkey1": "subvalue1",
919+
"subkey2": ["item1", "item2", "search_string"],
920+
},
921+
"key3": ["item3", {"subkey3": "search_string"}],
922+
}
923+
search_string = "search_string"
924+
result = search_nested_json(content, search_string)
925+
print(result)
926+
# -> True
927+
928+
search_string = "not_found"
929+
result = search_nested_json(content, search_string)
930+
print(result)
931+
# -> False
932+
```
933+
934+
"""
935+
if isinstance(obj, dict):
936+
for k, v in obj.items():
937+
if isinstance(v, (dict, list)):
938+
if search_nested_json(v, search_string):
939+
return True
940+
else:
941+
if isinstance(v, (str)) and search_string in v.lower():
942+
return True
943+
elif isinstance(obj, list):
944+
for item in obj:
945+
if isinstance(item, (dict, list)):
946+
if search_nested_json(item, search_string):
947+
return True
948+
else:
949+
if isinstance(item, (str)) and search_string in item.lower():
950+
return True
951+
elif isinstance(obj, str):
952+
if search_string in obj.lower():
953+
return True
954+
return False

plugins/modules/dcnm_vrf.py

Lines changed: 6 additions & 1 deletion
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

@@ -4141,6 +4141,11 @@ def handle_response(self, res, op):
41414141
if res.get("ERROR"):
41424142
fail = True
41434143
changed = False
4144+
if res.get("DATA"):
4145+
resp_val = search_nested_json(res.get("DATA"), "fail")
4146+
if resp_val:
4147+
fail = True
4148+
changed = False
41444149
if op == "attach" and "is in use already" in str(res.values()):
41454150
fail = True
41464151
changed = False

0 commit comments

Comments
 (0)