| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-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 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
module: na_ontap_vscan_on_demand_task |
| 12 |
short_description: NetApp ONTAP Vscan on demand task configuration. |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap |
| 15 |
version_added: 2.8.0 |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
description: |
| 18 |
- Configure on demand task for Vscan |
| 19 |
options: |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Whether a Vscan on demand task is present or not |
| 23 |
choices: ['present', 'absent'] |
| 24 |
type: str |
| 25 |
default: present |
| 26 |
|
| 27 |
vserver: |
| 28 |
description: |
| 29 |
- the name of the data vserver to use. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
|
| 33 |
cross_junction: |
| 34 |
description: |
| 35 |
- Specifies whether the On-Demand task is allowed to cross volume junctions |
| 36 |
- This option is not supported with REST. |
| 37 |
- This option defaults to False for ZAPI. |
| 38 |
type: bool |
| 39 |
|
| 40 |
directory_recursion: |
| 41 |
description: |
| 42 |
- Specifies whether the On-Demand task is allowed to recursively scan through sub-directories. |
| 43 |
- This option is not supported with REST. |
| 44 |
- This option defaults to False for ZAPI. |
| 45 |
type: bool |
| 46 |
|
| 47 |
file_ext_to_exclude: |
| 48 |
description: |
| 49 |
- File-Extensions for which scanning must not be performed. |
| 50 |
- File whose extension matches with both inclusion and exclusion list is not considered for scanning. |
| 51 |
type: list |
| 52 |
elements: str |
| 53 |
|
| 54 |
file_ext_to_include: |
| 55 |
description: |
| 56 |
- File extensions for which scanning is considered. |
| 57 |
- The default value is '*', which means that all files are considered for scanning except those which are excluded from scanning. |
| 58 |
- File whose extension matches with both inclusion and exclusion list is not considered for scanning. |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
|
| 62 |
max_file_size: |
| 63 |
description: |
| 64 |
- Max file-size (in bytes) allowed for scanning. The default value of 10737418240 (10GB) is taken if not provided at the time of creating a task. |
| 65 |
type: int |
| 66 |
|
| 67 |
paths_to_exclude: |
| 68 |
description: |
| 69 |
- File-paths for which scanning must not be performed. |
| 70 |
type: list |
| 71 |
elements: str |
| 72 |
|
| 73 |
report_directory: |
| 74 |
description: |
| 75 |
- Path from the vserver root where task report is created. The path must be a directory and provided in unix-format from the root of the Vserver. |
| 76 |
- Example /vol1/on-demand-reports. |
| 77 |
type: str |
| 78 |
|
| 79 |
report_log_level: |
| 80 |
description: |
| 81 |
- Log level for the On-Demand report. |
| 82 |
- This option is not supported with REST. |
| 83 |
- This option defaults to 'error' for ZAPI. |
| 84 |
choices: ['verbose', 'info', 'error'] |
| 85 |
type: str |
| 86 |
|
| 87 |
request_timeout: |
| 88 |
description: |
| 89 |
- Total request-service time-limit in seconds. If the virus-scanner does not respond within the provided time, scan will be timedout. |
| 90 |
- This option is not supported with REST. |
| 91 |
type: str |
| 92 |
|
| 93 |
scan_files_with_no_ext: |
| 94 |
description: |
| 95 |
- Specifies whether files without any extension are considered for scanning or not. |
| 96 |
type: bool |
| 97 |
default: True |
| 98 |
|
| 99 |
scan_paths: |
| 100 |
description: |
| 101 |
- List of paths that need to be scanned. The path must be provided in unix-format and from the root of the Vserver. |
| 102 |
- Example /vol1/large_files. |
| 103 |
type: list |
| 104 |
elements: str |
| 105 |
|
| 106 |
scan_priority: |
| 107 |
description: |
| 108 |
- Priority of the On-Demand scan requests generated by this task. |
| 109 |
- This option is not supported with REST. |
| 110 |
- This option default to 'low' for ZAPI |
| 111 |
choices: ['low', 'normal'] |
| 112 |
type: str |
| 113 |
|
| 114 |
schedule: |
| 115 |
description: |
| 116 |
- Schedule of the task. The task will be run as per the schedule. |
| 117 |
- For running the task immediately, vscan-on-demand-task-run api must be used after creating a task. |
| 118 |
type: str |
| 119 |
|
| 120 |
task_name: |
| 121 |
description: |
| 122 |
- Name of the task. |
| 123 |
type: str |
| 124 |
required: True |
| 125 |
''' |
| 126 |
|
| 127 |
EXAMPLES = """ |
| 128 |
- name: Create Vscan On Demand Task |
| 129 |
netapp.ontap.na_ontap_vscan_on_demand_task: |
| 130 |
state: present |
| 131 |
username: '{{ netapp_username }}' |
| 132 |
password: '{{ netapp_password }}' |
| 133 |
hostname: '{{ netapp_hostname }}' |
| 134 |
vserver: carchi-vsim2 |
| 135 |
task_name: carchiOnDemand |
| 136 |
scan_paths: / |
| 137 |
report_directory: / |
| 138 |
file_ext_to_exclude: ['py', 'yml'] |
| 139 |
max_file_size: 10737418241 |
| 140 |
paths_to_exclude: ['/tmp', '/var'] |
| 141 |
report_log_level: info |
| 142 |
request_timeout: 60 |
| 143 |
|
| 144 |
- name: Delete Vscan On Demand Task |
| 145 |
netapp.ontap.na_ontap_vscan_on_demand_task: |
| 146 |
state: absent |
| 147 |
username: '{{ netapp_username }}' |
| 148 |
password: '{{ netapp_password }}' |
| 149 |
hostname: '{{ netapp_hostname }}' |
| 150 |
vserver: carchi-vsim2 |
| 151 |
task_name: carchiOnDemand |
| 152 |
""" |
| 153 |
|
| 154 |
RETURN = """ |
| 155 |
|
| 156 |
""" |
| 157 |
|
| 158 |
import traceback |
| 159 |
|
| 160 |
from ansible.module_utils.basic import AnsibleModule |
| 161 |
from ansible.module_utils._text import to_native |
| 162 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 163 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 164 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 165 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 166 |
|
| 167 |
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() |
| 168 |
|
| 169 |
|
| 170 |
class NetAppOntapVscanOnDemandTask: |
| 171 |
def __init__(self): |
| 172 |
self.svm_uuid = None |
| 173 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 174 |
self.argument_spec.update(dict( |
| 175 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 176 |
vserver=dict(required=True, type='str'), |
| 177 |
cross_junction=dict(required=False, type='bool'), |
| 178 |
directory_recursion=dict(required=False, type='bool'), |
| 179 |
file_ext_to_exclude=dict(required=False, type='list', elements='str'), |
| 180 |
file_ext_to_include=dict(required=False, type='list', elements='str'), |
| 181 |
max_file_size=dict(required=False, type="int"), |
| 182 |
paths_to_exclude=dict(required=False, type='list', elements='str'), |
| 183 |
report_directory=dict(required=False, type='str'), |
| 184 |
report_log_level=dict(required=False, type='str', choices=['verbose', 'info', 'error']), |
| 185 |
request_timeout=dict(required=False, type='str'), |
| 186 |
scan_files_with_no_ext=dict(required=False, type='bool', default=True), |
| 187 |
scan_paths=dict(required=False, type='list', elements='str'), |
| 188 |
scan_priority=dict(required=False, type='str', choices=['low', 'normal']), |
| 189 |
schedule=dict(required=False, type="str"), |
| 190 |
task_name=dict(required=True, type="str") |
| 191 |
)) |
| 192 |
self.module = AnsibleModule( |
| 193 |
argument_spec=self.argument_spec, |
| 194 |
supports_check_mode=True, |
| 195 |
required_if=[ |
| 196 |
["state", "present", ["report_directory", "scan_paths"]] |
| 197 |
] |
| 198 |
) |
| 199 |
self.na_helper = NetAppModule() |
| 200 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 201 |
|
| 202 |
self.rest_api = OntapRestAPI(self.module) |
| 203 |
unsupported_rest_properties = ['cross_junction', 'directory_recursion', 'report_log_level', 'request_timeout', |
| 204 |
'scan_priority'] |
| 205 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties) |
| 206 |
if not self.use_rest: |
| 207 |
if self.parameters.get('cross_junction') is None: |
| 208 |
self.parameters['cross_junction'] = False |
| 209 |
if self.parameters.get('directory_recursion') is None: |
| 210 |
self.parameters['directory_recursion'] = False |
| 211 |
if not netapp_utils.has_netapp_lib(): |
| 212 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 213 |
else: |
| 214 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 215 |
|
| 216 |
def get_demand_task(self): |
| 217 |
""" |
| 218 |
Get a demand task |
| 219 |
:return: A vscan-on-demand-task-info or None |
| 220 |
""" |
| 221 |
if self.use_rest: |
| 222 |
self.get_svm_uuid() |
| 223 |
return self.get_demand_task_rest() |
| 224 |
demand_task_iter = netapp_utils.zapi.NaElement("vscan-on-demand-task-get-iter") |
| 225 |
demand_task_info = netapp_utils.zapi.NaElement("vscan-on-demand-task-info") |
| 226 |
demand_task_info.add_new_child('task-name', self.parameters['task_name']) |
| 227 |
query = netapp_utils.zapi.NaElement('query') |
| 228 |
query.add_child_elem(demand_task_info) |
| 229 |
demand_task_iter.add_child_elem(query) |
| 230 |
try: |
| 231 |
result = self.server.invoke_successfully(demand_task_iter, True) |
| 232 |
except netapp_utils.zapi.NaApiError as error: |
| 233 |
self.module.fail_json(msg='Error searching for Vscan on demand task %s: %s' % |
| 234 |
(self.parameters['task_name'], to_native(error)), |
| 235 |
exception=traceback.format_exc()) |
| 236 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 237 |
return result.get_child_by_name('attributes-list').get_child_by_name('vscan-on-demand-task-info') |
| 238 |
return None |
| 239 |
|
| 240 |
def create_demand_task(self): |
| 241 |
""" |
| 242 |
Create a Demand Task |
| 243 |
:return: None |
| 244 |
""" |
| 245 |
if self.use_rest: |
| 246 |
return self.create_demand_task_rest() |
| 247 |
demand_task_obj = netapp_utils.zapi.NaElement("vscan-on-demand-task-create") |
| 248 |
# Required items first |
| 249 |
demand_task_obj.add_new_child('report-directory', self.parameters['report_directory']) |
| 250 |
demand_task_obj.add_new_child('task-name', self.parameters['task_name']) |
| 251 |
scan_paths = netapp_utils.zapi.NaElement("scan-paths") |
| 252 |
for scan_path in self.parameters['scan_paths']: |
| 253 |
scan_paths.add_new_child('string', scan_path) |
| 254 |
demand_task_obj.add_child_elem(scan_paths) |
| 255 |
# Optional items next |
| 256 |
if self.parameters.get('cross_junction'): |
| 257 |
demand_task_obj.add_new_child('cross-junction', str(self.parameters['cross_junction']).lower()) |
| 258 |
if self.parameters.get('directory_recursion'): |
| 259 |
demand_task_obj.add_new_child('directory-recursion', str(self.parameters['directory_recursion']).lower()) |
| 260 |
if self.parameters.get('file_ext_to_exclude'): |
| 261 |
ext_to_exclude_obj = netapp_utils.zapi.NaElement('file-ext-to-exclude') |
| 262 |
for exclude_file in self.parameters['file_ext_to_exclude']: |
| 263 |
ext_to_exclude_obj.add_new_child('file-extension', exclude_file) |
| 264 |
demand_task_obj.add_child_elem(ext_to_exclude_obj) |
| 265 |
if self.parameters.get('file_ext_to_include'): |
| 266 |
ext_to_include_obj = netapp_utils.zapi.NaElement('file-ext-to-include') |
| 267 |
for include_file in self.parameters['file_ext_to_exclude']: |
| 268 |
ext_to_include_obj.add_child_elem(include_file) |
| 269 |
demand_task_obj.add_child_elem(ext_to_include_obj) |
| 270 |
if self.parameters.get('max_file_size'): |
| 271 |
demand_task_obj.add_new_child('max-file-size', str(self.parameters['max_file_size'])) |
| 272 |
if self.parameters.get('paths_to_exclude'): |
| 273 |
exclude_paths = netapp_utils.zapi.NaElement('paths-to-exclude') |
| 274 |
for path in self.parameters['paths_to_exclude']: |
| 275 |
exclude_paths.add_new_child('string', path) |
| 276 |
demand_task_obj.add_child_elem(exclude_paths) |
| 277 |
if self.parameters.get('report_log_level'): |
| 278 |
demand_task_obj.add_new_child('report-log-level', self.parameters['report_log_level']) |
| 279 |
if self.parameters.get('request_timeout'): |
| 280 |
demand_task_obj.add_new_child('request-timeout', self.parameters['request_timeout']) |
| 281 |
if self.parameters.get('scan_files_with_no_ext'): |
| 282 |
demand_task_obj.add_new_child('scan-files-with-no-ext', |
| 283 |
str(self.parameters['scan_files_with_no_ext']).lower()) |
| 284 |
if self.parameters.get('scan_priority'): |
| 285 |
demand_task_obj.add_new_child('scan-priority', self.parameters['scan_priority'].lower()) |
| 286 |
if self.parameters.get('schedule'): |
| 287 |
demand_task_obj.add_new_child('schedule', self.parameters['schedule']) |
| 288 |
try: |
| 289 |
self.server.invoke_successfully(demand_task_obj, True) |
| 290 |
except netapp_utils.zapi.NaApiError as error: |
| 291 |
self.module.fail_json(msg='Error creating on demand task %s: %s' % |
| 292 |
(self.parameters['task_name'], to_native(error)), |
| 293 |
exception=traceback.format_exc()) |
| 294 |
|
| 295 |
def delete_demand_task(self): |
| 296 |
""" |
| 297 |
Delete a Demand Task" |
| 298 |
:return: |
| 299 |
""" |
| 300 |
if self.use_rest: |
| 301 |
return self.delete_demand_task_rest() |
| 302 |
demand_task_obj = netapp_utils.zapi.NaElement('vscan-on-demand-task-delete') |
| 303 |
demand_task_obj.add_new_child('task-name', self.parameters['task_name']) |
| 304 |
try: |
| 305 |
self.server.invoke_successfully(demand_task_obj, True) |
| 306 |
except netapp_utils.zapi.NaApiError as error: |
| 307 |
self.module.fail_json(msg='Error deleting on demand task, %s: %s' % |
| 308 |
(self.parameters['task_name'], to_native(error)), |
| 309 |
exception=traceback.format_exc()) |
| 310 |
|
| 311 |
def get_svm_uuid(self): |
| 312 |
api = 'svm/svms' |
| 313 |
query = {'name': self.parameters['vserver']} |
| 314 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 315 |
if error: |
| 316 |
self.module.fail_json(msg='Error fetching svm uuid: %s' % to_native(error)) |
| 317 |
if not record: |
| 318 |
self.module.fail_json(msg='Could not find svm uuid for %s' % self.parameters['vserver']) |
| 319 |
self.svm_uuid = record['uuid'] |
| 320 |
|
| 321 |
def get_demand_task_rest(self): |
| 322 |
api = 'protocols/vscan/%s/on-demand-policies' % self.svm_uuid |
| 323 |
query = {'name': self.parameters['task_name'], |
| 324 |
'fields': 'scope.exclude_extensions,' |
| 325 |
'scope.include_extensions,' |
| 326 |
'scope.max_file_size,' |
| 327 |
'scope.exclude_paths,' |
| 328 |
'log_path,' |
| 329 |
'scope.scan_without_extension,' |
| 330 |
'scan_paths,' |
| 331 |
'schedule.name,' |
| 332 |
'name' |
| 333 |
} |
| 334 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 335 |
if error: |
| 336 |
self.module.fail_json(msg='Error fetching on demand task %s: %s' % (self.parameters['task_name'], to_native(error))) |
| 337 |
if record: |
| 338 |
return self.format_on_demand_task(record) |
| 339 |
return None |
| 340 |
|
| 341 |
def format_on_demand_task(self, record): |
| 342 |
return { |
| 343 |
'task_name': record['name'], |
| 344 |
'file_ext_to_exclude': self.na_helper.safe_get(record, ['scope', 'exclude_extensions']), |
| 345 |
'file_ext_to_include': self.na_helper.safe_get(record, ['scope', 'include_extensions']), |
| 346 |
'max_file_size': self.na_helper.safe_get(record, ['scope', 'max_file_size']), |
| 347 |
'paths_to_exclude': self.na_helper.safe_get(record, ['scope', 'exclude_paths']), |
| 348 |
'report_directory': self.na_helper.safe_get(record, ['log_path']), |
| 349 |
'scan_files_with_no_ext': self.na_helper.safe_get(record, ['scope', 'scan_without_extension']), |
| 350 |
'scan_paths': self.na_helper.safe_get(record, ['scan_paths']), |
| 351 |
'schedule': self.na_helper.safe_get(record, ['schedule', 'name']), |
| 352 |
} |
| 353 |
|
| 354 |
def create_demand_task_rest(self): |
| 355 |
api = 'protocols/vscan/%s/on-demand-policies' % self.svm_uuid |
| 356 |
body = { |
| 357 |
'name': self.parameters['task_name'], |
| 358 |
'log_path': self.parameters['report_directory'], |
| 359 |
'scan_paths': self.parameters['scan_paths'], |
| 360 |
} |
| 361 |
if self.parameters.get('file_ext_to_exclude'): |
| 362 |
body['scope.exclude_extensions'] = self.parameters['file_ext_to_exclude'] |
| 363 |
if self.parameters.get('file_ext_to_include'): |
| 364 |
body['scope.include_extensions'] = self.parameters['file_ext_to_include'] |
| 365 |
if self.parameters.get('max_file_size'): |
| 366 |
body['scope.max_file_size'] = self.parameters['max_file_size'] |
| 367 |
if self.parameters.get('paths_to_exclude'): |
| 368 |
body['scope.exclude_paths'] = self.parameters['paths_to_exclude'] |
| 369 |
if self.parameters.get('scan_files_with_no_ext'): |
| 370 |
body['scope.scan_without_extension'] = self.parameters['scan_files_with_no_ext'] |
| 371 |
if self.parameters.get('schedule'): |
| 372 |
body['schedule.name'] = self.parameters['schedule'] |
| 373 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 374 |
if error: |
| 375 |
self.module.fail_json(msg='Error creating on demand task %s: %s' % (self.parameters['task_name'], to_native(error))) |
| 376 |
|
| 377 |
def delete_demand_task_rest(self): |
| 378 |
api = 'protocols/vscan/%s/on-demand-policies' % self.svm_uuid |
| 379 |
dummy, error = rest_generic.delete_async(self.rest_api, api, self.parameters['task_name']) |
| 380 |
if error: |
| 381 |
self.module.fail_json(msg='Error deleting on demand task %s: %s' % (self.parameters['task_name'], to_native(error))) |
| 382 |
|
| 383 |
def apply(self): |
| 384 |
current = self.get_demand_task() |
| 385 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 386 |
if self.na_helper.changed: |
| 387 |
if self.module.check_mode: |
| 388 |
pass |
| 389 |
else: |
| 390 |
if cd_action == 'create': |
| 391 |
self.create_demand_task() |
| 392 |
elif cd_action == 'delete': |
| 393 |
self.delete_demand_task() |
| 394 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action) |
| 395 |
self.module.exit_json(**result) |
| 396 |
|
| 397 |
|
| 398 |
def main(): |
| 399 |
""" |
| 400 |
Execute action from playbook |
| 401 |
""" |
| 402 |
command = NetAppOntapVscanOnDemandTask() |
| 403 |
command.apply() |
| 404 |
|
| 405 |
|
| 406 |
if __name__ == '__main__': |
| 407 |
main() |