Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Sudhakar Shet Kudtarkar (@kudtarkar1)
5 # Copyright: (c) 2020, Shreyas Srish <[email protected]>
6 # Copyright: (c) 2021, Oleksandr Kreshchenko (@alexkross)
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: aci_l2out_extepg_to_contract
18 short_description: Bind Contracts to L2 External End Point Groups (EPGs) (fv:RsCons and fv:RsProv)
19 description:
20 - Bind Contracts to L2 External End Point Groups (EPGs) on ACI fabrics.
21 options:
22 tenant:
23 description:
24 - Name of existing tenant.
25 type: str
26 l2out:
27 description:
28 - Name of the l2out.
29 type: str
30 aliases: ['l2out_name']
31 extepg:
32 description:
33 - Name of the external end point group.
34 type: str
35 aliases: ['extepg_name', 'external_epg']
36 contract:
37 description:
38 - Name of the contract.
39 type: str
40 contract_type:
41 description:
42 - The type of contract.
43 type: str
44 required: true
45 choices: ['consumer', 'provider']
46 priority:
47 description:
48 - This has four levels of priority.
49 type: str
50 choices: ['level1', 'level2', 'level3', 'unspecified']
51 provider_match:
52 description:
53 - This is configurable for provided contracts.
54 type: str
55 choices: ['all', 'at_least_one', 'at_most_one', 'none']
56 state:
57 description:
58 - Use C(present) or C(absent) for adding or removing.
59 - Use C(query) for listing an object or multiple objects.
60 type: str
61 choices: [ absent, present, query ]
62 default: present
63 extends_documentation_fragment:
64 - cisco.aci.aci
65 - cisco.aci.annotation
66
67 notes:
68 - The C(tenant), C(l2out) and C(extepg) must exist before using this module in your playbook.
69 The M(cisco.aci.aci_tenant), M(cisco.aci.aci_l2out) and M(cisco.aci.aci_l2out_extepg) modules can be used for this.
70 seealso:
71 - name: APIC Management Information Model reference
72 description: More information about the internal APIC classes B((fv:RsCons) B(fv:RsProv).
73 link: https://developer.cisco.com/docs/apic-mim-ref/
74 author:
75 - Sudhakar Shet Kudtarkar (@kudtarkar1)
76 - Shreyas Srish (@shrsr)
77 - Oleksandr Kreshchenko (@alexkross)
78 """
79
80 EXAMPLES = r"""
81 - name: Bind a contract to an L2 external EPG
82 cisco.aci.aci_l2out_extepg_to_contract:
83 host: apic
84 username: admin
85 password: SomeSecretePassword
86 tenant: Auto-Demo
87 l2out: l2out
88 extepg: testEpg
89 contract: contract1
90 contract_type: provider
91 state: present
92 delegate_to: localhost
93
94 - name: Remove existing contract from an L2 external EPG
95 cisco.aco.aci_l2out_extepg_to_contract:
96 host: apic
97 username: admin
98 password: SomeSecretePassword
99 tenant: Auto-Demo
100 l2out: l2out
101 extepg: testEpg
102 contract: contract1
103 contract_type: provider
104 state: absent
105 delegate_to: localhost
106
107 - name: Query a contract bound to an L2 external EPG
108 cisco.aci.aci_l2out_extepg_to_contract:
109 host: apic
110 username: admin
111 password: SomeSecretePassword
112 tenant: ansible_tenant
113 l2out: ansible_l2out
114 extepg: ansible_extEpg
115 contract: ansible_contract
116 contract_type: provider
117 state: query
118 delegate_to: localhost
119 register: query_result
120
121 - name: Query all contracts relationships
122 cisco.aci.aci_l2out_extepg_to_contract:
123 host: apic
124 username: admin
125 password: SomeSecretePassword
126 contract_type: provider
127 state: query
128 delegate_to: localhost
129 register: query_result
130 """
131
132 RETURN = r"""
133 current:
134 description: The existing configuration from the APIC after the module has finished
135 returned: success
136 type: list
137 sample:
138 [
139 {
140 "fvTenant": {
141 "attributes": {
142 "descr": "Production environment",
143 "dn": "uni/tn-production",
144 "name": "production",
145 "nameAlias": "",
146 "ownerKey": "",
147 "ownerTag": ""
148 }
149 }
150 }
151 ]
152 error:
153 description: The error information as returned from the APIC
154 returned: failure
155 type: dict
156 sample:
157 {
158 "code": "122",
159 "text": "unknown managed object class foo"
160 }
161 raw:
162 description: The raw output returned by the APIC REST API (xml or json)
163 returned: parse error
164 type: str
165 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
166 sent:
167 description: The actual/minimal configuration pushed to the APIC
168 returned: info
169 type: list
170 sample:
171 {
172 "fvTenant": {
173 "attributes": {
174 "descr": "Production environment"
175 }
176 }
177 }
178 previous:
179 description: The original configuration from the APIC before the module has started
180 returned: info
181 type: list
182 sample:
183 [
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production",
188 "dn": "uni/tn-production",
189 "name": "production",
190 "nameAlias": "",
191 "ownerKey": "",
192 "ownerTag": ""
193 }
194 }
195 }
196 ]
197 proposed:
198 description: The assembled configuration from the user-provided parameters
199 returned: info
200 type: dict
201 sample:
202 {
203 "fvTenant": {
204 "attributes": {
205 "descr": "Production environment",
206 "name": "production"
207 }
208 }
209 }
210 filter_string:
211 description: The filter string used for the request
212 returned: failure or debug
213 type: str
214 sample: ?rsp-prop-include=config-only
215 method:
216 description: The HTTP method used for the request to the APIC
217 returned: failure or debug
218 type: str
219 sample: POST
220 response:
221 description: The HTTP response from the APIC
222 returned: failure or debug
223 type: str
224 sample: OK (30 bytes)
225 status:
226 description: The HTTP status from the APIC
227 returned: failure or debug
228 type: int
229 sample: 200
230 url:
231 description: The HTTP url used for the request to the APIC
232 returned: failure or debug
233 type: str
234 sample: https://10.11.12.13/api/mo/uni/tn-production.json
235 """
236
237 from ansible.module_utils.basic import AnsibleModule
238 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
239
240 ACI_CLASS_MAPPING = dict(
241 consumer={
242 "class": "fvRsCons",
243 "rn": "rscons-",
244 },
245 provider={
246 "class": "fvRsProv",
247 "rn": "rsprov-",
248 },
249 )
250
251 PROVIDER_MATCH_MAPPING = dict(
252 all="All",
253 at_least_one="AtleastOne",
254 at_most_one="tmostOne",
255 none="None",
256 )
257
258
259 def main():
260 argument_spec = aci_argument_spec()
261 argument_spec.update(aci_annotation_spec())
262 argument_spec.update(
263 contract_type=dict(type="str", required=True, choices=["consumer", "provider"]),
264 l2out=dict(type="str", aliases=["l2out_name"]),
265 contract=dict(type="str"),
266 priority=dict(type="str", choices=["level1", "level2", "level3", "unspecified"]),
267 provider_match=dict(type="str", choices=["all", "at_least_one", "at_most_one", "none"]),
268 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
269 tenant=dict(type="str"),
270 extepg=dict(type="str", aliases=["extepg_name", "external_epg"]),
271 )
272 module = AnsibleModule(
273 argument_spec=argument_spec,
274 supports_check_mode=True,
275 required_if=[
276 ["state", "absent", ["extepg", "contract", "l2out", "tenant"]],
277 ["state", "present", ["extepg", "contract", "l2out", "tenant"]],
278 ],
279 )
280
281 l2out = module.params.get("l2out")
282 contract = module.params.get("contract")
283 contract_type = module.params.get("contract_type")
284 extepg = module.params.get("extepg")
285 priority = module.params.get("priority")
286 provider_match = module.params.get("provider_match")
287 if provider_match is not None:
288 provider_match = PROVIDER_MATCH_MAPPING.get(provider_match)
289 state = module.params.get("state")
290 tenant = module.params.get("tenant")
291
292 aci_class = ACI_CLASS_MAPPING.get(contract_type)["class"]
293 aci_rn = ACI_CLASS_MAPPING.get(contract_type)["rn"]
294
295 if contract_type == "consumer" and provider_match is not None:
296 module.fail_json(msg="the 'provider_match' is only configurable for Provided Contracts")
297
298 aci = ACIModule(module)
299 aci.construct_url(
300 root_class=dict(
301 aci_class="fvTenant",
302 aci_rn="tn-{0}".format(tenant),
303 module_object=tenant,
304 target_filter={"name": tenant},
305 ),
306 subclass_1=dict(
307 aci_class="l2extOut",
308 aci_rn="l2out-{0}".format(l2out),
309 module_object=l2out,
310 target_filter={"name": l2out},
311 ),
312 subclass_2=dict(
313 aci_class="l2extInstP",
314 aci_rn="instP-{0}".format(extepg),
315 module_object=extepg,
316 target_filter={"name": extepg},
317 ),
318 subclass_3=dict(
319 aci_class=aci_class,
320 aci_rn="{0}{1}".format(aci_rn, contract),
321 module_object=contract,
322 target_filter={"tnVzBrCPName": contract},
323 ),
324 )
325
326 aci.get_existing()
327
328 if state == "present":
329 aci.payload(
330 aci_class=aci_class,
331 class_config=dict(
332 matchT=provider_match,
333 prio=priority,
334 tnVzBrCPName=contract,
335 ),
336 )
337
338 aci.get_diff(aci_class=aci_class)
339
340 aci.post_config()
341
342 elif state == "absent":
343 aci.delete_config()
344
345 aci.exit_json()
346
347
348 if __name__ == "__main__":
349 main()