| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-2025, 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 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
module: na_ontap_fpolicy_event |
| 11 |
short_description: NetApp ONTAP FPolicy policy event configuration |
| 12 |
extends_documentation_fragment: |
| 13 |
- netapp.ontap.netapp.na_ontap |
| 14 |
version_added: '21.4.0' |
| 15 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 16 |
description: |
| 17 |
- Create, delete or modify an FPolicy policy event. |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: |
| 21 |
- Whether the FPolicy policy event is present or not. |
| 22 |
choices: ['present', 'absent'] |
| 23 |
default: present |
| 24 |
type: str |
| 25 |
|
| 26 |
vserver: |
| 27 |
description: |
| 28 |
- The name of the vserver to create the event on. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
|
| 32 |
name: |
| 33 |
description: |
| 34 |
- Name of the Event. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
|
| 38 |
file_operations: |
| 39 |
description: |
| 40 |
- Name of file operations to be applied to the event. By default no operations are monitored. |
| 41 |
type: list |
| 42 |
elements: 'str' |
| 43 |
choices: ['close', 'create', 'create_dir', 'delete', 'delete_dir', 'getattr', 'link', 'lookup', |
| 44 |
'open', 'read', 'write', 'rename', 'rename_dir', 'setattr', 'symlink'] |
| 45 |
|
| 46 |
filters: |
| 47 |
description: |
| 48 |
- Name of filters to be applied to the event. It is notification filtering parameters. By default no filters are selected. |
| 49 |
type: list |
| 50 |
elements: 'str' |
| 51 |
choices: ['monitor_ads', 'close_with_modification', 'close_without_modification', 'first_read', 'first_write', 'offline_bit', 'open_with_delete_intent', |
| 52 |
'open_with_write_intent', 'write_with_size_change', 'close_with_read', 'setattr_with_owner_change', 'setattr_with_group_change', |
| 53 |
'setattr_with_sacl_change', 'setattr_with_dacl_change', 'setattr_with_modify_time_change', 'setattr_with_access_time_change', |
| 54 |
'setattr_with_creation_time_change', 'setattr_with_mode_change', 'setattr_with_size_change', 'setattr_with_allocation_size_change', 'exclude_directory'] |
| 55 |
|
| 56 |
protocol: |
| 57 |
description: |
| 58 |
- Name of protocol for which event is created. By default no protocol is selected. |
| 59 |
choices: ['cifs', 'nfsv3', 'nfsv4'] |
| 60 |
type: str |
| 61 |
|
| 62 |
volume_monitoring: |
| 63 |
description: |
| 64 |
- Indicator if the volume operation required for the event. If not specified the default Value is false. |
| 65 |
type: bool |
| 66 |
|
| 67 |
notes: |
| 68 |
- Support check_mode. |
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = """ |
| 72 |
- name: Create FPolicy Event |
| 73 |
netapp.ontap.na_ontap_fpolicy_event: |
| 74 |
state: present |
| 75 |
vserver: svm1 |
| 76 |
name: fpolicy_event |
| 77 |
file_operations: ['create', 'create_dir', 'delete', 'delete_dir', 'read', 'close', 'rename', 'rename_dir'] |
| 78 |
filters: ['first_read', 'close_with_modification'] |
| 79 |
protocol: cifs |
| 80 |
volume_monitoring: false |
| 81 |
username: "{{ netapp_username }}" |
| 82 |
password: "{{ netapp_password }}" |
| 83 |
hostname: "{{ netapp_hostname }}" |
| 84 |
|
| 85 |
- name: Modify FPolicy Event |
| 86 |
netapp.ontap.na_ontap_fpolicy_event: |
| 87 |
state: present |
| 88 |
vserver: svm1 |
| 89 |
name: fpolicy_event |
| 90 |
volume_monitoring: true |
| 91 |
username: "{{ netapp_username }}" |
| 92 |
password: "{{ netapp_password }}" |
| 93 |
hostname: "{{ netapp_hostname }}" |
| 94 |
|
| 95 |
- name: Delete FPolicy Event |
| 96 |
netapp.ontap.na_ontap_fpolicy_event: |
| 97 |
state: absent |
| 98 |
vserver: svm1 |
| 99 |
name: fpolicy_event |
| 100 |
username: "{{ netapp_username }}" |
| 101 |
password: "{{ netapp_password }}" |
| 102 |
hostname: "{{ netapp_hostname }}" |
| 103 |
""" |
| 104 |
|
| 105 |
RETURN = """ |
| 106 |
""" |
| 107 |
|
| 108 |
import traceback |
| 109 |
|
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
from ansible.module_utils._text import to_native |
| 112 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 113 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 114 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 115 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 116 |
|
| 117 |
|
| 118 |
class NetAppOntapFpolicyEvent(): |
| 119 |
|
| 120 |
def __init__(self): |
| 121 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 122 |
self.argument_spec.update(dict( |
| 123 |
state=dict(choices=['present', 'absent'], type='str', default='present'), |
| 124 |
vserver=dict(required=True, type='str'), |
| 125 |
name=dict(required=True, type='str'), |
| 126 |
file_operations=dict( |
| 127 |
required=False, |
| 128 |
type='list', |
| 129 |
elements='str', |
| 130 |
choices=['close', 'create', 'create_dir', 'delete', 'delete_dir', 'getattr', 'link', |
| 131 |
'lookup', 'open', 'read', 'write', 'rename', 'rename_dir', 'setattr', 'symlink']), |
| 132 |
filters=dict( |
| 133 |
required=False, |
| 134 |
type='list', |
| 135 |
elements='str', |
| 136 |
choices=['monitor_ads', 'close_with_modification', 'close_without_modification', 'first_read', |
| 137 |
'first_write', 'offline_bit', 'open_with_delete_intent', 'open_with_write_intent', 'write_with_size_change', 'close_with_read', |
| 138 |
'setattr_with_owner_change', 'setattr_with_group_change', 'setattr_with_sacl_change', 'setattr_with_dacl_change', |
| 139 |
'setattr_with_modify_time_change', 'setattr_with_access_time_change', 'setattr_with_creation_time_change', 'setattr_with_mode_change', |
| 140 |
'setattr_with_size_change', 'setattr_with_allocation_size_change', 'exclude_directory']), |
| 141 |
protocol=dict(required=False, type='str', choices=['cifs', 'nfsv3', 'nfsv4']), |
| 142 |
volume_monitoring=dict(required=False, type='bool') |
| 143 |
)) |
| 144 |
self.module = AnsibleModule( |
| 145 |
argument_spec=self.argument_spec, |
| 146 |
supports_check_mode=True, |
| 147 |
required_together=[ |
| 148 |
('protocol', 'file_operations')] |
| 149 |
) |
| 150 |
self.na_helper = NetAppModule() |
| 151 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 152 |
self.vserver_uuid = None |
| 153 |
|
| 154 |
self.rest_api = OntapRestAPI(self.module) |
| 155 |
self.use_rest = self.rest_api.is_rest() |
| 156 |
|
| 157 |
if not self.use_rest: |
| 158 |
if not netapp_utils.has_netapp_lib(): |
| 159 |
self.module.fail_json(msg=netapp_utils.netapp) |
| 160 |
else: |
| 161 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 162 |
|
| 163 |
def get_vserver_uuid(self): |
| 164 |
""" |
| 165 |
Get vserver uuid, used for API calls. |
| 166 |
""" |
| 167 |
api = "/svm/svms" |
| 168 |
query = { |
| 169 |
'name': self.parameters['vserver'] |
| 170 |
} |
| 171 |
message, error = self.rest_api.get(api, query) |
| 172 |
if error: |
| 173 |
self.module.fail_json(msg=error) |
| 174 |
# if vserver does not exist we expect message to be a dict containing 'records': [] |
| 175 |
if not message['records']: |
| 176 |
self.module.fail_json(msg="vserver does not exist") |
| 177 |
|
| 178 |
return message['records'][0]['uuid'] |
| 179 |
|
| 180 |
def list_to_dict(self, params): |
| 181 |
""" |
| 182 |
Converts a list of entries to a dictionary with the key as the parameter name and the value as True as expected by the REST API |
| 183 |
""" |
| 184 |
return dict((param, True) for param in params) |
| 185 |
|
| 186 |
def get_fpolicy_event(self): |
| 187 |
""" |
| 188 |
Get FPolicy event configuration if an event matching the parameters exists |
| 189 |
""" |
| 190 |
return_value = None |
| 191 |
if self.use_rest: |
| 192 |
api = "/protocols/fpolicy/%s/events" % (self.vserver_uuid) |
| 193 |
query = { |
| 194 |
'fields': 'protocol,filters,file_operations,volume_monitoring' |
| 195 |
} |
| 196 |
message, error = self.rest_api.get(api, query) |
| 197 |
records, error = rrh.check_for_0_or_more_records(api, message, error) |
| 198 |
if error: |
| 199 |
self.module.fail_json(msg=error) |
| 200 |
if records is not None: |
| 201 |
for record in records: |
| 202 |
if record['name'] == self.parameters['name']: |
| 203 |
return_value = {} |
| 204 |
for parameter in ('protocol', 'volume_monitoring'): |
| 205 |
return_value[parameter] = [] |
| 206 |
if parameter in record: |
| 207 |
return_value[parameter] = record[parameter] |
| 208 |
# file_operations and filters contains a dict all possible choices as the keys and True/False as the values. |
| 209 |
# Return a list of the choices that are True. |
| 210 |
return_value['file_operations'] = [] |
| 211 |
if 'file_operations' in record: |
| 212 |
file_operation_list = [file_operation for file_operation, enabled in record['file_operations'].items() if enabled] |
| 213 |
return_value['file_operations'] = file_operation_list |
| 214 |
|
| 215 |
return_value['filters'] = [] |
| 216 |
if 'filters' in record: |
| 217 |
filters_list = [filter for filter, enabled in record['filters'].items() if enabled] |
| 218 |
return_value['filters'] = filters_list |
| 219 |
|
| 220 |
return return_value |
| 221 |
|
| 222 |
else: |
| 223 |
fpolicy_event_obj = netapp_utils.zapi.NaElement('fpolicy-policy-event-get-iter') |
| 224 |
fpolicy_event_config = netapp_utils.zapi.NaElement('fpolicy-event-options-config') |
| 225 |
fpolicy_event_config.add_new_child('event-name', self.parameters['name']) |
| 226 |
fpolicy_event_config.add_new_child('vserver', self.parameters['vserver']) |
| 227 |
query = netapp_utils.zapi.NaElement('query') |
| 228 |
query.add_child_elem(fpolicy_event_config) |
| 229 |
fpolicy_event_obj.add_child_elem(query) |
| 230 |
|
| 231 |
try: |
| 232 |
result = self.server.invoke_successfully(fpolicy_event_obj, True) |
| 233 |
|
| 234 |
except netapp_utils.zapi.NaApiError as error: |
| 235 |
self.module.fail_json(msg='Error searching for FPolicy policy event %s on vserver %s: %s' % ( |
| 236 |
self.parameters['name'], self.parameters['vserver'], to_native(error)), |
| 237 |
exception=traceback.format_exc() |
| 238 |
) |
| 239 |
|
| 240 |
if result.get_child_by_name('attributes-list'): |
| 241 |
fpolicy_event_attributes = result['attributes-list']['fpolicy-event-options-config'] |
| 242 |
|
| 243 |
# get file operations as list |
| 244 |
file_operations = [] |
| 245 |
if fpolicy_event_attributes.get_child_by_name('file-operations'): |
| 246 |
for file_operation in fpolicy_event_attributes.get_child_by_name('file-operations').get_children(): |
| 247 |
file_operations.append(file_operation.get_content()) |
| 248 |
|
| 249 |
# get filter string as list |
| 250 |
filters = [] |
| 251 |
if fpolicy_event_attributes.get_child_by_name('filter-string'): |
| 252 |
for filter in fpolicy_event_attributes.get_child_by_name('filter-string').get_children(): |
| 253 |
filters.append(filter.get_content()) |
| 254 |
|
| 255 |
protocol = "" |
| 256 |
if fpolicy_event_attributes.get_child_by_name('protocol'): |
| 257 |
protocol = fpolicy_event_attributes.get_child_content('protocol') |
| 258 |
|
| 259 |
return_value = { |
| 260 |
'vserver': fpolicy_event_attributes.get_child_content('vserver'), |
| 261 |
'name': fpolicy_event_attributes.get_child_content('event-name'), |
| 262 |
'file_operations': file_operations, |
| 263 |
'filters': filters, |
| 264 |
'protocol': protocol, |
| 265 |
'volume_monitoring': self.na_helper.get_value_for_bool( |
| 266 |
from_zapi=True, value=fpolicy_event_attributes.get_child_content('volume-operation') |
| 267 |
) |
| 268 |
} |
| 269 |
|
| 270 |
return return_value |
| 271 |
|
| 272 |
def create_fpolicy_event(self): |
| 273 |
""" |
| 274 |
Create an FPolicy policy event |
| 275 |
:return: nothing |
| 276 |
""" |
| 277 |
if self.use_rest: |
| 278 |
api = "/protocols/fpolicy/%s/events" % (self.vserver_uuid) |
| 279 |
body = { |
| 280 |
'name': self.parameters['name'] |
| 281 |
} |
| 282 |
|
| 283 |
if 'protocol' in self.parameters: |
| 284 |
body['protocol'] = self.parameters['protocol'] |
| 285 |
if 'volume_monitoring' in self.parameters: |
| 286 |
body['volume_monitoring'] = self.parameters['volume_monitoring'] |
| 287 |
|
| 288 |
if 'filters' in self.parameters: |
| 289 |
body['filters'] = self.list_to_dict(self.parameters['filters']) |
| 290 |
if 'file_operations' in self.parameters: |
| 291 |
body['file_operations'] = self.list_to_dict(self.parameters['file_operations']) |
| 292 |
|
| 293 |
dummy, error = self.rest_api.post(api, body) |
| 294 |
|
| 295 |
if error: |
| 296 |
self.module.fail_json(msg=error) |
| 297 |
|
| 298 |
else: |
| 299 |
fpolicy_event_obj = netapp_utils.zapi.NaElement('fpolicy-policy-event-create') |
| 300 |
fpolicy_event_obj.add_new_child('event-name', self.parameters['name']) |
| 301 |
|
| 302 |
if 'file_operations' in self.parameters: |
| 303 |
|
| 304 |
file_operation_obj = netapp_utils.zapi.NaElement('file-operations') |
| 305 |
|
| 306 |
for file_operation in self.parameters['file_operations']: |
| 307 |
file_operation_obj.add_new_child('fpolicy-operation', file_operation) |
| 308 |
fpolicy_event_obj.add_child_elem(file_operation_obj) |
| 309 |
|
| 310 |
if 'filters' in self.parameters: |
| 311 |
|
| 312 |
filter_string_obj = netapp_utils.zapi.NaElement('filter-string') |
| 313 |
|
| 314 |
for filter in self.parameters['filters']: |
| 315 |
filter_string_obj.add_new_child('fpolicy-filter', filter) |
| 316 |
fpolicy_event_obj.add_child_elem(filter_string_obj) |
| 317 |
|
| 318 |
if 'protocol' in self.parameters: |
| 319 |
fpolicy_event_obj.add_new_child('protocol', self.parameters['protocol']) |
| 320 |
|
| 321 |
if 'volume_monitoring' in self.parameters: |
| 322 |
fpolicy_event_obj.add_new_child( |
| 323 |
'volume-operation', self.na_helper.get_value_for_bool(from_zapi=False, value=self.parameters['volume_monitoring']) |
| 324 |
) |
| 325 |
|
| 326 |
try: |
| 327 |
self.server.invoke_successfully(fpolicy_event_obj, True) |
| 328 |
except netapp_utils.zapi.NaApiError as error: |
| 329 |
self.module.fail_json(msg='Error creating fPolicy policy event %s on vserver %s: %s' % ( |
| 330 |
self.parameters['name'], self.parameters['vserver'], to_native(error)), |
| 331 |
exception=traceback.format_exc()) |
| 332 |
|
| 333 |
def modify_fpolicy_event(self, modify): |
| 334 |
""" |
| 335 |
Modify an FPolicy policy event |
| 336 |
:return: nothing |
| 337 |
""" |
| 338 |
if self.use_rest: |
| 339 |
api = "/private/cli/vserver/fpolicy/policy/event" |
| 340 |
query = { |
| 341 |
'vserver': self.parameters['vserver'], |
| 342 |
'event-name': self.parameters['name'] |
| 343 |
} |
| 344 |
body = {} |
| 345 |
# protocol and file_operations must be parsed into the API together |
| 346 |
# if filters exists filters,protocol and file_operations must be parsed together. |
| 347 |
for parameter in 'protocol', 'filters', 'file_operations': |
| 348 |
if parameter in modify: |
| 349 |
body[parameter] = modify[parameter] |
| 350 |
elif parameter in self.parameters: |
| 351 |
body[parameter] = self.parameters[parameter] |
| 352 |
if 'volume_monitoring' in modify: |
| 353 |
body['volume-operation'] = modify['volume_monitoring'] |
| 354 |
|
| 355 |
dummy, error = self.rest_api.patch(api, body, query) |
| 356 |
if error: |
| 357 |
self.module.fail_json(msg=error) |
| 358 |
|
| 359 |
else: |
| 360 |
fpolicy_event_obj = netapp_utils.zapi.NaElement('fpolicy-policy-event-modify') |
| 361 |
fpolicy_event_obj.add_new_child('event-name', self.parameters['name']) |
| 362 |
|
| 363 |
if 'file_operations' in self.parameters: |
| 364 |
file_operation_obj = netapp_utils.zapi.NaElement('file-operations') |
| 365 |
for file_operation in self.parameters['file_operations']: |
| 366 |
file_operation_obj.add_new_child('fpolicy-operation', file_operation) |
| 367 |
fpolicy_event_obj.add_child_elem(file_operation_obj) |
| 368 |
|
| 369 |
if 'filters' in self.parameters: |
| 370 |
filter_string_obj = netapp_utils.zapi.NaElement('filter-string') |
| 371 |
for filter in self.parameters['filters']: |
| 372 |
filter_string_obj.add_new_child('fpolicy-filter', filter) |
| 373 |
fpolicy_event_obj.add_child_elem(filter_string_obj) |
| 374 |
|
| 375 |
if 'protocol' in self.parameters: |
| 376 |
fpolicy_event_obj.add_new_child('protocol', self.parameters['protocol']) |
| 377 |
|
| 378 |
if 'volume_monitoring' in self.parameters: |
| 379 |
fpolicy_event_obj.add_new_child( |
| 380 |
'volume-operation', self.na_helper.get_value_for_bool(from_zapi=False, value=self.parameters['volume_monitoring']) |
| 381 |
) |
| 382 |
|
| 383 |
try: |
| 384 |
self.server.invoke_successfully(fpolicy_event_obj, True) |
| 385 |
except netapp_utils.zapi.NaApiError as error: |
| 386 |
self.module.fail_json(msg='Error modifying fPolicy policy event %s on vserver %s: %s' % ( |
| 387 |
self.parameters['name'], self.parameters['vserver'], to_native(error)), |
| 388 |
exception=traceback.format_exc()) |
| 389 |
|
| 390 |
def delete_fpolicy_event(self): |
| 391 |
""" |
| 392 |
Delete an FPolicy policy event |
| 393 |
:return: nothing |
| 394 |
""" |
| 395 |
if self.use_rest: |
| 396 |
api = "/protocols/fpolicy/%s/events/%s" % (self.vserver_uuid, self.parameters['name']) |
| 397 |
|
| 398 |
dummy, error = self.rest_api.delete(api) |
| 399 |
if error: |
| 400 |
self.module.fail_json(msg=error) |
| 401 |
else: |
| 402 |
fpolicy_event_obj = netapp_utils.zapi.NaElement('fpolicy-policy-event-delete') |
| 403 |
fpolicy_event_obj.add_new_child('event-name', self.parameters['name']) |
| 404 |
|
| 405 |
try: |
| 406 |
self.server.invoke_successfully(fpolicy_event_obj, True) |
| 407 |
except netapp_utils.zapi.NaApiError as error: |
| 408 |
self.module.fail_json(msg='Error deleting fPolicy policy event %s on vserver %s: %s' % ( |
| 409 |
self.parameters['name'], self.parameters['vserver'], |
| 410 |
to_native(error)), exception=traceback.format_exc() |
| 411 |
) |
| 412 |
|
| 413 |
def apply(self): |
| 414 |
if self.use_rest: |
| 415 |
self.vserver_uuid = self.get_vserver_uuid() |
| 416 |
|
| 417 |
current, modify = self.get_fpolicy_event(), None |
| 418 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 419 |
|
| 420 |
if cd_action is None and self.parameters['state'] == 'present': |
| 421 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 422 |
|
| 423 |
if self.na_helper.changed: |
| 424 |
if not self.module.check_mode: |
| 425 |
if cd_action == 'create': |
| 426 |
self.create_fpolicy_event() |
| 427 |
elif cd_action == 'delete': |
| 428 |
self.delete_fpolicy_event() |
| 429 |
elif modify: |
| 430 |
self.modify_fpolicy_event(modify) |
| 431 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 432 |
self.module.exit_json(**result) |
| 433 |
|
| 434 |
|
| 435 |
def main(): |
| 436 |
""" |
| 437 |
Execute action from playbook |
| 438 |
""" |
| 439 |
command = NetAppOntapFpolicyEvent() |
| 440 |
command.apply() |
| 441 |
|
| 442 |
|
| 443 |
if __name__ == '__main__': |
| 444 |
main() |