Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_ptp_policy_profiles.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_profiles
17 short_description: Manage PTP Policy Profiles on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage PTP Policy Profiles 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_profile_name:
31 description:
32 - The name of the PTP Policy profile.
33 type: str
34 aliases: [ name ]
35 ptp_policy_profile_uuid:
36 description:
37 - The UUID of the PTP Policy profile.
38 type: str
39 aliases: [ uuid ]
40 delay_interval:
41 description:
42 - Delay interval of the PTP Policy profile.
43 - The value should be between -4 to 4.
44 - This is required when O(state=present).
45 type: int
46 profile_template:
47 description:
48 - Profile template of the PTP Policy profile.
49 - The default value is aes67_2015.
50 type: str
51 choices: [ aes67_2015, default, telecom_8275_1, smpte_2059_2 ]
52 sync_interval:
53 description:
54 - Sync interval of the PTP Policy profile.
55 - The value should be between -4 to -1.
56 - This is required when O(state=present).
57 type: int
58 override_node_profile:
59 description:
60 - Node profile override of the PTP Policy profile.
61 type: bool
62 announce_timeout:
63 description:
64 - Announce timeout of the PTP Policy profile.
65 - The value should be between 2 to 10.
66 - This is required when O(state=present).
67 type: int
68 announce_interval:
69 description:
70 - Announce interval of the PTP Policy profile.
71 - The value should be between -3 to 1.
72 - This is required when O(state=present).
73 type: int
74 state:
75 description:
76 - Use C(absent) for removing.
77 - Use C(query) for listing an object or multiple objects.
78 - Use C(present) for creating or updating.
79 type: str
80 choices: [ absent, query, present ]
81 default: query
82 notes:
83 - The O(template) and ptp_policy object must exist before using this module in your playbook.
84 The M(cisco.mso.ndo_template) and M(cisco.mso.ndo_ptp_policy) modules can be used for this.
85 seealso:
86 - module: cisco.mso.ndo_template
87 - module: cisco.mso.ndo_ptp_policy
88 extends_documentation_fragment: cisco.mso.modules
89 """
90
91 EXAMPLES = r"""
92 - name: Create a new PTP profile
93 cisco.mso.ndo_ptp_policy_profiles:
94 host: mso_host
95 username: admin
96 password: SomeSecretPassword
97 template: ansible_fabric_policy_template
98 ptp_policy_profile_name: ansible_profile
99 delay_interval: 2
100 sync_interval: 1
101 announce_timeout: 3
102 announce_interval: 4
103 profile_template: aes67_2015
104 state: present
105 register: create_ptp_profile
106
107 - name: Query a PTP profile
108 cisco.mso.ndo_ptp_policy_profiles:
109 host: mso_host
110 username: admin
111 password: SomeSecretPassword
112 template: ansible_fabric_policy_template
113 ptp_policy_profile_name: ansible_profile
114 state: query
115 register: query_one
116
117 - name: Query a PTP profile using UUID
118 cisco.mso.ndo_ptp_policy_profiles:
119 host: mso_host
120 username: admin
121 password: SomeSecretPassword
122 template: ansible_fabric_policy_template
123 ptp_policy_profile_uuid: "{{ create_ptp_profile.current.uuid }}"
124 state: query
125 register: query_one_uuid
126
127 - name: Query all PTP profiles
128 cisco.mso.ndo_ptp_policy_profiles:
129 host: mso_host
130 username: admin
131 password: SomeSecretPassword
132 template: ansible_fabric_policy_template
133 state: query
134 register: query_all
135
136 - name: Update a PTP profile name with UUID
137 cisco.mso.ndo_ptp_policy_profiles:
138 host: mso_host
139 username: admin
140 password: SomeSecretPassword
141 ptp_policy_profile_name: profile_update
142 ptp_policy_profile_uuid: "{{ create_ptp_profile.current.uuid }}"
143
144 - name: Delete a PTP profile using name
145 cisco.mso.ndo_ptp_policy_profiles:
146 host: mso_host
147 username: admin
148 password: SomeSecretPassword
149 template: ansible_fabric_policy_template
150 ptp_policy_profile_name: ansible_profile
151 state: absent
152
153 - name: Delete a PTP profile using UUID
154 cisco.mso.ndo_ptp_policy_profiles:
155 host: mso_host
156 username: admin
157 password: SomeSecretPassword
158 template: ansible_fabric_policy_template
159 ptp_policy_profile_uuid: "{{ create_ptp_profile.current.uuid }}"
160 state: absent
161 """
162
163 RETURN = r"""
164 """
165
166 from ansible.module_utils.basic import AnsibleModule
167 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
168 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
169 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
170 from ansible_collections.cisco.mso.plugins.module_utils.constants import PROFILE_TEMPLATE
171 import copy
172
173
174 def main():
175 argument_spec = mso_argument_spec()
176 argument_spec.update(
177 dict(
178 template=dict(type="str", required=True),
179 ptp_policy_profile_name=dict(type="str", aliases=["name"]),
180 ptp_policy_profile_uuid=dict(type="str", aliases=["uuid"]),
181 delay_interval=dict(type="int"),
182 profile_template=dict(type="str", choices=list(PROFILE_TEMPLATE)),
183 sync_interval=dict(type="int"),
184 override_node_profile=dict(type="bool"),
185 announce_timeout=dict(type="int"),
186 announce_interval=dict(type="int"),
187 state=dict(type="str", choices=["absent", "query", "present"], default="query"),
188 )
189 )
190
191 module = AnsibleModule(
192 argument_spec=argument_spec,
193 supports_check_mode=True,
194 required_if=[
195 ["state", "present", ["ptp_policy_profile_name", "ptp_policy_profile_uuid"], True],
196 ["state", "absent", ["ptp_policy_profile_name", "ptp_policy_profile_uuid"], True],
197 ["state", "present", ["announce_interval", "announce_timeout", "sync_interval", "delay_interval"]],
198 ],
199 )
200
201 mso = MSOModule(module)
202
203 template = module.params.get("template")
204 ptp_policy_profile_name = module.params.get("ptp_policy_profile_name")
205 ptp_policy_profile_uuid = module.params.get("ptp_policy_profile_uuid")
206 delay_interval = module.params.get("delay_interval")
207 profile_template = PROFILE_TEMPLATE.get(module.params.get("profile_template"))
208 sync_interval = module.params.get("sync_interval")
209 override_node_profile = module.params.get("override_node_profile")
210 announce_timeout = module.params.get("announce_timeout")
211 announce_interval = module.params.get("announce_interval")
212 state = module.params.get("state")
213
214 ops = []
215 match = None
216
217 mso_template = MSOTemplate(mso, "fabric_policy", template)
218 mso_template.validate_template("fabricPolicy")
219 object_description = "Profiles"
220
221 path = "/fabricPolicyTemplate/template/ptpPolicy/profiles"
222
223 existing_profiles = mso_template.template.get("fabricPolicyTemplate", {}).get("template", {}).get("ptpPolicy", {}).get("profiles") or []
224
225 if ptp_policy_profile_name or ptp_policy_profile_uuid:
226 match = mso_template.get_object_by_key_value_pairs(
227 object_description,
228 existing_profiles,
229 [KVPair("uuid", ptp_policy_profile_uuid) if ptp_policy_profile_uuid else KVPair("name", ptp_policy_profile_name)],
230 )
231 if match:
232 mso.existing = mso.previous = copy.deepcopy(match.details)
233 else:
234 mso.existing = mso.previous = existing_profiles
235
236 if state == "present":
237 mso_values = dict(
238 name=ptp_policy_profile_name,
239 delayIntvl=delay_interval,
240 profileTemplate=profile_template,
241 syncIntvl=sync_interval,
242 nodeProfileOverride=override_node_profile,
243 announceTimeout=announce_timeout,
244 announceIntvl=announce_interval,
245 )
246
247 if match:
248 update_path = "{0}/{1}".format(path, match.index)
249 append_update_ops_data(ops, match.details, update_path, mso_values)
250 mso.sanitize(match.details, collate=True)
251 else:
252 mso.sanitize(mso_values)
253 ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent))
254
255 elif state == "absent":
256 if match:
257 ops.append(dict(op="remove", path="{0}/{1}".format(path, match.index)))
258
259 if not module.check_mode and ops:
260 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
261 profiles = response.get("fabricPolicyTemplate", {}).get("template", {}).get("ptpPolicy", {}).get("profiles") or []
262 match = mso_template.get_object_by_key_value_pairs(
263 object_description,
264 profiles,
265 [KVPair("uuid", ptp_policy_profile_uuid) if ptp_policy_profile_uuid else KVPair("name", ptp_policy_profile_name)],
266 )
267 if match:
268 mso.existing = match.details
269 else:
270 mso.existing = {}
271 elif module.check_mode and state != "query":
272 mso.existing = mso.proposed if state == "present" else {}
273
274 mso.exit_json()
275
276
277 if __name__ == "__main__":
278 main()