Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Akini Ross (@akinross) <[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_esg_to_contract
16 short_description: Bind ESGs to Contracts (fv:RsCons, fv:RsProv, fv:RsConsIf, and fv:RsIntraEpg)
17 description:
18 - Bind ESGs to Contracts on Cisco ACI fabrics.
19 notes:
20 - The C(tenant), C(ap), C(esg), and C(contract) used must exist before using this module in your playbook.
21 The M(cisco.aci.aci_tenant), M(cisco.aci.aci_ap), M(cisco.aci.aci_esg), and M(cisco.aci.aci_contract) modules can be used for this.
22 options:
23 tenant:
24 description:
25 - Name of an existing tenant.
26 type: str
27 aliases: [ tenant_name ]
28 ap:
29 description:
30 - Name of an existing application network profile, that will contain the ESGs.
31 type: str
32 aliases: [ app_profile, app_profile_name ]
33 esg:
34 description:
35 - The name of the end point security group.
36 type: str
37 aliases: [ esg_name ]
38 contract:
39 description:
40 - The name of the contract or contract interface.
41 type: str
42 aliases: [ contract_name, contract_interface ]
43 contract_type:
44 description:
45 - Determines the type of the Contract.
46 type: str
47 required: true
48 choices: [ consumer, provider, interface, intra_esg ]
49 priority:
50 description:
51 - QoS class.
52 - The APIC defaults to C(unspecified) when unset during creation.
53 - This argument is not supported for o(contract_type=intra_esg) contracts.
54 type: str
55 choices: [ level1, level2, level3, level4, level5, level6, unspecified ]
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 seealso:
68 - module: cisco.aci.aci_tenant
69 - module: cisco.aci.aci_ap
70 - module: cisco.aci.aci_esg
71 - module: cisco.aci.aci_contract
72 - name: APIC Management Information Model reference
73 description: More information about the internal APIC classes B(fv:RsCons), B(fv:RsProv), B(fv:RsConsIf), and B(fv:RsIntraEpg).
74 link: https://developer.cisco.com/docs/apic-mim-ref/
75 author:
76 - Akini Ross (@akinross)
77 """
78
79 EXAMPLES = r"""
80 - name: Add a new contract to ESG binding
81 cisco.aci.aci_esg_to_contract:
82 host: apic
83 username: admin
84 password: SomeSecretPassword
85 tenant: anstest
86 ap: anstest
87 esg: anstest
88 contract: anstest_http
89 contract_type: provider
90 state: present
91 delegate_to: localhost
92
93 - name: Query a specific contract to ESG binding
94 cisco.aci.aci_esg_to_contract:
95 host: apic
96 username: admin
97 password: SomeSecretPassword
98 tenant: anstest
99 ap: anstest
100 esg: anstest
101 contract: anstest_http
102 contract_type: provider
103 state: query
104 delegate_to: localhost
105 register: query_result
106
107 - name: Query all provider contract to ESG bindings
108 cisco.aci.aci_esg_to_contract:
109 host: apic
110 username: admin
111 password: SomeSecretPassword
112 contract_type: provider
113 state: query
114 delegate_to: localhost
115 register: query_result
116
117 - name: Remove an existing contract to ESG binding
118 cisco.aci.aci_esg_to_contract:
119 host: apic
120 username: admin
121 password: SomeSecretPassword
122 tenant: anstest
123 ap: anstest
124 esg: anstest
125 contract: anstest_http
126 contract_type: provider
127 state: absent
128 delegate_to: localhost
129 """
130
131 RETURN = r"""
132 current:
133 description: The existing configuration from the APIC after the module has finished
134 returned: success
135 type: list
136 sample:
137 [
138 {
139 "fvTenant": {
140 "attributes": {
141 "descr": "Production environment",
142 "dn": "uni/tn-production",
143 "name": "production",
144 "nameAlias": "",
145 "ownerKey": "",
146 "ownerTag": ""
147 }
148 }
149 }
150 ]
151 error:
152 description: The error information as returned from the APIC
153 returned: failure
154 type: dict
155 sample:
156 {
157 "code": "122",
158 "text": "unknown managed object class foo"
159 }
160 raw:
161 description: The raw output returned by the APIC REST API (xml or json)
162 returned: parse error
163 type: str
164 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
165 sent:
166 description: The actual/minimal configuration pushed to the APIC
167 returned: info
168 type: list
169 sample:
170 {
171 "fvTenant": {
172 "attributes": {
173 "descr": "Production environment"
174 }
175 }
176 }
177 previous:
178 description: The original configuration from the APIC before the module has started
179 returned: info
180 type: list
181 sample:
182 [
183 {
184 "fvTenant": {
185 "attributes": {
186 "descr": "Production",
187 "dn": "uni/tn-production",
188 "name": "production",
189 "nameAlias": "",
190 "ownerKey": "",
191 "ownerTag": ""
192 }
193 }
194 }
195 ]
196 proposed:
197 description: The assembled configuration from the user-provided parameters
198 returned: info
199 type: dict
200 sample:
201 {
202 "fvTenant": {
203 "attributes": {
204 "descr": "Production environment",
205 "name": "production"
206 }
207 }
208 }
209 filter_string:
210 description: The filter string used for the request
211 returned: failure or debug
212 type: str
213 sample: ?rsp-prop-include=config-only
214 method:
215 description: The HTTP method used for the request to the APIC
216 returned: failure or debug
217 type: str
218 sample: POST
219 response:
220 description: The HTTP response from the APIC
221 returned: failure or debug
222 type: str
223 sample: OK (30 bytes)
224 status:
225 description: The HTTP status from the APIC
226 returned: failure or debug
227 type: int
228 sample: 200
229 url:
230 description: The HTTP url used for the request to the APIC
231 returned: failure or debug
232 type: str
233 sample: https://10.11.12.13/api/mo/uni/tn-production.json
234 """
235
236 from ansible.module_utils.basic import AnsibleModule
237 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
238 from ansible_collections.cisco.aci.plugins.module_utils.constants import ACI_CLASS_MAPPING
239
240
241 def main():
242 argument_spec = aci_argument_spec()
243 argument_spec.update(aci_annotation_spec())
244 argument_spec.update(
245 contract_type=dict(type="str", required=True, choices=["consumer", "provider", "interface", "intra_esg"]),
246 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
247 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), # Not required for querying all objects
248 esg=dict(type="str", aliases=["esg_name"]), # Not required for querying all objects
249 contract=dict(type="str", aliases=["contract_name", "contract_interface"]), # Not required for querying all objects
250 priority=dict(type="str", choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"]),
251 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
252 )
253
254 module = AnsibleModule(
255 argument_spec=argument_spec,
256 supports_check_mode=True,
257 required_if=[
258 ["state", "absent", ["tenant", "ap", "esg", "contract"]],
259 ["state", "present", ["tenant", "ap", "esg", "contract"]],
260 ],
261 )
262
263 contract_type = module.params.get("contract_type")
264 tenant = module.params.get("tenant")
265 ap = module.params.get("ap")
266 esg = module.params.get("esg")
267 contract = module.params.get("contract")
268 priority = module.params.get("priority")
269 state = module.params.get("state")
270
271 if contract_type == "intra_esg" and priority is not None:
272 module.fail_json(msg="The 'priority' argument is not supported for the 'intra_esg' contract type.")
273
274 aci_class = ACI_CLASS_MAPPING[contract_type]["class"]
275 aci_rn = ACI_CLASS_MAPPING[contract_type]["rn"]
276 aci_name = ACI_CLASS_MAPPING[contract_type]["name"]
277
278 aci = ACIModule(module)
279 aci.construct_url(
280 root_class=dict(
281 aci_class="fvTenant",
282 aci_rn="tn-{0}".format(tenant),
283 module_object=tenant,
284 target_filter={"name": tenant},
285 ),
286 subclass_1=dict(
287 aci_class="fvAp",
288 aci_rn="ap-{0}".format(ap),
289 module_object=ap,
290 target_filter={"name": ap},
291 ),
292 subclass_2=dict(
293 aci_class="fvAEPg",
294 aci_rn="esg-{0}".format(esg),
295 module_object=esg,
296 target_filter={"name": esg},
297 ),
298 subclass_3=dict(
299 aci_class=aci_class,
300 aci_rn="{0}{1}".format(aci_rn, contract),
301 module_object=contract,
302 target_filter={aci_name: contract},
303 ),
304 )
305
306 aci.get_existing()
307
308 if state == "present":
309 aci.payload(
310 aci_class=aci_class,
311 class_config={"prio": priority, aci_name: contract},
312 )
313
314 aci.get_diff(aci_class=aci_class)
315
316 aci.post_config()
317
318 elif state == "absent":
319 aci.delete_config()
320
321 aci.exit_json()
322
323
324 if __name__ == "__main__":
325 main()