Skip to content

Commit bd96b2a

Browse files
anvitha-jainlhercot
authored andcommitted
[ignore_changes] Updated module to get name from UUID changes.
1 parent 3d2eb06 commit bd96b2a

File tree

3 files changed

+138
-60
lines changed

3 files changed

+138
-60
lines changed

plugins/module_utils/utils.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,16 @@ def check_if_all_elements_are_none(values):
148148
return all(value is None for value in values)
149149

150150

151-
def get_object_identifier(uuid, name):
151+
def get_template_object_name_by_uuid(mso, object_type, uuid):
152152
"""
153-
Retrieve the object identifier based on the input values
154-
:param uuid: UUID of the object -> Str
155-
:param name: Name of the object -> Str
156-
:return: Dict: The processed result which could be:
157-
When the UUID is not None, returns dict object with UUID -> Dict
158-
When the name is not None, returns dict object with name -> Dict
159-
When the UUID and name is None, returns empty dict -> Dict
153+
Retrieve the name of a specific object type in the MSO template using its UUID.
154+
:param mso: An instance of the MSO class, which provides methods for making API requests -> MSO Class instance
155+
:param object_type: The type of the object to retrieve the name for -> Str
156+
:param uuid: The UUID of the object to retrieve the name for -> Str
157+
:return: Str | None: The processed result which could be:
158+
When the UUID is existing, returns object name -> Str
159+
When the UUID is not existing -> None
160160
"""
161-
return {"uuid": uuid} if uuid is not None else ({"name": name} if name is not None else {})
161+
response_object = mso.request("templates/objects?type={0}&uuid={1}".format(object_type, uuid), "GET")
162+
if response_object:
163+
return response_object.get("name")

plugins/modules/ndo_tenant_igmp_interface_policy.py

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,56 +129,59 @@
129129
state_limit_route_map_uuid:
130130
description:
131131
- The UUID of the state limit route map.
132+
- Providing an empty string will remove the O(state_limit_route_map_uuid="") from IGMP Interface Policy.
132133
type: str
133134
aliases: [ state_limit_route_map_for_multicast ]
134135
state_limit_route_map:
135136
description:
136137
- The Route Map Policy for Multicast.
137138
- This parameter can be used instead of O(state_limit_route_map_uuid).
138139
- If both parameter are used, O(state_limit_route_map) will be ignored.
140+
- Providing an empty map will remove the O(state_limit_route_map={}) from IGMP Interface Policy.
139141
type: dict
140142
suboptions:
141143
name:
142144
description:
143145
- The name of the Route Map Policy for Multicast.
144146
type: str
145-
required: true
146147
aliases: [ state_limit_route_map_policy, state_limit_route_map_policy_multicast ]
147148
report_policy_route_map_uuid:
148149
description:
149150
- The UUID of the report policy route map.
151+
- Providing an empty string will remove the O(report_policy_route_map_uuid="") from IGMP Interface Policy.
150152
type: str
151153
aliases: [ report_policy_route_map_for_multicast ]
152154
report_policy_route_map:
153155
description:
154156
- The Route Map Policy for Multicast.
155157
- This parameter can be used instead of O(report_policy_route_map_uuid).
156158
- If both parameter are used, O(report_policy_route_map) will be ignored.
159+
- Providing an empty map will remove the O(report_policy_route_map={}) from IGMP Interface Policy.
157160
type: dict
158161
suboptions:
159162
name:
160163
description:
161164
- The name of the Route Map Policy for Multicast.
162165
type: str
163-
required: true
164166
aliases: [ report_policy_route_map_policy, report_policy_route_map_policy_multicast ]
165167
static_report_route_map_uuid:
166168
description:
167169
- The UUID of the static report route map.
170+
- Providing an empty string will remove the O(static_report_route_map_uuid="") from IGMP Interface Policy.
168171
type: str
169172
aliases: [ static_report_route_map_for_multicast ]
170173
static_report_route_map:
171174
description:
172175
- The Route Map Policy for Multicast.
173176
- This parameter can be used instead of O(static_report_route_map_uuid).
174177
- If both parameter are used, O(static_report_route_map) will be ignored.
178+
- Providing an empty map will remove the O(static_report_route_map={}) from IGMP Interface Policy.
175179
type: dict
176180
suboptions:
177181
name:
178182
description:
179183
- The name of the Route Map Policy for Multicast.
180184
type: str
181-
required: true
182185
aliases: [ static_report_route_map_policy, static_report_route_map_policy_multicast ]
183186
maximum_multicast_entries:
184187
description:
@@ -311,7 +314,8 @@
311314
from ansible_collections.cisco.mso.plugins.module_utils.constants import ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP
312315
from ansible_collections.cisco.mso.plugins.module_utils.utils import (
313316
append_update_ops_data,
314-
get_object_identifier,
317+
check_if_all_elements_are_none,
318+
get_template_object_name_by_uuid,
315319
)
316320

317321

@@ -340,23 +344,23 @@ def main():
340344
state_limit_route_map=dict(
341345
type="dict",
342346
options=dict(
343-
name=dict(type="str", required=True),
347+
name=dict(type="str"),
344348
),
345349
aliases=["state_limit_route_map_policy", "state_limit_route_map_policy_multicast"],
346350
),
347351
report_policy_route_map_uuid=dict(type="str", aliases=["report_policy_route_map_for_multicast"]),
348352
report_policy_route_map=dict(
349353
type="dict",
350354
options=dict(
351-
name=dict(type="str", required=True),
355+
name=dict(type="str"),
352356
),
353357
aliases=["report_policy_route_map_policy", "report_policy_route_map_policy_multicast"],
354358
),
355359
static_report_route_map_uuid=dict(type="str", aliases=["static_report_route_map_for_multicast"]),
356360
static_report_route_map=dict(
357361
type="dict",
358362
options=dict(
359-
name=dict(type="str", required=True),
363+
name=dict(type="str"),
360364
),
361365
aliases=["static_report_route_map_policy", "static_report_route_map_policy_multicast"],
362366
),
@@ -385,7 +389,8 @@ def main():
385389

386390
mso = MSOModule(module)
387391

388-
template_identifier = get_object_identifier(module.params.get("template_id"), module.params.get("template"))
392+
template = module.params.get("template")
393+
template_id = module.params.get("template_id")
389394
name = module.params.get("name")
390395
uuid = module.params.get("uuid")
391396
description = module.params.get("description")
@@ -412,38 +417,35 @@ def main():
412417
reserved_multicast_entries = module.params.get("reserved_multicast_entries")
413418
state = module.params.get("state")
414419

415-
mso_template = MSOTemplate(mso, "tenant", template_identifier.get("name"), template_identifier.get("uuid"))
420+
mso_template = MSOTemplate(mso, "tenant", template, template_id)
416421
mso_template.validate_template("tenantPolicy")
417422
ops = []
423+
empty_state_limit_route_map = False
424+
empty_report_policy_route_map = False
425+
empty_static_report_route_map = False
418426

419427
existing_igmp_interface_policies = mso_template.template.get("tenantPolicyTemplate", {}).get("template", {}).get("igmpInterfacePolicies", [])
420428
object_description = "IGMP Interface Policy"
429+
path = "/tenantPolicyTemplate/template/igmpInterfacePolicies"
421430

422-
if name or uuid:
431+
if uuid or name:
423432
match = mso_template.get_object_by_key_value_pairs(
424433
object_description,
425434
existing_igmp_interface_policies,
426435
[KVPair("uuid", uuid) if uuid else KVPair("name", name)],
427436
)
428437
if match:
429-
igmp_interface_policy_attrs_path = "/tenantPolicyTemplate/template/igmpInterfacePolicies/{0}".format(match.index)
438+
igmp_interface_policy_attrs_path = "{0}/{1}".format(path, match.index)
439+
set_names_for_references(mso, match.details)
430440
mso.existing = mso.previous = copy.deepcopy(match.details)
431441
else:
432-
mso.existing = mso.previous = existing_igmp_interface_policies
442+
mso.existing = mso.previous = [set_names_for_references(mso, igmp_interface_policy) for igmp_interface_policy in existing_igmp_interface_policies]
443+
# existing_igmp_interface_policies
433444

434445
if state == "present":
435446
if uuid and not mso.existing:
436447
mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
437448

438-
if state_limit_route_map and not state_limit_route_map_uuid:
439-
state_limit_route_map_uuid = mso_template.get_route_map_policy_for_multicast_uuid(state_limit_route_map.get("name"))
440-
441-
if report_policy_route_map and not report_policy_route_map_uuid:
442-
report_policy_route_map_uuid = mso_template.get_route_map_policy_for_multicast_uuid(report_policy_route_map.get("name"))
443-
444-
if static_report_route_map and not static_report_route_map_uuid:
445-
static_report_route_map_uuid = mso_template.get_route_map_policy_for_multicast_uuid(static_report_route_map.get("name"))
446-
447449
mso_values = dict(
448450
name=name,
449451
description=description,
@@ -460,23 +462,48 @@ def main():
460462
startQueryInterval=startup_query_interval,
461463
querierTimeout=querier_timeout,
462464
robustnessFactor=robustness_variable,
465+
maximumMulticastEntries=maximum_multicast_entries,
466+
reservedMulticastEntries=reserved_multicast_entries,
463467
stateLimitRouteMapRef=state_limit_route_map_uuid,
464468
reportPolicyRouteMapRef=report_policy_route_map_uuid,
465469
staticReportRouteMapRef=static_report_route_map_uuid,
466-
maximumMulticastEntries=maximum_multicast_entries,
467-
reservedMulticastEntries=reserved_multicast_entries,
468470
)
469471

472+
if state_limit_route_map:
473+
empty_state_limit_route_map = check_if_all_elements_are_none(list(state_limit_route_map.values()))
474+
if not empty_state_limit_route_map:
475+
mso_values["stateLimitRouteMapRef"] = mso_template.get_route_map_policy_for_multicast_uuid(state_limit_route_map.get("name"))
476+
477+
if report_policy_route_map:
478+
empty_report_policy_route_map = check_if_all_elements_are_none(list(report_policy_route_map.values()))
479+
if not empty_report_policy_route_map:
480+
mso_values["reportPolicyRouteMapRef"] = mso_template.get_route_map_policy_for_multicast_uuid(report_policy_route_map.get("name"))
481+
482+
if static_report_route_map:
483+
empty_static_report_route_map = check_if_all_elements_are_none(list(static_report_route_map.values()))
484+
if not empty_static_report_route_map:
485+
mso_values["staticReportRouteMapRef"] = mso_template.get_route_map_policy_for_multicast_uuid(static_report_route_map.get("name"))
486+
487+
if match:
488+
mso_values_remove = list()
489+
490+
if (state_limit_route_map_uuid == "" or empty_state_limit_route_map) and match.details.get("stateLimitRouteMapRef"):
491+
mso_values_remove.append("stateLimitRouteMapRef")
492+
if (report_policy_route_map_uuid == "" or empty_report_policy_route_map) and match.details.get("reportPolicyRouteMapRef"):
493+
mso_values_remove.append("reportPolicyRouteMapRef")
494+
if (static_report_route_map_uuid == "" or empty_static_report_route_map) and match.details.get("staticReportRouteMapRef"):
495+
mso_values_remove.append("staticReportRouteMapRef")
496+
470497
if mso.existing:
471-
append_update_ops_data(ops, match.details, igmp_interface_policy_attrs_path, mso_values)
498+
append_update_ops_data(ops, match.details, "{0}/{1}".format(path, match.index), mso_values, mso_values_remove)
472499
mso.sanitize(match.details, collate=True)
473500
else:
474501
mso.sanitize(mso_values)
475-
ops.append(dict(op="add", path="/tenantPolicyTemplate/template/igmpInterfacePolicies/-", value=mso.sent))
502+
ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent))
476503

477504
elif state == "absent":
478505
if mso.existing:
479-
ops.append(dict(op="remove", path=igmp_interface_policy_attrs_path))
506+
ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index)))
480507

481508
if not module.check_mode and ops:
482509
response_object = mso.request(mso_template.template_path, method="PATCH", data=ops)
@@ -485,14 +512,27 @@ def main():
485512
object_description, existing_igmp_interface_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]
486513
)
487514
if match:
515+
set_names_for_references(mso, match.details)
488516
mso.existing = match.details # When the state is present
489517
else:
490518
mso.existing = {} # When the state is absent
491519
elif module.check_mode and state != "query": # When the state is present/absent with check mode
520+
set_names_for_references(mso, mso.proposed)
492521
mso.existing = mso.proposed if state == "present" else {}
493522

494523
mso.exit_json()
495524

496525

526+
def set_names_for_references(mso, route_map_dict):
527+
if route_map_dict.get("stateLimitRouteMapRef"):
528+
route_map_dict["stateLimitRouteMapName"] = get_template_object_name_by_uuid(mso, "mcastRouteMap", route_map_dict.get("stateLimitRouteMapRef"))
529+
if route_map_dict.get("reportPolicyRouteMapRef"):
530+
route_map_dict["reportPolicyRouteMapName"] = get_template_object_name_by_uuid(mso, "mcastRouteMap", route_map_dict.get("reportPolicyRouteMapRef"))
531+
if route_map_dict.get("staticReportRouteMapRef"):
532+
route_map_dict["staticReportRouteMapName"] = get_template_object_name_by_uuid(mso, "mcastRouteMap", route_map_dict.get("staticReportRouteMapRef"))
533+
534+
return route_map_dict
535+
536+
497537
if __name__ == "__main__":
498538
main()

0 commit comments

Comments
 (0)