Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Gaspard Micol (@gmicol) <[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_class_policy
21 version_added: "2.11.0"
22 short_description: Manage QoS Class Policies on Cisco Nexus Dashboard Orchestrator (NDO).
23 description:
24 - Manage Quality of Service (QoS) Class Policies.
25 - This module can be used on Cisco Nexus Dashboard Orchestrator (NDO).
26 - There can only be a single QoS Class policy in a fabric policy template.
27 - This module is only supported on ND v3.1 (NDO v4.3) and later.
28 author:
29 - Gaspard Micol (@gmicol)
30 options:
31 template:
32 description:
33 - The name of the template.
34 - The template must be a fabric policy template.
35 - This parameter or O(template_id) is required.
36 type: str
37 template_id:
38 description:
39 - The ID of the fabric policy template.
40 - This parameter or O(template) is required.
41 type: str
42 name:
43 description:
44 - The name of the QoS Class Policy.
45 type: str
46 aliases: [ qos_class_policy ]
47 uuid:
48 description:
49 - The UUID of the QoS Class Policy.
50 type: str
51 aliases: [ qos_class_policy_uuid ]
52 description:
53 description:
54 - The description of the QoS Class Policy.
55 type: str
56 preserve_cos:
57 description:
58 - Whether to preserve the Class of Service (CoS).
59 - Defaults to true when unset during creation.
60 type: bool
61 qos_levels:
62 description:
63 - The list of configurable QoS levels for the QoS Class Policy.
64 - Providing a new list of O(qos_levels) will replace the existing one from the QoS Class Policy.
65 - Providing an empty list will remove the O(qos_levels=[]) from the QoS Class Policy.
66 type: list
67 elements: dict
68 suboptions:
69 level:
70 description:
71 - The QoS level.
72 type: str
73 choices: [ level1, level2, level3, level4, level5, level6 ]
74 required: true
75 mtu:
76 description:
77 - The MTU value.
78 - The value must be between 1500 and 9216.
79 - Defaults to 9216 when unset during creation.
80 type: int
81 minimum_buffer:
82 description:
83 - The minimum number of reserved buffers.
84 - The value must be between 0 and 3.
85 - Defaults to 0 when unset during creation.
86 type: int
87 congestion_algorithm:
88 description:
89 - The congestion algorithm used for this QoS Level.
90 - Defaults to C(tail_drop) when unset during creation.
91 type: str
92 choices: [ tail_drop, wred ]
93 wred_configuration:
94 description:
95 - The Weighted Random Early Detection (WRED) Algorithm configuration.
96 - Providing a new list of O(qos_levels.wred_configuration) will replace the existing one from the list of QoS levels.
97 - Providing an empty list will remove the O(qos_levels.wred_configuration=[]) from the list of QoS levels.
98 from the QoS Class Policy.
99 type: dict
100 suboptions:
101 congestion_notification:
102 description:
103 - The state of Explicit Congestion Notification (ECN) setting.
104 - Enabling Congestion Notification causes the packets that would be dropped to be ECN-marked instead.
105 - Defaults to C(disabled) when unset during creation.
106 type: str
107 choices: [ enabled, disabled ]
108 forward_non_ecn_traffic:
109 description:
110 - Whether to forward Non-ECN Traffic.
111 - This attribute should only be used when O(qos_levels.wred_configuration.congestion_notification="enabled").
112 - Defaults to false when unset during creation.
113 type: bool
114 minimum_threshold:
115 description:
116 - The minimum queue threshold as a percentage of the maximum queue length for WRED algorithm.
117 - The value must be between 0 and 100.
118 - Defaults to 0 when unset during creation.
119 type: int
120 maximum_threshold:
121 description:
122 - The maximum queue threshold as a percentage of the maximum queue length for WRED algorithm.
123 - The value must be between 0 and 100.
124 - Defaults to 100 when unset during creation.
125 type: int
126 probability:
127 description:
128 - The probability value for WRED algorithm.
129 - The probability used to determine whether a packet is dropped or queued
130 when the average queue size is between the minimum and the maximum threshold values.
131 - The value must be between 0 and 100.
132 - Defaults to 0 when unset during creation.
133 type: int
134 weight:
135 description:
136 - The weight value for WRED algorithm.
137 - Lower weight prioritizes current queue length, while higher weight prioritizes older queue lengths.
138 - The value must be between 0 and 7.
139 - Defaults to 0 when unset during creation.
140 type: int
141 scheduling_algorithm:
142 description:
143 - The QoS Scheduling Algorithm.
144 - Defaults to C(weighted_round_robin) when unset during creation.
145 type: str
146 choices: [ weighted_round_robin, strict_priority ]
147 bandwidth_allocated:
148 description:
149 - The percentage of total bandwidth allocated to this QoS Level.
150 - The value must be between 0 and 100.
151 - Defaults to 20 when unset during creation.
152 type: int
153 pfc_admin_state:
154 description:
155 - The administrative state of the Priority Flow Control (PFC) policy.
156 type: str
157 choices: [ enabled, disabled ]
158 default: disabled
159 admin_state:
160 description:
161 - The policy administrative state.
162 - Defaults to C(enabled) when unset during creation.
163 type: str
164 choices: [ enabled, disabled ]
165 no_drop_cos:
166 description:
167 - The Class of Service (CoS) level for which to enforce the no drop packet handling even in case of traffic congestion.
168 - This attribute must be specified when O(qos_levels.pfc_admin_state="enabled").
169 - Defaults to C(unspecified) when unset during creation.
170 type: str
171 choices: [ cos0, cos1, cos2, cos3, cos4, cos5, cos6, cos7, unspecified ]
172 pfc_scope:
173 description:
174 - The PFC scope.
175 - Defaults to C(fabric_wide) when unset during creation.
176 type: str
177 choices: [ fabric_wide, intra_tor ]
178 state:
179 description:
180 - Use C(absent) for removing.
181 - Use C(query) for listing an object or multiple objects.
182 - Use C(present) for creating or updating.
183 type: str
184 choices: [ absent, query, present ]
185 default: query
186 notes:
187 - The O(template) must exist before using this module in your playbook.
188 The M(cisco.mso.ndo_template) module can be used for this.
189 - Attempts to create any additional QoS Class policies will only update the existing
190 object in the Fabric Policy template.
191 seealso:
192 extends_documentation_fragment: cisco.mso.modules
193 """
194
195 EXAMPLES = r"""
196 - name: Create a new QoS Class policy with minimum configuration
197 cisco.mso.ndo_qos_class_policy:
198 host: mso_host
199 username: admin
200 password: SomeSecretPassword
201 template: ansible_tenant_template
202 name: ansible_test_qos_class_policy
203 state: present
204 register: create_qos_class_policy
205
206 - name: Update a QoS Class policy using UUID by adding QoS level1 with minimum configuration
207 cisco.mso.ndo_qos_class_policy:
208 host: mso_host
209 username: admin
210 password: SomeSecretPassword
211 template: ansible_tenant_template
212 uuid: "{{ create_qos_class_policy.current.uuid }}"
213 description: Ansible Test QoS Class Policy
214 preserve_cos: true
215 qos_levels:
216 - level: level1
217 state: present
218 register: update_qos_class_policy_with_qos_level1
219
220 - name: Update a QoS Class policy by adding QoS level2 with full configuration
221 cisco.mso.ndo_qos_class_policy:
222 host: mso_host
223 username: admin
224 password: SomeSecretPassword
225 template: ansible_tenant_template
226 name: ansible_test_qos_class_policy
227 description: Ansible Test QoS Class Policy
228 preserve_cos: true
229 qos_levels:
230 - level: level1
231 - level: level2
232 mtu: 9000
233 minimum_buffer: 1
234 congestion_algorithm: wred
235 wred_configuration:
236 congestion_notification: enabled
237 forward_non_ecn_traffic: false
238 minimum_threshold: 5
239 maximum_threshold: 95
240 probability: 80
241 weight: 1
242 scheduling_algorithm: weighted_round_robin
243 bandwidth_allocated: 50
244 pfc_admin_state: enabled
245 admin_state: enabled
246 no_drop_cos: cos1
247 pfc_scope: intra_tor
248 state: present
249 register: add_qos_class_policy_level2
250
251 - name: Update a QoS Class policy by removing QoS level2 and keeping QoS level1
252 cisco.mso.ndo_qos_class_policy:
253 host: mso_host
254 username: admin
255 password: SomeSecretPassword
256 template: ansible_tenant_template
257 name: ansible_test_qos_class_policy
258 description: Ansible Test QoS Class Policy
259 preserve_cos: true
260 qos_levels:
261 - level: level1
262 state: present
263 register: remove_qos_class_policy_level2
264
265 - name: Update a QoS Class policy by removing all QoS levels
266 cisco.mso.ndo_qos_class_policy:
267 host: mso_host
268 username: admin
269 password: SomeSecretPassword
270 template: ansible_tenant_template
271 name: ansible_test_qos_class_policy
272 description: Ansible Test QoS Class Policy
273 preserve_cos: true
274 qos_levels: []
275 state: present
276 register: remove_qos_class_policy_all_levels
277
278 - name: Query QoS Class policy using name
279 cisco.mso.ndo_qos_class_policy:
280 host: mso_host
281 username: admin
282 password: SomeSecretPassword
283 template: ansible_tenant_template
284 name: ansible_test_qos_class_policy
285 state: query
286 register: query_one
287
288 - name: Query QoS Class policy using UUID
289 cisco.mso.ndo_qos_class_policy:
290 host: mso_host
291 username: admin
292 password: SomeSecretPassword
293 template: ansible_tenant_template
294 uuid: "{{ create_qos_class_policy.current.uuid }}"
295 state: query
296 register: query_one_uuid
297
298 - name: Delete a QoS Class policy using name
299 cisco.mso.ndo_qos_class_policy:
300 host: mso_host
301 username: admin
302 password: SomeSecretPassword
303 template: ansible_tenant_template
304 name: ansible_test_qos_class_policy
305 state: absent
306
307 - name: Delete a QoS Class policy using UUID
308 cisco.mso.ndo_qos_class_policy:
309 host: mso_host
310 username: admin
311 password: SomeSecretPassword
312 template: ansible_tenant_template
313 uuid: "{{ query_one.current.uuid }}"
314 state: absent
315 """
316
317 RETURN = r"""
318 """
319 import copy
320 from ansible.module_utils.basic import AnsibleModule
321 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
322 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
323 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, delete_none_values
324 from ansible_collections.cisco.mso.plugins.module_utils.constants import (
325 QOS_CONGESTION_ALGORITHM_MAP,
326 QOS_SCHEDULING_ALGORITHM_MAP,
327 QOS_PFC_SCOPE_MAP,
328 QOS_LEVEL,
329 COS_VALUES,
330 )
331
332
333 def main():
334 qos_levels_to_remove = copy.copy(QOS_LEVEL) # A list that tracks of all levels to be removed during the PATCH operation when qos_levels is being updated
335 qos_levels_to_remove.remove("unspecified") # "unspecified" is removed as it is not a valid value for qos_levels.level
336 argument_spec = mso_argument_spec()
337 argument_spec.update(
338 template=dict(type="str"),
339 template_id=dict(type="str"),
340 name=dict(type="str", aliases=["qos_class_policy"]),
341 description=dict(type="str"),
342 uuid=dict(type="str", aliases=["qos_class_policy_uuid"]),
343 preserve_cos=dict(type="bool"),
344 qos_levels=dict(
345 type="list",
346 elements="dict",
347 options=dict(
348 level=dict(type="str", required=True, choices=qos_levels_to_remove),
349 mtu=dict(type="int"),
350 minimum_buffer=dict(type="int"),
351 congestion_algorithm=dict(type="str", choices=list(QOS_CONGESTION_ALGORITHM_MAP)),
352 wred_configuration=dict(
353 type="dict",
354 options=dict(
355 congestion_notification=dict(type="str", choices=["enabled", "disabled"]),
356 forward_non_ecn_traffic=dict(type="bool"),
357 minimum_threshold=dict(type="int"),
358 maximum_threshold=dict(type="int"),
359 probability=dict(type="int"),
360 weight=dict(type="int"),
361 ),
362 ),
363 scheduling_algorithm=dict(type="str", choices=list(QOS_SCHEDULING_ALGORITHM_MAP)),
364 bandwidth_allocated=dict(type="int"),
365 pfc_admin_state=dict(type="str", choices=["enabled", "disabled"], default="disabled"),
366 admin_state=dict(type="str", choices=["enabled", "disabled"]),
367 no_drop_cos=dict(type="str", choices=COS_VALUES),
368 pfc_scope=dict(type="str", choices=list(QOS_PFC_SCOPE_MAP)),
369 ),
370 required_if=[
371 ["pfc_admin_state", "enabled", ["no_drop_cos"]],
372 ],
373 ),
374 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
375 )
376
377 module = AnsibleModule(
378 argument_spec=argument_spec,
379 supports_check_mode=True,
380 required_if=[
381 ["state", "absent", ["name", "uuid"], True],
382 ["state", "present", ["name", "uuid"], True],
383 ],
384 required_one_of=[
385 ["template", "template_id"],
386 ],
387 mutually_exclusive=[
388 ["template", "template_id"],
389 ],
390 )
391
392 mso = MSOModule(module)
393
394 template = module.params.get("template")
395 template_id = module.params.get("template_id")
396 name = module.params.get("name")
397 description = module.params.get("description")
398 uuid = module.params.get("uuid")
399 preserve_cos = module.params.get("preserve_cos")
400 qos_levels = module.params.get("qos_levels")
401 state = module.params.get("state")
402
403 ops = []
404 match = None
405
406 mso_template = MSOTemplate(mso, "fabric_policy", template, template_id)
407 mso_template.validate_template("fabricPolicy")
408 object_description = "QoS Class Policy"
409
410 path = "/fabricPolicyTemplate/template/qosClass"
411 existing_qos_policies = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("qosClass", {})
412 if name or uuid:
413 match = mso_template.get_object_by_key_value_pairs(
414 object_description,
415 [existing_qos_policies],
416 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
417 )
418 if match:
419 mso.existing = mso.previous = copy.deepcopy(mso_template.update_config_with_template_and_references(match.details))
420 else:
421 mso.existing = mso.previous = mso_template.update_config_with_template_and_references(existing_qos_policies)
422
423 if state == "present":
424 if uuid and not mso.existing:
425 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
426
427 mso_values = {
428 "name": name,
429 "description": description,
430 "preserveCos": preserve_cos,
431 }
432 if qos_levels is not None:
433 for qos_level in qos_levels:
434 level = qos_level.get("level")
435 if level not in qos_levels_to_remove:
436 mso.fail_json(msg="Duplicate configurations for QoS {0}".format(level))
437 else:
438 qos_levels_to_remove.remove(level)
439
440 wred_configuration = None
441 if qos_level.get("wred_configuration"):
442 wred_configuration = {
443 "congestionNotification": qos_level["wred_configuration"].get("congestion_notification"),
444 "minThreshold": qos_level["wred_configuration"].get("minimum_threshold"),
445 "maxThreshold": qos_level["wred_configuration"].get("maximum_threshold"),
446 "probability": qos_level["wred_configuration"].get("probability"),
447 "weight": qos_level["wred_configuration"].get("weight"),
448 "forwardNonEcn": qos_level["wred_configuration"].get("forward_non_ecn_traffic"),
449 }
450
451 mso_values[level] = {
452 "adminState": qos_level.get("admin_state"),
453 "minBuffer": qos_level.get("minimum_buffer"),
454 "mtu": qos_level.get("mtu"),
455 "congestionAlgorithm": QOS_CONGESTION_ALGORITHM_MAP.get(qos_level.get("congestion_algorithm")),
456 "wredConfig": wred_configuration,
457 "schedulingAlgorithm": QOS_SCHEDULING_ALGORITHM_MAP.get(qos_level.get("scheduling_algorithm")),
458 "bandwidthAllocated": qos_level.get("bandwidth_allocated"),
459 "pfcAdminState": qos_level.get("pfc_admin_state"),
460 "noDropCoS": qos_level.get("no_drop_cos"),
461 "pfcScope": QOS_PFC_SCOPE_MAP.get(qos_level.get("pfc_scope")),
462 }
463 else:
464 qos_levels_to_remove = []
465
466 mso_values = delete_none_values(mso_values)
467
468 if match:
469 append_update_ops_data(ops, match.details, path, mso_values, remove_data=qos_levels_to_remove)
470 mso.sanitize(match.details, collate=True)
471
472 else:
473 mso.sanitize(mso_values)
474 ops.append(dict(op="add", path=path, value=mso.sent))
475
476 elif state == "absent":
477 if match:
478 ops.append(dict(op="remove", path=path))
479
480 if not module.check_mode and ops:
481 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
482 qos_policies = response.get("fabricPolicyTemplate", {}).get("template", {}).get("qosClass", {})
483 match = mso_template.get_object_by_key_value_pairs(
484 object_description,
485 [qos_policies],
486 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
487 )
488 if match:
489 mso.existing = mso_template.update_config_with_template_and_references(match.details)
490 else:
491 mso.existing = {}
492 elif module.check_mode and state != "query":
493 mso_template.update_config_with_template_and_references(mso.proposed)
494 mso.existing = mso.proposed if state == "present" else {}
495
496 mso.exit_json()
497
498
499 if __name__ == "__main__":
500 main()