Skip to content

Commit

Permalink
[ignore] Modify Documentations for both Flow Rules modules. Modify lo…
Browse files Browse the repository at this point in the history
…gic behind updating subnets and nodes for both modules
  • Loading branch information
gmicol authored and lhercot committed May 15, 2024
1 parent c17f4ef commit e9274b8
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 150 deletions.
10 changes: 10 additions & 0 deletions plugins/module_utils/nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def sanitize_dict(dict_to_sanitize, keys=None, values=None, recursive=True, remo
return result


def sanitize_list(list_to_sanitize, keys=None, values=None, list_recursive=True, dict_recursive=True, remove_none_values=True):
result = deepcopy(list_to_sanitize)
for index, item in enumerate(list_to_sanitize):
if isinstance(item, dict):
result[index] = sanitize_dict(item, keys, values, dict_recursive, remove_none_values)
elif isinstance(item, list) and list_recursive:
result[index] = sanitize_list(item, keys, values, list_recursive, dict_recursive, remove_none_values)
return result


if PY3:

def cmp(a, b):
Expand Down
23 changes: 9 additions & 14 deletions plugins/module_utils/ndi.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,18 +568,13 @@ def create_structured_data(self, tree, file):
f.close()

def create_flow_rules_subnet_payload(self, subnets, existing_subnets):
subnets_to_add = []
subnets_to_update = []
if isinstance(subnets, list):
existing_subnet_set = {item["subnet"] for item in existing_subnets}
all_subnet_set = sorted(existing_subnet_set.union(subnets))
for subnet in all_subnet_set:
if subnet in subnets and subnet not in existing_subnet_set:
subnets_to_add.append({"subnet": subnet})
subnets_to_update.append({"subnet": subnet, "operation": "ADD"})
elif subnet not in subnets and subnet in existing_subnet_set:
subnet_id = next((existing_subnet["flowRuleAttributeUuid"] for existing_subnet in existing_subnets if existing_subnet["subnet"] == subnet))
subnets_to_update.append({"subnet": subnet, "operation": "DELETE", "flowRuleAttributeUuid": subnet_id})
else:
subnets_to_add.append({"subnet": subnet})
return subnets_to_add, subnets_to_update
existing_subnet_set = {item["subnet"] for item in existing_subnets}
all_subnet_set = sorted(existing_subnet_set.union(subnets))
for subnet in all_subnet_set:
if subnet in subnets and subnet not in existing_subnet_set:
subnets_to_update.append({"subnet": subnet, "operation": "ADD"})
elif subnet not in subnets and subnet in existing_subnet_set:
subnet_id = next((existing_subnet["flowRuleAttributeUuid"] for existing_subnet in existing_subnets if existing_subnet["subnet"] == subnet))
subnets_to_update.append({"subnet": subnet, "operation": "DELETE", "flowRuleAttributeUuid": subnet_id})
return subnets_to_update
47 changes: 22 additions & 25 deletions plugins/modules/nd_flow_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
aliases: [ fab_name, ig_name ]
site_name:
description:
- The name of the ACI Fabric.
- The name of the Assurance Entity.
type: str
aliases: [ site ]
flow_rule:
Expand All @@ -38,13 +38,13 @@
tenant:
description:
- The name of an existing Tenant.
- Once the Flow Rule is created, this cannot be modified.
- The Flow Rule Tenant cannot be modified.
type: str
aliases: [ tenant_name ]
vrf:
description:
- The name of an existing VRF under an existing I(tenant).
- Once the Flow Rule is created, This cannot be modified.
- The Flow Rule VRF cannot be modified.
type: str
aliases: [ vrf_name ]
subnets:
Expand Down Expand Up @@ -135,8 +135,8 @@
def main():
argument_spec = nd_argument_spec()
argument_spec.update(
insights_group=dict(type="str", aliases=["fab_name", "ig_name"]),
site_name=dict(type="str", aliases=["site"]),
insights_group=dict(type="str",required=True, aliases=["fab_name", "ig_name"]),
site=dict(type="str",required=True, aliases=["site_name"]),
flow_rule=dict(type="str", aliases=["flow_rule_name", "name"]), # Not required to query all objects
tenant=dict(type="str", aliases=["tenant_name"]),
vrf=dict(type="str", aliases=["vrf_name"]),
Expand All @@ -148,9 +148,8 @@ def main():
argument_spec=argument_spec,
supports_check_mode=False,
required_if=[
["state", "present", ["insights_group", "site_name", "flow_rule"]],
["state", "absent", ["insights_group", "site_name", "flow_rule"]],
["state", "query", ["insights_group", "site_name"]],
["state", "present", ["flow_rule"]],
["state", "absent", ["flow_rule"]],
],
)

Expand All @@ -159,7 +158,7 @@ def main():

state = nd.params.get("state")
insights_group = nd.params.get("insights_group")
site_name = nd.params.get("site_name")
site = nd.params.get("site")
flow_rule = nd.params.get("flow_rule")
tenant = nd.params.get("tenant")
vrf = nd.params.get("vrf")
Expand All @@ -172,9 +171,8 @@ def main():
"fabricName",
]

trigger_path = ndi.config_ig_path + "/" + ndi.flow_rules_path.format(insights_group, site_name)
flow_rules_history = ndi.query_data(trigger_path)
nd.existing = {}
path = "{0}/{1}".format(ndi.config_ig_path, ndi.flow_rules_path.format(insights_group, site))
flow_rules_history = ndi.query_data(path)
uuid = None
existing_subnets = []
for flow_rules_config in flow_rules_history:
Expand All @@ -183,19 +181,18 @@ def main():
uuid = flow_rules_config.get("uuid")
existing_subnets.extend(flow_rules_config.get("flowRuleAttributeList", []))


if state == "present":
nd.previous = nd.existing
flow_rule_config = {"name": flow_rule, "tenant": tenant, "vrf": vrf}
subnets_to_add, subnets_to_update = ndi.create_flow_rules_subnet_payload(subnets, existing_subnets)
flow_rule_config.update(flowRuleAttributeList=subnets_to_add)
if flow_rule_config != nd.previous:
method = "POST"
payload = {"flowRulesList": [flow_rule_config]}
if uuid:
method = "PUT"
trigger_path = "{0}/{1}".format(trigger_path, uuid)
payload = {"flowRuleAttributeList": subnets_to_update}
resp = nd.request(trigger_path, method=method, prefix=ndi.prefix, data=payload)
if uuid:
if isinstance(subnets, list) and [item["subnet"] for item in existing_subnets] != subnets:
payload = {"flowRuleAttributeList": ndi.create_flow_rules_subnet_payload(subnets, existing_subnets)}
resp = nd.request("{0}/{1}".format(path, uuid), method="PUT", prefix=ndi.prefix, data=payload)
nd.existing = sanitize_dict(resp.get("value", {}).get("data", [])[0], delete_keys)
else:
subnets_to_add = [{"subnet": subnet} for subnet in subnets] if isinstance(subnets, list) else []
payload = {"flowRulesList": [{"name": flow_rule, "tenant": tenant, "vrf": vrf, "flowRuleAttributeList": subnets_to_add}]}
resp = nd.request(path, method="POST", prefix=ndi.prefix, data=payload)
nd.existing = sanitize_dict(resp.get("value", {}).get("data", [])[0], delete_keys)

elif state == "query":
Expand All @@ -204,8 +201,8 @@ def main():

elif state == "absent":
nd.previous = nd.existing
trigger_path = "{0}/{1}".format(trigger_path, uuid)
resp = nd.request(trigger_path, method="DELETE", prefix=ndi.prefix)
path = "{0}/{1}".format(path, uuid)
resp = nd.request(path, method="DELETE", prefix=ndi.prefix)
nd.existing = {}

nd.exit_json()
Expand Down
Loading

0 comments on commit e9274b8

Please sign in to comment.