Skip to content

dcnm_vrf: Handling "Fail" messages in Response Data from NDFC : Fix Issue #324 #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions plugins/module_utils/network/dcnm/dcnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,64 @@ def find_dict_in_list_by_key_value(search: list, key: str, value: str):
"""
match = (d for d in search if d[key] == value)
return next(match, None)


def search_nested_json(obj, search_string):
"""
# Summary

Recursively flattens a nested dictionary or list and searches all values
for the given search_string.

## Raises

None

## Parameters

- obj (dict or list): The dictionary or list to flatten.
- search_string (string): string to search in the values.

## Returns

true or false, based on the presence of the search
string in the nested json values.

## Usage

```python
content = {
"key1": "value1",
"key2": {
"subkey1": "subvalue1",
"subkey2": ["item1", "item2", "search_string"],
},
"key3": ["item3", {"subkey3": "search_string"}],
}
search_string = "search_string"
result = search_nested_json(content, search_string)
print(result)
# -> True

search_string = "not_found"
result = search_nested_json(content, search_string)
print(result)
# -> False
```

"""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
return search_nested_json(v, search_string)
else:
if isinstance(v, (str)) and search_string in v.lower():
return True
elif isinstance(obj, list):
for item in obj:
if isinstance(item, (dict, list)):
return search_nested_json(item, search_string)
else:
if isinstance(item, (str)) and search_string in item.lower():
return True
return False
7 changes: 6 additions & 1 deletion plugins/modules/dcnm_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
from ansible_collections.cisco.dcnm.plugins.module_utils.network.dcnm.dcnm import (
dcnm_get_ip_addr_info, dcnm_get_url, dcnm_send, dcnm_version_supported,
get_fabric_details, get_fabric_inventory_details, get_ip_sn_dict,
get_sn_fabric_dict, validate_list_of_dicts)
get_sn_fabric_dict, validate_list_of_dicts, search_nested_json)

from ..module_utils.common.log_v2 import Log

Expand Down Expand Up @@ -4135,6 +4135,11 @@ def handle_response(self, res, op):
if res.get("ERROR"):
fail = True
changed = False
if res.get("DATA"):
resp_val = search_nested_json(res.get("DATA"), "fail")
if resp_val:
fail = True
changed = False
if op == "attach" and "is in use already" in str(res.values()):
fail = True
changed = False
Expand Down