Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2024, Faiz Mohammad (@Ziaf007) <[email protected]>
4
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_oob_contract
16 version_added: "2.12.0"
17 short_description: Manage Out-of-Band (OOB) Contract resources (vz:OOBBrCP)
18 description:
19 - Manage Out-of-Band (OOB) Contract resources on Cisco ACI fabrics.
20 options:
21 contract:
22 description:
23 - The name of the OOB contract.
24 type: str
25 aliases: [ contract_name, name ]
26 description:
27 description:
28 - The description of the OOB Contract.
29 type: str
30 aliases: [ descr ]
31 scope:
32 description:
33 - The scope of the OOB contract.
34 - The APIC defaults to C(context) when unset during creation.
35 - The choice C(vrf) is equal to C(context).
36 type: str
37 choices: [ application-profile, context, global, tenant, vrf ]
38 priority:
39 description:
40 - The desired Quality of Service (QoS) class to be used.
41 - The APIC defaults to C(unspecified) when unset during creation.
42 type: str
43 choices: [ level1, level2, level3, level4, level5, level6, unspecified ]
44 state:
45 description:
46 - Use C(present) or C(absent) for adding or removing.
47 - Use C(query) for listing an object or multiple objects.
48 type: str
49 choices: [ absent, present, query ]
50 default: present
51 name_alias:
52 description:
53 - The alias for the current object. This relates to the nameAlias field in ACI.
54 type: str
55 extends_documentation_fragment:
56 - cisco.aci.aci
57 - cisco.aci.annotation
58 - cisco.aci.owner
59
60 seealso:
61 - name: APIC Management Information Model reference
62 description: More information about the internal APIC class B(vz:OOBBrCP).
63 link: https://developer.cisco.com/docs/apic-mim-ref/
64 author:
65 - Faiz Mohammad (@faizmoh)
66 """
67
68 EXAMPLES = r"""
69 - name: Add a new contract
70 cisco.aci.aci_oob_contract:
71 host: apic
72 username: admin
73 password: SomeSecretPassword
74 contract: web_to_db
75 description: Communication between web-servers and database
76 priority: level2
77 scope: global
78 state: present
79 delegate_to: localhost
80
81 - name: Update a specific contract
82 cisco.aci.aci_oob_contract:
83 host: apic
84 username: admin
85 password: SomeSecretPassword
86 contract: web_to_db
87 state: present
88 scope: tenant
89 description: Communication between web-servers and database on port 6744
90 delegate_to: localhost
91 register: query_result
92
93 - name: Query a specific contract
94 cisco.aci.aci_oob_contract:
95 host: apic
96 username: admin
97 password: SomeSecretPassword
98 contract: web_to_db
99 state: query
100 delegate_to: localhost
101 register: query_result
102
103 - name: Query all contracts
104 cisco.aci.aci_oob_contract:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 state: query
109 delegate_to: localhost
110 register: query_result
111
112 - name: Remove an existing contract
113 cisco.aci.aci_oob_contract:
114 host: apic
115 username: admin
116 password: SomeSecretPassword
117 contract: web_to_db
118 state: absent
119 delegate_to: localhost
120 """
121
122 RETURN = r"""
123 current:
124 description: The existing configuration from the APIC after the module has finished
125 returned: success
126 type: list
127 sample:
128 [
129 {
130 "fvTenant": {
131 "attributes": {
132 "descr": "Production environment",
133 "dn": "uni/tn-production",
134 "name": "production",
135 "nameAlias": "",
136 "ownerKey": "",
137 "ownerTag": ""
138 }
139 }
140 }
141 ]
142 error:
143 description: The error information as returned from the APIC
144 returned: failure
145 type: dict
146 sample:
147 {
148 "code": "122",
149 "text": "unknown managed object class foo"
150 }
151 raw:
152 description: The raw output returned by the APIC REST API (xml or json)
153 returned: parse error
154 type: str
155 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
156 sent:
157 description: The actual/minimal configuration pushed to the APIC
158 returned: info
159 type: list
160 sample:
161 {
162 "fvTenant": {
163 "attributes": {
164 "descr": "Production environment"
165 }
166 }
167 }
168 previous:
169 description: The original configuration from the APIC before the module has started
170 returned: info
171 type: list
172 sample:
173 [
174 {
175 "fvTenant": {
176 "attributes": {
177 "descr": "Production",
178 "dn": "uni/tn-production",
179 "name": "production",
180 "nameAlias": "",
181 "ownerKey": "",
182 "ownerTag": ""
183 }
184 }
185 }
186 ]
187 proposed:
188 description: The assembled configuration from the user-provided parameters
189 returned: info
190 type: dict
191 sample:
192 {
193 "fvTenant": {
194 "attributes": {
195 "descr": "Production environment",
196 "name": "production"
197 }
198 }
199 }
200 filter_string:
201 description: The filter string used for the request
202 returned: failure or debug
203 type: str
204 sample: ?rsp-prop-include=config-only
205 method:
206 description: The HTTP method used for the request to the APIC
207 returned: failure or debug
208 type: str
209 sample: POST
210 response:
211 description: The HTTP response from the APIC
212 returned: failure or debug
213 type: str
214 sample: OK (30 bytes)
215 status:
216 description: The HTTP status from the APIC
217 returned: failure or debug
218 type: int
219 sample: 200
220 url:
221 description: The HTTP url used for the request to the APIC
222 returned: failure or debug
223 type: str
224 sample: https://10.11.12.13/api/mo/uni/tn-production.json
225 """
226
227 from ansible.module_utils.basic import AnsibleModule
228 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
229
230
231 def main():
232 argument_spec = aci_argument_spec()
233 argument_spec.update(aci_annotation_spec())
234 argument_spec.update(aci_owner_spec())
235 argument_spec.update(
236 contract=dict(type="str", aliases=["contract_name", "name"]), # Not required for querying all objects
237 description=dict(type="str", aliases=["descr"]),
238 scope=dict(type="str", choices=["application-profile", "context", "global", "tenant", "vrf"]),
239 priority=dict(type="str", choices=["level1", "level2", "level3", "level4", "level5", "level6", "unspecified"]), # No default provided on purpose
240 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
241 name_alias=dict(type="str"),
242 )
243
244 module = AnsibleModule(
245 argument_spec=argument_spec,
246 supports_check_mode=True,
247 required_if=[
248 ["state", "absent", ["contract"]],
249 ["state", "present", ["contract"]],
250 ],
251 )
252
253 contract = module.params.get("contract")
254 description = module.params.get("description")
255 scope = module.params.get("scope")
256 priority = module.params.get("priority")
257 state = module.params.get("state")
258 name_alias = module.params.get("name_alias")
259
260 aci = ACIModule(module)
261 aci.construct_url(
262 root_class=dict(
263 aci_class="vzOOBBrCP",
264 aci_rn="tn-mgmt/oobbrc-{0}".format(contract),
265 module_object=contract,
266 target_filter={"name": contract} if contract else None,
267 )
268 )
269
270 aci.get_existing()
271
272 if state == "present":
273 aci.payload(
274 aci_class="vzOOBBrCP",
275 class_config=dict(
276 name=contract,
277 descr=description,
278 scope=scope if scope != "vrf" else "context",
279 prio=priority,
280 nameAlias=name_alias,
281 ),
282 )
283
284 aci.get_diff(aci_class="vzOOBBrCP")
285
286 aci.post_config()
287
288 elif state == "absent":
289 aci.delete_config()
290
291 aci.exit_json()
292
293
294 if __name__ == "__main__":
295 main()