Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_aaa_certificate_authority.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_aaa_certificate_authority
16 short_description: Manage AAA Certificate Authorities (pki:TP)
17 description:
18 - Manage AAA Certificate Authorities on Cisco ACI fabrics.
19 options:
20 name:
21 description:
22 - The name of the Certificate Authority.
23 type: str
24 aliases: [ certificate_authority, cert_authority, cert_authority_name, certificate_authority_name ]
25 cloud_tenant:
26 description:
27 - The name of the cloud tenant.
28 - This attribute is only applicable for Cloud APIC.
29 type: str
30 aliases: [ tenant, tenant_name ]
31 description:
32 description:
33 - The description of the Certificate Authority.
34 type: str
35 aliases: [ descr ]
36 certificate_chain:
37 description:
38 - The PEM-encoded chain of trust from the trustpoint to a trusted root authority.
39 type: str
40 aliases: [ cert_data, certificate_data, cert, certificate ]
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 name_alias:
49 description:
50 - The alias for the current object. This relates to the nameAlias field in ACI.
51 type: str
52 extends_documentation_fragment:
53 - cisco.aci.aci
54 - cisco.aci.annotation
55 - cisco.aci.owner
56
57 notes:
58 - The C(cloud_tenant) used must exist before using this module in your playbook.
59 The M(cisco.aci.aci_tenant) module can be used for this.
60 seealso:
61 - name: APIC Management Information Model reference
62 description: More information about the internal APIC class B(pki:TP).
63 link: https://developer.cisco.com/docs/apic-mim-ref/
64 author:
65 - Akini Ross (@akinross)
66 """
67
68 EXAMPLES = r"""
69 - name: Create a Certificate Authority
70 cisco.aci.aci_aaa_certificate_authority:
71 host: apic
72 username: admin
73 password: SomeSecretPassword
74 name: example_authority
75 certificate_chain: '{{ lookup("file", "pki/example_authority.crt") }}'
76 state: present
77 delegate_to: localhost
78
79 - name: Query a Certificate Authority
80 cisco.aci.aci_aaa_certificate_authority:
81 host: apic
82 username: admin
83 password: SomeSecretPassword
84 name: example_authority
85 state: query
86 delegate_to: localhost
87 register: query_result
88
89 - name: Query all Certificate Authorities
90 cisco.aci.aci_aaa_certificate_authority:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 state: query
95 delegate_to: localhost
96 register: query_result
97
98 - name: Delete a Certificate Authority
99 cisco.aci.aci_aaa_certificate_authority:
100 host: apic
101 username: admin
102 password: SomeSecretPassword
103 name: example_authority
104 state: absent
105 delegate_to: localhost
106 """
107
108 RETURN = r"""
109 current:
110 description: The existing configuration from the APIC after the module has finished
111 returned: success
112 type: list
113 sample:
114 [
115 {
116 "fvTenant": {
117 "attributes": {
118 "descr": "Production environment",
119 "dn": "uni/tn-production",
120 "name": "production",
121 "nameAlias": "",
122 "ownerKey": "",
123 "ownerTag": ""
124 }
125 }
126 }
127 ]
128 error:
129 description: The error information as returned from the APIC
130 returned: failure
131 type: dict
132 sample:
133 {
134 "code": "122",
135 "text": "unknown managed object class foo"
136 }
137 raw:
138 description: The raw output returned by the APIC REST API (xml or json)
139 returned: parse error
140 type: str
141 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
142 sent:
143 description: The actual/minimal configuration pushed to the APIC
144 returned: info
145 type: list
146 sample:
147 {
148 "fvTenant": {
149 "attributes": {
150 "descr": "Production environment"
151 }
152 }
153 }
154 previous:
155 description: The original configuration from the APIC before the module has started
156 returned: info
157 type: list
158 sample:
159 [
160 {
161 "fvTenant": {
162 "attributes": {
163 "descr": "Production",
164 "dn": "uni/tn-production",
165 "name": "production",
166 "nameAlias": "",
167 "ownerKey": "",
168 "ownerTag": ""
169 }
170 }
171 }
172 ]
173 proposed:
174 description: The assembled configuration from the user-provided parameters
175 returned: info
176 type: dict
177 sample:
178 {
179 "fvTenant": {
180 "attributes": {
181 "descr": "Production environment",
182 "name": "production"
183 }
184 }
185 }
186 filter_string:
187 description: The filter string used for the request
188 returned: failure or debug
189 type: str
190 sample: ?rsp-prop-include=config-only
191 method:
192 description: The HTTP method used for the request to the APIC
193 returned: failure or debug
194 type: str
195 sample: POST
196 response:
197 description: The HTTP response from the APIC
198 returned: failure or debug
199 type: str
200 sample: OK (30 bytes)
201 status:
202 description: The HTTP status from the APIC
203 returned: failure or debug
204 type: int
205 sample: 200
206 url:
207 description: The HTTP url used for the request to the APIC
208 returned: failure or debug
209 type: str
210 sample: https://10.11.12.13/api/mo/uni/tn-production.json
211 """
212
213 from ansible.module_utils.basic import AnsibleModule
214 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
215
216
217 def main():
218 argument_spec = aci_argument_spec()
219 argument_spec.update(aci_annotation_spec())
220 argument_spec.update(aci_owner_spec())
221 argument_spec.update(
222 name=dict(
223 type="str", aliases=["certificate_authority", "cert_authority", "cert_authority_name", "certificate_authority_name"]
224 ), # Not required for querying all objects
225 cloud_tenant=dict(type="str", aliases=["tenant", "tenant_name"]),
226 description=dict(type="str", aliases=["descr"]),
227 certificate_chain=dict(type="str", aliases=["cert_data", "certificate_data", "cert", "certificate"]),
228 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
229 name_alias=dict(type="str"),
230 )
231
232 module = AnsibleModule(
233 argument_spec=argument_spec,
234 supports_check_mode=True,
235 required_if=[
236 ["state", "absent", ["name"]],
237 ["state", "present", ["name", "certificate_chain"]],
238 ],
239 )
240
241 name = module.params.get("name")
242 cloud_tenant = module.params.get("cloud_tenant")
243 description = module.params.get("description")
244 certificate_chain = module.params.get("certificate_chain")
245 state = module.params.get("state")
246 name_alias = module.params.get("name_alias")
247
248 aci = ACIModule(module)
249 aci_class = "pkiTP"
250 parent_class = "cloudCertStore" if cloud_tenant else "pkiEp"
251 parent_rn = "tn-{0}/certstore".format(cloud_tenant) if cloud_tenant else "userext/pkiext"
252
253 aci.construct_url(
254 root_class=dict(
255 aci_class=parent_class,
256 aci_rn=parent_rn,
257 ),
258 subclass_1=dict(
259 aci_class=aci_class,
260 aci_rn="tp-{0}".format(name),
261 module_object=name,
262 target_filter={"name": name},
263 ),
264 )
265 aci.get_existing()
266
267 if state == "present":
268 aci.payload(
269 aci_class=aci_class,
270 class_config=dict(
271 name=name,
272 descr=description,
273 certChain=certificate_chain,
274 nameAlias=name_alias,
275 ),
276 )
277
278 aci.get_diff(aci_class=aci_class)
279
280 # Only wrap the payload in parent class if cloud_tenant is set to avoid apic error
281 aci.post_config(parent_class=parent_class if cloud_tenant else None)
282
283 elif state == "absent":
284 aci.delete_config()
285
286 aci.exit_json()
287
288
289 if __name__ == "__main__":
290 main()