Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_route_map_policy_multicast.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: ndo_route_map_policy_multicast
17 short_description: Manage Multicast Route Map Policies on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage Multicast Route Map Policies on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Akini Ross (@akinross)
23 options:
24 template:
25 description:
26 - The name of the template.
27 - The template must be a tenant template.
28 type: str
29 required: true
30 route_map_policy:
31 description:
32 - The name of the Multicast Route Map Policy.
33 type: str
34 aliases: [ name ]
35 route_map_policy_uuid:
36 description:
37 - The uuid of the Multicast Route Map Policy.
38 - This parameter is required when the O(route_map_policy) needs to be updated.
39 type: str
40 aliases: [ uuid ]
41 description:
42 description:
43 - The description of the Multicast Route Map Policy.
44 type: str
45 entries:
46 description:
47 - A list of entries attached to the Multicast Route Map Policy.
48 - The list of configured entries must contain at least one entry.
49 - When the list of entries is null the update will not change existing entry configuration.
50 type: list
51 elements: dict
52 suboptions:
53 order:
54 description:
55 - The order of the entry.
56 type: int
57 required: true
58 group:
59 description:
60 - The name of the entry.
61 type: str
62 required: true
63 source:
64 description:
65 - The data of the entry.
66 type: str
67 required: true
68 rp:
69 description:
70 - The rendezvous point of the entry.
71 type: str
72 aliases: [ rendezvous_point ]
73 action:
74 description:
75 - The action of the entry.
76 type: str
77 choices: [ permit, deny ]
78 default: permit
79 state:
80 description:
81 - Use C(absent) for removing.
82 - Use C(query) for listing an object or multiple objects.
83 - Use C(present) for creating or updating.
84 type: str
85 choices: [ absent, query, present ]
86 default: query
87 extends_documentation_fragment: cisco.mso.modules
88 """
89
90 EXAMPLES = r"""
91 - name: Create a new multicast route map policy
92 cisco.mso.ndo_route_map_policy_multicast:
93 host: mso_host
94 username: admin
95 password: SomeSecretPassword
96 template: ansible_tenant_template
97 route_map_policy: ansible_test_route_map_policy
98 entries:
99 - order: 1
100 group: 226.2.2.0/24
101 source: 1.1.1.1/24
102 rp: 1.1.1.2
103 action: permit
104 state: present
105 register: create
106
107 - name: Query a multicast route map policy with name
108 cisco.mso.ndo_route_map_policy_multicast:
109 host: mso_host
110 username: admin
111 password: SomeSecretPassword
112 template: ansible_tenant_template
113 route_map_policy: ansible_test_route_map_policy
114 state: query
115 register: query_one
116
117 - name: Query a multicast route map policy with UUID
118 cisco.mso.ndo_route_map_policy_multicast:
119 host: mso_host
120 username: admin
121 password: SomeSecretPassword
122 template: ansible_tenant_template
123 route_map_policy_uuid: '{{ create.current.uuid }}'
124 state: query
125 register: query_with_uuid
126
127 - name: Query all multicast route map policy in the template
128 cisco.mso.ndo_route_map_policy_multicast:
129 host: mso_host
130 username: admin
131 password: SomeSecretPassword
132 template: ansible_tenant_template
133 state: query
134 register: query_all
135
136 - name: Delete a multicast route map policy
137 cisco.mso.ndo_route_map_policy_multicast:
138 host: mso_host
139 username: admin
140 password: SomeSecretPassword
141 template: ansible_tenant_template
142 route_map_policy: ansible_test_route_map_policy
143 state: absent
144 """
145
146 RETURN = r"""
147 """
148
149
150 import copy
151 from ansible.module_utils.basic import AnsibleModule
152 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
153 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
154
155
156 def main():
157 argument_spec = mso_argument_spec()
158 argument_spec.update(
159 template=dict(type="str", required=True),
160 route_map_policy=dict(type="str", aliases=["name"]),
161 route_map_policy_uuid=dict(type="str", aliases=["uuid"]),
162 description=dict(type="str"),
163 entries=dict(
164 type="list",
165 elements="dict",
166 options=dict(
167 order=dict(type="int", required=True),
168 group=dict(type="str", required=True),
169 source=dict(type="str", required=True),
170 rp=dict(type="str", aliases=["rendezvous_point"]),
171 action=dict(type="str", choices=["permit", "deny"], default="permit"),
172 ),
173 ),
174 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
175 )
176
177 module = AnsibleModule(
178 argument_spec=argument_spec,
179 supports_check_mode=True,
180 required_if=[
181 ["state", "absent", ["route_map_policy"]],
182 ["state", "present", ["route_map_policy"]],
183 ],
184 )
185
186 mso = MSOModule(module)
187
188 template = module.params.get("template")
189 route_map_policy = module.params.get("route_map_policy")
190 route_map_policy_uuid = module.params.get("route_map_policy_uuid")
191 entries = get_entries_payload(module.params.get("entries")) if module.params.get("entries") else []
192 description = module.params.get("description")
193 state = module.params.get("state")
194
195 ops = []
196 match = None
197 err_message_min_entries = "At least one entry is required when state is present."
198
199 mso_template = MSOTemplate(mso, "tenant", template)
200 mso_template.validate_template("tenantPolicy")
201
202 path = "/tenantPolicyTemplate/template/mcastRouteMapPolicies"
203 match = get_multicast_route_map_policy(mso_template, route_map_policy_uuid, route_map_policy)
204
205 if route_map_policy_uuid or route_map_policy:
206 if match: # Query a specific object
207 mso.existing = copy.deepcopy(match.details)
208 mso.previous = copy.deepcopy(match.details)
209 elif match:
210 mso.existing = match # Query all objects
211
212 if state == "present":
213 if match:
214 if module.params.get("entries") is not None and len(entries) == 0:
215 mso.fail_json(msg=err_message_min_entries)
216
217 if route_map_policy and match.details.get("name") != route_map_policy:
218 ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=route_map_policy))
219 match.details["name"] = route_map_policy
220
221 if description is not None and match.details.get("description") != description:
222 ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description))
223 match.details["description"] = description
224
225 if module.params.get("entries") is not None and match.details.get("mcastRtMapEntryList") != entries:
226 ops.append(dict(op="replace", path="{0}/{1}/mcastRtMapEntryList".format(path, match.index), value=entries))
227 match.details["mcastRtMapEntryList"] = entries
228
229 mso.sanitize(match.details)
230
231 else:
232 if not entries:
233 mso.fail_json(msg=err_message_min_entries)
234
235 payload = {"name": route_map_policy, "mcastRtMapEntryList": entries}
236 if description:
237 payload["description"] = description
238
239 ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload)))
240
241 mso.sanitize(payload)
242
243 mso.existing = mso.proposed
244
245 elif state == "absent":
246 if match:
247 ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index)))
248 mso.existing = {}
249
250 if not module.check_mode and ops:
251 mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops)
252 match = get_multicast_route_map_policy(mso_template, route_map_policy_uuid, route_map_policy)
253 if match:
254 mso.existing = match.details # When the state is present
255 else:
256 mso.existing = {} # When the state is absent
257 elif module.check_mode and state != "query": # When the state is present/absent with check mode
258 mso.existing = mso.proposed if state == "present" else {}
259
260 mso.exit_json()
261
262
263 def get_entries_payload(entries):
264 payload = []
265 for entry in entries:
266 entries_payload = {
267 "order": entry.get("order"),
268 "group": entry.get("group"),
269 "source": entry.get("source"),
270 "action": entry.get("action"),
271 }
272 if entry.get("rp"):
273 entries_payload["rp"] = entry.get("rp")
274 payload.append(entries_payload)
275 return payload
276
277
278 def get_multicast_route_map_policy(mso_template, uuid=None, name=None, fail_module=False):
279 """
280 Get the Multicast Route Map Policy by UUID or Name.
281 :param uuid: UUID of the Multicast Route Map Policy to search for -> Str
282 :param name: Name of the Multicast Route Map Policy to search for -> Str
283 :param fail_module: When match is not found fail the ansible module -> Bool
284 :return: Dict | None | List[Dict] | List[]: The processed result which could be:
285 When the UUID | Name is existing in the search list -> Dict
286 When the UUID | Name is not existing in the search list -> None
287 When both UUID and Name are None, and the search list is not empty -> List[Dict]
288 When both UUID and Name are None, and the search list is empty -> List[]
289 """
290 match = mso_template.template.get("tenantPolicyTemplate", {}).get("template", {}).get("mcastRouteMapPolicies", [])
291 if uuid or name: # Query a specific object
292 return mso_template.get_object_by_key_value_pairs(
293 "Multicast Route Map Policy", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module
294 )
295 return match # Query all objects
296
297
298 if __name__ == "__main__":
299 main()