Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Akini Ross <[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_span_src_group
16 short_description: Manage Access SPAN source groups (span:SrcGrp)
17 description:
18 - Manage SPAN source groups on Cisco ACI fabrics.
19 options:
20 source_group:
21 description:
22 - The name of the Access SPAN source group.
23 type: str
24 aliases: [ name, src_group ]
25 description:
26 description:
27 - The description for Access SPAN source group.
28 type: str
29 aliases: [ descr ]
30 admin_state:
31 description:
32 - Enable C(true) or disable C(false) the SPAN sources.
33 - The APIC defaults to C(true) when unset during creation.
34 type: bool
35 filter_group:
36 description:
37 - The name of the Access SPAN filter group to associate with the source group.
38 type: str
39 destination_group:
40 description:
41 - The name of the Access SPAN destination group to associate with the source group.
42 type: str
43 aliases: [ dst_group ]
44 state:
45 description:
46 - Use C(present) or C(absent) for adding or removing.
47 - Use C(query) for listing an object or multiple objects.
48 type: str
49 choices: [ absent, present, query ]
50 default: present
51 name_alias:
52 description:
53 - The alias for the current object. This relates to the nameAlias field in ACI.
54 type: str
55 extends_documentation_fragment:
56 - cisco.aci.aci
57 - cisco.aci.annotation
58 - cisco.aci.owner
59
60 notes:
61 - The I(filter_group) and I(destination_group) must exist before using this module in your playbook.
62 The M(cisco.aci.aci_access_span_filter_group) and M(cisco.aci.aci_access_span_dst_group) modules can be used for this.
63 seealso:
64 - module: cisco.aci.aci_access_span_filter_group
65 - module: cisco.aci.aci_access_span_dst_group
66 - name: APIC Management Information Model reference
67 description: More information about the internal APIC class B(span:SrcGrp).
68 link: https://developer.cisco.com/docs/apic-mim-ref/
69 author:
70 - Akini Ross (@akinross)
71 """
72
73 EXAMPLES = r"""
74 - name: Create a Access SPAN source group
75 cisco.aci.aci_access_span_src_group:
76 host: apic
77 username: admin
78 password: SomeSecretPassword
79 source_group: my_span_source_group
80 destination_group: my_span_dest_group
81 state: present
82 delegate_to: localhost
83
84 - name: Delete a Access SPAN source group
85 cisco.aci.aci_access_span_src_group:
86 host: apic
87 username: admin
88 password: SomeSecretPassword
89 source_group: my_span_source_group
90 state: absent
91 delegate_to: localhost
92
93 - name: Query all Access SPAN source groups
94 cisco.aci.aci_access_span_src_group:
95 host: apic
96 username: admin
97 password: SomeSecretPassword
98 state: query
99 delegate_to: localhost
100 register: query_result
101
102 - name: Query a specific Access SPAN source group
103 cisco.aci.aci_access_span_src_group:
104 host: apic
105 username: admin
106 password: SomeSecretPassword
107 source_group: my_span_source_group
108 state: query
109 delegate_to: localhost
110 register: query_result
111 """
112
113 RETURN = r"""
114 current:
115 description: The existing configuration from the APIC after the module has finished
116 returned: success
117 type: list
118 sample:
119 [
120 {
121 "fvTenant": {
122 "attributes": {
123 "descr": "Production environment",
124 "dn": "uni/tn-production",
125 "name": "production",
126 "nameAlias": "",
127 "ownerKey": "",
128 "ownerTag": ""
129 }
130 }
131 }
132 ]
133 error:
134 description: The error information as returned from the APIC
135 returned: failure
136 type: dict
137 sample:
138 {
139 "code": "122",
140 "text": "unknown managed object class foo"
141 }
142 raw:
143 description: The raw output returned by the APIC REST API (xml or json)
144 returned: parse error
145 type: str
146 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
147 sent:
148 description: The actual/minimal configuration pushed to the APIC
149 returned: info
150 type: list
151 sample:
152 {
153 "fvTenant": {
154 "attributes": {
155 "descr": "Production environment"
156 }
157 }
158 }
159 previous:
160 description: The original configuration from the APIC before the module has started
161 returned: info
162 type: list
163 sample:
164 [
165 {
166 "fvTenant": {
167 "attributes": {
168 "descr": "Production",
169 "dn": "uni/tn-production",
170 "name": "production",
171 "nameAlias": "",
172 "ownerKey": "",
173 "ownerTag": ""
174 }
175 }
176 }
177 ]
178 proposed:
179 description: The assembled configuration from the user-provided parameters
180 returned: info
181 type: dict
182 sample:
183 {
184 "fvTenant": {
185 "attributes": {
186 "descr": "Production environment",
187 "name": "production"
188 }
189 }
190 }
191 filter_string:
192 description: The filter string used for the request
193 returned: failure or debug
194 type: str
195 sample: ?rsp-prop-include=config-only
196 method:
197 description: The HTTP method used for the request to the APIC
198 returned: failure or debug
199 type: str
200 sample: POST
201 response:
202 description: The HTTP response from the APIC
203 returned: failure or debug
204 type: str
205 sample: OK (30 bytes)
206 status:
207 description: The HTTP status from the APIC
208 returned: failure or debug
209 type: int
210 sample: 200
211 url:
212 description: The HTTP url used for the request to the APIC
213 returned: failure or debug
214 type: str
215 sample: https://10.11.12.13/api/mo/uni/tn-production.json
216 """
217
218 from ansible.module_utils.basic import AnsibleModule
219 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
220
221
222 def main():
223 argument_spec = aci_argument_spec()
224 argument_spec.update(aci_annotation_spec())
225 argument_spec.update(aci_owner_spec())
226 argument_spec.update(
227 source_group=dict(type="str", aliases=["name", "src_group"]), # Not required for querying all objects
228 description=dict(type="str", aliases=["descr"]),
229 admin_state=dict(type="bool"),
230 filter_group=dict(type="str"),
231 destination_group=dict(type="str", aliases=["dst_group"]),
232 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
233 name_alias=dict(type="str"),
234 )
235
236 module = AnsibleModule(
237 argument_spec=argument_spec,
238 supports_check_mode=True,
239 required_if=[
240 ["state", "absent", ["source_group"]],
241 ["state", "present", ["source_group", "destination_group"]],
242 ],
243 )
244
245 aci = ACIModule(module)
246
247 source_group = module.params.get("source_group")
248 description = module.params.get("description")
249 admin_state = aci.boolean(module.params.get("admin_state"), "enabled", "disabled")
250 filter_group = module.params.get("filter_group")
251 destination_group = module.params.get("destination_group")
252 state = module.params.get("state")
253 name_alias = module.params.get("name_alias")
254
255 aci.construct_url(
256 root_class=dict(
257 aci_class="infra",
258 aci_rn="infra",
259 ),
260 subclass_1=dict(
261 aci_class="spanSrcGrp",
262 aci_rn="srcgrp-{0}".format(source_group),
263 module_object=source_group,
264 target_filter={"name": source_group},
265 ),
266 child_classes=["spanSpanLbl", "spanRsSrcGrpToFilterGrp"],
267 )
268
269 aci.get_existing()
270
271 if state == "present":
272 # Create new child configs payload
273 filter_group_tdn = "uni/infra/filtergrp-{0}".format(filter_group)
274 child_configs = [{"spanSpanLbl": {"attributes": {"name": destination_group}}}]
275 if filter_group:
276 child_configs.append({"spanRsSrcGrpToFilterGrp": {"attributes": {"tDn": filter_group_tdn}}})
277
278 # Validate if existing and remove child objects when do not match provided configuration
279 if isinstance(aci.existing, list) and len(aci.existing) > 0:
280 for child in aci.existing[0].get("spanSrcGrp", {}).get("children", {}):
281 if child.get("spanRsSrcGrpToFilterGrp") and child.get("spanRsSrcGrpToFilterGrp").get("attributes").get("tDn") != filter_group_tdn:
282 # Appending to child_config list not possible because of APIC Error 103: child (Rn) of class spanRsSrcGrpToFilterGrp is already attached.
283 # A seperate delete request to dn of the spanRsSrcGrpToFilterGrp is needed to remove the object prior to adding to child_configs.
284 # Failed child_config is displayed in below:
285 #
286 # child_configs.append(
287 # {
288 # "spanRsSrcGrpToFilterGrp": {
289 # "attributes": {
290 # "dn": "uni/infra/srcgrp-{0}/rssrcGrpToFilterGrp".format(source_group),
291 # "status": "deleted",
292 # }
293 # }
294 # }
295 # )
296 aci.api_call("DELETE", "/api/mo/uni/infra/srcgrp-{0}/rssrcGrpToFilterGrp.json".format(source_group))
297 elif child.get("spanSpanLbl") and child.get("spanSpanLbl").get("attributes").get("name") != destination_group:
298 child_configs.append(
299 {
300 "spanSpanLbl": {
301 "attributes": {
302 "dn": "uni/infra/srcgrp-{0}/spanlbl-{1}".format(source_group, child.get("spanSpanLbl").get("attributes").get("name")),
303 "status": "deleted",
304 }
305 }
306 }
307 )
308
309 aci.payload(
310 aci_class="spanSrcGrp",
311 class_config=dict(adminSt=admin_state, descr=description, name=source_group, nameAlias=name_alias),
312 child_configs=child_configs,
313 )
314
315 aci.get_diff(aci_class="spanSrcGrp")
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()