Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_kmip.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING 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 = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_kmip
20 version_added: '1.10.0'
21 short_description: Manage FlashArray KMIP server objects
22 description:
23 - Manage FlashArray KMIP Server objects
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 name:
28 description:
29 - Name of the KMIP server object
30 type: str
31 required: true
32 certificate:
33 description:
34 - Name of existing certificate used to verify FlashArray
35 authenticity to the KMIP server.
36 - Use the I(purestorage.flasharray.purefa_certs) module to create certificates.
37 type: str
38 state:
39 description:
40 - Action for the module to perform
41 default: present
42 choices: [ absent, present, test ]
43 type: str
44 ca_certificate:
45 type: str
46 description:
47 - The text of the CA certificate for the KMIP server.
48 - Includes the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" lines
49 - Does not exceed 3000 characters in length
50 uris:
51 type: list
52 elements: str
53 description:
54 - A list of URIs for the configured KMIP servers.
55 extends_documentation_fragment:
56 - purestorage.flasharray.purestorage.fa
57 """
58
59 EXAMPLES = r"""
60 - name: Create KMIP object
61 purestorage.flasharray.purefa_kmip:
62 name: foo
63 certificate: bar
64 ca_certificate: "{{lookup('file', 'example.crt') }}"
65 uris:
66 - 1.1.1.1:8888
67 - 2.3.3.3:9999
68 fa_url: 10.10.10.2
69 api_token: e31060a7-21fc-e277-6240-25983c6c4592
70
71 - name: Delete KMIP object
72 purestorage.flasharray.purefa_kmip:
73 name: foo
74 state: absent
75 fa_url: 10.10.10.2
76 api_token: e31060a7-21fc-e277-6240-25983c6c4592
77
78 - name: Update KMIP object
79 purestorage.flasharray.purefa_kmip:
80 name: foo
81 ca_certificate: "{{lookup('file', 'example2.crt') }}"
82 uris:
83 - 3.3.3.3:8888
84 - 4.4.4.4:9999
85 fa_url: 10.10.10.2
86 api_token: e31060a7-21fc-e277-6240-25983c6c4592
87 """
88
89 RETURN = r"""
90 """
91
92 HAS_PURESTORAGE = True
93 try:
94 from pypureclient import flasharray
95 except ImportError:
96 HAS_PURESTORAGE = False
97
98 from ansible.module_utils.basic import AnsibleModule
99 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
100 get_array,
101 purefa_argument_spec,
102 )
103
104
105 def test_kmip(module, array):
106 """Test KMIP object configuration"""
107 test_response = []
108 response = list(array.get_kmip_test(names=[module.params["name"]]).items)
109 for component in response:
110 if component.enabled:
111 enabled = "true"
112 else:
113 enabled = "false"
114 if component.success:
115 success = "true"
116 else:
117 success = "false"
118 test_response.append(
119 {
120 "component_address": component.component_address,
121 "component_name": component.component_name,
122 "description": component.description,
123 "destination": component.destination,
124 "enabled": enabled,
125 "result_details": getattr(component, "result_details", ""),
126 "success": success,
127 "test_type": component.test_type,
128 "resource_name": component.resource.name,
129 }
130 )
131 module.exit_json(changed=True, test_response=test_response)
132
133
134 def update_kmip(module, array):
135 """Update existing KMIP object"""
136 changed = False
137 current_kmip = list(array.get_kmip(names=[module.params["name"]]).items)[0]
138 if (
139 module.params["certificate"]
140 and current_kmip.certificate.name != module.params["certificate"]
141 ):
142 if (
143 array.get_certificates(names=[module.params["certificate"]]).status_code
144 != 200
145 ):
146 module.fail_json(
147 msg="Array certificate {0} does not exist.".format(
148 module.params["certificate"]
149 )
150 )
151 changed = True
152 certificate = module.params["certificate"]
153 else:
154 certificate = current_kmip.certificate.name
155 if module.params["uris"] and sorted(current_kmip.uris) != sorted(
156 module.params["uris"]
157 ):
158 changed = True
159 uris = sorted(module.params["uris"])
160 else:
161 uris = sorted(current_kmip.uris)
162 if (
163 module.params["ca_certificate"]
164 and module.params["ca_certificate"] != current_kmip.ca_certificate
165 ):
166 changed = True
167 ca_cert = module.params["ca_certificate"]
168 else:
169 ca_cert = current_kmip.ca_certificate
170 if not module.check_mode:
171 if changed:
172 kmip = flasharray.KmipPost(
173 uris=uris,
174 ca_certificate=ca_cert,
175 certificate=flasharray.ReferenceNoId(name=certificate),
176 )
177 res = array.patch_kmip(names=[module.params["name"]], kmip=kmip)
178 if res.status_code != 200:
179 module.fail_json(
180 msg="Updating existing KMIP object {0} failed. Error: {1}".format(
181 module.params["name"], res.errors[0].message
182 )
183 )
184
185 module.exit_json(changed=changed)
186
187
188 def create_kmip(module, array):
189 """Create KMIP object"""
190 if array.get_certificates(names=[module.params["certificate"]]).status_code != 200:
191 module.fail_json(
192 msg="Array certificate {0} does not exist.".format(
193 module.params["certificate"]
194 )
195 )
196 changed = True
197 kmip = flasharray.KmipPost(
198 uris=sorted(module.params["uris"]),
199 ca_certificate=module.params["ca_certificate"],
200 certificate=flasharray.ReferenceNoId(name=module.params["certificate"]),
201 )
202 if not module.check_mode:
203 res = array.post_kmip(names=[module.params["name"]], kmip=kmip)
204 if res.status_code != 200:
205 module.fail_json(
206 msg="Creating KMIP object {0} failed. Error: {1}".format(
207 module.params["name"], res.errors[0].message
208 )
209 )
210 module.exit_json(changed=changed)
211
212
213 def delete_kmip(module, array):
214 """Delete existing KMIP object"""
215 changed = True
216 if not module.check_mode:
217 res = array.delete_kmip(names=[module.params["name"]])
218 if res.status_code != 200:
219 module.fail_json(
220 msg="Failed to delete {0} KMIP object. Error: {1}".format(
221 module.params["name"], res.errors[0].message
222 )
223 )
224 module.exit_json(changed=changed)
225
226
227 def main():
228 argument_spec = purefa_argument_spec()
229 argument_spec.update(
230 dict(
231 state=dict(
232 type="str",
233 default="present",
234 choices=["absent", "present", "test"],
235 ),
236 name=dict(type="str", required=True),
237 certificate=dict(type="str"),
238 ca_certificate=dict(type="str", no_log=True),
239 uris=dict(type="list", elements="str"),
240 )
241 )
242
243 module = AnsibleModule(
244 argument_spec,
245 supports_check_mode=True,
246 )
247
248 if not HAS_PURESTORAGE:
249 module.fail_json(msg="py-pure-client sdk is required for this module")
250
251 array = get_array(module)
252
253 state = module.params["state"]
254 exists = bool(array.get_kmip(names=[module.params["name"]]).status_code == 200)
255 if module.params["certificate"] and len(module.params["certificate"]) > 3000:
256 module.fail_json(msg="Certificate exceeds 3000 characters")
257
258 if not exists and state == "present":
259 create_kmip(module, array)
260 elif exists and state == "present":
261 update_kmip(module, array)
262 elif exists and state == "absent":
263 delete_kmip(module, array)
264 elif exists and state == "test":
265 test_kmip(module, array)
266
267 module.exit_json(changed=False)
268
269
270 if __name__ == "__main__":
271 main()