Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Akini Ross (@akinross) <[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_pod_profile
17 version_added: "2.11.0"
18 short_description: Manage Pod Profiles on Cisco Nexus Dashboard Orchestrator (NDO).
19 description:
20 - Manage Pod Profiles on Cisco Nexus Dashboard Orchestrator (NDO).
21 - This module is only supported on ND v3.2 (NDO v4.4) and later.
22 author:
23 - Akini Ross (@akinross)
24 options:
25 template:
26 description:
27 - The name of the template.
28 - The template must be a Fabric Resource Policy template.
29 - This parameter or O(template_id) is required.
30 type: str
31 template_id:
32 description:
33 - The ID of the template.
34 - The template must be a Fabric Resource Policy template.
35 - This parameter or O(template) is required.
36 type: str
37 name:
38 description:
39 - The name of the Pod Profile.
40 type: str
41 aliases: [ pod_profile ]
42 uuid:
43 description:
44 - The UUID of the Pod Profile.
45 - This parameter is required when the O(name) needs to be updated.
46 type: str
47 aliases: [ pod_profile_uuid ]
48 description:
49 description:
50 - The description of the Pod Profile.
51 type: str
52 pods:
53 description:
54 - A list of pod IDs to be used in the Pod Profile.
55 - When provided, the Pod Profile will apply to pods specified in the range.
56 - When an empty list O(pods=[]) is provided, the Pod Profile will apply to all pods.
57 - When not provided on initial create, the Pod Profile will apply to all pods.
58 type: list
59 elements: int
60 aliases: [ blocks ]
61 pod_settings_uuid:
62 description:
63 - The UUID of the Pod Settings Policy to be used.
64 type: str
65 pod_settings:
66 description:
67 - The reference of the Pod Settings Policy to be used.
68 type: dict
69 suboptions:
70 name:
71 description:
72 - The name of the Pod Settings Policy.
73 type: str
74 required: true
75 template:
76 description:
77 - The name of the Pod Settings template.
78 - This parameter or O(pod_settings.template_id) is required when O(pod_settings) is used.
79 type: str
80 template_id:
81 description:
82 - The ID of the Pod Settings template.
83 - This parameter or O(pod_settings.template) is required when O(pod_settings) is used.
84 type: str
85 state:
86 description:
87 - Use C(absent) for removing.
88 - Use C(query) for listing an object or multiple objects.
89 - Use C(present) for creating or updating.
90 type: str
91 choices: [ absent, query, present ]
92 default: query
93 notes:
94 - The O(template) must exist before using this module in your playbook.
95 Use M(cisco.mso.ndo_template) to create the Fabric Resource Policy template.
96 - The O(pod_settings) or O(pod_settings_uuid) must exist before using it with this module in your playbook.
97 Use M(cisco.mso.ndo_pod_settings) to create the Pod Settings Policy.
98 seealso:
99 - module: cisco.mso.ndo_template
100 - module: cisco.mso.ndo_pod_settings
101 extends_documentation_fragment: cisco.mso.modules
102 """
103
104 EXAMPLES = r"""
105 - name: Create a Pod Profile
106 cisco.mso.ndo_pod_profile:
107 host: mso_host
108 username: admin
109 password: SomeSecretPassword
110 template: ansible_test
111 name: ansible_pod_profile
112 pod_settings:
113 name: ansible_pod_settings
114 template: ansible_fabric_policy_template
115 state: present
116 register: create_pod_profile
117
118 - name: Update a Pod Profile using name
119 cisco.mso.ndo_pod_profile:
120 host: mso_host
121 username: admin
122 password: SomeSecretPassword
123 template: ansible_test
124 name: ansible_pod_profile
125 pod_settings:
126 name: ansible_pod_settings
127 template: ansible_fabric_policy_template
128 description: Updated Pod Profile
129 pods:
130 - 1
131 - 2
132 state: present
133
134 - name: Update a Pod Profile using UUID
135 cisco.mso.ndo_pod_profile:
136 host: mso_host
137 username: admin
138 password: SomeSecretPassword
139 template: ansible_test
140 name: ansible_pod_profile_changed
141 uuid: "{{ create_pod_profile.current.uuid }}"
142 pod_settings_uuid: "{{ create_pod_settings.current.uuid }}"
143 state: present
144
145 - name: Query a Pod Profile using name
146 cisco.mso.ndo_pod_profile:
147 host: mso_host
148 username: admin
149 password: SomeSecretPassword
150 template: ansible_test
151 name: ansible_pod_profile
152 state: query
153 register: query_with_name
154
155 - name: Query a Pod Profile using UUID
156 cisco.mso.ndo_pod_profile:
157 host: mso_host
158 username: admin
159 password: SomeSecretPassword
160 template: ansible_test
161 uuid: "{{ create_pod_profile.current.uuid }}"
162 state: query
163 register: query_with_uuid
164
165 - name: Query all Pod Profile in a template
166 cisco.mso.ndo_pod_profile:
167 host: mso_host
168 username: admin
169 password: SomeSecretPassword
170 template: ansible_test
171 state: query
172 register: query_all_objects
173
174 - name: Delete a Pod Profile using Name
175 cisco.mso.ndo_pod_profile:
176 host: mso_host
177 username: admin
178 password: SomeSecretPassword
179 template: ansible_test
180 name: ansible_pod_profile
181 state: absent
182
183 - name: Delete a Pod Profile using UUID
184 cisco.mso.ndo_pod_profile:
185 host: mso_host
186 username: admin
187 password: SomeSecretPassword
188 template_id: ansible_test
189 uuid: "{{ create_pod_profile.current.uuid }}"
190 state: absent
191 """
192
193 RETURN = r"""
194 """
195
196 from ansible.module_utils.basic import AnsibleModule
197 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
198 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
199 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate
200 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
201 import copy
202
203 POD_SETTINGS_CACHE = {}
204
205
206 def main():
207 argument_spec = mso_argument_spec()
208 argument_spec.update(
209 template=dict(type="str"),
210 template_id=dict(type="str"),
211 name=dict(type="str", aliases=["pod_profile"]),
212 uuid=dict(type="str", aliases=["pod_profile_uuid"]),
213 description=dict(type="str"),
214 pods=dict(type="list", elements="int", aliases=["blocks"]), # UI uses blocks but chose pods since it is more descriptive
215 pod_settings=dict(
216 type="dict",
217 options=dict(
218 name=dict(type="str", required=True),
219 template=dict(type="str"),
220 template_id=dict(type="str"),
221 ),
222 required_one_of=[
223 ["template", "template_id"],
224 ],
225 mutually_exclusive=[
226 ("template", "template_id"),
227 ],
228 ),
229 pod_settings_uuid=dict(type="str"),
230 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
231 )
232
233 module = AnsibleModule(
234 argument_spec=argument_spec,
235 supports_check_mode=True,
236 mutually_exclusive=[
237 ("template", "template_id"),
238 ("pod_settings", "pod_settings_uuid"),
239 ],
240 required_if=[
241 ["state", "absent", ["name", "uuid"], True],
242 ["state", "present", ["name", "uuid"], True],
243 ["state", "present", ["pod_settings", "pod_settings_uuid"], True],
244 ],
245 required_one_of=[
246 ["template", "template_id"],
247 ],
248 )
249
250 mso = MSOModule(module)
251 mso_templates = MSOTemplates(mso)
252
253 template_name = module.params.get("template")
254 template_id = module.params.get("template_id")
255 name = module.params.get("name")
256 uuid = module.params.get("uuid")
257 description = module.params.get("description")
258 pods = module.params.get("pods")
259 pod_settings = module.params.get("pod_settings")
260 pod_settings_uuid = module.params.get("pod_settings_uuid")
261 state = module.params.get("state")
262
263 mso_template = mso_templates.get_template("fabric_resource", template_name, template_id)
264 mso_template.validate_template("fabricResource")
265
266 match = mso_template.get_pod_profile_object(uuid, name)
267
268 if (uuid or name) and match:
269 set_pod_profile_policy_names(mso, mso_templates, mso_template, match.details)
270 mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object
271 elif match:
272 mso.existing = [set_pod_profile_policy_names(mso, mso_templates, mso_template, obj) for obj in match] # Query all objects
273
274 pod_profile_path = "/fabricResourceTemplate/template/podProfiles/{0}".format(match.index if match else "-")
275
276 ops = []
277
278 if state == "present":
279
280 mso_values = {
281 "name": name,
282 "description": description,
283 }
284
285 if pods:
286 mso_values["blocks"] = pods
287 mso_values["kind"] = "podRange"
288 elif pods == []:
289 mso_values["kind"] = "all"
290
291 if pod_settings_uuid:
292 mso_values["policy"] = pod_settings_uuid
293 else:
294 pod_settings_mso_template = mso_templates.get_template("fabric_policy", pod_settings.get("template"), pod_settings.get("template_id"))
295 mso_values["policy"] = pod_settings_mso_template.get_pod_settings_object(
296 pod_settings_uuid, pod_settings.get("name"), fail_module=True
297 ).details.get("uuid")
298
299 if match:
300 remove_data = []
301 if pods == []:
302 remove_data.append("blocks")
303
304 append_update_ops_data(ops, match.details, pod_profile_path, mso_values, remove_data)
305 mso.sanitize(mso_values, collate=True, unwanted=remove_data)
306 set_pod_profile_policy_names(mso, mso_templates, mso_template, mso.proposed)
307 else:
308 if not pods:
309 mso_values["kind"] = "all"
310 mso.sanitize(mso_values)
311 ops.append(dict(op="add", path=pod_profile_path, value=mso.sent))
312
313 elif state == "absent":
314 if match:
315 ops.append(dict(op="remove", path=pod_profile_path))
316
317 if not module.check_mode and ops:
318 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
319 match = mso_template.get_pod_profile_object(uuid, name, search_object=response)
320 if match:
321 set_pod_profile_policy_names(mso, mso_templates, mso_template, match.details)
322 mso.existing = match.details # When the state is present
323 else:
324 mso.existing = {} # When the state is absent
325 elif module.check_mode and state != "query": # When the state is present/absent with check mode
326 if mso.proposed:
327 set_pod_profile_policy_names(mso, mso_templates, mso_template, mso.proposed)
328 mso.existing = mso.proposed if state == "present" else {}
329 mso.exit_json()
330
331
332 def set_pod_profile_policy_names(mso, mso_templates, mso_template, pod_profile):
333
334 # TODO: leverage update_config_with_template_and_references when podPolicyGroup type is supported by objects API endpoint
335 # "mso/api/v1/templates/objects?type=podPolicyGroup&uuid=0809339e-65f7-4000-a23a-a39241865db8"
336 # {
337 # "code": 400,
338 # "message": "unsupported policy type: podPolicyGroup"
339 # }
340 # reference_dict = {
341 # "policy": {
342 # "name": "pod_settings",
343 # "reference": "policy",
344 # "type": "podPolicyGroup",
345 # }
346 # }
347 # if pod_profile and pod_profile.get("policy"):
348 # mso_template.update_config_with_template_and_references(pod_profile, reference_dict)
349
350 # Workaround template looping until podPolicyGroup type is supported by objects API endpoint
351 # Check if a policy is set in the pod profile, this is the UUID of the pod settings policy
352
353 pod_profile["templateName"] = mso_template.template_name
354 pod_profile["templateId"] = mso_template.template_id
355 if pod_profile.get("policy"):
356 pod_settings_uuid = pod_profile.get("policy")
357
358 # Check if the pod settings UUID is already in the cache
359 if pod_settings_uuid in POD_SETTINGS_CACHE:
360 pod_profile_details = POD_SETTINGS_CACHE[pod_settings_uuid]
361 pod_profile["policyName"] = pod_profile_details.get("policyName")
362 pod_profile["policyTemplateName"] = pod_profile_details.get("policyTemplateName")
363 pod_profile["policyTemplateId"] = pod_profile_details.get("policyTemplateId")
364 return pod_profile
365
366 # Retrieve a summary of all fabric_policy templates from NDO and loop through all templates to find the pod settings policy
367 for template in MSOTemplate(mso, "fabric_policy").template:
368 for policy in template.get("policies", []):
369 # Only retrieve the template details if the fabric_policy template has a pod settings policy defined
370 if policy.get("objType") == "podPolicyGroup" and policy.get("count") > 0:
371 pod_settings_mso_template = mso_templates.get_template("fabric_policy", template.get("templateName"), template.get("templateId"))
372 match = pod_settings_mso_template.get_pod_settings_object(pod_settings_uuid)
373 if match:
374 pod_profile["policyName"] = match.details.get("name")
375 pod_profile["policyTemplateName"] = pod_settings_mso_template.template_name
376 pod_profile["policyTemplateId"] = pod_settings_mso_template.template_id
377 POD_SETTINGS_CACHE[pod_settings_uuid] = {
378 "policyName": match.details.get("name"),
379 "policyTemplateName": pod_settings_mso_template.template_name,
380 "policyTemplateId": pod_settings_mso_template.template_id,
381 }
382 return pod_profile
383
384
385 if __name__ == "__main__":
386 main()