Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_log_forward.py

raw
1 #!/usr/bin/python
2
3 # (c) 2021-2024, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['preview'],
12 'supported_by': 'community'}
13
14 DOCUMENTATION = '''
15 module: na_ontap_log_forward
16 short_description: NetApp ONTAP Log Forward Configuration
17 extends_documentation_fragment:
18 - netapp.ontap.netapp.na_ontap
19 version_added: '21.2.0'
20 author: NetApp Ansible Team (@carchi8py) <[email protected]>
21 description:
22 - Create, delete or modify the log forward configuration
23 options:
24 state:
25 description:
26 - Whether the log forward configuration should exist or not
27 choices: ['present', 'absent']
28 default: present
29 type: str
30
31 destination:
32 description:
33 - Destination address that the log messages will be forwarded to. Can be a hostname or IP address.
34 required: true
35 type: str
36
37 port:
38 description:
39 - The destination port used to forward the message.
40 required: true
41 type: int
42
43 facility:
44 description:
45 - Facility code used to indicate the type of software that generated the message.
46 type: str
47 choices: ['kern', 'user', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7']
48
49 force:
50 description:
51 - Skip the Connectivity Test
52 type: bool
53
54 protocol:
55 description:
56 - Log Forwarding Protocol
57 choices: ['udp_unencrypted', 'tcp_unencrypted', 'tcp_encrypted']
58 type: str
59
60 verify_server:
61 description:
62 - Verify Destination Server Identity
63 type: bool
64 '''
65
66 EXAMPLES = """
67 - name: Create log forward configuration
68 netapp.ontap.na_ontap_log_forward:
69 state: present
70 destination: 10.11.12.13
71 port: 514
72 protocol: udp_unencrypted
73 username: "{{ netapp_username }}"
74 password: "{{ netapp_password }}"
75 hostname: "{{ netapp_hostname }}"
76
77 - name: Modify log forward configuration
78 netapp.ontap.na_ontap_log_forward:
79 state: present
80 destination: 10.11.12.13
81 port: 514
82 protocol: tcp_unencrypted
83 username: "{{ netapp_username }}"
84 password: "{{ netapp_password }}"
85 hostname: "{{ netapp_hostname }}"
86
87 - name: Delete log forward configuration
88 netapp.ontap.na_ontap_log_forward:
89 state: absent
90 destination: 10.11.12.13
91 port: 514
92 username: "{{ netapp_username }}"
93 password: "{{ netapp_password }}"
94 hostname: "{{ netapp_hostname }}"
95 """
96
97 RETURN = """
98 """
99
100 import traceback
101
102 from ansible.module_utils.basic import AnsibleModule
103 from ansible.module_utils._text import to_native
104 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
105 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
106 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
107
108 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
109
110
111 class NetAppOntapLogForward(object):
112
113 def __init__(self):
114 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
115 self.argument_spec.update(dict(
116 state=dict(choices=['present', 'absent'], default='present'),
117 destination=dict(required=True, type='str'),
118 port=dict(required=True, type='int'),
119 facility=dict(required=False, type='str', choices=['kern', 'user', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7']),
120 force=dict(required=False, type='bool'),
121 protocol=dict(required=False, type='str', choices=['udp_unencrypted', 'tcp_unencrypted', 'tcp_encrypted']),
122 verify_server=dict(required=False, type='bool')
123 ))
124 self.module = AnsibleModule(
125 argument_spec=self.argument_spec,
126 supports_check_mode=True
127 )
128
129 self.na_helper = NetAppModule()
130 self.parameters = self.na_helper.set_parameters(self.module.params)
131
132 self.rest_api = OntapRestAPI(self.module)
133 self.use_rest = self.rest_api.is_rest()
134
135 if not self.use_rest:
136 if HAS_NETAPP_LIB is False:
137 self.module.fail_json(msg="the python NetApp-Lib module is required")
138 else:
139 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
140
141 def get_log_forward_config(self):
142 """
143 gets log forward configuration
144 :return: dict of log forward properties if exist, None if not
145 """
146
147 if self.use_rest:
148 log_forward_config = None
149 api = "security/audit/destinations"
150 query = {'fields': 'port,protocol,facility,address,verify_server',
151 'address': self.parameters['destination'],
152 'port': self.parameters['port']}
153
154 message, error = self.rest_api.get(api, query)
155 if error:
156 self.module.fail_json(msg=error)
157 if len(message.keys()) == 0:
158 return None
159 elif 'records' in message and len(message['records']) == 0:
160 return None
161 elif 'records' not in message:
162 error = "Unexpected response in get_security_key_manager from %s: %s" % (api, repr(message))
163 self.module.fail_json(msg=error)
164 log_forward_config = {
165 'destination': message['records'][0]['address'],
166 'facility': message['records'][0]['facility'],
167 'port': message['records'][0]['port'],
168 'protocol': message['records'][0]['protocol'],
169 'verify_server': message['records'][0]['verify_server']
170 }
171
172 return log_forward_config
173
174 else:
175 log_forward_config = None
176
177 log_forward_get = netapp_utils.zapi.NaElement('cluster-log-forward-get')
178 log_forward_get.add_new_child('destination', self.parameters['destination'])
179 log_forward_get.add_new_child('port', self.na_helper.get_value_for_int(False, self.parameters['port']))
180
181 try:
182 result = self.server.invoke_successfully(log_forward_get, True)
183 except netapp_utils.zapi.NaApiError as error:
184 if to_native(error.code) == "15661":
185 # config doesnt exist
186 return None
187 else:
188 self.module.fail_json(
189 msg='Error getting log forward configuration for destination %s on port %s: %s' %
190 (self.parameters['destination'], self.na_helper.get_value_for_int(False, self.parameters['port']), to_native(error)),
191 exception=traceback.format_exc()
192 )
193
194 if result.get_child_by_name('attributes'):
195 log_forward_attributes = result.get_child_by_name('attributes')
196 cluster_log_forward_info = log_forward_attributes.get_child_by_name('cluster-log-forward-info')
197 log_forward_config = {
198 'destination': cluster_log_forward_info.get_child_content('destination'),
199 'facility': cluster_log_forward_info.get_child_content('facility'),
200 'port': self.na_helper.get_value_for_int(True, cluster_log_forward_info.get_child_content('port')),
201 'protocol': cluster_log_forward_info.get_child_content('protocol'),
202 'verify_server': self.na_helper.get_value_for_bool(True, cluster_log_forward_info.get_child_content('verify-server'))
203 }
204
205 return log_forward_config
206
207 def create_log_forward_config(self):
208 """
209 Creates a log forward config
210 :return: nothing
211 """
212
213 if self.use_rest:
214 api = "security/audit/destinations"
215 body = dict()
216 body['address'] = self.parameters['destination']
217 body['port'] = self.parameters['port']
218
219 for attr in ('protocol', 'facility', 'verify_server', 'force'):
220 if attr in self.parameters:
221 body[attr] = self.parameters[attr]
222
223 dummy, error = self.rest_api.post(api, body)
224 if error:
225 self.module.fail_json(msg=error)
226
227 else:
228 log_forward_config_obj = netapp_utils.zapi.NaElement('cluster-log-forward-create')
229 log_forward_config_obj.add_new_child('destination', self.parameters['destination'])
230 log_forward_config_obj.add_new_child('port', self.na_helper.get_value_for_int(False, self.parameters['port']))
231
232 if 'facility' in self.parameters:
233 log_forward_config_obj.add_new_child('facility', self.parameters['facility'])
234
235 if 'force' in self.parameters:
236 log_forward_config_obj.add_new_child('force', self.na_helper.get_value_for_bool(False, self.parameters['force']))
237
238 if 'protocol' in self.parameters:
239 log_forward_config_obj.add_new_child('protocol', self.parameters['protocol'])
240
241 if 'verify_server' in self.parameters:
242 log_forward_config_obj.add_new_child('verify-server', self.na_helper.get_value_for_bool(False, self.parameters['verify_server']))
243
244 try:
245 self.server.invoke_successfully(log_forward_config_obj, True)
246 except netapp_utils.zapi.NaApiError as error:
247 self.module.fail_json(msg='Error creating log forward config with destination %s on port %s: %s' %
248 (self.parameters['destination'], self.na_helper.get_value_for_int(False, self.parameters['port']), to_native(error)),
249 exception=traceback.format_exc())
250
251 def modify_log_forward_config(self):
252 # need to recreate as protocol can't be changed
253 self.destroy_log_forward_config()
254 self.create_log_forward_config()
255
256 def destroy_log_forward_config(self):
257 """
258 Delete a log forward configuration
259 :return: nothing
260 """
261 if self.use_rest:
262
263 api = "security/audit/destinations/%s/%s" % (self.parameters['destination'], self.parameters['port'])
264 body = None
265 query = {'return_timeout': 3}
266 dummy, error = self.rest_api.delete(api, body, query)
267 if error:
268 self.module.fail_json(msg=error)
269
270 else:
271 log_forward_config_obj = netapp_utils.zapi.NaElement('cluster-log-forward-destroy')
272 log_forward_config_obj.add_new_child('destination', self.parameters['destination'])
273 log_forward_config_obj.add_new_child('port', self.na_helper.get_value_for_int(False, self.parameters['port']))
274
275 try:
276 self.server.invoke_successfully(log_forward_config_obj, True)
277 except netapp_utils.zapi.NaApiError as error:
278 self.module.fail_json(msg='Error destroying log forward destination %s on port %s: %s' %
279 (self.parameters['destination'], self.na_helper.get_value_for_int(False, self.parameters['port']), to_native(error)),
280 exception=traceback.format_exc())
281
282 def apply(self):
283 current = self.get_log_forward_config()
284 cd_action = self.na_helper.get_cd_action(current, self.parameters)
285 modify = None
286
287 if cd_action is None and self.parameters['state'] == 'present':
288 modify = self.na_helper.get_modified_attributes(current, self.parameters)
289
290 if self.na_helper.changed:
291 if not self.module.check_mode:
292 if cd_action == 'create':
293 self.create_log_forward_config()
294 elif cd_action == 'delete':
295 self.destroy_log_forward_config()
296 elif modify:
297 self.modify_log_forward_config()
298 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
299 self.module.exit_json(**result)
300
301
302 def main():
303 """
304 Execute action from playbook
305 """
306 command = NetAppOntapLogForward()
307 command.apply()
308
309
310 if __name__ == '__main__':
311 main()