Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_vzany_to_contract.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_vzany_to_contract
15 short_description: Attach contracts to vzAny (vz:RsAnyToProv, vz:RsAnyToCons, and vz:RsAnyToConsIf)
16 description:
17 - Bind contracts to vzAny on Cisco ACI fabrics.
18 options:
19 tenant:
20 description:
21 - The name of the Tenant.
22 type: str
23 aliases: [ tenant_name ]
24 vrf:
25 description:
26 - The name of the VRF.
27 type: str
28 aliases: [ context, vrf_name ]
29 contract:
30 description:
31 - The name of the contract or contract interface.
32 type: str
33 aliases: [ contract_name ]
34 type:
35 description:
36 - Determines if this is a provided or consumed contract or a consumed contract interface.
37 type: str
38 aliases: [ contract_type ]
39 required: true
40 choices: [ provider, consumer, interface ]
41 state:
42 description:
43 - Use C(present) or C(absent) for adding or removing.
44 - Use C(query) for listing an object or multiple objects.
45 type: str
46 choices: [ absent, present, query ]
47 default: present
48 extends_documentation_fragment:
49 - cisco.aci.aci
50 - cisco.aci.annotation
51
52 seealso:
53 - module: cisco.aci.aci_tenant
54 - module: cisco.aci.aci_vrf
55 - module: cisco.aci.aci_contract
56 - name: APIC Management Information Model reference
57 description: More information about the internal APIC classes B(vz:RsAnyToProv), B(vz:RsAnyToCons), and B(vz:RsAnyToConsIf).
58 link: https://developer.cisco.com/docs/apic-mim-ref/
59 author:
60 - Marcel Zehnder (@maercu)
61 """
62
63 EXAMPLES = r"""
64 - name: Add a new contract to vzAny
65 cisco.aci.aci_vzany_to_contract:
66 host: apic
67 username: admin
68 password: SomeSecretPassword
69 tenant: vzatest
70 vrf: vzatest
71 contract: vzatest_http
72 type: provider
73 state: present
74 delegate_to: localhost
75
76 - name: Remove an existing contract from vzAny
77 cisco.aci.aci_vzany_to_contract:
78 host: apic
79 username: admin
80 password: SomeSecretPassword
81 tenant: vzatest
82 vrf: vzatest
83 contract: vzatest_http
84 type: provider
85 state: absent
86 delegate_to: localhost
87
88 - name: Query a specific contract to vzAny binding
89 cisco.aci.aci_vzany_to_contract:
90 host: apic
91 username: admin
92 password: SomeSecretPassword
93 tenant: vzatest
94 vrf: vzatest
95 contract: vzatest_http
96 type: provider
97 state: query
98 delegate_to: localhost
99 register: query_result
100
101 - name: Query all provider contract to vzAny bindings
102 cisco.aci.aci_vzany_to_contract:
103 host: apic
104 username: admin
105 password: SomeSecretPassword
106 type: provider
107 state: query
108 delegate_to: localhost
109 register: query_result
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
218 from ansible.module_utils.basic import AnsibleModule
219 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
220
221
222 ACI_CLASS_MAPPING = dict(
223 provider={"class": "vzRsAnyToProv", "rn": "rsanyToProv-", "target_attribute": "tnVzBrCPName"},
224 consumer={"class": "vzRsAnyToCons", "rn": "rsanyToCons-", "target_attribute": "tnVzBrCPName"},
225 interface={"class": "vzRsAnyToConsIf", "rn": "rsanyToConsIf-", "target_attribute": "tnVzCPIfName"},
226 )
227
228
229 def main():
230 argument_spec = aci_argument_spec()
231 argument_spec.update(aci_annotation_spec())
232 argument_spec.update(
233 tenant=dict(type="str", aliases=["tenant_name"]),
234 vrf=dict(type="str", aliases=["context", "vrf_name"]),
235 contract=dict(type="str", aliases=["contract_name"]),
236 type=dict(type="str", required=True, choices=["provider", "consumer", "interface"], aliases=["contract_type"]),
237 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
238 )
239
240 module = AnsibleModule(
241 argument_spec=argument_spec,
242 supports_check_mode=True,
243 required_if=[["state", "absent", ["contract", "vrf", "tenant"]], ["state", "present", ["contract", "vrf", "tenant"]]],
244 )
245
246 tenant = module.params.get("tenant")
247 vrf = module.params.get("vrf")
248 contract = module.params.get("contract")
249 type = module.params.get("type")
250 state = module.params.get("state")
251
252 aci_class = ACI_CLASS_MAPPING[type]["class"]
253 aci_rn = ACI_CLASS_MAPPING[type]["rn"]
254 aci_target_attribute = ACI_CLASS_MAPPING[type]["target_attribute"]
255
256 aci = ACIModule(module)
257 aci.construct_url(
258 root_class=dict(aci_class="fvTenant", aci_rn="tn-{0}".format(tenant), module_object=tenant, target_filter={"name": tenant}),
259 subclass_1=dict(aci_class="fvCtx", aci_rn="ctx-{0}".format(vrf), module_object=vrf, target_filter={"name": vrf}),
260 subclass_2=dict(aci_class="vzAny", aci_rn="any", module_object="any", target_filter={"name": "any"}),
261 subclass_3=dict(aci_class=aci_class, aci_rn="{0}{1}".format(aci_rn, contract), module_object=contract, target_filter={aci_target_attribute: contract}),
262 )
263
264 aci.get_existing()
265
266 if state == "present":
267 aci.payload(aci_class=aci_class, class_config={aci_target_attribute: contract})
268
269 aci.get_diff(aci_class=aci_class)
270
271 aci.post_config()
272
273 elif state == "absent":
274 aci.delete_config()
275
276 aci.exit_json()
277
278
279 if __name__ == "__main__":
280 main()