Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_syslog.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2020, 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: purefb_syslog
20 version_added: '1.4.0'
21 short_description: Configure Pure Storage FlashBlade syslog settings
22 description:
23 - Configure syslog configuration for Pure Storage FlashBlades.
24 - Add or delete an individual syslog server to the existing
25 list of serves.
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 name:
30 description:
31 - Unique identifier for the syslog server address
32 type: str
33 required: true
34 state:
35 description:
36 - Create update, delete or test syslog servers configuration
37 default: present
38 type: str
39 choices: [ absent, present, test ]
40 protocol:
41 description:
42 - Protocol which server uses
43 type: str
44 choices: [ tcp, tls, udp ]
45 port:
46 description:
47 - Port at which the server is listening. If no port is specified
48 the system will use 514
49 type: str
50 address:
51 description:
52 - Syslog server address.
53 This field supports IPv4 or FQDN.
54 An invalid IP addresses will cause the module to fail.
55 No validation is performed for FQDNs.
56 type: str
57 services:
58 description:
59 - Syslog service type(s)
60 type: list
61 elements: str
62 choices: [ management, data-audit ]
63 default: management
64 extends_documentation_fragment:
65 - purestorage.flashblade.purestorage.fb
66 """
67
68 EXAMPLES = r"""
69 - name: Delete exisitng syslog server entries
70 purestorage.flashblade.purefb_syslog:
71 name: syslog1
72 state: absent
73 fb_url: 10.10.10.2
74 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
75
76 - name: Set array syslog servers
77 purestorage.flashblade.purefb_syslog:
78 state: present
79 name: syslog1
80 services:
81 - data-audit
82 address: syslog1.com
83 protocol: udp
84 fb_url: 10.10.10.2
85 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
86 """
87
88 RETURN = r"""
89 """
90
91
92 HAS_PURESTORAGE = True
93 try:
94 from pypureclient.flashblade import SyslogServerPost, SyslogServerPatch
95 except ImportError:
96 HAS_PURESTORAGE = False
97
98
99 from ansible.module_utils.basic import AnsibleModule
100 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
101 get_system,
102 purefb_argument_spec,
103 )
104
105
106 SYSLOG_SERVICES_API = "2.14"
107
108
109 def delete_syslog(module, blade):
110 """Delete Syslog Server"""
111 changed = True
112 if not module.check_mode:
113 res = blade.delete_syslog_servers(names=[module.params["name"]])
114 if res.status_code != 200:
115 module.fail_json(
116 msg="Failed to remove syslog server {0}. Error: {1}".format(
117 module.params["name"], res.errors[0].message
118 )
119 )
120
121 module.exit_json(changed=changed)
122
123
124 def add_syslog(module, blade):
125 """Add Syslog Server"""
126 changed = False
127 noport_address = module.params["protocol"] + "://" + module.params["address"]
128
129 if module.params["port"]:
130 full_address = noport_address + ":" + module.params["port"]
131 else:
132 full_address = noport_address
133 api_version = list(blade.get_versions().items)
134
135 changed = True
136 if not module.check_mode:
137 if SYSLOG_SERVICES_API in api_version:
138 res = blade.post_syslog_servers(
139 names=[module.params["name"]],
140 syslog_server=SyslogServerPost(
141 uri=full_address, services=module.params["services"]
142 ),
143 )
144 if res.status_code != 200:
145 module.fail_json(
146 msg="Failed to add syslog server. Error: {0}".format(
147 res.errors[0].message
148 )
149 )
150 else:
151 res = blade.post_syslog_servers(
152 syslog_server=SyslogServerPost(uri=full_address),
153 names=[module.params["name"]],
154 )
155 if res.status_code != 200:
156 module.fail_json(
157 msg="Failed to add syslog server {0} - {1}. Error: {2}".format(
158 module.params["name"], full_address, res.errors[0].message
159 )
160 )
161
162 module.exit_json(changed=changed)
163
164
165 def update_syslog(module, blade):
166 """Update Syslog Server"""
167 changed = False
168 api_version = list(blade.get_versions().items)
169 if SYSLOG_SERVICES_API not in api_version:
170 module.exit_json(
171 msg="Purity//FB needs upgrading to support modification of existing syslog server"
172 )
173 syslog_config = list(blade.get_syslog_servers(names=[module.params["name"]]).items)[
174 0
175 ]
176 noport_address = module.params["protocol"] + "://" + module.params["address"]
177
178 if module.params["port"]:
179 full_address = noport_address + ":" + module.params["port"]
180 else:
181 full_address = noport_address
182 new_uri = syslog_config.uri
183 if full_address != new_uri:
184 changed = True
185 new_uri = full_address
186 new_services = syslog_config.services
187 if module.params["services"] != new_services:
188 changed = True
189 new_services = module.params["services"]
190 if changed and not module.check_mode:
191 res = blade.patch_syslog_servers(
192 names=[module.params["name"]],
193 syslog_server=SyslogServerPatch(uri=new_uri, services=new_services),
194 )
195 if res.status_code != 200:
196 module.fail_json(
197 msg="Updating syslog server {0} failed. Error: {1}".format(
198 module.params["name"], res.errors[0].message
199 )
200 )
201 module.exit_json(changed=changed)
202
203
204 def test_syslog(module, blade):
205 """Test syslog configuration"""
206 test_response = []
207 response = list(blade.get_syslog_servers_test().items)
208 for component in range(len(response)):
209 if response[component].enabled:
210 enabled = "true"
211 else:
212 enabled = "false"
213 if response[component].success:
214 success = "true"
215 else:
216 success = "false"
217 test_response.append(
218 {
219 "component_address": response[component].component_address,
220 "component_name": response[component].component_name,
221 "description": response[component].description,
222 "destination": response[component].destination,
223 "enabled": enabled,
224 "result_details": getattr(response[component], "result_details", ""),
225 "success": success,
226 "test_type": response[component].test_type,
227 "resource_name": response[component].resource.name,
228 }
229 )
230 module.exit_json(changed=True, test_response=test_response)
231
232
233 def main():
234 argument_spec = purefb_argument_spec()
235 argument_spec.update(
236 dict(
237 address=dict(type="str"),
238 protocol=dict(type="str", choices=["tcp", "tls", "udp"]),
239 port=dict(type="str"),
240 name=dict(type="str", required=True),
241 services=dict(
242 type="list",
243 elements="str",
244 choices=["management", "data-audit"],
245 default=["management"],
246 ),
247 state=dict(
248 type="str", default="present", choices=["absent", "present", "test"]
249 ),
250 )
251 )
252
253 required_if = [["state", "present", ["address", "protocol"]]]
254
255 module = AnsibleModule(
256 argument_spec, required_if=required_if, supports_check_mode=True
257 )
258
259 blade = get_system(module)
260
261 res = blade.get_syslog_servers(names=[module.params["name"]])
262 exists = bool(res.status_code == 200)
263
264 if module.params["state"] == "absent" and exists:
265 delete_syslog(module, blade)
266 elif module.params["state"] == "present" and exists:
267 update_syslog(module, blade)
268 elif module.params["state"] == "present" and not exists:
269 add_syslog(module, blade)
270 elif module.params["state"] == "test" and exists:
271 test_syslog(module, blade)
272
273 module.exit_json(changed=False)
274
275
276 if __name__ == "__main__":
277 main()