Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Shreyas Srish (@shrsr) <[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 = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: ndo_qos_dscp_cos_translation_policy
21 short_description: Manage QoS DSCP CoS Translation Policies on Cisco Nexus Dashboard Orchestrator (NDO).
22 description:
23 - Manage Quality of Service (QoS) Differentiated Services Code Point (DSCP) Class of Service (CoS) Translation Policies.
24 - This module can be used on Cisco Nexus Dashboard Orchestrator (NDO).
25 - There can only be a single QoS DSCP CoS translation policy in a fabric policy template.
26 - This module is only supported on ND v3.1 (NDO v4.3) and later.
27 author:
28 - Shreyas Srish (@shrsr)
29 options:
30 template:
31 description:
32 - The name of the template.
33 - The template must be a fabric policy template.
34 - This parameter or O(template_id) is required.
35 type: str
36 template_id:
37 description:
38 - The ID of the fabric policy template.
39 - This parameter or O(template) is required.
40 type: str
41 qos_dscp_translation_policy:
42 description:
43 - The name of the QoS DSCP Translation Policy.
44 type: str
45 aliases: [ name ]
46 description:
47 description:
48 - The description of the QoS DSCP Translation Policy.
49 type: str
50 qos_dscp_translation_policy_uuid:
51 description:
52 - The UUID of the QoS DSCP Translation Policy.
53 type: str
54 aliases: [ uuid ]
55 admin_state:
56 description:
57 - The administrative state of the policy.
58 type: str
59 choices: [ enabled, disabled ]
60 control_plane_traffic:
61 description:
62 - DSCP value for control plane traffic.
63 type: str
64 choices:
65 - af11
66 - af12
67 - af13
68 - af21
69 - af22
70 - af23
71 - af31
72 - af32
73 - af33
74 - af41
75 - af42
76 - af43
77 - cs0
78 - cs1
79 - cs2
80 - cs3
81 - cs4
82 - cs5
83 - cs6
84 - cs7
85 - expedited_forwarding
86 - unspecified
87 - voice_admit
88 policy_plane_traffic:
89 description:
90 - DSCP value for policy plane traffic.
91 type: str
92 choices:
93 - af11
94 - af12
95 - af13
96 - af21
97 - af22
98 - af23
99 - af31
100 - af32
101 - af33
102 - af41
103 - af42
104 - af43
105 - cs0
106 - cs1
107 - cs2
108 - cs3
109 - cs4
110 - cs5
111 - cs6
112 - cs7
113 - expedited_forwarding
114 - unspecified
115 - voice_admit
116 span_traffic:
117 description:
118 - DSCP value for SPAN traffic.
119 type: str
120 choices:
121 - af11
122 - af12
123 - af13
124 - af21
125 - af22
126 - af23
127 - af31
128 - af32
129 - af33
130 - af41
131 - af42
132 - af43
133 - cs0
134 - cs1
135 - cs2
136 - cs3
137 - cs4
138 - cs5
139 - cs6
140 - cs7
141 - expedited_forwarding
142 - unspecified
143 - voice_admit
144 traceroute_traffic:
145 description:
146 - DSCP value for traceroute traffic.
147 type: str
148 choices:
149 - af11
150 - af12
151 - af13
152 - af21
153 - af22
154 - af23
155 - af31
156 - af32
157 - af33
158 - af41
159 - af42
160 - af43
161 - cs0
162 - cs1
163 - cs2
164 - cs3
165 - cs4
166 - cs5
167 - cs6
168 - cs7
169 - expedited_forwarding
170 - unspecified
171 - voice_admit
172 level1:
173 description:
174 - DSCP value for level 1 traffic.
175 type: str
176 choices:
177 - af11
178 - af12
179 - af13
180 - af21
181 - af22
182 - af23
183 - af31
184 - af32
185 - af33
186 - af41
187 - af42
188 - af43
189 - cs0
190 - cs1
191 - cs2
192 - cs3
193 - cs4
194 - cs5
195 - cs6
196 - cs7
197 - expedited_forwarding
198 - unspecified
199 - voice_admit
200 level2:
201 description:
202 - DSCP value for level 2 traffic.
203 type: str
204 choices:
205 - af11
206 - af12
207 - af13
208 - af21
209 - af22
210 - af23
211 - af31
212 - af32
213 - af33
214 - af41
215 - af42
216 - af43
217 - cs0
218 - cs1
219 - cs2
220 - cs3
221 - cs4
222 - cs5
223 - cs6
224 - cs7
225 - expedited_forwarding
226 - unspecified
227 - voice_admit
228 level3:
229 description:
230 - DSCP value for level 3 traffic.
231 type: str
232 choices:
233 - af11
234 - af12
235 - af13
236 - af21
237 - af22
238 - af23
239 - af31
240 - af32
241 - af33
242 - af41
243 - af42
244 - af43
245 - cs0
246 - cs1
247 - cs2
248 - cs3
249 - cs4
250 - cs5
251 - cs6
252 - cs7
253 - expedited_forwarding
254 - unspecified
255 - voice_admit
256 level4:
257 description:
258 - DSCP value for level 4 traffic.
259 type: str
260 choices:
261 - af11
262 - af12
263 - af13
264 - af21
265 - af22
266 - af23
267 - af31
268 - af32
269 - af33
270 - af41
271 - af42
272 - af43
273 - cs0
274 - cs1
275 - cs2
276 - cs3
277 - cs4
278 - cs5
279 - cs6
280 - cs7
281 - expedited_forwarding
282 - unspecified
283 - voice_admit
284 level5:
285 description:
286 - DSCP value for level 5 traffic.
287 type: str
288 choices:
289 - af11
290 - af12
291 - af13
292 - af21
293 - af22
294 - af23
295 - af31
296 - af32
297 - af33
298 - af41
299 - af42
300 - af43
301 - cs0
302 - cs1
303 - cs2
304 - cs3
305 - cs4
306 - cs5
307 - cs6
308 - cs7
309 - expedited_forwarding
310 - unspecified
311 - voice_admit
312 level6:
313 description:
314 - DSCP value for level 6 traffic.
315 type: str
316 choices:
317 - af11
318 - af12
319 - af13
320 - af21
321 - af22
322 - af23
323 - af31
324 - af32
325 - af33
326 - af41
327 - af42
328 - af43
329 - cs0
330 - cs1
331 - cs2
332 - cs3
333 - cs4
334 - cs5
335 - cs6
336 - cs7
337 - expedited_forwarding
338 - unspecified
339 - voice_admit
340 state:
341 description:
342 - Use C(absent) for removing.
343 - Use C(query) for listing an object or multiple objects.
344 - Use C(present) for creating or updating.
345 type: str
346 choices: [ absent, query, present ]
347 default: query
348 notes:
349 - The O(template) must exist before using this module in your playbook.
350 The M(cisco.mso.ndo_template) module can be used for this.
351 - Attempts to create any additional QoS DSCP CoS translation policies will only update the existing
352 object in the Fabric Policy template.
353 seealso:
354 extends_documentation_fragment: cisco.mso.modules
355 """
356
357 EXAMPLES = r"""
358 - name: Create a new QoS DSCP Translation policy
359 cisco.mso.ndo_qos_dscp_cos_translation_policy:
360 host: mso_host
361 username: admin
362 password: SomeSecretPassword
363 template: ansible_tenant_template
364 qos_dscp_translation_policy: ansible_test_qos_dscp_policy
365 admin_state: enabled
366 control_plane_traffic: cs0
367 policy_plane_traffic: cs4
368 span_traffic: cs5
369 traceroute_traffic: cs6
370 level1: cs1
371 level2: cs2
372 level3: cs3
373 level4: af11
374 level5: af21
375 level6: af31
376 state: present
377 register: create_qos_dscp_translation_policy
378
379 - name: Query QoS DSCP Translation policy with name
380 cisco.mso.ndo_qos_dscp_cos_translation_policy:
381 host: mso_host
382 username: admin
383 password: SomeSecretPassword
384 template: ansible_tenant_template
385 qos_dscp_translation_policy: ansible_test_qos_dscp_policy
386 state: query
387 register: query_one
388
389 - name: Query QoS DSCP Translation policy with uuid
390 cisco.mso.ndo_qos_dscp_cos_translation_policy:
391 host: mso_host
392 username: admin
393 password: SomeSecretPassword
394 template: ansible_tenant_template
395 qos_dscp_translation_policy_uuid: "{{ create_qos_dscp_translation_policy.current.uuid }}"
396 state: query
397 register: query_one_uuid
398
399 - name: Delete a QoS DSCP Translation policy with name
400 cisco.mso.ndo_qos_dscp_cos_translation_policy:
401 host: mso_host
402 username: admin
403 password: SomeSecretPassword
404 template: ansible_tenant_template
405 qos_dscp_translation_policy: ansible_test_qos_dscp_policy
406 state: absent
407
408 - name: Delete a QoS DSCP Translation policy with uuid
409 cisco.mso.ndo_qos_dscp_cos_translation_policy:
410 host: mso_host
411 username: admin
412 password: SomeSecretPassword
413 template: ansible_tenant_template
414 qos_dscp_translation_policy_uuid: "{{ query_one.current.uuid }}"
415 state: absent
416 """
417
418 RETURN = r"""
419 """
420 import copy
421 from ansible.module_utils.basic import AnsibleModule
422 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
423 MSOModule,
424 mso_argument_spec,
425 )
426 from ansible_collections.cisco.mso.plugins.module_utils.template import (
427 MSOTemplate,
428 KVPair,
429 )
430 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
431 from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP
432
433
434 def main():
435 argument_spec = mso_argument_spec()
436 argument_spec.update(
437 template=dict(type="str"),
438 template_id=dict(type="str"),
439 qos_dscp_translation_policy=dict(type="str", aliases=["name"]),
440 description=dict(type="str"),
441 qos_dscp_translation_policy_uuid=dict(type="str", aliases=["uuid"]),
442 admin_state=dict(type="str", choices=["enabled", "disabled"]),
443 control_plane_traffic=dict(type="str", choices=list(TARGET_DSCP_MAP)),
444 policy_plane_traffic=dict(type="str", choices=list(TARGET_DSCP_MAP)),
445 span_traffic=dict(type="str", choices=list(TARGET_DSCP_MAP)),
446 traceroute_traffic=dict(type="str", choices=list(TARGET_DSCP_MAP)),
447 level1=dict(type="str", choices=list(TARGET_DSCP_MAP)),
448 level2=dict(type="str", choices=list(TARGET_DSCP_MAP)),
449 level3=dict(type="str", choices=list(TARGET_DSCP_MAP)),
450 level4=dict(type="str", choices=list(TARGET_DSCP_MAP)),
451 level5=dict(type="str", choices=list(TARGET_DSCP_MAP)),
452 level6=dict(type="str", choices=list(TARGET_DSCP_MAP)),
453 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
454 )
455
456 module = AnsibleModule(
457 argument_spec=argument_spec,
458 supports_check_mode=True,
459 required_if=[
460 ["state", "absent", ["qos_dscp_translation_policy", "qos_dscp_translation_policy_uuid"], True],
461 ["state", "present", ["qos_dscp_translation_policy", "qos_dscp_translation_policy_uuid"], True],
462 ],
463 required_one_of=[
464 ["template", "template_id"],
465 ],
466 mutually_exclusive=[
467 ["template", "template_id"],
468 ],
469 )
470
471 mso = MSOModule(module)
472
473 template = module.params.get("template")
474 template_id = module.params.get("template_id")
475 qos_dscp_translation_policy = module.params.get("qos_dscp_translation_policy")
476 description = module.params.get("description")
477 qos_dscp_translation_policy_uuid = module.params.get("qos_dscp_translation_policy_uuid")
478 admin_state = module.params.get("admin_state")
479 control_plane_traffic = TARGET_DSCP_MAP.get(module.params.get("control_plane_traffic"))
480 policy_plane_traffic = TARGET_DSCP_MAP.get(module.params.get("policy_plane_traffic"))
481 span_traffic = TARGET_DSCP_MAP.get(module.params.get("span_traffic"))
482 traceroute_traffic = TARGET_DSCP_MAP.get(module.params.get("traceroute_traffic"))
483 level1 = TARGET_DSCP_MAP.get(module.params.get("level1"))
484 level2 = TARGET_DSCP_MAP.get(module.params.get("level2"))
485 level3 = TARGET_DSCP_MAP.get(module.params.get("level3"))
486 level4 = TARGET_DSCP_MAP.get(module.params.get("level4"))
487 level5 = TARGET_DSCP_MAP.get(module.params.get("level5"))
488 level6 = TARGET_DSCP_MAP.get(module.params.get("level6"))
489 state = module.params.get("state")
490
491 ops = []
492 match = None
493
494 mso_template = MSOTemplate(mso, "fabric_policy", template, template_id)
495 mso_template.validate_template("fabricPolicy")
496 object_description = "QoS DSCP CoS Translation Policy"
497
498 path = "/fabricPolicyTemplate/template/qosDscpTranslation"
499 existing_qos_policies = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("qosDscpTranslation", {})
500 if qos_dscp_translation_policy or qos_dscp_translation_policy_uuid:
501 match = mso_template.get_object_by_key_value_pairs(
502 object_description,
503 [existing_qos_policies],
504 [KVPair("uuid", qos_dscp_translation_policy_uuid) if qos_dscp_translation_policy_uuid else KVPair("name", qos_dscp_translation_policy)],
505 )
506 if match:
507 mso.existing = mso.previous = copy.deepcopy(match.details)
508 else:
509 mso.existing = mso.previous = existing_qos_policies
510
511 if state == "present":
512 mso_values = dict(
513 name=qos_dscp_translation_policy,
514 description=description,
515 adminState=admin_state,
516 controlPlaneTraffic=control_plane_traffic,
517 policyPlaneTraffic=policy_plane_traffic,
518 spanTraffic=span_traffic,
519 tracerouteTraffic=traceroute_traffic,
520 level1=level1,
521 level2=level2,
522 level3=level3,
523 level4=level4,
524 level5=level5,
525 level6=level6,
526 )
527 if match:
528 append_update_ops_data(ops, match.details, path, mso_values)
529 mso.sanitize(match.details, collate=True)
530
531 else:
532 mso.sanitize(mso_values)
533 ops.append(dict(op="add", path=path, value=mso.sent))
534
535 elif state == "absent":
536 if match:
537 ops.append(dict(op="remove", path=path))
538
539 if not module.check_mode and ops:
540 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
541 qos_policies = response.get("fabricPolicyTemplate", {}).get("template", {}).get("qosDscpTranslation", {})
542 match = mso_template.get_object_by_key_value_pairs(
543 object_description,
544 [qos_policies],
545 [KVPair("uuid", qos_dscp_translation_policy_uuid) if qos_dscp_translation_policy_uuid else KVPair("name", qos_dscp_translation_policy)],
546 )
547 if match:
548 mso.existing = match.details
549 else:
550 mso.existing = {}
551 elif module.check_mode and state != "query":
552 mso.existing = mso.proposed if state == "present" else {}
553
554 mso.exit_json()
555
556
557 if __name__ == "__main__":
558 main()