Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, 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 = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: ndo_ptp_policy
17 short_description: Manage PTP Policies on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage Precision Time Protocol (PTP) 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 - Shreyas Srish (@shrsr)
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 ptp_policy:
31 description:
32 - The name of the PTP policy.
33 type: str
34 aliases: [ name ]
35 ptp_policy_uuid:
36 description:
37 - The UUID of the PTP policy.
38 - This parameter is required when the O(ptp_policy) needs to be updated.
39 type: str
40 aliases: [ uuid ]
41 description:
42 description:
43 - The description of the PTP Policy.
44 type: str
45 admin_state:
46 description:
47 - The administrative state of the PTP Policy.
48 type: str
49 choices: [ enabled, disabled ]
50 fabric_sync_interval:
51 description:
52 - Fabric synchronization interval for the PTP Policy.
53 - The value should be between -4 to 1.
54 - This is required when O(state=present).
55 type: int
56 global_domain:
57 description:
58 - Global domain number for the PTP Policy.
59 - The value should be between 1 to 128.
60 - This is required when O(state=present).
61 type: int
62 fabric_delay_interval:
63 description:
64 - Fabric delay interval for the PTP policy.
65 - The value should be between -3 to 5.
66 - This is required when O(state=present).
67 type: int
68 global_priority1:
69 description:
70 - Priority 1 for the PTP Policy.
71 - The value should be between 1 to 255.
72 - This is required when O(state=present).
73 type: int
74 global_priority2:
75 description:
76 - Priority 2 for the PTP Policy.
77 - The value should be between 1 to 255.
78 - This is required when O(state=present).
79 type: int
80 fabric_announce_timeout:
81 description:
82 - Fabric announce timeout for the PTP policy.
83 - The value should be between 2 to 10.
84 - This is required when O(state=present).
85 type: int
86 fabric_announce_interval:
87 description:
88 - Fabric announce interval for the PTP policy.
89 - The value should be between 0 to 4.
90 - This is required when O(state=present).
91 type: int
92 fabric_profile_template:
93 description:
94 - Fabric profile template for the PTP policy.
95 type: str
96 choices: [ aes67_2015, default, smpte_2059_2 ]
97 state:
98 description:
99 - Use C(absent) for removing.
100 - Use C(query) for listing an object or multiple objects.
101 - Use C(present) for creating or updating.
102 type: str
103 choices: [ absent, query, present ]
104 default: query
105 notes:
106 - The O(template) must exist before using this module in your playbook.
107 The M(cisco.mso.ndo_template) module can be used for this.
108 seealso:
109 - module: cisco.mso.ndo_template
110 extends_documentation_fragment: cisco.mso.modules
111 """
112
113 EXAMPLES = r"""
114 - name: Create a new PTP policy
115 cisco.mso.ndo_ptp_policy:
116 host: mso_host
117 username: admin
118 password: SomeSecretPassword
119 template: ansible_fabric_policy_template
120 ptp_policy: ansible_test_ptp_policy
121 admin_state: enabled
122 fabric_sync_interval: -3
123 global_domain: 0
124 fabric_delay_interval: -2
125 global_priority1: 255
126 global_priority2: 255
127 fabric_announce_timeout: 3
128 fabric_announce_interval: 1
129 fabric_profile_template: aes67_2015
130 state: present
131 register: create_ptp
132
133 - name: Query a PTP policy with ptp_policy name
134 cisco.mso.ndo_ptp_policy:
135 host: mso_host
136 username: admin
137 password: SomeSecretPassword
138 template: ansible_fabric_policy_template
139 ptp_policy: ansible_test_ptp_policy
140 state: query
141 register: query_one
142
143 - name: Query a PTP policy with ptp_policy UUID
144 cisco.mso.ndo_ptp_policy:
145 host: mso_host
146 username: admin
147 password: SomeSecretPassword
148 template: ansible_fabric_policy_template
149 ptp_policy_uuid: "{{ create_ptp.current.uuid }}"
150 state: query
151 register: query_one_uuid
152
153 - name: Update a PTP policy name with UUID
154 cisco.mso.ndo_ptp_policy:
155 host: mso_host
156 username: admin
157 password: SomeSecretPassword
158 ptp_policy: ansible_test_ptp_policy_updated
159 ptp_policy_uuid: "{{ create_ptp.current.uuid }}"
160 state: present
161
162 - name: Delete a PTP policy
163 cisco.mso.ndo_ptp_policy:
164 host: mso_host
165 username: admin
166 password: SomeSecretPassword
167 template: ansible_test_ptp_policy_updated
168 ptp_policy: ansible_test_ptp_policy
169 state: absent
170
171 - name: Delete a PTP policy using UUID
172 cisco.mso.ndo_ptp_policy:
173 host: mso_host
174 username: admin
175 password: SomeSecretPassword
176 template: ansible_fabric_policy_template
177 ptp_policy_uuid: "{{ query_one.current.uuid }}"
178 state: absent
179 """
180
181 RETURN = r"""
182 """
183
184 from ansible.module_utils.basic import AnsibleModule
185 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
186 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
187 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
188 from ansible_collections.cisco.mso.plugins.module_utils.constants import PROFILE_TEMPLATE
189 import copy
190
191
192 def main():
193 argument_spec = mso_argument_spec()
194 argument_spec.update(
195 dict(
196 template=dict(type="str", required=True),
197 ptp_policy=dict(type="str", aliases=["name"]),
198 ptp_policy_uuid=dict(type="str", aliases=["uuid"]),
199 description=dict(type="str"),
200 admin_state=dict(type="str", choices=["enabled", "disabled"]),
201 fabric_sync_interval=dict(type="int"),
202 global_domain=dict(type="int"),
203 fabric_delay_interval=dict(type="int"),
204 global_priority1=dict(type="int"),
205 global_priority2=dict(type="int"),
206 fabric_announce_timeout=dict(type="int"),
207 fabric_announce_interval=dict(type="int"),
208 fabric_profile_template=dict(type="str", choices=["aes67_2015", "default", "smpte_2059_2"]),
209 state=dict(type="str", choices=["absent", "query", "present"], default="query"),
210 )
211 )
212
213 module = AnsibleModule(
214 argument_spec=argument_spec,
215 supports_check_mode=True,
216 required_if=[
217 ["state", "present", ["ptp_policy", "ptp_policy_uuid"], True],
218 ["state", "absent", ["ptp_policy", "ptp_policy_uuid"], True],
219 [
220 "state",
221 "present",
222 [
223 "global_priority1",
224 "global_priority2",
225 "global_domain",
226 "fabric_announce_timeout",
227 "fabric_announce_interval",
228 "fabric_sync_interval",
229 "fabric_delay_interval",
230 ],
231 ],
232 ],
233 )
234
235 mso = MSOModule(module)
236
237 template = module.params.get("template")
238 ptp_policy = module.params.get("ptp_policy")
239 ptp_policy_uuid = module.params.get("ptp_policy_uuid")
240 description = module.params.get("description")
241 admin_state = module.params.get("admin_state")
242 fabric_sync_interval = module.params.get("fabric_sync_interval")
243 global_domain = module.params.get("global_domain")
244 fabric_delay_interval = module.params.get("fabric_delay_interval")
245 global_priority1 = module.params.get("global_priority1")
246 global_priority2 = module.params.get("global_priority2")
247 fabric_announce_timeout = module.params.get("fabric_announce_timeout")
248 fabric_announce_interval = module.params.get("fabric_announce_interval")
249 fabric_profile_template = PROFILE_TEMPLATE.get(module.params.get("fabric_profile_template"))
250 state = module.params.get("state")
251
252 ops = []
253 match = None
254
255 mso_template = MSOTemplate(mso, "fabric_policy", template)
256 mso_template.validate_template("fabricPolicy")
257 object_description = "PTP Policy"
258
259 path = "/fabricPolicyTemplate/template/ptpPolicy"
260
261 existing_ptp_policies = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("ptpPolicy", {})
262
263 if ptp_policy or ptp_policy_uuid:
264 match = mso_template.get_object_by_key_value_pairs(
265 object_description,
266 [existing_ptp_policies],
267 [KVPair("uuid", ptp_policy_uuid) if ptp_policy_uuid else KVPair("name", ptp_policy)],
268 )
269 if match:
270 mso.existing = mso.previous = copy.deepcopy(match.details)
271 else:
272 mso.existing = mso.previous = existing_ptp_policies
273
274 if state == "present":
275 if match:
276 mso_values_update = {
277 "name": ptp_policy,
278 "description": description,
279 ("global", "adminState"): admin_state,
280 ("global", "fabSyncIntvl"): fabric_sync_interval,
281 ("global", "globalDomain"): global_domain,
282 ("global", "fabDelayIntvl"): fabric_delay_interval,
283 ("global", "prio1"): global_priority1,
284 ("global", "prio2"): global_priority2,
285 ("global", "fabAnnounceTimeout"): fabric_announce_timeout,
286 ("global", "fabAnnounceIntvl"): fabric_announce_interval,
287 ("global", "fabProfileTemplate"): fabric_profile_template,
288 }
289 append_update_ops_data(ops, match.details, path, mso_values_update)
290 mso.sanitize(match.details, collate=True)
291 else:
292 mso_values = {
293 "name": ptp_policy,
294 "description": description,
295 "global": {
296 "adminState": admin_state,
297 "fabSyncIntvl": fabric_sync_interval,
298 "globalDomain": global_domain,
299 "fabDelayIntvl": fabric_delay_interval,
300 "prio1": global_priority1,
301 "prio2": global_priority2,
302 "fabAnnounceTimeout": fabric_announce_timeout,
303 "fabAnnounceIntvl": fabric_announce_interval,
304 "fabProfileTemplate": fabric_profile_template,
305 },
306 }
307 mso.sanitize(mso_values)
308 ops.append(dict(op="add", path="{0}".format(path), value=mso.sent))
309
310 elif state == "absent":
311 if match:
312 ops.append(dict(op="remove", path="{0}".format(path)))
313
314 if not module.check_mode and ops:
315 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
316 ptp_policies = response.get("fabricPolicyTemplate", {}).get("template", {}).get("ptpPolicy", {})
317 match = mso_template.get_object_by_key_value_pairs(
318 object_description,
319 [ptp_policies],
320 [KVPair("uuid", ptp_policy_uuid) if ptp_policy_uuid else KVPair("name", ptp_policy)],
321 )
322 if match:
323 mso.existing = match.details
324 else:
325 mso.existing = {}
326 elif module.check_mode and state != "query":
327 mso.existing = mso.proposed if state == "present" else {}
328
329 mso.exit_json()
330
331
332 if __name__ == "__main__":
333 main()