Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2024, Samita Bhattacharjee (@samitab) <samitab.cisco.com>
4
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_fabric_external_routing_profile
16 short_description: Manage Fabric External Routing Profiles (l3ext:FabricExtRoutingP)
17 description:
18 - Manage Fabric External Routing Profiles on Cisco ACI fabrics.
19 options:
20 name:
21 description:
22 - The name of the Fabric External Routing Profile.
23 type: str
24 aliases: [ routing_profile, profile ]
25 fabric_id:
26 description:
27 - The Fabric ID associated with the Fabric External Routing Profile.
28 type: int
29 aliases: [ fabric, fid]
30 description:
31 description:
32 - The description of the Fabric External Routing Profile.
33 type: str
34 aliases: [ descr ]
35 subnets:
36 description:
37 - The list of external subnet IP addresses.
38 - Duplicate subnet IP addresses are not valid and would be ignored.
39 type: list
40 elements: str
41 aliases: [ ip_addresses, ips ]
42 state:
43 description:
44 - Use C(present) or C(absent) for adding or removing.
45 - Use C(query) for listing an object or multiple objects.
46 type: str
47 choices: [ absent, present, query ]
48 default: present
49 extends_documentation_fragment:
50 - cisco.aci.aci
51 - cisco.aci.annotation
52 - cisco.aci.owner
53
54 notes:
55 - This module requires an existing I(fabric_external_connection_profile).
56 The module M(cisco.aci.aci_fabric_external_connection_profile) can be used for this.
57 seealso:
58 - name: APIC Management Information Model reference
59 description: More information about the internal APIC class B(l3ext:FabricExtRoutingP).
60 link: https://developer.cisco.com/docs/apic-mim-ref/
61 author:
62 - Samita Bhattacharjee (@samitab)
63 """
64
65 # TODO EXAMPLES
66 EXAMPLES = r"""
67 - name: Add an External Routing Profile
68 cisco.aci.aci_fabric_external_routing_profile:
69 host: apic
70 username: admin
71 password: SomeSecretPassword
72 fabric_id: "1"
73 description: "Fabric external routing profile"
74 name: "ansible_fabric_ext_routing_profile"
75 subnets:
76 - 1.2.3.4/24
77 - 5.6.7.8/24
78 state: present
79 delegate_to: localhost
80
81 - name: Query an External Routing Profile
82 cisco.aci.aci_fabric_external_routing_profile:
83 host: apic
84 username: admin
85 password: SomeSecretPassword
86 fabric_id: 1
87 name: ansible_fabric_ext_routing_profile
88 state: query
89 delegate_to: localhost
90 register: query_result
91
92 - name: Query all External Routing Profiles
93 cisco.aci.aci_fabric_external_routing_profile:
94 host: apic
95 username: admin
96 password: SomeSecretPassword
97 state: query
98 delegate_to: localhost
99 register: query_result
100
101 - name: Remove an External Routing Profile
102 cisco.aci.aci_fabric_external_routing_profile:
103 host: apic
104 username: admin
105 password: SomeSecretPassword
106 fabric_id: 1
107 name: ansible_fabric_ext_routing_profile
108 state: absent
109 delegate_to: localhost
110 """
111
112 RETURN = r"""
113 current:
114 description: The existing configuration from the APIC after the module has finished
115 returned: success
116 type: list
117 sample:
118 [
119 {
120 "fvTenant": {
121 "attributes": {
122 "descr": "Production environment",
123 "dn": "uni/tn-production",
124 "name": "production",
125 "nameAlias": "",
126 "ownerKey": "",
127 "ownerTag": ""
128 }
129 }
130 }
131 ]
132 error:
133 description: The error information as returned from the APIC
134 returned: failure
135 type: dict
136 sample:
137 {
138 "code": "122",
139 "text": "unknown managed object class foo"
140 }
141 raw:
142 description: The raw output returned by the APIC REST API (xml or json)
143 returned: parse error
144 type: str
145 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
146 sent:
147 description: The actual/minimal configuration pushed to the APIC
148 returned: info
149 type: list
150 sample:
151 {
152 "fvTenant": {
153 "attributes": {
154 "descr": "Production environment"
155 }
156 }
157 }
158 previous:
159 description: The original configuration from the APIC before the module has started
160 returned: info
161 type: list
162 sample:
163 [
164 {
165 "fvTenant": {
166 "attributes": {
167 "descr": "Production",
168 "dn": "uni/tn-production",
169 "name": "production",
170 "nameAlias": "",
171 "ownerKey": "",
172 "ownerTag": ""
173 }
174 }
175 }
176 ]
177 proposed:
178 description: The assembled configuration from the user-provided parameters
179 returned: info
180 type: dict
181 sample:
182 {
183 "fvTenant": {
184 "attributes": {
185 "descr": "Production environment",
186 "name": "production"
187 }
188 }
189 }
190 filter_string:
191 description: The filter string used for the request
192 returned: failure or debug
193 type: str
194 sample: ?rsp-prop-include=config-only
195 method:
196 description: The HTTP method used for the request to the APIC
197 returned: failure or debug
198 type: str
199 sample: POST
200 response:
201 description: The HTTP response from the APIC
202 returned: failure or debug
203 type: str
204 sample: OK (30 bytes)
205 status:
206 description: The HTTP status from the APIC
207 returned: failure or debug
208 type: int
209 sample: 200
210 url:
211 description: The HTTP url used for the request to the APIC
212 returned: failure or debug
213 type: str
214 sample: https://10.11.12.13/api/mo/uni/tn-production.json
215 """
216
217 from ansible.module_utils.basic import AnsibleModule
218 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
219
220
221 def main():
222 argument_spec = aci_argument_spec()
223 argument_spec.update(aci_annotation_spec())
224 argument_spec.update(aci_owner_spec())
225 argument_spec.update(
226 name=dict(type="str", aliases=["routing_profile", "profile"]),
227 fabric_id=dict(type="int", aliases=["fabric", "fid"]),
228 description=dict(type="str", aliases=["descr"]),
229 subnets=dict(type="list", elements="str", aliases=["ip_addresses", "ips"]),
230 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
231 )
232
233 module = AnsibleModule(
234 argument_spec=argument_spec,
235 supports_check_mode=True,
236 required_if=[
237 ["state", "absent", ["fabric_id", "name"]],
238 ["state", "present", ["fabric_id", "name"]],
239 ],
240 )
241
242 aci = ACIModule(module)
243
244 name = module.params.get("name")
245 fabric_id = module.params.get("fabric_id")
246 description = module.params.get("description")
247 subnets = module.params.get("subnets")
248 state = module.params.get("state")
249
250 # Remove duplicate subnets
251 if isinstance(subnets, list):
252 subnets = list(dict.fromkeys(subnets))
253
254 aci.construct_url(
255 root_class=dict(
256 aci_class="fvFabricExtConnP",
257 aci_rn="tn-infra/fabricExtConnP-{0}".format(fabric_id),
258 module_object=fabric_id,
259 target_filter={"id": fabric_id},
260 ),
261 subclass_1=dict(
262 aci_class="l3extFabricExtRoutingP",
263 aci_rn="fabricExtRoutingP-{0}".format(name),
264 module_object=name,
265 target_filter={"name": name},
266 ),
267 child_classes=["l3extSubnet"],
268 )
269
270 aci.get_existing()
271
272 if state == "present":
273 child_configs = []
274
275 # Validate if existing and remove subnet objects when the config does not match the provided config.
276 if isinstance(aci.existing, list) and len(aci.existing) > 0:
277 subnets = [] if subnets is None else subnets
278 for child in aci.existing[0].get("l3extFabricExtRoutingP", {}).get("children", {}):
279 if child.get("l3extSubnet") and child.get("l3extSubnet").get("attributes").get("ip") not in subnets:
280 child_configs.append(
281 {
282 "l3extSubnet": {
283 "attributes": {
284 "ip": child.get("l3extSubnet").get("attributes").get("ip"),
285 "status": "deleted",
286 }
287 }
288 }
289 )
290
291 if subnets is not None:
292 for subnet in subnets:
293 child_configs.append({"l3extSubnet": {"attributes": {"ip": subnet}}})
294
295 aci.payload(
296 aci_class="l3extFabricExtRoutingP",
297 class_config=dict(
298 name=name,
299 descr=description,
300 ),
301 child_configs=child_configs,
302 )
303
304 aci.get_diff(aci_class="l3extFabricExtRoutingP")
305
306 aci.post_config()
307
308 elif state == "absent":
309 aci.delete_config()
310
311 aci.exit_json()
312
313
314 if __name__ == "__main__":
315 main()