Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions plugins/module_utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,12 @@
ND_REST_KEYS_TO_SANITIZE = ["metadata"]

ND_SETUP_NODE_DEPLOYMENT_TYPE = {"physical": "cimc", "virtual": "vnode"}

USER_ROLES_MAPPING = {
"fabric_admin": "fabric-admin",
"observer": "observer",
"super_admin": "super-admin",
"support_engineer": "support-engineer",
"approver": "approver",
"designer": "designer",
}
74 changes: 23 additions & 51 deletions plugins/module_utils/nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from ansible.module_utils.basic import json
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.six import PY3
from ansible.module_utils.six.moves import filterfalse
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.connection import Connection
Expand Down Expand Up @@ -73,53 +72,27 @@ def cmp(a, b):


def issubset(subset, superset):
"""Recurse through nested dictionary and compare entries"""
"""Recurse through a nested dictionary and check if it is a subset of another."""

# Both objects are the same object
if subset is superset:
return True

# Both objects are identical
if subset == superset:
return True

# Both objects have a different type
if isinstance(subset) is not isinstance(superset):
if type(subset) is not type(superset):
return False

if not isinstance(subset, dict):
if isinstance(subset, list):
return all(item in superset for item in subset)
return subset == superset

for key, value in subset.items():
# Ignore empty values
if value is None:
return True
continue

# Item from subset is missing from superset
if key not in superset:
return False

# Item has different types in subset and superset
if isinstance(superset.get(key)) is not isinstance(value):
return False
superset_value = superset.get(key)

# Compare if item values are subset
if isinstance(value, dict):
if not issubset(superset.get(key), value):
return False
elif isinstance(value, list):
try:
# NOTE: Fails for lists of dicts
if not set(value) <= set(superset.get(key)):
return False
except TypeError:
# Fall back to exact comparison for lists of dicts
diff = list(filterfalse(lambda i: i in value, superset.get(key))) + list(filterfalse(lambda j: j in superset.get(key), value))
if diff:
return False
elif isinstance(value, set):
if not value <= superset.get(key):
return False
else:
if not value == superset.get(key):
return False
if not issubset(value, superset_value):
return False

return True

Expand Down Expand Up @@ -252,7 +225,7 @@ def request(
if file is not None:
info = conn.send_file_request(method, uri, file, data, None, file_key, file_ext)
else:
if data:
if data is not None:
info = conn.send_request(method, uri, json.dumps(data))
else:
info = conn.send_request(method, uri)
Expand Down Expand Up @@ -310,6 +283,8 @@ def request(
self.fail_json(msg="ND Error: {0}".format(self.error.get("message")), data=data, info=info)
self.error = payload
if "code" in payload:
if self.status == 404 and ignore_not_found_error:
return {}
self.fail_json(msg="ND Error {code}: {message}".format(**payload), data=data, info=info, payload=payload)
elif "messages" in payload and len(payload.get("messages")) > 0:
self.fail_json(msg="ND Error {code} ({severity}): {message}".format(**payload["messages"][0]), data=data, info=info, payload=payload)
Expand Down Expand Up @@ -506,30 +481,27 @@ def get_diff(self, unwanted=None):
if not self.existing and self.sent:
return True

existing = self.existing
sent = self.sent
existing = deepcopy(self.existing)
sent = deepcopy(self.sent)

for key in unwanted:
if isinstance(key, str):
if key in existing:
try:
del existing[key]
except KeyError:
pass
try:
del sent[key]
except KeyError:
pass
del existing[key]
if key in sent:
del sent[key]
elif isinstance(key, list):
key_path, last = key[:-1], key[-1]
try:
existing_parent = reduce(dict.get, key_path, existing)
del existing_parent[last]
if existing_parent is not None:
del existing_parent[last]
except KeyError:
pass
try:
sent_parent = reduce(dict.get, key_path, sent)
del sent_parent[last]
if sent_parent is not None:
del sent_parent[last]
except KeyError:
pass
return not issubset(sent, existing)
Expand Down
Loading
Loading