Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_application_alerts_syslog.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.12.4
7 # Copyright (C) 2021-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12 from __future__ import (absolute_import, division, print_function)
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 ---
18 module: ome_application_alerts_syslog
19 short_description: Configure syslog forwarding settings on OpenManage Enterprise and OpenManage Enterprise Modular
20 description: This module allows to configure syslog forwarding settings on OpenManage Enterprise and OpenManage Enterprise Modular.
21 version_added: 4.3.0
22 extends_documentation_fragment:
23 - dellemc.openmanage.ome_auth_options
24 options:
25 syslog_servers:
26 description: List of servers to forward syslog.
27 type: list
28 elements: dict
29 suboptions:
30 id:
31 description: The ID of the syslog server.
32 type: int
33 choices: [1, 2, 3, 4]
34 required: true
35 enabled:
36 description: Enable or disable syslog forwarding.
37 type: bool
38 destination_address:
39 description:
40 - The IP address, FQDN or hostname of the syslog server.
41 - This is required if I(enabled) is C(true).
42 type: str
43 port_number:
44 description: The UDP port number of the syslog server.
45 type: int
46 requirements:
47 - "python >= 3.9.6"
48 author:
49 - Jagadeesh N V(@jagadeeshnv)
50 - Mangirish Kenkare(@MangirishK)
51 notes:
52 - Run this module from a system that has direct access to Dell OpenManage Enterprise or Dell OpenManage Enterprise Modular.
53 - This module supports C(check_mode).
54 """
55
56 EXAMPLES = """
57 ---
58 - name: Configure single server to forward syslog
59 dellemc.openmanage.ome_application_alerts_syslog:
60 hostname: 192.168.0.1
61 username: "username"
62 password: "password"
63 ca_path: "/path/to/ca_cert.pem"
64 syslog_servers:
65 - id: 1
66 enabled: true
67 destination_address: 192.168.0.2
68 port_number: 514
69
70 - name: Configure multiple server to forward syslog
71 dellemc.openmanage.ome_application_alerts_syslog:
72 hostname: 192.168.0.1
73 username: "username"
74 password: "password"
75 ca_path: "/path/to/ca_cert.pem"
76 syslog_servers:
77 - id: 1
78 port_number: 523
79 - id: 2
80 enabled: true
81 destination_address: sysloghost1.lab.com
82 - id: 3
83 enabled: false
84 - id: 4
85 enabled: true
86 destination_address: 192.168.0.4
87 port_number: 514
88 """
89
90 RETURN = """
91 ---
92 msg:
93 type: str
94 description: Overall status of the syslog forwarding operation.
95 returned: always
96 sample: Successfully updated the syslog forwarding settings.
97 syslog_details:
98 type: list
99 description: Syslog forwarding settings list applied.
100 returned: on success
101 sample: [
102 {
103 "DestinationAddress": "192.168.10.43",
104 "Enabled": false,
105 "Id": 1,
106 "PortNumber": 514
107 },
108 {
109 "DestinationAddress": "192.168.10.46",
110 "Enabled": true,
111 "Id": 2,
112 "PortNumber": 514
113 },
114 {
115 "DestinationAddress": "192.168.10.44",
116 "Enabled": true,
117 "Id": 3,
118 "PortNumber": 514
119 },
120 {
121 "DestinationAddress": "192.168.10.42",
122 "Enabled": true,
123 "Id": 4,
124 "PortNumber": 515
125 }
126 ]
127 error_info:
128 description: Details of the HTTP Error.
129 returned: on HTTP error
130 type: dict
131 sample: {
132 "error": {
133 "code": "Base.1.0.GeneralError",
134 "message": "A general error has occurred. See ExtendedInfo for more information.",
135 "@Message.ExtendedInfo": [
136 {
137 "MessageId": "CAPP1108",
138 "RelatedProperties": [],
139 "Message": "Unable to update the Syslog settings because the request contains an invalid number of
140 configurations. The request must contain no more than 4 configurations but contains 5.",
141 "MessageArgs": [
142 "4",
143 "5"
144 ],
145 "Severity": "Warning",
146 "Resolution": "Enter only the required number of configurations as identified in the message and
147 retry the operation."
148 }
149 ]
150 }
151 }
152 """
153
154 import json
155 from ssl import SSLError
156 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
157 from ansible.module_utils.urls import ConnectionError
158 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
159 from ansible.module_utils.common.dict_transformations import recursive_diff
160 from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
161
162 SYSLOG_GET = "AlertService/AlertDestinations/SyslogConfiguration"
163 SYSLOG_SET = "AlertService/AlertDestinations/Actions/AlertDestinations.ApplySyslogConfig"
164 SUCCESS_MSG = "Successfully updated the syslog forwarding settings."
165 DUP_ID_MSG = "Duplicate server IDs are provided."
166 NO_CHANGES_MSG = "No changes found to be applied."
167 CHANGES_FOUND = "Changes found to be applied."
168 SYSLOG_UDP = 514
169
170
171 def validate_input(module):
172 mparams = module.params
173 syslog_list = mparams.get("syslog_servers")
174 if not syslog_list:
175 module.exit_json(msg=NO_CHANGES_MSG)
176 syslog_dict = {}
177 for sys in syslog_list:
178 trim_sys = dict((k, v) for k, v in sys.items() if v is not None)
179 syslog_dict[sys.get('id')] = snake_dict_to_camel_dict(trim_sys, capitalize_first=True)
180 if len(syslog_dict) < len(syslog_list):
181 module.exit_json(msg=DUP_ID_MSG, failed=True)
182 return syslog_dict
183
184
185 def strip_substr_dict(odata_dict, chkstr='@odata.'):
186 cp = odata_dict.copy()
187 klist = cp.keys()
188 for k in klist:
189 if chkstr in str(k).lower():
190 odata_dict.pop(k)
191 if not odata_dict.get('PortNumber'):
192 odata_dict['PortNumber'] = SYSLOG_UDP
193 return odata_dict
194
195
196 def get_current_syslog(rest_obj):
197 resp = rest_obj.invoke_request("GET", SYSLOG_GET)
198 syslog_list = resp.json_data.get('value')
199 return syslog_list
200
201
202 def compare_get_payload(module, current_list, input_config):
203 payload_list = [strip_substr_dict(sys) for sys in current_list] # preserving list order
204 current_config = dict([(sys.get('Id'), sys) for sys in payload_list])
205 diff = 0
206 for k, v in current_config.items():
207 i_dict = input_config.get(k)
208 if i_dict:
209 d = recursive_diff(i_dict, v)
210 if d and d[0]:
211 v.update(d[0])
212 diff = diff + 1
213 v.pop("Id", None) # not mandatory
214 payload_list[int(k) - 1] = v # The order in list needs to be maintained
215 if not diff:
216 module.exit_json(msg=NO_CHANGES_MSG)
217 if module.check_mode:
218 module.exit_json(msg=CHANGES_FOUND, changed=True)
219 return payload_list
220
221
222 def main():
223 specs = {
224 "syslog_servers":
225 {"type": 'list', "elements": 'dict', "options":
226 {"id": {"type": 'int', "choices": [1, 2, 3, 4], "required": True},
227 "enabled": {"type": 'bool'},
228 "destination_address": {"type": 'str'},
229 "port_number": {"type": 'int'}
230 },
231 "required_one_of": [("enabled", "destination_address", "port_number")],
232 "required_if": [("enabled", True, ("destination_address",))]
233 }
234 }
235
236 module = OmeAnsibleModule(
237 argument_spec=specs,
238 supports_check_mode=True
239 )
240 try:
241 with RestOME(module.params, req_session=True) as rest_obj:
242 input_config = validate_input(module)
243 current_list = get_current_syslog(rest_obj)
244 payload = compare_get_payload(module, current_list, input_config)
245 resp = rest_obj.invoke_request("POST", SYSLOG_SET, data=payload, api_timeout=120)
246 # POST Call taking average 50-60 seconds so api_timeout=120
247 module.exit_json(msg=SUCCESS_MSG, syslog_details=resp.json_data, changed=True)
248 except HTTPError as err:
249 module.exit_json(msg=str(err), error_info=json.load(err), failed=True)
250 except URLError as err:
251 module.exit_json(msg=str(err), unreachable=True)
252 except (
253 IOError, ValueError, SSLError, TypeError, ConnectionError, AttributeError, IndexError, KeyError,
254 OSError) as err:
255 module.exit_json(msg=str(err), failed=True)
256
257
258 if __name__ == '__main__':
259 main()