Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_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_contract
15 short_description: Manage contract resources (vz:BrCP)
16 description:
17 - Manage Contract resources on Cisco ACI fabrics.
18 options:
19 contract:
20 description:
21 - The name of the contract.
22 type: str
23 aliases: [ contract_name, name ]
24 description:
25 description:
26 - The description of the contract.
27 type: str
28 aliases: [ descr ]
29 tenant:
30 description:
31 - The name of the tenant.
32 type: str
33 aliases: [ tenant_name ]
34 scope:
35 description:
36 - The scope of a service contract.
37 - The APIC defaults to C(context) when unset during creation.
38 - The choice C(vrf) is equal to C(context).
39 type: str
40 choices: [ application-profile, context, global, tenant, vrf ]
41 priority:
42 description:
43 - The desired QoS class to be used.
44 - The APIC defaults to C(unspecified) when unset during creation.
45 type: str
46 choices: [ level1, level2, level3, level4, level5, level6, unspecified ]
47 dscp:
48 description:
49 - The target Differentiated Service (DSCP) value.
50 - The APIC defaults to C(unspecified) when unset during creation.
51 type: str
52 choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ]
53 aliases: [ target ]
54 state:
55 description:
56 - Use C(present) or C(absent) for adding or removing.
57 - Use C(query) for listing an object or multiple objects.
58 type: str
59 choices: [ absent, present, query ]
60 default: present
61 name_alias:
62 description:
63 - The alias for the current object. This relates to the nameAlias field in ACI.
64 type: str
65 extends_documentation_fragment:
66 - cisco.aci.aci
67 - cisco.aci.annotation
68 - cisco.aci.owner
69
70 notes:
71 - This module does not manage Contract Subjects, see M(cisco.aci.aci_contract_subject) to do this.
72 Contract Subjects can still be removed using this module.
73 - The C(tenant) used must exist before using this module in your playbook.
74 The M(cisco.aci.aci_tenant) module can be used for this.
75 seealso:
76 - module: cisco.aci.aci_contract_subject
77 - module: cisco.aci.aci_tenant
78 - name: APIC Management Information Model reference
79 description: More information about the internal APIC class B(vz:BrCP).
80 link: https://developer.cisco.com/docs/apic-mim-ref/
81 author:
82 - Dag Wieers (@dagwieers)
83 """
84
85 EXAMPLES = r"""
86 - name: Add a new contract
87 cisco.aci.aci_contract:
88 host: apic
89 username: admin
90 password: SomeSecretPassword
91 tenant: production
92 contract: web_to_db
93 description: Communication between web-servers and database
94 scope: application-profile
95 state: present
96 delegate_to: localhost
97
98 - name: Remove an existing contract
99 cisco.aci.aci_contract:
100 host: apic
101 username: admin
102 password: SomeSecretPassword
103 tenant: production
104 contract: web_to_db
105 state: absent
106 delegate_to: localhost
107
108 - name: Query a specific contract
109 cisco.aci.aci_contract:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 tenant: production
114 contract: web_to_db
115 state: query
116 delegate_to: localhost
117 register: query_result
118
119 - name: Query all contracts
120 cisco.aci.aci_contract:
121 host: apic
122 username: admin
123 password: SomeSecretPassword
124 state: query
125 delegate_to: localhost
126 register: query_result
127 """
128
129 RETURN = r"""
130 current:
131 description: The existing configuration from the APIC after the module has finished
132 returned: success
133 type: list
134 sample:
135 [
136 {
137 "fvTenant": {
138 "attributes": {
139 "descr": "Production environment",
140 "dn": "uni/tn-production",
141 "name": "production",
142 "nameAlias": "",
143 "ownerKey": "",
144 "ownerTag": ""
145 }
146 }
147 }
148 ]
149 error:
150 description: The error information as returned from the APIC
151 returned: failure
152 type: dict
153 sample:
154 {
155 "code": "122",
156 "text": "unknown managed object class foo"
157 }
158 raw:
159 description: The raw output returned by the APIC REST API (xml or json)
160 returned: parse error
161 type: str
162 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
163 sent:
164 description: The actual/minimal configuration pushed to the APIC
165 returned: info
166 type: list
167 sample:
168 {
169 "fvTenant": {
170 "attributes": {
171 "descr": "Production environment"
172 }
173 }
174 }
175 previous:
176 description: The original configuration from the APIC before the module has started
177 returned: info
178 type: list
179 sample:
180 [
181 {
182 "fvTenant": {
183 "attributes": {
184 "descr": "Production",
185 "dn": "uni/tn-production",
186 "name": "production",
187 "nameAlias": "",
188 "ownerKey": "",
189 "ownerTag": ""
190 }
191 }
192 }
193 ]
194 proposed:
195 description: The assembled configuration from the user-provided parameters
196 returned: info
197 type: dict
198 sample:
199 {
200 "fvTenant": {
201 "attributes": {
202 "descr": "Production environment",
203 "name": "production"
204 }
205 }
206 }
207 filter_string:
208 description: The filter string used for the request
209 returned: failure or debug
210 type: str
211 sample: ?rsp-prop-include=config-only
212 method:
213 description: The HTTP method used for the request to the APIC
214 returned: failure or debug
215 type: str
216 sample: POST
217 response:
218 description: The HTTP response from the APIC
219 returned: failure or debug
220 type: str
221 sample: OK (30 bytes)
222 status:
223 description: The HTTP status from the APIC
224 returned: failure or debug
225 type: int
226 sample: 200
227 url:
228 description: The HTTP url used for the request to the APIC
229 returned: failure or debug
230 type: str
231 sample: https://10.11.12.13/api/mo/uni/tn-production.json
232 """
233
234 from ansible.module_utils.basic import AnsibleModule
235 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
236
237
238 def main():
239 argument_spec = aci_argument_spec()
240 argument_spec.update(aci_annotation_spec())
241 argument_spec.update(aci_owner_spec())
242 argument_spec.update(
243 contract=dict(type="str", aliases=["contract_name", "name"]), # Not required for querying all objects
244 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
245 description=dict(type="str", aliases=["descr"]),
246 scope=dict(type="str", choices=["application-profile", "context", "global", "tenant", "vrf"]),
247 priority=dict(type="str", choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"]), # No default provided on purpose
248 dscp=dict(
249 type="str",
250 choices=[
251 "AF11",
252 "AF12",
253 "AF13",
254 "AF21",
255 "AF22",
256 "AF23",
257 "AF31",
258 "AF32",
259 "AF33",
260 "AF41",
261 "AF42",
262 "AF43",
263 "CS0",
264 "CS1",
265 "CS2",
266 "CS3",
267 "CS4",
268 "CS5",
269 "CS6",
270 "CS7",
271 "EF",
272 "VA",
273 "unspecified",
274 ],
275 aliases=["target"],
276 ), # No default provided on purpose
277 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
278 name_alias=dict(type="str"),
279 )
280
281 module = AnsibleModule(
282 argument_spec=argument_spec,
283 supports_check_mode=True,
284 required_if=[
285 ["state", "absent", ["contract", "tenant"]],
286 ["state", "present", ["contract", "tenant"]],
287 ],
288 )
289
290 contract = module.params.get("contract")
291 description = module.params.get("description")
292 scope = module.params.get("scope")
293 priority = module.params.get("priority")
294 dscp = module.params.get("dscp")
295 state = module.params.get("state")
296 tenant = module.params.get("tenant")
297 name_alias = module.params.get("name_alias")
298
299 aci = ACIModule(module)
300 aci.construct_url(
301 root_class=dict(
302 aci_class="fvTenant",
303 aci_rn="tn-{0}".format(tenant),
304 module_object=tenant,
305 target_filter={"name": tenant},
306 ),
307 subclass_1=dict(
308 aci_class="vzBrCP",
309 aci_rn="brc-{0}".format(contract),
310 module_object=contract,
311 target_filter={"name": contract},
312 ),
313 )
314
315 aci.get_existing()
316
317 if state == "present":
318 aci.payload(
319 aci_class="vzBrCP",
320 class_config=dict(
321 name=contract,
322 descr=description,
323 scope=scope if scope != "vrf" else "context",
324 prio=priority,
325 targetDscp=dscp,
326 nameAlias=name_alias,
327 ),
328 )
329
330 aci.get_diff(aci_class="vzBrCP")
331
332 aci.post_config()
333
334 elif state == "absent":
335 aci.delete_config()
336
337 aci.exit_json()
338
339
340 if __name__ == "__main__":
341 main()