Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_l3out_extsubnet
15 short_description: Manage External Subnet objects (l3ext:Subnet)
16 description:
17 - Manage External Subnet objects.
18 options:
19 tenant:
20 description:
21 - Name of an existing tenant.
22 type: str
23 aliases: [ tenant_name ]
24 required: true
25 l3out:
26 description:
27 - Name of an existing L3Out.
28 type: str
29 aliases: [ l3out_name ]
30 required: true
31 extepg:
32 description:
33 - Name of an existing ExtEpg.
34 type: str
35 aliases: [ extepg_name ]
36 required: true
37 network:
38 description:
39 - The network address for the Subnet.
40 type: str
41 aliases: [ address, ip ]
42 subnet_name:
43 description:
44 - Name of External Subnet being created.
45 type: str
46 aliases: [ name ]
47 description:
48 description:
49 - Description for the External Subnet.
50 type: str
51 aliases: [ descr ]
52 scope:
53 description:
54 - Determines the scope of the Subnet.
55 - The C(export-rtctrl) option controls which external networks are advertised out of the fabric using route-maps and IP prefix-lists.
56 - The C(import-rtctrl) option controls which external networks are advertised in to the fabric using route-maps and IP prefix-lists.
57 - The C(import-security) option classifies for the external EPG.
58 The rules and contracts defined in this external EPG apply to networks matching this subnet.
59 - The C(shared-rtctrl) option controls which external prefixes are advertised to other tenants for shared services.
60 - The C(shared-security) option configures the classifier for the subnets in the VRF where the routes are leaked.
61 - The APIC defaults to C(import-security) when unset during creation.
62 - The C(import-rtctrl) is only supported for BGP and OSPF.
63 type: list
64 elements: str
65 choices: [ export-rtctrl, import-rtctrl, import-security, shared-rtctrl, shared-security ]
66 aggregate:
67 description:
68 - Determines the Aggregate Routes for the Subnet.
69 - The C(export-rtctrl) option to export all transit routes of a VRF (0/0 subnets).
70 - The C(import-rtctrl) option to import all incoming routes of given L3 peers (0/0 subnets).
71 - The C(shared-rtctrl) option to share routes learned in one VRF which needs to be advertised to another VRF.
72 0/0 can be used to share all subnet routes across multiple VRFs.
73 - The C(import-rtctrl) is only supported for BGP and OSPF.
74 - Aggregate import route control is only available if the L3Out has 'Import Route Control Enforcement' enabled.
75 Default this is disabled, M(cisco.aci.aci_l3out) with C(route_control) can be used to enable.
76 type: list
77 elements: str
78 choices: [ export-rtctrl, import-rtctrl, shared-rtctrl]
79 state:
80 description:
81 - Use C(present) or C(absent) for adding or removing.
82 - Use C(query) for listing an object or multiple objects.
83 type: str
84 choices: [ absent, present, query ]
85 default: present
86 name_alias:
87 description:
88 - The alias for the current object. This relates to the nameAlias field in ACI.
89 type: str
90 extends_documentation_fragment:
91 - cisco.aci.aci
92 - cisco.aci.annotation
93
94 notes:
95 - The C(tenant) and C(domain) and C(vrf) used must exist before using this module in your playbook.
96 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_domain) and M(cisco.aci.aci_vrf) modules can be used for this.
97 seealso:
98 - module: cisco.aci.aci_tenant
99 - module: cisco.aci.aci_domain
100 - module: cisco.aci.aci_vrf
101 - name: APIC Management Information Model reference
102 description: More information about the internal APIC class B(l3ext:Subnet).
103 link: https://developer.cisco.com/docs/apic-mim-ref/
104 author:
105 - Rostyslav Davydenko (@rost-d)
106 - Cindy Zhao (@cizhao)
107 """
108
109 EXAMPLES = r"""
110 - name: Add a new External Subnet
111 cisco.aci.aci_l3out_extsubnet:
112 host: apic
113 username: admin
114 password: SomeSecretPassword
115 tenant: production
116 l3out: prod_l3out
117 extepg: prod_extepg
118 description: External Subnet for Production ExtEpg
119 network: 192.0.2.0/24
120 scope: export-rtctrl
121 aggregate: export-rtctrl
122 state: present
123 delegate_to: localhost
124
125 - name: Delete External Subnet
126 cisco.aci.aci_l3out_extsubnet:
127 host: apic
128 username: admin
129 password: SomeSecretPassword
130 tenant: production
131 l3out: prod_l3out
132 extepg: prod_extepg
133 network: 192.0.2.0/24
134 state: absent
135 delegate_to: localhost
136
137 - name: Query ExtEpg Subnet information
138 cisco.aci.aci_l3out_extsubnet:
139 host: apic
140 username: admin
141 password: SomeSecretPassword
142 tenant: production
143 l3out: prod_l3out
144 extepg: prod_extepg
145 network: 192.0.2.0/24
146 state: query
147 delegate_to: localhost
148 register: query_result
149 """
150
151 RETURN = r"""
152 current:
153 description: The existing configuration from the APIC after the module has finished
154 returned: success
155 type: list
156 sample:
157 [
158 {
159 "fvTenant": {
160 "attributes": {
161 "descr": "Production environment",
162 "dn": "uni/tn-production",
163 "name": "production",
164 "nameAlias": "",
165 "ownerKey": "",
166 "ownerTag": ""
167 }
168 }
169 }
170 ]
171 error:
172 description: The error information as returned from the APIC
173 returned: failure
174 type: dict
175 sample:
176 {
177 "code": "122",
178 "text": "unknown managed object class foo"
179 }
180 raw:
181 description: The raw output returned by the APIC REST API (xml or json)
182 returned: parse error
183 type: str
184 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
185 sent:
186 description: The actual/minimal configuration pushed to the APIC
187 returned: info
188 type: list
189 sample:
190 {
191 "fvTenant": {
192 "attributes": {
193 "descr": "Production environment"
194 }
195 }
196 }
197 previous:
198 description: The original configuration from the APIC before the module has started
199 returned: info
200 type: list
201 sample:
202 [
203 {
204 "fvTenant": {
205 "attributes": {
206 "descr": "Production",
207 "dn": "uni/tn-production",
208 "name": "production",
209 "nameAlias": "",
210 "ownerKey": "",
211 "ownerTag": ""
212 }
213 }
214 }
215 ]
216 proposed:
217 description: The assembled configuration from the user-provided parameters
218 returned: info
219 type: dict
220 sample:
221 {
222 "fvTenant": {
223 "attributes": {
224 "descr": "Production environment",
225 "name": "production"
226 }
227 }
228 }
229 filter_string:
230 description: The filter string used for the request
231 returned: failure or debug
232 type: str
233 sample: ?rsp-prop-include=config-only
234 method:
235 description: The HTTP method used for the request to the APIC
236 returned: failure or debug
237 type: str
238 sample: POST
239 response:
240 description: The HTTP response from the APIC
241 returned: failure or debug
242 type: str
243 sample: OK (30 bytes)
244 status:
245 description: The HTTP status from the APIC
246 returned: failure or debug
247 type: int
248 sample: 200
249 url:
250 description: The HTTP url used for the request to the APIC
251 returned: failure or debug
252 type: str
253 sample: https://10.11.12.13/api/mo/uni/tn-production.json
254 """
255
256 from ansible.module_utils.basic import AnsibleModule
257 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
258
259
260 def main():
261 argument_spec = aci_argument_spec()
262 argument_spec.update(aci_annotation_spec())
263 argument_spec.update(
264 tenant=dict(type="str", required=True, aliases=["tenant_name"]),
265 l3out=dict(type="str", required=True, aliases=["l3out_name"]),
266 extepg=dict(type="str", required=True, aliases=["extepg_name"]),
267 network=dict(type="str", aliases=["address", "ip"]),
268 description=dict(type="str", aliases=["descr"]),
269 subnet_name=dict(type="str", aliases=["name"]),
270 scope=dict(type="list", elements="str", choices=["import-security", "export-rtctrl", "import-rtctrl", "shared-rtctrl", "shared-security"]),
271 aggregate=dict(type="list", elements="str", choices=["export-rtctrl", "import-rtctrl", "shared-rtctrl"]),
272 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
273 name_alias=dict(type="str"),
274 )
275
276 module = AnsibleModule(
277 argument_spec=argument_spec,
278 supports_check_mode=True,
279 required_if=[
280 ["state", "present", ["network"]],
281 ["state", "absent", ["network"]],
282 ],
283 required_by={"aggregate": "scope"},
284 )
285
286 aci = ACIModule(module)
287
288 tenant = module.params.get("tenant")
289 l3out = module.params.get("l3out")
290 extepg = module.params.get("extepg")
291 network = module.params.get("network")
292 description = module.params.get("description")
293 subnet_name = module.params.get("subnet_name")
294 scope = module.params.get("scope")
295 aggregate = module.params.get("aggregate")
296 state = module.params.get("state")
297 name_alias = module.params.get("name_alias")
298
299 # Validation rule to only allow aggregate choice when there is a match in scope choice
300 if aggregate and not set(aggregate).issubset(scope):
301 aci.module.fail_json(msg="All aggregate values {0} need to be defined in scope {1}.".format(aggregate, scope))
302
303 class_config = dict(ip=network, descr=description, name=subnet_name, nameAlias=name_alias)
304 if scope:
305 class_config["scope"] = ",".join(sorted(scope))
306 if aggregate:
307 class_config["aggregate"] = ",".join(sorted(aggregate))
308
309 aci.construct_url(
310 root_class=dict(
311 aci_class="fvTenant",
312 aci_rn="tn-{0}".format(tenant),
313 module_object=tenant,
314 target_filter={"name": tenant},
315 ),
316 subclass_1=dict(
317 aci_class="l3extOut",
318 aci_rn="out-{0}".format(l3out),
319 module_object=l3out,
320 target_filter={"name": l3out},
321 ),
322 subclass_2=dict(
323 aci_class="l3extInstP",
324 aci_rn="instP-{0}".format(extepg),
325 module_object=extepg,
326 target_filter={"name": extepg},
327 ),
328 subclass_3=dict(
329 aci_class="l3extSubnet",
330 aci_rn="extsubnet-[{0}]".format(network),
331 module_object=network,
332 target_filter={"name": network},
333 ),
334 )
335
336 aci.get_existing()
337
338 if state == "present":
339 aci.payload(aci_class="l3extSubnet", class_config=class_config)
340
341 aci.get_diff(aci_class="l3extSubnet")
342
343 aci.post_config()
344
345 elif state == "absent":
346 aci.delete_config()
347
348 aci.exit_json()
349
350
351 if __name__ == "__main__":
352 main()