Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Gaspard Micol (@gmicol) <[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": "community"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_key_policy
16 short_description: Manage Key Policy (fv:KeyPol)
17 description:
18 - Manage Key Policies for KeyChain Policies on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - The name of an existing tenant.
23 type: str
24 aliases: [ tenant_name ]
25 keychain_policy:
26 description:
27 - The name of an existing keychain policy.
28 type: str
29 aliases: [ keychain_policy_name ]
30 id:
31 description:
32 - The object identifier.
33 type: int
34 start_time:
35 description:
36 - The start time of the key policy.
37 - The APIC defaults to C(now) when unset during creation.
38 - The format is YYYY-MM-DD HH:MM:SS
39 type: str
40 end_time:
41 description:
42 - The end time of the key policy.
43 - The APIC defaults to C(infinite) when unset during creation.
44 - The format is YYYY-MM-DD HH:MM:SS
45 type: str
46 pre_shared_key:
47 description:
48 - The pre-shared authentifcation key.
49 - When using I(pre_shared_key) this module will always show as C(changed) as the module cannot know what the currently configured key is.
50 type: str
51 description:
52 description:
53 - The description for the keychain policy.
54 type: str
55 aliases: [ descr ]
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 - cisco.aci.owner
67
68 notes:
69 - The C(tenant) and C(keychain_policy) must exist before using this module in your playbook.
70 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_keychain_policy) can be used for this.
71 seealso:
72 - module: cisco.aci.aci_tenant
73 - module: cisco.aci.aci_keychain_policy
74 - name: APIC Management Information Model reference
75 description: More information about the internal APIC classes
76 link: https://developer.cisco.com/docs/apic-mim-ref/
77 author:
78 - Gaspard Micol (@gmicol)
79 """
80
81 EXAMPLES = r"""
82 - name: Add a new key policy
83 cisco.aci.aci_key_policy:
84 host: apic
85 username: admin
86 password: SomeSecretPassword
87 tenant: my_tenant
88 keychain_policy: my_keychain_policy
89 id: 1
90 start_time: now
91 end_time: infinite
92 pre_shared_key: my_password
93 state: present
94 delegate_to: localhost
95
96 - name: Delete an key policy
97 cisco.aci.aci_key_policy:
98 host: apic
99 username: admin
100 password: SomeSecretPassword
101 tenant: my_tenant
102 keychain_policy: my_keychain_policy
103 id: 1
104 state: absent
105 delegate_to: localhost
106
107 - name: Query an key policy
108 cisco.aci.aci_key_policy:
109 host: apic
110 username: admin
111 password: SomeSecretPassword
112 tenant: my_tenant
113 keychain_policy: my_keychain_policy
114 id: 1
115 state: query
116 delegate_to: localhost
117 register: query_result
118
119 - name: Query all key policies in my_keychain_policy
120 cisco.aci.aci_key_policy:
121 host: apic
122 username: admin
123 password: SomeSecretPassword
124 tenant: my_tenant
125 keychain_policy: my_keychain_policy
126 state: query
127 delegate_to: localhost
128 register: query_result
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
237 from ansible.module_utils.basic import AnsibleModule
238 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
239
240
241 def main():
242 argument_spec = aci_argument_spec()
243 argument_spec.update(aci_annotation_spec())
244 argument_spec.update(aci_owner_spec())
245 argument_spec.update(
246 tenant=dict(type="str", aliases=["tenant_name"]),
247 keychain_policy=dict(type="str", aliases=["keychain_policy_name"], no_log=False),
248 id=dict(type="int"),
249 start_time=dict(type="str"),
250 end_time=dict(type="str"),
251 pre_shared_key=dict(type="str", no_log=True),
252 description=dict(type="str", aliases=["descr"]),
253 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
254 )
255
256 module = AnsibleModule(
257 argument_spec=argument_spec,
258 supports_check_mode=True,
259 required_if=[
260 ["state", "absent", ["tenant", "keychain_policy", "id"]],
261 ["state", "present", ["tenant", "keychain_policy", "id"]],
262 ],
263 )
264
265 tenant = module.params.get("tenant")
266 keychain_policy = module.params.get("keychain_policy")
267 id = module.params.get("id")
268 start_time = module.params.get("start_time")
269 end_time = module.params.get("end_time")
270 pre_shared_key = module.params.get("pre_shared_key")
271 description = module.params.get("description")
272 state = module.params.get("state")
273
274 aci = ACIModule(module)
275
276 aci.construct_url(
277 root_class=dict(
278 aci_class="fvTenant",
279 aci_rn="tn-{0}".format(tenant),
280 module_object=tenant,
281 target_filter={"name": tenant},
282 ),
283 subclass_1=dict(
284 aci_class="fvKeyChainPol",
285 aci_rn="keychainp-{0}".format(keychain_policy),
286 module_object=keychain_policy,
287 target_filter={"name": keychain_policy},
288 ),
289 subclass_2=dict(
290 aci_class="fvKeyPol",
291 aci_rn="keyp-{0}".format(id),
292 module_object=id,
293 target_filter={"id": id},
294 ),
295 )
296
297 aci.get_existing()
298
299 if state == "present":
300 class_config = dict(
301 id=id,
302 startTime=start_time,
303 endTime=end_time,
304 descr=description,
305 )
306 if pre_shared_key is not None:
307 class_config.update(preSharedKey=pre_shared_key)
308
309 aci.payload(
310 aci_class="fvKeyPol",
311 class_config=class_config,
312 )
313
314 aci.get_diff(aci_class="fvKeyPol")
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()