| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, 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 = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: ndo_tenant_custom_qos_policy |
| 17 |
short_description: Manage Custom QoS Policies in Tenant Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage Custom Quality of Service (QoS) Policies in Tenant Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 21 |
author: |
| 22 |
- Gaspard Micol (@gmicol) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the tenant template. |
| 27 |
type: str |
| 28 |
aliases: [ tenant_template ] |
| 29 |
required: true |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the Custom QoS Policy. |
| 33 |
type: str |
| 34 |
aliases: [ custom_qos_policy ] |
| 35 |
uuid: |
| 36 |
description: |
| 37 |
- The UUID of the Custom QoS Policy. |
| 38 |
- This parameter is required when the Custom QoS Policy O(name) needs to be updated. |
| 39 |
aliases: [ custom_qos_policy_uuid ] |
| 40 |
type: str |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the Custom QoS Policy. |
| 44 |
- Providing an empty string will remove the O(description="") from the Custom QoS Policy. |
| 45 |
type: str |
| 46 |
dscp_mappings: |
| 47 |
description: |
| 48 |
- The Differentiated Services Code Point (DSCP) mappings of the Custom QoS Policy. |
| 49 |
- Both O(dscp_mappings.dscp_from) and O(dscp_mappings.dscp_to) cannot be set to C(unspecified). |
| 50 |
- Providing a new list of O(dscp_mappings) will completely replace an existing one from the Custom QoS Policy. |
| 51 |
- Providing an empty list will remove the O(cos_mappings=[]) from the Custom QoS Policy. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
suboptions: |
| 55 |
dscp_from: |
| 56 |
description: |
| 57 |
- The starting encoding point of the DSCP range. |
| 58 |
- Defaults to C(unspecified) when unset during creation. |
| 59 |
type: str |
| 60 |
choices: |
| 61 |
- af11 |
| 62 |
- af12 |
| 63 |
- af13 |
| 64 |
- af21 |
| 65 |
- af22 |
| 66 |
- af23 |
| 67 |
- af31 |
| 68 |
- af32 |
| 69 |
- af33 |
| 70 |
- af41 |
| 71 |
- af42 |
| 72 |
- af43 |
| 73 |
- cs0 |
| 74 |
- cs1 |
| 75 |
- cs2 |
| 76 |
- cs3 |
| 77 |
- cs4 |
| 78 |
- cs5 |
| 79 |
- cs6 |
| 80 |
- cs7 |
| 81 |
- expedited_forwarding |
| 82 |
- unspecified |
| 83 |
- voice_admit |
| 84 |
aliases: [ from ] |
| 85 |
dscp_to: |
| 86 |
description: |
| 87 |
- The ending encoding point of the DSCP range. |
| 88 |
- Defaults to C(unspecified) when unset during creation. |
| 89 |
type: str |
| 90 |
choices: |
| 91 |
- af11 |
| 92 |
- af12 |
| 93 |
- af13 |
| 94 |
- af21 |
| 95 |
- af22 |
| 96 |
- af23 |
| 97 |
- af31 |
| 98 |
- af32 |
| 99 |
- af33 |
| 100 |
- af41 |
| 101 |
- af42 |
| 102 |
- af43 |
| 103 |
- cs0 |
| 104 |
- cs1 |
| 105 |
- cs2 |
| 106 |
- cs3 |
| 107 |
- cs4 |
| 108 |
- cs5 |
| 109 |
- cs6 |
| 110 |
- cs7 |
| 111 |
- expedited_forwarding |
| 112 |
- unspecified |
| 113 |
- voice_admit |
| 114 |
aliases: [ to ] |
| 115 |
dscp_target: |
| 116 |
description: |
| 117 |
- The DSCP target encoding point for egressing traffic. |
| 118 |
- Defaults to C(unspecified) when unset during creation. |
| 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 |
aliases: [ target ] |
| 145 |
target_cos: |
| 146 |
description: |
| 147 |
- The target CoS value/traffic type for the egressing traffic. |
| 148 |
- Defaults to C(unspecified) when unset during creation. |
| 149 |
- Both CoS value and traffic type are allowed. |
| 150 |
For example, O(dscp_mappings.target_cos=cos0) or O(dscp_mappings.target_cos=background) are the same valid inputs. |
| 151 |
type: str |
| 152 |
choices: |
| 153 |
- background |
| 154 |
- cos0 |
| 155 |
- best_effort |
| 156 |
- cos1 |
| 157 |
- excellent_effort |
| 158 |
- cos2 |
| 159 |
- critical_applications |
| 160 |
- cos3 |
| 161 |
- video |
| 162 |
- cos4 |
| 163 |
- voice |
| 164 |
- cos5 |
| 165 |
- internetwork_control |
| 166 |
- cos6 |
| 167 |
- network_control |
| 168 |
- cos7 |
| 169 |
- unspecified |
| 170 |
- cos8 |
| 171 |
qos_priority: |
| 172 |
description: |
| 173 |
- The QoS priority level to which the DSCP values will be mapped. |
| 174 |
- Defaults to C(unspecified) when unset during creation. |
| 175 |
type: str |
| 176 |
choices: [ level1, level2, level3, level4, level5, level6, unspecified ] |
| 177 |
aliases: [ priority, prio ] |
| 178 |
cos_mappings: |
| 179 |
description: |
| 180 |
- The Class of Service (CoS) mappings of the Custom QoS Policy. |
| 181 |
- Both O(cos_mappings.dot1p_from) and O(cos_mappings.dot1p_to) cannot be set to C(unspecified). |
| 182 |
- Providing a new list of O(cos_mappings) will completely replace an existing one from the Custom QoS Policy. |
| 183 |
- Providing an empty list will remove the O(cos_mappings=[]) from the Custom QoS Policy. |
| 184 |
type: list |
| 185 |
elements: dict |
| 186 |
suboptions: |
| 187 |
dot1p_from: |
| 188 |
description: |
| 189 |
- The starting value/traffic type of the CoS range. |
| 190 |
- Defaults to C(unspecified) when unset during creation. |
| 191 |
- Both CoS values and their respective traffic types are allowed. |
| 192 |
For example, O(cos_mappings.dot1p_from=cos0) or O(cos_mappings.dot1p_from=background) are the same valid inputs. |
| 193 |
type: str |
| 194 |
choices: |
| 195 |
- background |
| 196 |
- cos0 |
| 197 |
- best_effort |
| 198 |
- cos1 |
| 199 |
- excellent_effort |
| 200 |
- cos2 |
| 201 |
- critical_applications |
| 202 |
- cos3 |
| 203 |
- video |
| 204 |
- cos4 |
| 205 |
- voice |
| 206 |
- cos5 |
| 207 |
- internetwork_control |
| 208 |
- cos6 |
| 209 |
- network_control |
| 210 |
- cos7 |
| 211 |
- unspecified |
| 212 |
- cos8 |
| 213 |
aliases: [ from ] |
| 214 |
dot1p_to: |
| 215 |
description: |
| 216 |
- The ending value/traffic type of the CoS range. |
| 217 |
- Defaults to C(unspecified) when unset during creation. |
| 218 |
- Both CoS value and traffic type are allowed. |
| 219 |
For example, O(cos_mappings.dot1p_to=cos0) or O(cos_mappings.dot1p_to=background) are the same valid inputs. |
| 220 |
type: str |
| 221 |
choices: |
| 222 |
- background |
| 223 |
- cos0 |
| 224 |
- best_effort |
| 225 |
- cos1 |
| 226 |
- excellent_effort |
| 227 |
- cos2 |
| 228 |
- critical_applications |
| 229 |
- cos3 |
| 230 |
- video |
| 231 |
- cos4 |
| 232 |
- voice |
| 233 |
- cos5 |
| 234 |
- internetwork_control |
| 235 |
- cos6 |
| 236 |
- network_control |
| 237 |
- cos7 |
| 238 |
- unspecified |
| 239 |
- cos8 |
| 240 |
aliases: [ to ] |
| 241 |
dscp_target: |
| 242 |
description: |
| 243 |
- The DSCP target encoding point for egressing traffic. |
| 244 |
- Defaults to C(unspecified) when unset during creation. |
| 245 |
type: str |
| 246 |
choices: |
| 247 |
- af11 |
| 248 |
- af12 |
| 249 |
- af13 |
| 250 |
- af21 |
| 251 |
- af22 |
| 252 |
- af23 |
| 253 |
- af31 |
| 254 |
- af32 |
| 255 |
- af33 |
| 256 |
- af41 |
| 257 |
- af42 |
| 258 |
- af43 |
| 259 |
- cs0 |
| 260 |
- cs1 |
| 261 |
- cs2 |
| 262 |
- cs3 |
| 263 |
- cs4 |
| 264 |
- cs5 |
| 265 |
- cs6 |
| 266 |
- cs7 |
| 267 |
- expedited_forwarding |
| 268 |
- unspecified |
| 269 |
- voice_admit |
| 270 |
aliases: [ target ] |
| 271 |
target_cos: |
| 272 |
description: |
| 273 |
- The target CoS value/traffic type for the egressing traffic. |
| 274 |
- Defaults to C(unspecified) when unset during creation. |
| 275 |
- Both CoS values and their respective traffic types are allowed. |
| 276 |
For example, O(cos_mappings.target_cos=cos0) or O(cos_mappings.target_cos=background) are the same valid inputs. |
| 277 |
type: str |
| 278 |
choices: |
| 279 |
- background |
| 280 |
- cos0 |
| 281 |
- best_effort |
| 282 |
- cos1 |
| 283 |
- excellent_effort |
| 284 |
- cos2 |
| 285 |
- critical_applications |
| 286 |
- cos3 |
| 287 |
- video |
| 288 |
- cos4 |
| 289 |
- voice |
| 290 |
- cos5 |
| 291 |
- internetwork_control |
| 292 |
- cos6 |
| 293 |
- network_control |
| 294 |
- cos7 |
| 295 |
- unspecified |
| 296 |
- cos8 |
| 297 |
qos_priority: |
| 298 |
description: |
| 299 |
- The QoS priority level to which the DSCP values will be mapped. |
| 300 |
- Defaults to C(unspecified) when unset during creation. |
| 301 |
type: str |
| 302 |
choices: [ level1, level2, level3, level4, level5, level6, unspecified ] |
| 303 |
aliases: [ priority, prio ] |
| 304 |
state: |
| 305 |
description: |
| 306 |
- Use C(absent) for removing. |
| 307 |
- Use C(query) for listing an object or multiple objects. |
| 308 |
- Use C(present) for creating or updating. |
| 309 |
type: str |
| 310 |
choices: [ absent, query, present ] |
| 311 |
default: query |
| 312 |
notes: |
| 313 |
- The O(template) must exist before using this module in your playbook. |
| 314 |
Use M(cisco.mso.ndo_template) to create the Tenant template. |
| 315 |
seealso: |
| 316 |
- module: cisco.mso.ndo_template |
| 317 |
extends_documentation_fragment: cisco.mso.modules |
| 318 |
""" |
| 319 |
|
| 320 |
EXAMPLES = r""" |
| 321 |
- name: Create a new Custom QoS Policy object |
| 322 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 323 |
host: mso_host |
| 324 |
username: admin |
| 325 |
password: SomeSecretPassword |
| 326 |
template: tenant_template |
| 327 |
name: custom_qos_policy_1 |
| 328 |
dscp_mappings: |
| 329 |
- dscp_from: af11 |
| 330 |
dscp_to: af12 |
| 331 |
dscp_target: af11 |
| 332 |
target_cos: background |
| 333 |
qos_priority: level1 |
| 334 |
cos_mappings: |
| 335 |
- dot1p_from: background |
| 336 |
dot1p_to: best_effort |
| 337 |
target: af11 |
| 338 |
target_cos: background |
| 339 |
qos_priority: level1 |
| 340 |
state: present |
| 341 |
register: custom_qos_policy_1 |
| 342 |
|
| 343 |
- name: Update a Custom QoS Policy object name with UUID |
| 344 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 345 |
host: mso_host |
| 346 |
username: admin |
| 347 |
password: SomeSecretPassword |
| 348 |
template: tenant_template |
| 349 |
name: custom_qos_policy_2 |
| 350 |
uuid: "{{ custom_qos_policy_1.current.uuid }}" |
| 351 |
state: present |
| 352 |
|
| 353 |
- name: Query a Custom QoS Policy object with name |
| 354 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 355 |
host: mso_host |
| 356 |
username: admin |
| 357 |
password: SomeSecretPassword |
| 358 |
template: tenant_template |
| 359 |
name: custom_qos_policy_1 |
| 360 |
state: query |
| 361 |
register: query_name |
| 362 |
|
| 363 |
- name: Query a Custom QoS Policy object with UUID |
| 364 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 365 |
host: mso_host |
| 366 |
username: admin |
| 367 |
password: SomeSecretPassword |
| 368 |
template: tenant_template |
| 369 |
uuid: "{{ custom_qos_policy_1.current.uuid }}" |
| 370 |
state: query |
| 371 |
register: query_uuid |
| 372 |
|
| 373 |
- name: Query all Custom QoS Policy objects in a Tenant Template |
| 374 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 375 |
host: mso_host |
| 376 |
username: admin |
| 377 |
password: SomeSecretPassword |
| 378 |
template: tenant_template |
| 379 |
state: query |
| 380 |
register: query_all |
| 381 |
|
| 382 |
- name: Delete a Custom QoS Policy object with name |
| 383 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 384 |
host: mso_host |
| 385 |
username: admin |
| 386 |
password: SomeSecretPassword |
| 387 |
template: tenant_template |
| 388 |
name: custom_qos_policy_1 |
| 389 |
state: absent |
| 390 |
|
| 391 |
- name: Delete a Custom QoS Policy object with UUID |
| 392 |
cisco.mso.ndo_tenant_custom_qos_policy: |
| 393 |
host: mso_host |
| 394 |
username: admin |
| 395 |
password: SomeSecretPassword |
| 396 |
template: tenant_template |
| 397 |
uuid: "{{ custom_qos_policy_1.current.uuid }}" |
| 398 |
state: absent |
| 399 |
""" |
| 400 |
|
| 401 |
RETURN = r""" |
| 402 |
""" |
| 403 |
|
| 404 |
import copy |
| 405 |
from ansible.module_utils.basic import AnsibleModule |
| 406 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 407 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 408 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data |
| 409 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import ( |
| 410 |
TARGET_DSCP_MAP, |
| 411 |
TARGET_COS_MAP, |
| 412 |
DSCP_COS_KEY_MAP, |
| 413 |
QOS_LEVEL, |
| 414 |
) |
| 415 |
|
| 416 |
|
| 417 |
def main(): |
| 418 |
COS_MAP_CHOICES = list(TARGET_COS_MAP) + list(TARGET_COS_MAP.values()) |
| 419 |
argument_spec = mso_argument_spec() |
| 420 |
argument_spec.update( |
| 421 |
template=dict(type="str", required=True, aliases=["tenant_template"]), |
| 422 |
name=dict(type="str", aliases=["custom_qos_policy"]), |
| 423 |
uuid=dict(type="str", aliases=["custom_qos_policy_uuid"]), |
| 424 |
description=dict(type="str"), |
| 425 |
dscp_mappings=dict( |
| 426 |
type="list", |
| 427 |
elements="dict", |
| 428 |
options=dict( |
| 429 |
dscp_from=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["from"]), |
| 430 |
dscp_to=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["to"]), |
| 431 |
dscp_target=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["target"]), |
| 432 |
target_cos=dict(type="str", choices=COS_MAP_CHOICES), |
| 433 |
qos_priority=dict( |
| 434 |
type="str", |
| 435 |
choices=QOS_LEVEL, |
| 436 |
aliases=["priority", "prio"], |
| 437 |
), |
| 438 |
), |
| 439 |
), |
| 440 |
cos_mappings=dict( |
| 441 |
type="list", |
| 442 |
elements="dict", |
| 443 |
options=dict( |
| 444 |
dot1p_from=dict(type="str", choices=COS_MAP_CHOICES, aliases=["from"]), |
| 445 |
dot1p_to=dict(type="str", choices=COS_MAP_CHOICES, aliases=["to"]), |
| 446 |
dscp_target=dict(type="str", choices=list(TARGET_DSCP_MAP), aliases=["target"]), |
| 447 |
target_cos=dict(type="str", choices=COS_MAP_CHOICES), |
| 448 |
qos_priority=dict( |
| 449 |
type="str", |
| 450 |
choices=QOS_LEVEL, |
| 451 |
aliases=["priority", "prio"], |
| 452 |
), |
| 453 |
), |
| 454 |
), |
| 455 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 456 |
) |
| 457 |
|
| 458 |
module = AnsibleModule( |
| 459 |
argument_spec=argument_spec, |
| 460 |
supports_check_mode=True, |
| 461 |
required_if=[ |
| 462 |
["state", "absent", ["name", "uuid"], True], |
| 463 |
["state", "present", ["name", "uuid"], True], |
| 464 |
], |
| 465 |
) |
| 466 |
|
| 467 |
mso = MSOModule(module) |
| 468 |
|
| 469 |
template = module.params.get("template") |
| 470 |
name = module.params.get("name") |
| 471 |
uuid = module.params.get("uuid") |
| 472 |
description = module.params.get("description") |
| 473 |
dscp_mappings = module.params.get("dscp_mappings") |
| 474 |
if dscp_mappings: |
| 475 |
dscp_mappings = [ |
| 476 |
{ |
| 477 |
DSCP_COS_KEY_MAP.get(k): TARGET_DSCP_MAP.get(v) if k in ("dscp_from", "dscp_to", "dscp_target") else TARGET_COS_MAP.get(v, v) |
| 478 |
for k, v in d.items() |
| 479 |
} |
| 480 |
for d in dscp_mappings |
| 481 |
] |
| 482 |
cos_mappings = module.params.get("cos_mappings") |
| 483 |
if cos_mappings: |
| 484 |
cos_mappings = [ |
| 485 |
{DSCP_COS_KEY_MAP.get(k): TARGET_DSCP_MAP.get(v) if k == "dscp_target" else TARGET_COS_MAP.get(v, v) for k, v in d.items()} for d in cos_mappings |
| 486 |
] |
| 487 |
state = module.params.get("state") |
| 488 |
|
| 489 |
template_object = MSOTemplate(mso, "tenant", template) |
| 490 |
template_object.validate_template("tenantPolicy") |
| 491 |
|
| 492 |
custom_qos_policies = template_object.template.get("tenantPolicyTemplate", {}).get("template", {}).get("qosPolicies", []) |
| 493 |
object_description = "Custom QoS Policy" |
| 494 |
custom_qos_policy_attrs_path = None |
| 495 |
match = None |
| 496 |
|
| 497 |
if state in ["query", "absent"] and custom_qos_policies == []: |
| 498 |
mso.exit_json() |
| 499 |
elif state == "query" and not (name or uuid): |
| 500 |
mso.existing = custom_qos_policies |
| 501 |
elif custom_qos_policies and (name or uuid): |
| 502 |
match = template_object.get_object_by_key_value_pairs( |
| 503 |
object_description, custom_qos_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)] |
| 504 |
) |
| 505 |
if match: |
| 506 |
custom_qos_policy_attrs_path = "/tenantPolicyTemplate/template/qosPolicies/{0}".format(match.index) |
| 507 |
mso.existing = mso.previous = copy.deepcopy(match.details) |
| 508 |
|
| 509 |
ops = [] |
| 510 |
|
| 511 |
if state == "present": |
| 512 |
if uuid and not mso.existing: |
| 513 |
mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid)) |
| 514 |
|
| 515 |
mso_values = dict( |
| 516 |
name=name, |
| 517 |
description=description, |
| 518 |
cosMappings=cos_mappings, |
| 519 |
dscpMappings=dscp_mappings, |
| 520 |
) |
| 521 |
|
| 522 |
if mso.existing and match: |
| 523 |
append_update_ops_data(ops, match.details, custom_qos_policy_attrs_path, mso_values) |
| 524 |
mso.sanitize(match.details, collate=True) |
| 525 |
else: |
| 526 |
mso.sanitize(mso_values) |
| 527 |
ops.append(dict(op="add", path="/tenantPolicyTemplate/template/qosPolicies/-", value=mso.sent)) |
| 528 |
|
| 529 |
elif state == "absent": |
| 530 |
if mso.existing and match: |
| 531 |
ops.append(dict(op="remove", path=custom_qos_policy_attrs_path)) |
| 532 |
|
| 533 |
if not module.check_mode and ops: |
| 534 |
response_object = mso.request(template_object.template_path, method="PATCH", data=ops) |
| 535 |
custom_qos_policies = response_object.get("tenantPolicyTemplate", {}).get("template", {}).get("qosPolicies", []) |
| 536 |
match = template_object.get_object_by_key_value_pairs( |
| 537 |
object_description, custom_qos_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)] |
| 538 |
) |
| 539 |
if match: |
| 540 |
mso.existing = match.details # When the state is present |
| 541 |
else: |
| 542 |
mso.existing = {} # When the state is absent |
| 543 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 544 |
mso.existing = mso.proposed if state == "present" else {} |
| 545 |
|
| 546 |
mso.exit_json() |
| 547 |
|
| 548 |
|
| 549 |
if __name__ == "__main__": |
| 550 |
main() |