Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Bruno Calogero <[email protected]>
5 # Copyright: (c) 2024, Gaspard Micol (@gmicol) <[email protected]>
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": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_switch_leaf_selector
17 short_description: Bind leaf selectors to switch policy leaf profiles (infra:LeafS, infra:NodeBlk, and infra:RsAccNodePGrp)
18 description:
19 - Bind leaf selectors (with node block range and policy group) to switch policy leaf profiles on Cisco ACI fabrics.
20 options:
21 description:
22 description:
23 - The description to assign to the C(leaf).
24 type: str
25 leaf_profile:
26 description:
27 - Name of the Leaf Profile to which we add a Selector.
28 type: str
29 aliases: [ leaf_profile_name ]
30 leaf:
31 description:
32 - Name of Leaf Selector.
33 type: str
34 aliases: [ name, leaf_name, leaf_profile_leaf_name, leaf_selector_name ]
35 leaf_node_blk:
36 description:
37 - Name of Node Block range to be added to Leaf Selector of given Leaf Profile.
38 type: str
39 aliases: [ leaf_node_blk_name, node_blk_name ]
40 leaf_node_blk_description:
41 description:
42 - The description to assign to the C(leaf_node_blk)
43 type: str
44 from:
45 description:
46 - Start of Node Block range.
47 type: int
48 aliases: [ node_blk_range_from, from_range, range_from ]
49 to:
50 description:
51 - Start of Node Block range.
52 type: int
53 aliases: [ node_blk_range_to, to_range, range_to ]
54 policy_group:
55 description:
56 - Name of the Policy Group to be added to Leaf Selector of given Leaf Profile.
57 type: str
58 aliases: [ policy_group_name ]
59 state:
60 description:
61 - Use C(present) or C(absent) for adding or removing.
62 - Use C(query) for listing an object or multiple objects.
63 type: str
64 choices: [ absent, present, query ]
65 default: present
66 name_alias:
67 description:
68 - The alias for the current object. This relates to the nameAlias field in ACI.
69 type: str
70 extends_documentation_fragment:
71 - cisco.aci.aci
72 - cisco.aci.annotation
73 - cisco.aci.owner
74
75 notes:
76 - This module is to be used with M(cisco.aci.aci_switch_policy_leaf_profile).
77 One first creates a leaf profile (infra:NodeP) and then creates an associated selector (infra:LeafS),
78 seealso:
79 - module: cisco.aci.aci_switch_policy_leaf_profile
80 - name: APIC Management Information Model reference
81 description: More information about the internal APIC classes B(infra:LeafS),
82 B(infra:NodeBlk) and B(infra:RsAccNodePGrp).
83 link: https://developer.cisco.com/docs/apic-mim-ref/
84 author:
85 - Bruno Calogero (@brunocalogero)
86 - Gaspard Micol (@gmicol)
87 """
88
89 EXAMPLES = r"""
90 - name: adding a switch policy leaf profile selector associated Node Block range (w/ policy group)
91 cisco.aci.aci_switch_leaf_selector:
92 host: apic
93 username: admin
94 password: SomeSecretPassword
95 leaf_profile: sw_name
96 leaf: leaf_selector_name
97 leaf_node_blk: node_blk_name
98 from: 1011
99 to: 1011
100 policy_group: somepolicygroupname
101 state: present
102 delegate_to: localhost
103
104 - name: adding a switch policy leaf profile selector associated Node Block range (w/o policy group)
105 cisco.aci.aci_switch_leaf_selector:
106 host: apic
107 username: admin
108 password: SomeSecretPassword
109 leaf_profile: sw_name
110 leaf: leaf_selector_name
111 leaf_node_blk: node_blk_name
112 from: 1011
113 to: 1011
114 state: present
115 delegate_to: localhost
116
117 - name: Removing a switch policy leaf profile selector
118 cisco.aci.aci_switch_leaf_selector:
119 host: apic
120 username: admin
121 password: SomeSecretPassword
122 leaf_profile: sw_name
123 leaf: leaf_selector_name
124 state: absent
125 delegate_to: localhost
126
127 - name: Querying a switch policy leaf profile selector
128 cisco.aci.aci_switch_leaf_selector:
129 host: apic
130 username: admin
131 password: SomeSecretPassword
132 leaf_profile: sw_name
133 leaf: leaf_selector_name
134 state: query
135 delegate_to: localhost
136 register: query_result
137 """
138
139 RETURN = r"""
140 current:
141 description: The existing configuration from the APIC after the module has finished
142 returned: success
143 type: list
144 sample:
145 [
146 {
147 "fvTenant": {
148 "attributes": {
149 "descr": "Production environment",
150 "dn": "uni/tn-production",
151 "name": "production",
152 "nameAlias": "",
153 "ownerKey": "",
154 "ownerTag": ""
155 }
156 }
157 }
158 ]
159 error:
160 description: The error information as returned from the APIC
161 returned: failure
162 type: dict
163 sample:
164 {
165 "code": "122",
166 "text": "unknown managed object class foo"
167 }
168 raw:
169 description: The raw output returned by the APIC REST API (xml or json)
170 returned: parse error
171 type: str
172 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
173 sent:
174 description: The actual/minimal configuration pushed to the APIC
175 returned: info
176 type: list
177 sample:
178 {
179 "fvTenant": {
180 "attributes": {
181 "descr": "Production environment"
182 }
183 }
184 }
185 previous:
186 description: The original configuration from the APIC before the module has started
187 returned: info
188 type: list
189 sample:
190 [
191 {
192 "fvTenant": {
193 "attributes": {
194 "descr": "Production",
195 "dn": "uni/tn-production",
196 "name": "production",
197 "nameAlias": "",
198 "ownerKey": "",
199 "ownerTag": ""
200 }
201 }
202 }
203 ]
204 proposed:
205 description: The assembled configuration from the user-provided parameters
206 returned: info
207 type: dict
208 sample:
209 {
210 "fvTenant": {
211 "attributes": {
212 "descr": "Production environment",
213 "name": "production"
214 }
215 }
216 }
217 filter_string:
218 description: The filter string used for the request
219 returned: failure or debug
220 type: str
221 sample: ?rsp-prop-include=config-only
222 method:
223 description: The HTTP method used for the request to the APIC
224 returned: failure or debug
225 type: str
226 sample: POST
227 response:
228 description: The HTTP response from the APIC
229 returned: failure or debug
230 type: str
231 sample: OK (30 bytes)
232 status:
233 description: The HTTP status from the APIC
234 returned: failure or debug
235 type: int
236 sample: 200
237 url:
238 description: The HTTP url used for the request to the APIC
239 returned: failure or debug
240 type: str
241 sample: https://10.11.12.13/api/mo/uni/tn-production.json
242 """
243
244 from ansible.module_utils.basic import AnsibleModule
245 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
246
247
248 def main():
249 argument_spec = aci_argument_spec()
250 argument_spec.update(aci_annotation_spec())
251 argument_spec.update(aci_owner_spec())
252 argument_spec.update(
253 {
254 "description": dict(type="str"),
255 "leaf_profile": dict(type="str", aliases=["leaf_profile_name"]), # Not required for querying all objects
256 "leaf": dict(type="str", aliases=["name", "leaf_name", "leaf_profile_leaf_name", "leaf_selector_name"]), # Not required for querying all objects
257 "leaf_node_blk": dict(type="str", aliases=["leaf_node_blk_name", "node_blk_name"]),
258 "leaf_node_blk_description": dict(type="str"),
259 # NOTE: Keyword 'from' is a reserved word in python, so we need it as a string
260 "from": dict(type="int", aliases=["node_blk_range_from", "from_range", "range_from"]),
261 "to": dict(type="int", aliases=["node_blk_range_to", "to_range", "range_to"]),
262 "policy_group": dict(type="str", aliases=["policy_group_name"]),
263 "state": dict(type="str", default="present", choices=["absent", "present", "query"]),
264 "name_alias": dict(type="str"),
265 }
266 )
267
268 module = AnsibleModule(
269 argument_spec=argument_spec,
270 supports_check_mode=True,
271 required_if=[["state", "absent", ["leaf_profile", "leaf"]], ["state", "present", ["leaf_profile", "leaf"]]],
272 )
273
274 description = module.params.get("description")
275 leaf_profile = module.params.get("leaf_profile")
276 leaf = module.params.get("leaf")
277 leaf_node_blk = module.params.get("leaf_node_blk")
278 leaf_node_blk_description = module.params.get("leaf_node_blk_description")
279 from_ = module.params.get("from")
280 to_ = module.params.get("to")
281 policy_group = module.params.get("policy_group")
282 state = module.params.get("state")
283 name_alias = module.params.get("name_alias")
284
285 child_configs = []
286 # Add infraNodeBlk only when leaf_node_blk was defined
287 if leaf_node_blk is not None:
288 child_configs.append(
289 dict(
290 infraNodeBlk=dict(
291 attributes=dict(
292 descr=leaf_node_blk_description,
293 name=leaf_node_blk,
294 from_=from_,
295 to_=to_,
296 ),
297 ),
298 ),
299 )
300
301 # Add infraRsAccNodePGrp only when policy_group was defined
302 if policy_group is not None:
303 child_configs.append(
304 dict(
305 infraRsAccNodePGrp=dict(
306 attributes=dict(
307 tDn="uni/infra/funcprof/accnodepgrp-{0}".format(policy_group),
308 ),
309 ),
310 )
311 )
312
313 aci = ACIModule(module)
314 aci.construct_url(
315 root_class=dict(
316 aci_class="infraNodeP",
317 aci_rn="infra/nprof-{0}".format(leaf_profile),
318 module_object=leaf_profile,
319 target_filter={"name": leaf_profile},
320 ),
321 subclass_1=dict(
322 aci_class="infraLeafS",
323 # NOTE: normal rn: leaves-{name}-typ-{type}, hence here hardcoded to range for purposes of module
324 aci_rn="leaves-{0}-typ-range".format(leaf),
325 module_object=leaf,
326 target_filter={"name": leaf},
327 ),
328 # NOTE: infraNodeBlk is not made into a subclass because there is a 1-1 mapping between node block and leaf selector name
329 child_classes=["infraNodeBlk", "infraRsAccNodePGrp"],
330 )
331
332 aci.get_existing()
333
334 if state == "present":
335 aci.payload(
336 aci_class="infraLeafS",
337 class_config=dict(
338 descr=description,
339 name=leaf,
340 nameAlias=name_alias,
341 ),
342 child_configs=child_configs,
343 )
344
345 aci.get_diff(aci_class="infraLeafS")
346
347 aci.post_config()
348
349 elif state == "absent":
350 aci.delete_config()
351
352 aci.exit_json()
353
354
355 if __name__ == "__main__":
356 main()