| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Anvitha Jain (@anvjain) <[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_synce_interface_policy |
| 17 |
short_description: Manage syncE Interface Policies on Cisco Nexus Dashboard Orchestrator (NDO). |
| 18 |
description: |
| 19 |
- Manage syncE Interface 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 |
- Anvitha Jain (@anvjain) |
| 23 |
options: |
| 24 |
template: |
| 25 |
description: |
| 26 |
- The name of the template. |
| 27 |
- The template must be a fabric policy template. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
interface_policy: |
| 31 |
description: |
| 32 |
- The name of the syncE Interface Policy. |
| 33 |
type: str |
| 34 |
aliases: [ name ] |
| 35 |
interface_policy_uuid: |
| 36 |
description: |
| 37 |
- The uuid of the syncE Interface Policy. |
| 38 |
- This parameter is required when the O(interface_policy) needs to be updated. |
| 39 |
type: str |
| 40 |
aliases: [ uuid ] |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the syncE Interface Policy. |
| 44 |
type: str |
| 45 |
admin_state: |
| 46 |
description: |
| 47 |
- The administrative state of the syncE Interface Policy. |
| 48 |
- The default value is disabled. |
| 49 |
type: str |
| 50 |
choices: [ enabled, disabled ] |
| 51 |
sync_state_msg: |
| 52 |
description: |
| 53 |
- The sync state message of the syncE Interface Policy. |
| 54 |
- The default value is enabled. |
| 55 |
type: str |
| 56 |
choices: [ enabled, disabled ] |
| 57 |
selection_input: |
| 58 |
description: |
| 59 |
- The selection input of the syncE Interface Policy. |
| 60 |
- The default value is disabled. |
| 61 |
type: str |
| 62 |
choices: [ enabled, disabled ] |
| 63 |
src_priority: |
| 64 |
description: |
| 65 |
- The source priority of the syncE Interface Policy. |
| 66 |
- The value must be an integer between 1 and 254. |
| 67 |
- The default value is 100. |
| 68 |
type: int |
| 69 |
wait_to_restore: |
| 70 |
description: |
| 71 |
- The delay before attempting to restore synchronization on a SyncE interface after a disruption. |
| 72 |
- The value must be an integer between 0 and 12. |
| 73 |
- The default value is 5. |
| 74 |
type: int |
| 75 |
state: |
| 76 |
description: |
| 77 |
- Use C(absent) for removing. |
| 78 |
- Use C(query) for listing an object or multiple objects. |
| 79 |
- Use C(present) for creating or updating. |
| 80 |
type: str |
| 81 |
choices: [ absent, query, present ] |
| 82 |
default: query |
| 83 |
extends_documentation_fragment: cisco.mso.modules |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
- name: Create a new syncE interface policy |
| 88 |
cisco.mso.ndo_synce_interface_policy: |
| 89 |
host: mso_host |
| 90 |
username: admin |
| 91 |
password: SomeSecretPassword |
| 92 |
template: ansible_fabric_policy_template |
| 93 |
interface_policy: ansible_test_interface_policy |
| 94 |
admin_state: enabled |
| 95 |
sync_state_msg: enabled |
| 96 |
selection_input: enabled |
| 97 |
src_priority: 100 |
| 98 |
wait_to_restore: 5 |
| 99 |
state: present |
| 100 |
register: create |
| 101 |
|
| 102 |
- name: Query a syncE interface policy with interface_policy name |
| 103 |
cisco.mso.ndo_synce_interface_policy: |
| 104 |
host: mso_host |
| 105 |
username: admin |
| 106 |
password: SomeSecretPassword |
| 107 |
template: ansible_fabric_policy_template |
| 108 |
interface_policy: ansible_test_interface_policy |
| 109 |
state: query |
| 110 |
register: query_one |
| 111 |
|
| 112 |
- name: Query all syncE interface policies in the template |
| 113 |
cisco.mso.ndo_synce_interface_policy: |
| 114 |
host: mso_host |
| 115 |
username: admin |
| 116 |
password: SomeSecretPassword |
| 117 |
template: ansible_fabric_policy_template |
| 118 |
state: query |
| 119 |
register: query_all |
| 120 |
|
| 121 |
- name: Query a syncE interface policy with interface_policy UUID |
| 122 |
cisco.mso.ndo_synce_interface_policy: |
| 123 |
host: mso_host |
| 124 |
username: admin |
| 125 |
password: SomeSecretPassword |
| 126 |
template: ansible_fabric_policy_template |
| 127 |
interface_policy_uuid: '{{ create.current.uuid }}' |
| 128 |
state: query |
| 129 |
register: query_one_by_uuid |
| 130 |
|
| 131 |
- name: Delete a syncE interface policy |
| 132 |
cisco.mso.ndo_synce_interface_policy: |
| 133 |
host: mso_host |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
template: ansible_fabric_policy_template |
| 137 |
interface_policy: ansible_test_interface_policy |
| 138 |
state: absent |
| 139 |
""" |
| 140 |
|
| 141 |
RETURN = r""" |
| 142 |
""" |
| 143 |
|
| 144 |
|
| 145 |
from ansible.module_utils.basic import AnsibleModule |
| 146 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 147 |
from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair |
| 148 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP |
| 149 |
import copy |
| 150 |
|
| 151 |
|
| 152 |
def main(): |
| 153 |
argument_spec = mso_argument_spec() |
| 154 |
argument_spec.update( |
| 155 |
dict( |
| 156 |
template=dict(type="str", required=True), |
| 157 |
interface_policy=dict(type="str", aliases=["name"]), |
| 158 |
interface_policy_uuid=dict(type="str", aliases=["uuid"]), |
| 159 |
description=dict(type="str"), |
| 160 |
admin_state=dict(type="str", choices=["enabled", "disabled"]), |
| 161 |
sync_state_msg=dict(type="str", choices=["enabled", "disabled"]), |
| 162 |
selection_input=dict(type="str", choices=["enabled", "disabled"]), |
| 163 |
src_priority=dict(type="int"), |
| 164 |
wait_to_restore=dict(type="int"), |
| 165 |
state=dict(type="str", choices=["absent", "query", "present"], default="query"), |
| 166 |
) |
| 167 |
) |
| 168 |
|
| 169 |
module = AnsibleModule( |
| 170 |
argument_spec=argument_spec, |
| 171 |
supports_check_mode=True, |
| 172 |
required_if=[ |
| 173 |
["state", "present", ["interface_policy"]], |
| 174 |
["state", "absent", ["interface_policy"]], |
| 175 |
], |
| 176 |
) |
| 177 |
|
| 178 |
mso = MSOModule(module) |
| 179 |
|
| 180 |
template = module.params.get("template") |
| 181 |
interface_policy = module.params.get("interface_policy") |
| 182 |
interface_policy_uuid = module.params.get("interface_policy_uuid") |
| 183 |
description = module.params.get("description") |
| 184 |
admin_state = module.params.get("admin_state") |
| 185 |
sync_state_msg = module.params.get("sync_state_msg") |
| 186 |
selection_input = module.params.get("selection_input") |
| 187 |
src_priority = module.params.get("src_priority") |
| 188 |
wait_to_restore = module.params.get("wait_to_restore") |
| 189 |
state = module.params.get("state") |
| 190 |
|
| 191 |
ops = [] |
| 192 |
match = None |
| 193 |
|
| 194 |
mso_template = MSOTemplate(mso, "fabric_policy", template) |
| 195 |
mso_template.validate_template("fabricPolicy") |
| 196 |
|
| 197 |
path = "/fabricPolicyTemplate/template/syncEthIntfPolicies" |
| 198 |
match = get_synce_interface_policy(mso_template, interface_policy_uuid, interface_policy) |
| 199 |
|
| 200 |
if interface_policy_uuid or interface_policy: |
| 201 |
if match: |
| 202 |
mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object |
| 203 |
elif match: |
| 204 |
mso.existing = match # Query all objects |
| 205 |
|
| 206 |
if state == "present": |
| 207 |
mso.existing = {} |
| 208 |
|
| 209 |
if match: |
| 210 |
sync_state_msg_value = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(sync_state_msg) |
| 211 |
selection_input_value = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(selection_input) |
| 212 |
|
| 213 |
if interface_policy and match.details.get("name") != interface_policy: |
| 214 |
ops.append(dict(op="replace", path="{0}/{1}/name".format(path, match.index), value=interface_policy)) |
| 215 |
match.details["name"] = interface_policy |
| 216 |
|
| 217 |
if description is not None and match.details.get("description") != description: |
| 218 |
ops.append(dict(op="replace", path="{0}/{1}/description".format(path, match.index), value=description)) |
| 219 |
match.details["description"] = description |
| 220 |
|
| 221 |
if admin_state and match.details.get("adminState") != admin_state: |
| 222 |
ops.append(dict(op="replace", path="{0}/{1}/adminState".format(path, match.index), value=admin_state)) |
| 223 |
match.details["adminState"] = admin_state |
| 224 |
|
| 225 |
if sync_state_msg and match.details.get("syncStateMsgEnabled") != sync_state_msg_value: |
| 226 |
ops.append(dict(op="replace", path="{0}/{1}/syncStateMsgEnabled".format(path, match.index), value=sync_state_msg_value)) |
| 227 |
match.details["syncStateMsgEnabled"] = sync_state_msg_value |
| 228 |
|
| 229 |
if selection_input and match.details.get("selectionInputEnabled") != selection_input_value: |
| 230 |
ops.append(dict(op="replace", path="{0}/{1}/selectionInputEnabled".format(path, match.index), value=selection_input_value)) |
| 231 |
match.details["selectionInputEnabled"] = selection_input_value |
| 232 |
|
| 233 |
if src_priority and match.details.get("srcPriority") != src_priority: |
| 234 |
ops.append(dict(op="replace", path="{0}/{1}/srcPriority".format(path, match.index), value=src_priority)) |
| 235 |
match.details["srcPriority"] = src_priority |
| 236 |
|
| 237 |
if wait_to_restore and match.details.get("waitToRestore") != wait_to_restore: |
| 238 |
ops.append(dict(op="replace", path="{0}/{1}/waitToRestore".format(path, match.index), value=wait_to_restore)) |
| 239 |
match.details["waitToRestore"] = wait_to_restore |
| 240 |
|
| 241 |
mso.sanitize(match.details) |
| 242 |
|
| 243 |
else: |
| 244 |
payload = {"name": interface_policy, "templateId": mso_template.template.get("templateId"), "schemaId": mso_template.template.get("schemaId")} |
| 245 |
if description: |
| 246 |
payload["description"] = description |
| 247 |
if admin_state: |
| 248 |
payload["adminState"] = admin_state |
| 249 |
if sync_state_msg: |
| 250 |
payload["syncStateMsgEnabled"] = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(sync_state_msg) |
| 251 |
if selection_input: |
| 252 |
payload["selectionInputEnabled"] = ENABLED_OR_DISABLED_TO_BOOL_STRING_MAP.get(selection_input) |
| 253 |
if src_priority: |
| 254 |
payload["srcPriority"] = src_priority |
| 255 |
if wait_to_restore: |
| 256 |
payload["waitToRestore"] = wait_to_restore |
| 257 |
|
| 258 |
ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload))) |
| 259 |
|
| 260 |
mso.sanitize(payload) |
| 261 |
|
| 262 |
mso.existing = mso.proposed |
| 263 |
|
| 264 |
elif state == "absent": |
| 265 |
if match: |
| 266 |
ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index))) |
| 267 |
mso.existing = {} |
| 268 |
|
| 269 |
if not module.check_mode and ops: |
| 270 |
mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops) |
| 271 |
match = get_synce_interface_policy(mso_template, interface_policy_uuid, interface_policy) |
| 272 |
if match: |
| 273 |
mso.existing = match.details # When the state is present |
| 274 |
else: |
| 275 |
mso.existing = {} # When the state is absent |
| 276 |
elif module.check_mode and state != "query": # When the state is present/absent with check mode |
| 277 |
mso.existing = mso.proposed if state == "present" else {} |
| 278 |
|
| 279 |
mso.exit_json() |
| 280 |
|
| 281 |
|
| 282 |
def get_synce_interface_policy(mso_template, uuid=None, name=None, fail_module=False): |
| 283 |
""" |
| 284 |
Get the SyncE Interface Policy by UUID or Name. |
| 285 |
:param uuid: UUID of the SyncE Interface Policy to search for -> Str |
| 286 |
:param name: Name of the SyncE Interface Policy to search for -> Str |
| 287 |
:param fail_module: When match is not found fail the ansible module -> Bool |
| 288 |
:return: Dict | None | List[Dict] | List[]: The processed result which could be: |
| 289 |
When the UUID | Name is existing in the search list -> Dict |
| 290 |
When the UUID | Name is not existing in the search list -> None |
| 291 |
When both UUID and Name are None, and the search list is not empty -> List[Dict] |
| 292 |
When both UUID and Name are None, and the search list is empty -> List[] |
| 293 |
""" |
| 294 |
match = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("syncEthIntfPolicies", []) |
| 295 |
if uuid or name: # Query a specific object |
| 296 |
return mso_template.get_object_by_key_value_pairs( |
| 297 |
"SyncE Interface Policy", match, [KVPair("uuid", uuid) if uuid else KVPair("name", name)], fail_module |
| 298 |
) |
| 299 |
return match # Query all objects |
| 300 |
|
| 301 |
|
| 302 |
if __name__ == "__main__": |
| 303 |
main() |