Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Eric Girard <@netgirard>
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 = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "certified",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: aci_access_spine_switch_selector
21 short_description: Manage Fabric Access Policy Spine Switch Port Selectors (infra:SpineS)
22 description:
23 - Manage Fabric Access Policy Spine Switch Port Selectors on Cisco ACI fabrics.
24 options:
25 spine_switch_profile:
26 description:
27 - The name of the Fabric access policy spine switch profile.
28 type: str
29 aliases: [ spine_switch_profile_name, switch_profile, switch_profile_name ]
30 spine_switch_selector:
31 description:
32 - The name of the Fabric access spine switch port selector.
33 type: str
34 aliases: [ name, spine_switch_selector_name, switch_selector, switch_selector_name, access_port_selector, access_port_selector_name ]
35 description:
36 description:
37 - The description for the spine switch port selector.
38 type: str
39 policy_group:
40 description:
41 - The name of the fabric access policy group to be associated with the spine switch port selector.
42 type: str
43 aliases: [ policy_group_name ]
44 selector_type:
45 description:
46 - The host port selector type.
47 - If using a port block to specify range of switches, the type must be set to C(range).
48 type: str
49 choices: [ all, range ]
50 aliases: [ type ]
51 state:
52 description:
53 - Use C(present) or C(absent) for adding or removing.
54 - Use C(query) for listing an object or multiple objects.
55 type: str
56 choices: [ absent, present, query ]
57 default: present
58 extends_documentation_fragment:
59 - cisco.aci.aci
60 - cisco.aci.annotation
61 - cisco.aci.owner
62
63 notes:
64 - The I(spine_switch_profile) must exist before using this module in your playbook.
65 The M(cisco.aci.aci_access_spine_switch_profile) module can be used for this.
66 - If a I(policy_group) is used, it must exist before using this module in your playbook.
67 The M(cisco.aci.aci_switch_policy_group) module can be used for this.
68 seealso:
69 - module: cisco.aci.aci_access_spine_switch_profile
70 - name: APIC Management Information Model reference
71 description: More information about the internal APIC classes B(infra:SpineS) and B(infra:RsAccNodePGrp).
72 link: https://developer.cisco.com/docs/apic-mim-ref/
73 author:
74 - Eric Girard (@netgirard)
75 - Gaspard Micol (@gmicol)
76 """
77
78 EXAMPLES = r"""
79 - name: Add a switch policy spine profile selector (with policy group)
80 cisco.aci.aci_access_spine_switch_selector:
81 host: apic
82 username: admin
83 password: SomeSecretPassword
84 spine_switch_profile: sw_name
85 spine_switch_selector: spine_selector_name
86 selector_type: range
87 policy_group: somepolicygroupname
88 state: present
89 delegate_to: localhost
90
91 - name: Query a switch policy spine profile selector
92 cisco.aci.aci_access_spine_switch_selector:
93 host: apic
94 username: admin
95 password: SomeSecretPassword
96 spine_switch_profile: sw_name
97 spine_switch_selector: spine_selector_name
98 selector_type: range
99 state: query
100 delegate_to: localhost
101
102 - name: Query all switch policy spine profile selectors
103 cisco.aci.aci_access_spine_switch_selector:
104 host: apic
105 username: admin
106 password: SomeSecretPassword
107 state: query
108 delegate_to: localhost
109
110 - name: Remove a switch policy spine profile selector
111 cisco.aci.aci_access_spine_switch_selector:
112 host: apic
113 username: admin
114 password: SomeSecretPassword
115 spine_switch_profile: sw_name
116 spine_switch_selector: spine_selector_name
117 selector_type: range
118 state: absent
119 delegate_to: localhost
120 """
121
122 RETURN = r"""
123 current:
124 description: The existing configuration from the APIC after the module has finished
125 returned: success
126 type: list
127 sample:
128 [
129 {
130 "fvTenant": {
131 "attributes": {
132 "descr": "Production environment",
133 "dn": "uni/tn-production",
134 "name": "production",
135 "nameAlias": "",
136 "ownerKey": "",
137 "ownerTag": ""
138 }
139 }
140 }
141 ]
142 error:
143 description: The error information as returned from the APIC
144 returned: failure
145 type: dict
146 sample:
147 {
148 "code": "122",
149 "text": "unknown managed object class foo"
150 }
151 raw:
152 description: The raw output returned by the APIC REST API (xml or json)
153 returned: parse error
154 type: str
155 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
156 sent:
157 description: The actual/minimal configuration pushed to the APIC
158 returned: info
159 type: list
160 sample:
161 {
162 "fvTenant": {
163 "attributes": {
164 "descr": "Production environment"
165 }
166 }
167 }
168 previous:
169 description: The original configuration from the APIC before the module has started
170 returned: info
171 type: list
172 sample:
173 [
174 {
175 "fvTenant": {
176 "attributes": {
177 "descr": "Production",
178 "dn": "uni/tn-production",
179 "name": "production",
180 "nameAlias": "",
181 "ownerKey": "",
182 "ownerTag": ""
183 }
184 }
185 }
186 ]
187 proposed:
188 description: The assembled configuration from the user-provided parameters
189 returned: info
190 type: dict
191 sample:
192 {
193 "fvTenant": {
194 "attributes": {
195 "descr": "Production environment",
196 "name": "production"
197 }
198 }
199 }
200 filter_string:
201 description: The filter string used for the request
202 returned: failure or debug
203 type: str
204 sample: ?rsp-prop-include=config-only
205 method:
206 description: The HTTP method used for the request to the APIC
207 returned: failure or debug
208 type: str
209 sample: POST
210 response:
211 description: The HTTP response from the APIC
212 returned: failure or debug
213 type: str
214 sample: OK (30 bytes)
215 status:
216 description: The HTTP status from the APIC
217 returned: failure or debug
218 type: int
219 sample: 200
220 url:
221 description: The HTTP url used for the request to the APIC
222 returned: failure or debug
223 type: str
224 sample: https://10.11.12.13/api/mo/uni/tn-production.json
225 """
226
227 from ansible.module_utils.basic import AnsibleModule
228 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
229 from ansible_collections.cisco.aci.plugins.module_utils.constants import MATCH_ACCESS_POLICIES_SELECTOR_TYPE
230
231
232 def main():
233 argument_spec = aci_argument_spec()
234 argument_spec.update(aci_annotation_spec())
235 argument_spec.update(aci_owner_spec())
236 argument_spec.update(
237 spine_switch_profile=dict(type="str", aliases=["spine_switch_profile_name", "switch_profile", "switch_profile_name"]),
238 spine_switch_selector=dict(
239 type="str",
240 aliases=[
241 "name",
242 "spine_switch_selector_name",
243 "switch_selector",
244 "switch_selector_name",
245 "access_port_selector",
246 "access_port_selector_name",
247 ],
248 ), # Not required for querying all objects
249 description=dict(type="str"),
250 policy_group=dict(type="str", aliases=["policy_group_name"]),
251 selector_type=dict(type="str", choices=list(MATCH_ACCESS_POLICIES_SELECTOR_TYPE.keys()), aliases=["type"]),
252 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
253 )
254
255 module = AnsibleModule(
256 argument_spec=argument_spec,
257 supports_check_mode=True,
258 required_if=[
259 ["state", "absent", ["spine_switch_profile", "spine_switch_selector", "selector_type"]],
260 ["state", "present", ["spine_switch_profile", "spine_switch_selector", "selector_type"]],
261 ],
262 )
263
264 spine_switch_profile = module.params.get("spine_switch_profile")
265 spine_switch_selector = module.params.get("spine_switch_selector")
266 description = module.params.get("description")
267 policy_group = module.params.get("policy_group")
268 selector_type = MATCH_ACCESS_POLICIES_SELECTOR_TYPE.get(module.params.get("selector_type"))
269 state = module.params.get("state")
270
271 child_configs = []
272 if policy_group is not None:
273 child_configs.append(dict(infraRsSpineAccNodePGrp=dict(attributes=dict(tDn="uni/infra/funcprof/spaccnodepgrp-{0}".format(policy_group)))))
274
275 aci = ACIModule(module)
276
277 aci.construct_url(
278 root_class=dict(
279 aci_class="infraInfra",
280 aci_rn="infra",
281 ),
282 subclass_1=dict(
283 aci_class="infraSpineP",
284 aci_rn="spprof-{0}".format(spine_switch_profile),
285 module_object=spine_switch_profile,
286 target_filter={"name": spine_switch_profile},
287 ),
288 subclass_2=dict(
289 aci_class="infraSpineS",
290 aci_rn="spines-{0}-typ-{1}".format(spine_switch_selector, selector_type),
291 module_object=spine_switch_selector,
292 target_filter={"name": spine_switch_selector},
293 ),
294 child_classes=["infraRsSpineAccNodePGrp"],
295 )
296
297 aci.get_existing()
298
299 if state == "present":
300 aci.payload(
301 aci_class="infraSpineS",
302 class_config=dict(
303 descr=description,
304 name=spine_switch_selector,
305 type=selector_type,
306 ),
307 child_configs=child_configs,
308 )
309
310 aci.get_diff(aci_class="infraSpineS")
311
312 aci.post_config()
313
314 elif state == "absent":
315 aci.delete_config()
316
317 aci.exit_json()
318
319
320 if __name__ == "__main__":
321 main()