Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_syslog_settings.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_syslog_settings
20 version_added: '1.10.0'
21 short_description: Manage FlashArray syslog servers settings
22 description:
23 - Manage FlashArray syslog servers settings
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 severity:
28 description:
29 - Logging severity threshold for which events will be forwarded to the
30 configured syslog servers.
31 default: info
32 choices: [ debug, info, notice ]
33 type: str
34 ca_certificate:
35 type: str
36 description:
37 - The text of the CA certificate for configured syslog servers.
38 - Includes the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" lines
39 - Does not exceed 3000 characters in length
40 - To delete the existing CA certificate use the special string `DELETE`
41 tls_audit:
42 type: bool
43 default: true
44 description:
45 - If messages that are necessary in order to audit TLS negotiations
46 performed by the array are forwarded to the syslog servers.
47 context:
48 description:
49 - Name of fleet member on which to perform the operation.
50 - This requires the array receiving the request is a member of a fleet
51 and the context name to be a member of the same fleet.
52 type: str
53 default: ""
54 version_added: '1.39.0'
55 extends_documentation_fragment:
56 - purestorage.flasharray.purestorage.fa
57 """
58
59 EXAMPLES = r"""
60 - name: Change syslog servers settings
61 purestorage.flasharray.purefa_syslog_settings:
62 tls_audit: false
63 severity: debug
64 ca_certificate: "{{lookup('file', 'example.crt') }}"
65 fa_url: 10.10.10.2
66 api_token: e31060a7-21fc-e277-6240-25983c6c4592
67
68 - name: Delete existing CA certificate for syslog servers settings
69 purestorage.flasharray.purefa_syslog_settings:
70 ca_certificate: DELETE
71 fa_url: 10.10.10.2
72 api_token: e31060a7-21fc-e277-6240-25983c6c4592
73 """
74
75 RETURN = r"""
76 """
77
78 HAS_PURESTORAGE = True
79 try:
80 from pypureclient import flasharray
81 except ImportError:
82 HAS_PURESTORAGE = False
83
84 from ansible.module_utils.basic import AnsibleModule
85 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
86 get_array,
87 purefa_argument_spec,
88 )
89 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
90 LooseVersion,
91 )
92
93 MIN_REQUIRED_API_VERSION = "2.9"
94 CONTEXT_VERSION = "2.38"
95
96
97 def main():
98 argument_spec = purefa_argument_spec()
99 argument_spec.update(
100 dict(
101 severity=dict(
102 type="str",
103 default="info",
104 choices=["info", "debug", "notice"],
105 ),
106 tls_audit=dict(type="bool", default=True),
107 ca_certificate=dict(type="str", no_log=True),
108 context=dict(type="str", default=""),
109 )
110 )
111
112 module = AnsibleModule(
113 argument_spec,
114 supports_check_mode=True,
115 )
116
117 if not HAS_PURESTORAGE:
118 module.fail_json(msg="py-pure-client sdk is required for this module")
119
120 array = get_array(module)
121 api_version = array.get_rest_version()
122
123 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
124 module.fail_json(
125 msg="Purity//FA version not supported. Minimum version required: 6.2.0"
126 )
127
128 changed = cert_change = False
129 if module.params["ca_certificate"] and len(module.params["ca_certificate"]) > 3000:
130 module.fail_json(msg="Certificate exceeds 3000 characters")
131 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
132 current = list(
133 array.get_syslog_servers_settings(
134 context_names=[module.params["context"]]
135 ).items
136 )[0]
137 else:
138 current = list(array.get_syslog_servers_settings().items)[0]
139 try:
140 if hasattr(current, "ca_certificate"):
141 pass
142 except AttributeError:
143 current.ca_certificate = None
144 if current.tls_audit_enabled != module.params["tls_audit"]:
145 changed = True
146 new_tls = module.params["tls_audit"]
147 else:
148 new_tls = current.tls_audit_enabled
149 if current.logging_severity != module.params["severity"]:
150 changed = True
151 new_sev = module.params["severity"]
152 else:
153 new_sev = current.logging_severity
154 new_cert = "" # Initialize for pylint
155 if module.params["ca_certificate"]:
156 if module.params["ca_certificate"].upper() == "DELETE":
157 if hasattr(current, "ca_certificate"):
158 cert_change = changed = True
159 new_cert = ""
160 elif (
161 hasattr(current, "ca_certificate")
162 and current.ca_certificate != module.params["ca_certificate"]
163 ):
164 cert_change = changed = True
165 new_cert = module.params["ca_certificate"]
166 if changed and not module.check_mode:
167 if cert_change:
168 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
169 res = array.patch_syslog_servers_settings(
170 syslog_server_settings=flasharray.SyslogServerSettings(
171 ca_certificate=new_cert,
172 tls_audit_enabled=new_tls,
173 logging_severity=new_sev,
174 ),
175 context_names=[module.params["context"]],
176 )
177 else:
178 res = array.patch_syslog_servers_settings(
179 syslog_server_settings=flasharray.SyslogServerSettings(
180 ca_certificate=new_cert,
181 tls_audit_enabled=new_tls,
182 logging_severity=new_sev,
183 )
184 )
185 else:
186 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
187 res = array.patch_syslog_servers_settings(
188 context_names=[module.params["context"]],
189 syslog_server_settings=flasharray.SyslogServerSettings(
190 tls_audit_enabled=new_tls, logging_severity=new_sev
191 ),
192 )
193 else:
194 res = array.patch_syslog_servers_settings(
195 syslog_server_settings=flasharray.SyslogServerSettings(
196 tls_audit_enabled=new_tls, logging_severity=new_sev
197 )
198 )
199 if res.status_code != 200:
200 module.fail_json(
201 msg="Changing syslog settings failed. Error: {0}".format(
202 res.errors[0].message
203 )
204 )
205
206 module.exit_json(changed=changed)
207
208
209 if __name__ == "__main__":
210 main()