Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_l3out_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) 2023, Akini Ross (@akinross) <[email protected]>
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_l3out_extepg_to_contract
18 short_description: Bind Contracts to External End Point Groups (EPGs) (fv:RsCons, fv:RsProv, fv:RsProtBy, fv:RsConsIf, and fv:RsIntraEpg)
19 description:
20 - Bind Contracts to External End Point Groups (EPGs) on ACI fabrics.
21 options:
22 tenant:
23 description:
24 - Name of existing tenant.
25 type: str
26 l3out:
27 description:
28 - Name of the l3out.
29 type: str
30 aliases: [ l3out_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 - The name of the contract or contract interface.
39 type: str
40 aliases: [ contract_name, contract_interface ]
41 contract_type:
42 description:
43 - Determines the type of the Contract.
44 type: str
45 required: true
46 choices: [ consumer, provider, taboo, interface, intra_epg ]
47 priority:
48 description:
49 - QoS class.
50 - The APIC defaults to C(unspecified) when unset during creation.
51 type: str
52 choices: [ level1, level2, level3, level4, level5, level6, unspecified ]
53 provider_match:
54 description:
55 - The matching algorithm for Provided Contracts.
56 - The APIC defaults to C(at_least_one) when unset during creation.
57 type: str
58 choices: [ all, at_least_one, at_most_one, none ]
59 contract_label:
60 description:
61 - Contract label to match.
62 type: str
63 subject_label:
64 description:
65 - Subject label to match.
66 type: str
67 state:
68 description:
69 - Use C(present) or C(absent) for adding or removing.
70 - Use C(query) for listing an object or multiple objects.
71 type: str
72 choices: [ absent, present, query ]
73 default: present
74 extends_documentation_fragment:
75 - cisco.aci.aci
76 - cisco.aci.annotation
77
78 notes:
79 - The C(tenant), C(l3out), C(extepg), and C(Contract) must exist before using this module in your playbook.
80 The M(cisco.aci.aci_tenant), M(cisco.aci.aci_l3out), M(cisco.aci.aci_l3out_extepg), and M(cisco.aci.aci_contract) modules can be used for this.
81 seealso:
82 - name: APIC Management Information Model reference
83 description: More information about the internal APIC class B(fv:RsCons), B(fv:RsProv), B(fv:RsProtBy, B(fv:RsConsIf, and B(fv:RsIntraEpg).
84 link: https://developer.cisco.com/docs/apic-mim-ref/
85 author:
86 - Sudhakar Shet Kudtarkar (@kudtarkar1)
87 - Shreyas Srish (@shrsr)
88 - Akini Ross (@akinross)
89 """
90
91 EXAMPLES = r"""
92 - name: Bind a contract to an external EPG
93 cisco.aci.aci_l3out_extepg_to_contract:
94 host: apic
95 username: admin
96 password: SomeSecretePassword
97 tenant: Auto-Demo
98 l3out: l3out
99 extepg: testEpg
100 contract: contract1
101 contract_type: provider
102 state: present
103 delegate_to: localhost
104
105 - name: Remove existing contract from an external EPG
106 cisco.aci.aci_l3out_extepg_to_contract:
107 host: apic
108 username: admin
109 password: SomeSecretePassword
110 tenant: Auto-Demo
111 l3out: l3out
112 extepg: testEpg
113 contract: contract1
114 contract_type: provider
115 state: absent
116 delegate_to: localhost
117
118 - name: Query a contract bound to an external EPG
119 cisco.aci.aci_l3out_extepg_to_contract:
120 host: apic
121 username: admin
122 password: SomeSecretePassword
123 tenant: ansible_tenant
124 l3out: ansible_l3out
125 extepg: ansible_extEpg
126 contract: ansible_contract
127 contract_type: provider
128 state: query
129 delegate_to: localhost
130 register: query_result
131
132 - name: Query all contracts relationships
133 cisco.aci.aci_l3out_extepg_to_contract:
134 host: apic
135 username: admin
136 password: SomeSecretePassword
137 contract_type: provider
138 state: query
139 delegate_to: localhost
140 register: query_result
141 """
142
143 RETURN = r"""
144 current:
145 description: The existing configuration from the APIC after the module has finished
146 returned: success
147 type: list
148 sample:
149 [
150 {
151 "fvTenant": {
152 "attributes": {
153 "descr": "Production environment",
154 "dn": "uni/tn-production",
155 "name": "production",
156 "nameAlias": "",
157 "ownerKey": "",
158 "ownerTag": ""
159 }
160 }
161 }
162 ]
163 error:
164 description: The error information as returned from the APIC
165 returned: failure
166 type: dict
167 sample:
168 {
169 "code": "122",
170 "text": "unknown managed object class foo"
171 }
172 raw:
173 description: The raw output returned by the APIC REST API (xml or json)
174 returned: parse error
175 type: str
176 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
177 sent:
178 description: The actual/minimal configuration pushed to the APIC
179 returned: info
180 type: list
181 sample:
182 {
183 "fvTenant": {
184 "attributes": {
185 "descr": "Production environment"
186 }
187 }
188 }
189 previous:
190 description: The original configuration from the APIC before the module has started
191 returned: info
192 type: list
193 sample:
194 [
195 {
196 "fvTenant": {
197 "attributes": {
198 "descr": "Production",
199 "dn": "uni/tn-production",
200 "name": "production",
201 "nameAlias": "",
202 "ownerKey": "",
203 "ownerTag": ""
204 }
205 }
206 }
207 ]
208 proposed:
209 description: The assembled configuration from the user-provided parameters
210 returned: info
211 type: dict
212 sample:
213 {
214 "fvTenant": {
215 "attributes": {
216 "descr": "Production environment",
217 "name": "production"
218 }
219 }
220 }
221 filter_string:
222 description: The filter string used for the request
223 returned: failure or debug
224 type: str
225 sample: ?rsp-prop-include=config-only
226 method:
227 description: The HTTP method used for the request to the APIC
228 returned: failure or debug
229 type: str
230 sample: POST
231 response:
232 description: The HTTP response from the APIC
233 returned: failure or debug
234 type: str
235 sample: OK (30 bytes)
236 status:
237 description: The HTTP status from the APIC
238 returned: failure or debug
239 type: int
240 sample: 200
241 url:
242 description: The HTTP url used for the request to the APIC
243 returned: failure or debug
244 type: str
245 sample: https://10.11.12.13/api/mo/uni/tn-production.json
246 """
247
248 from ansible.module_utils.basic import AnsibleModule
249 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
250 from ansible_collections.cisco.aci.plugins.module_utils.constants import ACI_CLASS_MAPPING, CONTRACT_LABEL_MAPPING, PROVIDER_MATCH_MAPPING, SUBJ_LABEL_MAPPING
251
252
253 def main():
254 argument_spec = aci_argument_spec()
255 argument_spec.update(aci_annotation_spec())
256 argument_spec.update(
257 contract_type=dict(type="str", required=True, choices=["consumer", "provider", "taboo", "interface", "intra_epg"]),
258 l3out=dict(type="str", aliases=["l3out_name"]),
259 contract=dict(type="str", aliases=["contract_name", "contract_interface"]), # Not required for querying all objects
260 priority=dict(type="str", choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"]),
261 provider_match=dict(type="str", choices=["all", "at_least_one", "at_most_one", "none"]),
262 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
263 tenant=dict(type="str"),
264 extepg=dict(type="str", aliases=["extepg_name", "external_epg"]),
265 contract_label=dict(type="str"),
266 subject_label=dict(type="str"),
267 )
268 module = AnsibleModule(
269 argument_spec=argument_spec,
270 supports_check_mode=True,
271 required_if=[
272 ["state", "absent", ["extepg", "contract", "l3out", "tenant"]],
273 ["state", "present", ["extepg", "contract", "l3out", "tenant"]],
274 ],
275 )
276
277 l3out = module.params.get("l3out")
278 contract = module.params.get("contract")
279 contract_type = module.params.get("contract_type")
280 extepg = module.params.get("extepg")
281 priority = module.params.get("priority")
282 provider_match = module.params.get("provider_match")
283 if provider_match is not None:
284 provider_match = PROVIDER_MATCH_MAPPING.get(provider_match)
285 state = module.params.get("state")
286 tenant = module.params.get("tenant")
287 contract_label = module.params.get("contract_label")
288 subject_label = module.params.get("subject_label")
289
290 aci_class = ACI_CLASS_MAPPING[contract_type]["class"]
291 aci_rn = ACI_CLASS_MAPPING[contract_type]["rn"]
292 aci_name = ACI_CLASS_MAPPING[contract_type]["name"]
293 child_classes = []
294
295 if contract_type != "provider" and provider_match is not None:
296 module.fail_json(msg="the 'provider_match' is only configurable for Provided Contracts")
297
298 if contract_type in ["taboo", "interface", "intra_epg"] and (contract_label is not None or subject_label is not None):
299 module.fail_json(msg="the 'contract_label' and 'subject_label' are not configurable for {0} contracts".format(contract_type))
300
301 if contract_type not in ["taboo", "interface", "intra_epg"]:
302 contract_label_class = CONTRACT_LABEL_MAPPING.get(contract_type)
303 subject_label_class = SUBJ_LABEL_MAPPING.get(contract_type)
304 child_classes = [subject_label_class, contract_label_class]
305
306 aci = ACIModule(module)
307 aci.construct_url(
308 root_class=dict(
309 aci_class="fvTenant",
310 aci_rn="tn-{0}".format(tenant),
311 module_object=tenant,
312 target_filter={"name": tenant},
313 ),
314 subclass_1=dict(
315 aci_class="l3extOut",
316 aci_rn="out-{0}".format(l3out),
317 module_object=l3out,
318 target_filter={"name": l3out},
319 ),
320 subclass_2=dict(
321 aci_class="l3extInstP",
322 aci_rn="instP-{0}".format(extepg),
323 module_object=extepg,
324 target_filter={"name": extepg},
325 ),
326 subclass_3=dict(
327 aci_class=aci_class,
328 aci_rn="{0}{1}".format(aci_rn, contract),
329 module_object=contract,
330 target_filter={aci_name: contract},
331 ),
332 child_classes=child_classes,
333 )
334
335 aci.get_existing()
336
337 if state == "present":
338 child_configs = []
339 if contract_label is not None:
340 child_configs.append({contract_label_class: {"attributes": {"name": contract_label}}})
341 if subject_label is not None:
342 child_configs.append({subject_label_class: {"attributes": {"name": subject_label}}})
343 aci.payload(
344 aci_class=aci_class,
345 class_config={"matchT": provider_match, "prio": priority, aci_name: contract},
346 child_configs=child_configs,
347 )
348
349 aci.get_diff(aci_class=aci_class)
350
351 aci.post_config()
352
353 elif state == "absent":
354 aci.delete_config()
355
356 aci.exit_json()
357
358
359 if __name__ == "__main__":
360 main()