Debian Codegraph @

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

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