Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Sabari Jaganathan (@sajagana) <[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_l3out_node_group_policy
17 short_description: Manage L3Out Node Group Policy on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage L3Out Node Group Policy on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Sabari Jaganathan (@sajagana)
23 options:
24 template:
25 description:
26 - The name of the template.
27 - The template must be an L3Out template.
28 type: str
29 aliases: [ l3out_template ]
30 required: true
31 l3out:
32 description:
33 - The name of the L3Out.
34 type: str
35 aliases: [ l3out_name ]
36 required: true
37 name:
38 description:
39 - The name of the L3Out Node Group Policy.
40 type: str
41 aliases: [ l3out_node_group_policy ]
42 description:
43 description:
44 - The description of the L3Out Node Group Policy.
45 type: str
46 node_routing_policy:
47 description:
48 - The name of the L3Out Node Routing Policy.
49 - Providing an empty string will remove the O(node_routing_policy="") from L3Out Node Group Policy.
50 type: str
51 bfd:
52 description:
53 - The Bidirectional Forwarding Detection (BFD) multi-hop configuration of the L3Out Node Group Policy.
54 - Providing an empty dictionary will remove the O(bfd={}) from the L3Out Node Group Policy.
55 type: dict
56 suboptions:
57 auth:
58 description:
59 - The BFD multi-hop authentication of the L3Out Node Group Policy.
60 type: str
61 choices: [ enabled, disabled ]
62 aliases: [ bfd_multi_hop_authentication ]
63 key_id:
64 description:
65 - The BFD multi-hop key ID of the L3Out Node Group Policy.
66 type: int
67 key:
68 description:
69 - The BFD multi-hop key of the L3Out Node Group Policy.
70 type: str
71 aliases: [ bfd_multi_hop ]
72 target_dscp:
73 description:
74 - The DSCP Level of the L3Out Node Group Policy.
75 type: str
76 choices:
77 - af11
78 - af12
79 - af13
80 - af21
81 - af22
82 - af23
83 - af31
84 - af32
85 - af33
86 - af41
87 - af42
88 - af43
89 - cs0
90 - cs1
91 - cs2
92 - cs3
93 - cs4
94 - cs5
95 - cs6
96 - cs7
97 - expedited_forwarding
98 - unspecified
99 - voice_admit
100 state:
101 description:
102 - Use C(absent) for removing.
103 - Use C(query) for listing an object or multiple objects.
104 - Use C(present) for creating or updating.
105 type: str
106 choices: [ absent, query, present ]
107 default: query
108 notes:
109 - The O(template) must exist before using this module in your playbook.
110 Use M(cisco.mso.ndo_template) to create the L3Out template.
111 - The O(l3out) must exist before using this module in your playbook.
112 Use M(cisco.mso.ndo_l3out_template) to create the L3Out object under the L3Out template.
113 - The O(node_routing_policy) must exist before using this module in your playbook.
114 Use M(cisco.mso.ndo_l3out_node_routing_policy) to create the L3Out Node Routing Policy.
115 seealso:
116 - module: cisco.mso.ndo_template
117 - module: cisco.mso.ndo_l3out_template
118 - module: cisco.mso.ndo_l3out_node_routing_policy
119 extends_documentation_fragment: cisco.mso.modules
120 """
121
122 EXAMPLES = r"""
123 - name: Create a new L3Out node group policy
124 cisco.mso.ndo_l3out_node_group_policy:
125 host: mso_host
126 username: admin
127 password: SomeSecretPassword
128 template: l3out_template
129 l3out: l3out
130 name: node_group_policy_1
131 state: present
132
133 - name: Update an existing L3Out node group policy
134 cisco.mso.ndo_l3out_node_group_policy:
135 host: mso_host
136 username: admin
137 password: SomeSecretPassword
138 template: l3out_template
139 l3out: l3out
140 name: node_group_policy_1
141 description: "Updated description"
142 node_routing_policy: ans_node_policy_group_1
143 bfd:
144 auth: enabled
145 key_id: 1
146 key: TestKey
147 target_dscp: af11
148 state: present
149
150 - name: Query an existing L3Out node group policy with name
151 cisco.mso.ndo_l3out_node_group_policy:
152 host: mso_host
153 username: admin
154 password: SomeSecretPassword
155 template: l3out_template
156 l3out: l3out
157 name: node_group_policy_1
158 state: query
159 register: query_with_name
160
161 - name: Query all L3Out node group policy
162 cisco.mso.ndo_l3out_node_group_policy:
163 host: mso_host
164 username: admin
165 password: SomeSecretPassword
166 template: l3out_template
167 l3out: l3out
168 state: query
169 register: query_all
170
171 - name: Delete an existing L3Out node group policy with name
172 cisco.mso.ndo_l3out_node_group_policy:
173 host: mso_host
174 username: admin
175 password: SomeSecretPassword
176 template: l3out_template
177 l3out: l3out
178 name: node_group_policy_1
179 state: absent
180 """
181
182 RETURN = r"""
183 """
184
185
186 import copy
187 from ansible.module_utils.basic import AnsibleModule
188 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
189 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
190 from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP, ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP
191 from ansible_collections.cisco.mso.plugins.module_utils.utils import generate_api_endpoint, check_if_all_elements_are_none, append_update_ops_data
192
193
194 def bfd_multi_hop_mso_values(bfd):
195 return dict(
196 authEnabled=ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(bfd.get("auth")),
197 keyID=bfd.get("key_id"),
198 key=dict(
199 value=bfd.get("key"),
200 ),
201 )
202
203
204 def main():
205 argument_spec = mso_argument_spec()
206 argument_spec.update(
207 template=dict(type="str", required=True, aliases=["l3out_template"]),
208 l3out=dict(type="str", required=True, aliases=["l3out_name"]),
209 name=dict(type="str", aliases=["l3out_node_group_policy"]),
210 description=dict(type="str"),
211 node_routing_policy=dict(type="str"),
212 bfd=dict(
213 type="dict",
214 options=dict(
215 auth=dict(type="str", choices=["enabled", "disabled"], aliases=["bfd_multi_hop_authentication"]),
216 key_id=dict(type="int"),
217 key=dict(type="str", no_log=True),
218 ),
219 aliases=["bfd_multi_hop"],
220 ),
221 target_dscp=dict(type="str", choices=list(TARGET_DSCP_MAP)),
222 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
223 )
224
225 module = AnsibleModule(
226 argument_spec=argument_spec,
227 supports_check_mode=True,
228 required_if=[
229 ["state", "absent", ["name"]],
230 ["state", "present", ["name"]],
231 ],
232 )
233
234 mso = MSOModule(module)
235
236 template = module.params.get("template")
237 l3out = module.params.get("l3out")
238 name = module.params.get("name")
239 description = module.params.get("description")
240 node_routing_policy = module.params.get("node_routing_policy")
241 bfd = module.params.get("bfd")
242 target_dscp = TARGET_DSCP_MAP.get(module.params.get("target_dscp"))
243 state = module.params.get("state")
244
245 mso_template = MSOTemplate(mso, "l3out", template)
246 mso_template.validate_template("l3out")
247
248 l3out_object = mso_template.get_l3out_object(name=l3out, fail_module=True)
249 l3out_node_group = mso_template.get_l3out_node_group(name, l3out_object.details)
250
251 if name and l3out_node_group:
252 mso.existing = copy.deepcopy(l3out_node_group.details)
253 mso.previous = copy.deepcopy(l3out_node_group.details) # Query a specific object
254 elif l3out_node_group:
255 mso.existing = l3out_node_group # Query all objects
256
257 if state != "query":
258 node_group_policy_path = "/l3outTemplate/l3outs/{0}/nodeGroups/{1}".format(l3out_object.index, l3out_node_group.index if l3out_node_group else "-")
259
260 ops = []
261
262 if state == "present":
263 if bfd:
264 # True if all elements are None, False otherwise.
265 bfd_is_empty = check_if_all_elements_are_none(list(bfd.values()))
266
267 l3out_node_routing_policy_object = None
268 if node_routing_policy:
269 l3out_node_routing_policy_objects = mso.query_objs(
270 generate_api_endpoint(
271 "templates/objects", **{"type": "l3OutNodePolGroup", "tenant-id": mso_template.template_summary.get("tenantId"), "include-common": "true"}
272 )
273 )
274 l3out_node_routing_policy_object = mso_template.get_object_by_key_value_pairs(
275 "L3Out Node Routing Policy", l3out_node_routing_policy_objects, [KVPair("name", node_routing_policy)], True
276 )
277
278 if mso.existing:
279 mso_values = dict()
280 mso_values_remove = list()
281
282 if mso.existing.get("bfdMultiHop", {}).get("key", {}).get("ref"):
283 mso.existing["bfdMultiHop"]["key"].pop("ref", None)
284 mso.previous["bfdMultiHop"]["key"].pop("ref", None)
285
286 mso_values["description"] = description
287
288 if node_routing_policy == "" and mso.existing.get(
289 "nodeRoutingPolicyRef"
290 ): # Clear the node routing policy when node_routing_policy is empty string
291 mso_values_remove.append("nodeRoutingPolicyRef")
292 else:
293 mso_values["nodeRoutingPolicyRef"] = l3out_node_routing_policy_object.details.get("uuid") if l3out_node_routing_policy_object else None
294
295 if bfd:
296 if bfd_is_empty and mso.existing.get("bfdMultiHop"):
297 mso_values_remove.append("bfdMultiHop")
298
299 elif not bfd_is_empty and not mso.existing.get("bfdMultiHop"):
300 mso_values["bfdMultiHop"] = bfd_multi_hop_mso_values(bfd)
301
302 elif not bfd_is_empty and mso.existing.get("bfdMultiHop"):
303 mso_values[("bfdMultiHop", "authEnabled")] = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(bfd.get("auth"))
304 mso_values[("bfdMultiHop", "keyID")] = bfd.get("key_id")
305
306 if bfd.get("key") is not None:
307 if mso.existing.get("bfdMultiHop", {}).get("key") is not None:
308 mso_values[("bfdMultiHop", "key", "value")] = bfd.get("key")
309 else:
310 mso_values[("bfdMultiHop", "key")] = dict(value=bfd.get("key"))
311
312 mso_values["targetDscp"] = target_dscp
313
314 append_update_ops_data(ops, mso.existing, node_group_policy_path, mso_values, mso_values_remove)
315 mso.sanitize(mso.existing, collate=True)
316 else:
317 mso_values = dict(name=name)
318 mso_values["description"] = description
319
320 if l3out_node_routing_policy_object:
321 mso_values["nodeRoutingPolicyRef"] = l3out_node_routing_policy_object.details.get("uuid")
322
323 if bfd and not bfd_is_empty:
324 mso_values["bfdMultiHop"] = bfd_multi_hop_mso_values(bfd)
325
326 mso_values["targetDscp"] = target_dscp
327
328 mso.sanitize(mso_values)
329 ops.append(dict(op="add", path=node_group_policy_path, value=mso_values))
330
331 mso.existing = mso.proposed
332
333 elif state == "absent":
334 if mso.existing:
335 ops.append(dict(op="remove", path=node_group_policy_path))
336
337 if not module.check_mode and ops:
338 mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops)
339 l3out_object = mso_template.get_l3out_object(name=l3out, fail_module=True)
340 l3out_node_group = mso_template.get_l3out_node_group(name, l3out_object.details)
341 if l3out_node_group:
342 mso.existing = l3out_node_group.details # When the state is present
343 if mso.existing.get("bfdMultiHop", {}).get("key", {}).get("ref"):
344 mso.existing["bfdMultiHop"]["key"].pop("ref")
345 else:
346 mso.existing = {} # When the state is absent
347 elif module.check_mode and state != "query": # When the state is present/absent with check mode
348 mso.existing = mso.proposed if state == "present" else {}
349
350 mso.exit_json()
351
352
353 if __name__ == "__main__":
354 main()