Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_fabric_switch_policy_group.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Tim Cragg (@timcragg)
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_fabric_switch_policy_group
16 short_description: Manage Fabric Switch Policy Group objects (fabric:LeNodePGrp and fabric:SpNodePGrp)
17 description:
18 - Manage Fabric Switch Policy Group configuration on Cisco ACI fabrics.
19 options:
20 name:
21 description:
22 - The name of the Fabric Switch Policy Group.
23 type: str
24 aliases: [ 'policy_group', 'policy_group_name' ]
25 description:
26 description:
27 - Description for the Fabric Switch Policy Group.
28 type: str
29 switch_type:
30 description:
31 - Whether this is a leaf or spine Fabric Switch Policy Group.
32 type: str
33 choices: [ leaf, spine ]
34 required: true
35 monitoring_policy:
36 description:
37 - Monitoring Policy to attach to this Fabric Switch Policy Group.
38 type: str
39 aliases: [ 'monitoring', 'fabricRsMonInstFabricPol' ]
40 tech_support_export_policy:
41 description:
42 - Tech Support Export Policy to attach to this Fabric Switch Policy Group.
43 type: str
44 aliases: [ 'tech_support', 'tech_support_export', 'fabricRsNodeTechSupP']
45 core_export_policy:
46 description:
47 - Core Export Policy to attach to this Fabric Switch Policy Group.
48 type: str
49 aliases: [ 'core', 'core_export', 'fabricRsNodeCoreP' ]
50 inventory_policy:
51 description:
52 - Inventory Policy to attach to this Fabric Switch Policy Group.
53 type: str
54 aliases: [ 'inventory', 'fabricRsCallhomeInvPol' ]
55 power_redundancy_policy:
56 description:
57 - Power Redundancy Policy to atttach to this Fabric Switch Policy Group.
58 type: str
59 aliases: [ 'power_redundancy', 'fabricRsPsuInstPol' ]
60 twamp_server_policy:
61 description:
62 - TWAMP Server Policy to attach to this Fabric Switch Policy Group.
63 type: str
64 aliases: [ 'twamp_server', 'fabricRsTwampServerPol' ]
65 twamp_responder_policy:
66 description:
67 - TWAMP Responder Policy to attach to this Fabric Switch Policy Group.
68 type: str
69 aliases: [ 'twamp_responder', 'fabricRsTwampResponderPol' ]
70 node_control_policy:
71 description:
72 - Node Control Policy to attach to this Fabric Switch Policy Group.
73 type: str
74 aliases: [ 'node_control', 'fabricRsNodeCtrl' ]
75 analytics_cluster:
76 description:
77 - Name of the analytics cluster. Requires analytics_name to be present.
78 type: str
79 analytics_name:
80 description:
81 - Name of the analytics policy. Requires analytics_cluster to be present.
82 type: str
83 state:
84 description:
85 - Use C(present) or C(absent) for adding or removing.
86 - Use C(query) for listing an object or multiple objects.
87 type: str
88 choices: [ absent, present, query ]
89 default: present
90 extends_documentation_fragment:
91 - cisco.aci.aci
92 - cisco.aci.annotation
93
94 seealso:
95 - name: APIC Management Information Model reference
96 description: More information about the internal APIC classes B(fabric:LeNodePGrp) and B(fabric:SpNodePGrp).
97 link: https://developer.cisco.com/docs/apic-mim-ref/
98 author:
99 - Tim Cragg (@timcragg)
100 """
101
102 EXAMPLES = r"""
103 - name: Add a new Fabric Leaf Policy Group
104 cisco.aci.aci_fabric_switch_policy_group:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 name: my_fabric_leaf_policy_group
109 switch_type: leaf
110 monitoring_policy: my_monitor_policy
111 inventory_policy: my_inv_policy
112 state: present
113 delegate_to: localhost
114
115 - name: Remove existing analytics and monitoring policy bindings from a Policy Group
116 cisco.aci.aci_fabric_switch_policy_group:
117 host: apic
118 username: admin
119 password: SomeSecretPassword
120 name: my_fabric_leaf_policy_group
121 switch_type: leaf
122 monitoring_policy: ""
123 analytics_cluster: ""
124 analytics_name: ""
125 state: present
126 delegate_to: localhost
127
128 - name: Remove a Fabric Leaf Policy Group
129 cisco.aci.aci_fabric_switch_policy_group:
130 host: apic
131 username: admin
132 password: SomeSecretPassword
133 name: my_fabric_leaf_policy_group
134 switch_type: leaf
135 state: absent
136 delegate_to: localhost
137
138 - name: Query a Fabric Leaf Policy Group
139 cisco.aci.aci_fabric_switch_policy_group:
140 host: apic
141 username: admin
142 password: SomeSecretPassword
143 name: my_fabric_leaf_policy_group
144 switch_type: leaf
145 state: query
146 delegate_to: localhost
147 register: query_result
148
149 - name: Query all Fabric Leaf Policy Groups
150 cisco.aci.aci_fabric_switch_policy_group:
151 host: apic
152 username: admin
153 password: SomeSecretPassword
154 switch_type: leaf
155 state: query
156 delegate_to: localhost
157 register: query_result
158 """
159
160 RETURN = r"""
161 current:
162 description: The existing configuration from the APIC after the module has finished
163 returned: success
164 type: list
165 sample:
166 [
167 {
168 "fvTenant": {
169 "attributes": {
170 "descr": "Production environment",
171 "dn": "uni/tn-production",
172 "name": "production",
173 "nameAlias": "",
174 "ownerKey": "",
175 "ownerTag": ""
176 }
177 }
178 }
179 ]
180 error:
181 description: The error information as returned from the APIC
182 returned: failure
183 type: dict
184 sample:
185 {
186 "code": "122",
187 "text": "unknown managed object class foo"
188 }
189 raw:
190 description: The raw output returned by the APIC REST API (xml or json)
191 returned: parse error
192 type: str
193 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
194 sent:
195 description: The actual/minimal configuration pushed to the APIC
196 returned: info
197 type: list
198 sample:
199 {
200 "fvTenant": {
201 "attributes": {
202 "descr": "Production environment"
203 }
204 }
205 }
206 previous:
207 description: The original configuration from the APIC before the module has started
208 returned: info
209 type: list
210 sample:
211 [
212 {
213 "fvTenant": {
214 "attributes": {
215 "descr": "Production",
216 "dn": "uni/tn-production",
217 "name": "production",
218 "nameAlias": "",
219 "ownerKey": "",
220 "ownerTag": ""
221 }
222 }
223 }
224 ]
225 proposed:
226 description: The assembled configuration from the user-provided parameters
227 returned: info
228 type: dict
229 sample:
230 {
231 "fvTenant": {
232 "attributes": {
233 "descr": "Production environment",
234 "name": "production"
235 }
236 }
237 }
238 filter_string:
239 description: The filter string used for the request
240 returned: failure or debug
241 type: str
242 sample: ?rsp-prop-include=config-only
243 method:
244 description: The HTTP method used for the request to the APIC
245 returned: failure or debug
246 type: str
247 sample: POST
248 response:
249 description: The HTTP response from the APIC
250 returned: failure or debug
251 type: str
252 sample: OK (30 bytes)
253 status:
254 description: The HTTP status from the APIC
255 returned: failure or debug
256 type: int
257 sample: 200
258 url:
259 description: The HTTP url used for the request to the APIC
260 returned: failure or debug
261 type: str
262 sample: https://10.11.12.13/api/mo/uni/tn-production.json
263 """
264
265 from ansible.module_utils.basic import AnsibleModule
266 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
267
268 ACI_CLASS_MAPPING = dict(
269 spine={
270 "class": "fabricSpNodePGrp",
271 "rn": "spnodepgrp-",
272 },
273 leaf={
274 "class": "fabricLeNodePGrp",
275 "rn": "lenodepgrp-",
276 },
277 )
278
279
280 def main():
281 argument_spec = aci_argument_spec()
282 argument_spec.update(aci_annotation_spec())
283 argument_spec.update(
284 name=dict(type="str", aliases=["policy_group", "policy_group_name"]),
285 switch_type=dict(type="str", choices=["leaf", "spine"], required=True),
286 monitoring_policy=dict(type="str", aliases=["monitoring", "fabricRsMonInstFabricPol"]),
287 tech_support_export_policy=dict(type="str", aliases=["tech_support", "tech_support_export", "fabricRsNodeTechSupP"]),
288 core_export_policy=dict(type="str", aliases=["core", "core_export", "fabricRsNodeCoreP"]),
289 inventory_policy=dict(type="str", aliases=["inventory", "fabricRsCallhomeInvPol"]),
290 power_redundancy_policy=dict(type="str", aliases=["power_redundancy", "fabricRsPsuInstPol"]),
291 twamp_server_policy=dict(type="str", aliases=["twamp_server", "fabricRsTwampServerPol"]),
292 twamp_responder_policy=dict(type="str", aliases=["twamp_responder", "fabricRsTwampResponderPol"]),
293 node_control_policy=dict(type="str", aliases=["node_control", "fabricRsNodeCtrl"]),
294 analytics_cluster=dict(type="str"),
295 analytics_name=dict(type="str"),
296 description=dict(type="str"),
297 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
298 )
299
300 module = AnsibleModule(
301 argument_spec=argument_spec,
302 supports_check_mode=True,
303 required_if=[
304 ["state", "absent", ["name"]],
305 ["state", "present", ["name"]],
306 ],
307 required_together=[
308 ("analytics_cluster", "analytics_name"),
309 ],
310 )
311
312 name = module.params.get("name")
313 switch_type = module.params.get("switch_type")
314 description = module.params.get("description")
315 monitoring_policy = module.params.get("monitoring_policy")
316 tech_support_export_policy = module.params.get("tech_support_export_policy")
317 core_export_policy = module.params.get("core_export_policy")
318 inventory_policy = module.params.get("inventory_policy")
319 power_redundancy_policy = module.params.get("power_redundancy_policy")
320 twamp_server_policy = module.params.get("twamp_server_policy")
321 twamp_responder_policy = module.params.get("twamp_responder_policy")
322 node_control_policy = module.params.get("node_control_policy")
323 analytics_cluster = module.params.get("analytics_cluster")
324 analytics_name = module.params.get("analytics_name")
325 state = module.params.get("state")
326 child_classes = [
327 "fabricRsMonInstFabricPol",
328 "fabricRsNodeTechSupP",
329 "fabricRsNodeCoreP",
330 "fabricRsCallhomeInvPol",
331 "fabricRsPsuInstPol",
332 "fabricRsTwampServerPol",
333 "fabricRsTwampResponderPol",
334 "fabricRsNodeCtrl",
335 "fabricRsNodeCfgSrv",
336 ]
337
338 aci_class = ACI_CLASS_MAPPING[switch_type]["class"]
339 aci_rn = ACI_CLASS_MAPPING[switch_type]["rn"]
340
341 aci = ACIModule(module)
342 aci.construct_url(
343 root_class=dict(
344 aci_class=aci_class,
345 aci_rn="fabric/funcprof/{0}{1}".format(aci_rn, name),
346 module_object=name,
347 target_filter={"name": name},
348 ),
349 child_classes=child_classes,
350 )
351
352 aci.get_existing()
353
354 if state == "present":
355 child_configs = []
356
357 if monitoring_policy is not None:
358 child_configs.append(dict(fabricRsMonInstFabricPol=dict(attributes=dict(tnMonFabricPolName=monitoring_policy))))
359 if tech_support_export_policy is not None:
360 child_configs.append(dict(fabricRsNodeTechSupP=dict(attributes=dict(tnDbgexpTechSupPName=tech_support_export_policy))))
361 if core_export_policy is not None:
362 child_configs.append(dict(fabricRsNodeCoreP=dict(attributes=dict(tnDbgexpCorePName=core_export_policy))))
363 if inventory_policy is not None:
364 child_configs.append(dict(fabricRsCallhomeInvPol=dict(attributes=dict(tnCallhomeInvPName=inventory_policy))))
365 if power_redundancy_policy is not None:
366 child_configs.append(dict(fabricRsPsuInstPol=dict(attributes=dict(tnPsuInstPolName=power_redundancy_policy))))
367 if twamp_server_policy is not None:
368 child_configs.append(dict(fabricRsTwampServerPol=dict(attributes=dict(tnTwampServerPolName=twamp_server_policy))))
369 if twamp_responder_policy is not None:
370 child_configs.append(dict(fabricRsTwampResponderPol=dict(attributes=dict(tnTwampResponderPolName=twamp_responder_policy))))
371 if node_control_policy is not None:
372 child_configs.append(dict(fabricRsNodeCtrl=dict(attributes=dict(tnFabricNodeControlName=node_control_policy))))
373 if analytics_cluster and analytics_name:
374 analytics_tdn = "uni/fabric/analytics/cluster-{0}/cfgsrv-{1}".format(analytics_cluster, analytics_name)
375 child_configs.append(dict(fabricRsNodeCfgSrv=dict(attributes=dict(tDn=analytics_tdn))))
376
377 aci.payload(
378 aci_class=aci_class,
379 class_config=dict(
380 name=name,
381 descr=description,
382 ),
383 child_configs=child_configs,
384 )
385
386 aci.get_diff(aci_class=aci_class)
387
388 aci.post_config()
389
390 elif state == "absent":
391 aci.delete_config()
392
393 aci.exit_json()
394
395
396 if __name__ == "__main__":
397 main()