Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, 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_syslog
20 version_added: '1.0.0'
21 short_description: Configure Pure Storage FlashArray syslog settings
22 description:
23 - Configure syslog configuration for Pure Storage FlashArrays.
24 - Manage individual syslog servers.
25 author:
26 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 state:
29 description:
30 - Create, updatee, delete or test syslog servers configuration
31 default: present
32 type: str
33 choices: [ absent, present, test ]
34 protocol:
35 description:
36 - Protocol which server uses
37 required: true
38 type: str
39 choices: [ tcp, tls, udp ]
40 port:
41 description:
42 - Port at which the server is listening. If no port is specified
43 the system will use 514
44 type: str
45 address:
46 description:
47 - Syslog server address.
48 This field supports IPv4, IPv6 or FQDN.
49 An invalid IP addresses will cause the module to fail.
50 No validation is performed for FQDNs.
51 type: str
52 required: true
53 name:
54 description:
55 - A user-specified name.
56 The name must be locally unique and cannot be changed.
57 type: str
58 required: true
59 context:
60 description:
61 - Name of fleet member on which to perform the syslog operation.
62 - This requires the array receiving the request is a member of a fleet
63 and the context name to be a member of the same fleet.
64 type: str
65 default: ""
66 version_added: '1.37.0'
67 extends_documentation_fragment:
68 - purestorage.flasharray.purestorage.fa
69 """
70
71 EXAMPLES = r"""
72 - name: Delete existing syslog server entry
73 purestorage.flasharray.purefa_syslog:
74 name: syslog1
75 address: syslog1.com
76 protocol: tcp
77 state: absent
78 fa_url: 10.10.10.2
79 api_token: e31060a7-21fc-e277-6240-25983c6c4592
80
81 - name: Add syslog server entry
82 purestorage.flasharray.purefa_syslog:
83 name: syslog1
84 address: syslog1.com
85 port: 8081
86 protocol: udp
87 fa_url: 10.10.10.2
88 api_token: e31060a7-21fc-e277-6240-25983c6c4592
89
90 - name: Update syslog server entry
91 purestorage.flasharray.purefa_syslog:
92 name: syslog1
93 address: syslog1.com
94 port: 8081
95 protocol: tcp
96 fa_url: 10.10.10.2
97 api_token: e31060a7-21fc-e277-6240-25983c6c4592
98 """
99
100 RETURN = r"""
101 """
102
103
104 HAS_PURESTORAGE = True
105 try:
106 from pypureclient.flasharray import SyslogServer
107 except ImportError:
108 HAS_PURESTORAGE = False
109
110
111 from ansible.module_utils.basic import AnsibleModule
112 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
113 get_array,
114 purefa_argument_spec,
115 )
116 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
117 LooseVersion,
118 )
119
120 CONTEXT_API_VERSION = "2.38"
121
122
123 def test_syslog(module, array):
124 """Test syslog configuration"""
125 api_version = array.get_rest_version()
126 test_response = []
127 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
128 response = list(
129 array.get_syslog_servers_test(
130 context_names=[module.params["context"]]
131 ).items
132 )
133 else:
134 response = list(array.get_syslog_servers_test().items)
135 for component in response:
136 if component.enabled:
137 enabled = "true"
138 else:
139 enabled = "false"
140 if component.success:
141 success = "true"
142 else:
143 success = "false"
144 test_response.append(
145 {
146 "component_address": component.component_address,
147 "component_name": component.component_name,
148 "description": component.description,
149 "destination": component.destination,
150 "enabled": enabled,
151 "result_details": getattr(component, "result_details", ""),
152 "success": success,
153 "test_type": component.test_type,
154 "resource_name": component.resource.name,
155 }
156 )
157 module.exit_json(changed=True, test_response=test_response)
158
159
160 def delete_syslog(module, array):
161 """Delete Syslog Server"""
162 api_version = array.get_rest_version()
163 changed = True
164 if not module.check_mode:
165 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
166 res = array.delete_syslog_servers(
167 names=[module.params["name"]],
168 context_names=[module.params["context"]],
169 )
170 else:
171 res = array.delete_syslog_servers(names=[module.params["name"]])
172 if res.status_code != 200:
173 module.fail_json(
174 msg="Failed to remove syslog server {0}. Error: {1}".format(
175 module.params["name"], res.errors[0].message
176 )
177 )
178 module.exit_json(changed=changed)
179
180
181 def add_syslog(module, array):
182 """Add Syslog Server"""
183 api_version = array.get_rest_version()
184 changed = True
185 noport_address = module.params["protocol"] + "://" + module.params["address"]
186
187 if module.params["port"]:
188 full_address = noport_address + ":" + module.params["port"]
189 else:
190 full_address = noport_address
191 if not module.check_mode:
192 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
193 res = array.post_syslog_servers(
194 names=[module.params["name"]],
195 syslog_server=SyslogServer(
196 name=module.params["name"], uri=full_address
197 ),
198 context_names=[module.params["context"]],
199 )
200 else:
201 res = array.post_syslog_servers(
202 names=[module.params["name"]],
203 syslog_server=SyslogServer(
204 name=module.params["name"], uri=full_address
205 ),
206 )
207 if res.status_code != 200:
208 module.fail_json(
209 msg="Adding syslog server {0} failed. Error: {1}".format(
210 module.params["name"], res.errors[0].message
211 )
212 )
213 module.exit_json(changed=changed)
214
215
216 def update_syslog(module, array):
217 """Update Syslog Server"""
218 api_version = array.get_rest_version()
219 changed = False
220 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
221 syslog_server_list = array.get_syslog_servers(
222 names=[module.params["name"]],
223 context_names=[module.params["context"]],
224 )
225 else:
226 syslog_server_list = array.get_syslog_servers(names=[module.params["name"]])
227 syslog_config = list(syslog_server_list.items)[0]
228 noport_address = module.params["protocol"] + "://" + module.params["address"]
229
230 if module.params["port"]:
231 full_address = noport_address + ":" + module.params["port"]
232 else:
233 full_address = noport_address
234 if full_address != syslog_config.uri:
235 changed = True
236 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
237 res = array.patch_syslog_servers(
238 names=[module.params["name"]],
239 syslog_server=SyslogServer(uri=full_address),
240 context_names=[module.params["context"]],
241 )
242 else:
243 res = array.patch_syslog_servers(
244 names=[module.params["name"]],
245 syslog_server=SyslogServer(uri=full_address),
246 )
247 if res.status_code != 200:
248 module.fail_json(
249 msg="Updating syslog server {0} failed. Error: {1}".format(
250 module.params["name"], res.errors[0].message
251 )
252 )
253 module.exit_json(changed=changed)
254
255
256 def main():
257 argument_spec = purefa_argument_spec()
258 argument_spec.update(
259 dict(
260 address=dict(type="str", required=True),
261 protocol=dict(type="str", choices=["tcp", "tls", "udp"], required=True),
262 port=dict(type="str"),
263 name=dict(type="str", required=True),
264 state=dict(
265 type="str", default="present", choices=["absent", "present", "test"]
266 ),
267 context=dict(type="str", default=""),
268 )
269 )
270
271 module = AnsibleModule(argument_spec, supports_check_mode=True)
272
273 array = get_array(module)
274
275 if not HAS_PURESTORAGE:
276 module.fail_json(msg="py-pure-client sdk is required for this module")
277
278 api_version = array.get_rest_version()
279 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
280 res = array.get_syslog_servers(
281 names=[module.params["name"]],
282 context_names=[module.params["context"]],
283 )
284 else:
285 res = array.get_syslog_servers(names=[module.params["name"]])
286 exists = bool(res.status_code == 200)
287
288 if module.params["state"] == "absent" and exists:
289 delete_syslog(module, array)
290 elif module.params["state"] == "present" and not exists:
291 add_syslog(module, array)
292 elif module.params["state"] == "present" and exists:
293 update_syslog(module, array)
294 elif module.params["state"] == "test" and exists:
295 test_syslog(module, array)
296
297 module.exit_json(changed=False)
298
299
300 if __name__ == "__main__":
301 main()