Debian Codegraph @

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

raw
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_scope
11 short_description: NetApp ONTAP - Create, delete or modify an FPolicy policy scope 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 scope.
18 options:
19 state:
20 description:
21 - Whether the FPolicy policy scope 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 scope on
29 required: true
30 type: str
31
32 name:
33 description:
34 - Name of the policy. The FPolicy policy must exist for the scope to be created.
35 required: true
36 type: str
37
38 check_extensions_on_directories:
39 description:
40 - Indicates whether directory names are also subjected to extensions check, similar to file names.
41 - By default, the value is true if policy is configured with Native engine, false otherwise.
42 type: bool
43
44 export_policies_to_exclude:
45 description:
46 - Export Policies to exclude for file access monitoring. By default no export policy is selected.
47 type: list
48 elements: str
49
50 export_policies_to_include:
51 description:
52 - Export policies to include for file access monitoring. By default no export policy is selected.
53 type: list
54 elements: str
55
56 file_extensions_to_exclude:
57 description:
58 - File extensions excluded for screening. By default no file extension is selected.
59 type: list
60 elements: str
61
62 file_extensions_to_include:
63 description:
64 - File extensions included for screening. By default no file extension is selected.
65 type: list
66 elements: str
67
68 is_monitoring_of_objects_with_no_extension_enabled:
69 description:
70 - Indicates whether monitoring of objects with no extension is required. By default, the value is false.
71 type: bool
72
73 shares_to_exclude:
74 description:
75 - Shares to exclude for file access monitoring. By default no share is selected.
76 type: list
77 elements: str
78
79 shares_to_include:
80 description:
81 - Shares to include for file access monitoring. By default no share is selected.
82 type: list
83 elements: str
84
85 volumes_to_exclude:
86 description:
87 - Volumes that are inactive for the file policy. The list can include items which are regular expressions, such as 'vol*' or 'user?'.
88 - Note that if a policy has both an exclude list and an include list, the include list is ignored by the filer when processing user requests.
89 - By default no volume is selected.
90 type: list
91 elements: str
92
93 volumes_to_include:
94 description:
95 - Volumes that are active for the file policy. The list can include items which are regular expressions, such as 'vol*' or 'user?'.
96 - By default no volume is selected.
97 type: list
98 elements: str
99
100 '''
101
102 EXAMPLES = """
103 - name: Create FPolicy scope
104 netapp.ontap.na_ontap_fpolicy_scope:
105 state: present
106 vserver: GBSMNAS80LD
107 name: policy1
108 export_policies_to_include: export1
109 shares_to_include: share1
110 username: "{{ netapp_username }}"
111 password: "{{ netapp_password }}"
112 hostname: "{{ netapp_hostname }}"
113
114 - name: Modify FPolicy scope
115 netapp.ontap.na_ontap_fpolicy_scope:
116 state: present
117 vserver: GBSMNAS80LD
118 name: policy1
119 export_policies_to_include: export1,export2
120 shares_to_include: share1,share2
121 username: "{{ netapp_username }}"
122 password: "{{ netapp_password }}"
123 hostname: "{{ netapp_hostname }}"
124
125 - name: Delete FPolicy scope
126 netapp.ontap.na_ontap_fpolicy_scope:
127 state: absent
128 vserver: GBSMNAS80LD
129 name: policy1
130 username: "{{ netapp_username }}"
131 password: "{{ netapp_password }}"
132 hostname: "{{ netapp_hostname }}"
133 """
134
135 RETURN = """
136 """
137
138 import traceback
139
140 from ansible.module_utils.basic import AnsibleModule
141 from ansible.module_utils._text import to_native
142 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
143 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
144 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
145 import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh
146
147
148 class NetAppOntapFpolicyScope():
149
150 def __init__(self):
151 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
152 self.argument_spec.update(dict(
153 state=dict(choices=['present', 'absent'], default='present'),
154 vserver=dict(required=True, type='str'),
155 name=dict(required=True, type='str'),
156 check_extensions_on_directories=dict(required=False, type='bool'),
157 export_policies_to_exclude=dict(required=False, type='list', elements='str'),
158 export_policies_to_include=dict(required=False, type='list', elements='str'),
159 file_extensions_to_exclude=dict(required=False, type='list', elements='str'),
160 file_extensions_to_include=dict(required=False, type='list', elements='str'),
161 is_monitoring_of_objects_with_no_extension_enabled=dict(required=False, type='bool'),
162 shares_to_exclude=dict(required=False, type='list', elements='str'),
163 shares_to_include=dict(required=False, type='list', elements='str'),
164 volumes_to_exclude=dict(required=False, type='list', elements='str'),
165 volumes_to_include=dict(required=False, type='list', elements='str')
166 ))
167 self.module = AnsibleModule(
168 argument_spec=self.argument_spec,
169 supports_check_mode=True
170 )
171
172 self.na_helper = NetAppModule()
173 self.parameters = self.na_helper.set_parameters(self.module.params)
174
175 self.rest_api = OntapRestAPI(self.module)
176 self.use_rest = self.rest_api.is_rest()
177
178 if not self.use_rest:
179 if not netapp_utils.has_netapp_lib():
180 self.module.fail_json(msg=netapp_utils.netapp_lib_is_required())
181 else:
182 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
183
184 def get_fpolicy_scope(self):
185 """
186 Check to see if the fPolicy scope exists or not
187 :return: dict of scope properties if exist, None if not
188 """
189 return_value = None
190
191 if self.use_rest:
192 api = "/private/cli/vserver/fpolicy/policy/scope"
193 query = {
194 'vserver': self.parameters['vserver'],
195 'policy-name': self.parameters['name'],
196 'fields': 'shares-to-include,shares-to-exclude,volumes-to-include,volumes-to-exclude,export-policies-to-include,\
197 export-policies-to-exclude,file-extensions-to-include,file-extensions-to-exclude,\
198 is-file-extension-check-on-directories-enabled,is-monitoring-of-objects-with-no-extension-enabled'
199 }
200 message, error = self.rest_api.get(api, query)
201 records, error = rrh.check_for_0_or_more_records(api, message, error)
202 if error:
203 self.module.fail_json(msg=error)
204
205 if records is not None:
206 return_value = {
207 'name': records[0]['policy_name'],
208 'check_extensions_on_directories': records[0]['is_file_extension_check_on_directories_enabled'],
209 'is_monitoring_of_objects_with_no_extension_enabled': records[0]['is_monitoring_of_objects_with_no_extension_enabled']
210 }
211
212 for field in (
213 'export_policies_to_exclude', 'export_policies_to_include', 'export_policies_to_include', 'file_extensions_to_exclude',
214 'file_extensions_to_include', 'shares_to_exclude', 'shares_to_include', 'volumes_to_exclude', 'volumes_to_include'
215 ):
216 return_value[field] = []
217 if field in records[0]:
218 return_value[field] = records[0][field]
219
220 return return_value
221
222 else:
223 fpolicy_scope_obj = netapp_utils.zapi.NaElement('fpolicy-policy-scope-get-iter')
224 fpolicy_scope_config = netapp_utils.zapi.NaElement('fpolicy-scope-config')
225 fpolicy_scope_config.add_new_child('policy-name', self.parameters['name'])
226 fpolicy_scope_config.add_new_child('vserver', self.parameters['vserver'])
227 query = netapp_utils.zapi.NaElement('query')
228 query.add_child_elem(fpolicy_scope_config)
229 fpolicy_scope_obj.add_child_elem(query)
230
231 try:
232 result = self.server.invoke_successfully(fpolicy_scope_obj, True)
233
234 except netapp_utils.zapi.NaApiError as error:
235 self.module.fail_json(
236 msg='Error searching for FPolicy policy scope %s on vserver %s: %s' % (
237 self.parameters['name'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()
238 )
239
240 if result.get_child_by_name('attributes-list'):
241 fpolicy_scope_attributes = result['attributes-list']['fpolicy-scope-config']
242 param_dict = {
243 'export_policies_to_exclude': [],
244 'export_policies_to_include': [],
245 'file_extensions_to_exclude': [],
246 'file_extensions_to_include': [],
247 'shares_to_exclude': [],
248 'shares_to_include': [],
249 'volumes_to_exclude': [],
250 'volumes_to_include': []
251 }
252
253 for param in param_dict.keys():
254 if fpolicy_scope_attributes.get_child_by_name(param.replace('_', '-')):
255 param_dict[param] = [
256 child_name.get_content() for child_name in fpolicy_scope_attributes.get_child_by_name((param.replace('_', '-'))).get_children()
257 ]
258
259 return_value = {
260 'name': fpolicy_scope_attributes.get_child_content('policy-name'),
261 'check_extensions_on_directories': self.na_helper.get_value_for_bool(
262 from_zapi=True, value=fpolicy_scope_attributes.get_child_content('check-extensions-on-directories')),
263 'is_monitoring_of_objects_with_no_extension_enabled': self.na_helper.get_value_for_bool(
264 from_zapi=True, value=fpolicy_scope_attributes.get_child_content('is-monitoring-of-objects-with-no-extension-enabled')),
265 }
266 return_value.update(param_dict)
267 return return_value
268
269 def create_fpolicy_scope(self):
270 """
271 Create an FPolicy policy scope
272 :return: nothing
273 """
274 if self.use_rest:
275 api = "/private/cli/vserver/fpolicy/policy/scope"
276 body = {
277 'vserver': self.parameters['vserver'],
278 'policy-name': self.parameters['name']
279 }
280 for parameter in (
281 'export_policies_to_exclude', 'export_policies_to_include', 'export_policies_to_include', 'file_extensions_to_exclude',
282 'file_extensions_to_include', 'shares_to_exclude', 'shares_to_include', 'volumes_to_exclude', 'volumes_to_include',
283 'is-file-extension-check-on-directories-enabled', 'is-monitoring-of-objects-with-no-extension-enabled'
284 ):
285 if parameter in self.parameters:
286 body[parameter.replace('_', '-')] = self.parameters[parameter]
287
288 dummy, error = self.rest_api.post(api, body)
289 if error:
290 self.module.fail_json(msg=error)
291 else:
292 fpolicy_scope_obj = netapp_utils.zapi.NaElement('fpolicy-policy-scope-create')
293 fpolicy_scope_obj.add_new_child('policy-name', self.parameters['name'])
294
295 if 'check_extensions_on_directories' in self.parameters:
296 fpolicy_scope_obj.add_new_child(
297 'check-extensions-on-directories', self.na_helper.get_value_for_bool(
298 from_zapi=False, value=self.parameters['check_extensions_on_directories']
299 )
300 )
301
302 if 'is_monitoring_of_objects_with_no_extension_enabled' in self.parameters:
303 fpolicy_scope_obj.add_new_child(
304 'is-monitoring-of-objects-with-no-extension-enabled', self.na_helper.get_value_for_bool(
305 from_zapi=False, value=self.parameters['is_monitoring_of_objects_with_no_extension_enabled']
306 )
307 )
308
309 if 'export_policies_to_exclude' in self.parameters:
310 export_policies_to_exclude_obj = netapp_utils.zapi.NaElement('export-policies-to-exclude')
311 for export_policies_to_exclude in self.parameters['export_policies_to_exclude']:
312 export_policies_to_exclude_obj.add_new_child('string', export_policies_to_exclude)
313 fpolicy_scope_obj.add_child_elem(export_policies_to_exclude_obj)
314
315 if 'export_policies_to_include' in self.parameters:
316 export_policies_to_include_obj = netapp_utils.zapi.NaElement('export-policies-to-include')
317 for export_policies_to_include in self.parameters['export_policies_to_include']:
318 export_policies_to_include_obj.add_new_child('string', export_policies_to_include)
319 fpolicy_scope_obj.add_child_elem(export_policies_to_include_obj)
320
321 if 'file_extensions_to_exclude' in self.parameters:
322 file_extensions_to_exclude_obj = netapp_utils.zapi.NaElement('file-extensions-to-exclude')
323 for file_extensions_to_exclude in self.parameters['file_extensions_to_exclude']:
324 file_extensions_to_exclude_obj.add_new_child('string', file_extensions_to_exclude)
325 fpolicy_scope_obj.add_child_elem(file_extensions_to_exclude_obj)
326
327 if 'file_extensions_to_include' in self.parameters:
328 file_extensions_to_include_obj = netapp_utils.zapi.NaElement('file-extensions-to-include')
329 for file_extensions_to_include in self.parameters['file_extensions_to_include']:
330 file_extensions_to_include_obj.add_new_child('string', file_extensions_to_include)
331 fpolicy_scope_obj.add_child_elem(file_extensions_to_include_obj)
332
333 if 'shares_to_exclude' in self.parameters:
334 shares_to_exclude_obj = netapp_utils.zapi.NaElement('shares-to-exclude')
335 for shares_to_exclude in self.parameters['shares_to_exclude']:
336 shares_to_exclude_obj.add_new_child('string', shares_to_exclude)
337 fpolicy_scope_obj.add_child_elem(shares_to_exclude_obj)
338
339 if 'volumes_to_exclude' in self.parameters:
340 volumes_to_exclude_obj = netapp_utils.zapi.NaElement('volumes-to-exclude')
341 for volumes_to_exclude in self.parameters['volumes_to_exclude']:
342 volumes_to_exclude_obj.add_new_child('string', volumes_to_exclude)
343 fpolicy_scope_obj.add_child_elem(volumes_to_exclude_obj)
344
345 if 'shares_to_include' in self.parameters:
346 shares_to_include_obj = netapp_utils.zapi.NaElement('shares-to-include')
347 for shares_to_include in self.parameters['shares_to_include']:
348 shares_to_include_obj.add_new_child('string', shares_to_include)
349 fpolicy_scope_obj.add_child_elem(shares_to_include_obj)
350
351 if 'volumes_to_include' in self.parameters:
352 volumes_to_include_obj = netapp_utils.zapi.NaElement('volumes-to-include')
353 for volumes_to_include in self.parameters['volumes_to_include']:
354 volumes_to_include_obj.add_new_child('string', volumes_to_include)
355 fpolicy_scope_obj.add_child_elem(volumes_to_include_obj)
356
357 try:
358 self.server.invoke_successfully(fpolicy_scope_obj, True)
359 except netapp_utils.zapi.NaApiError as error:
360 self.module.fail_json(
361 msg='Error creating fPolicy policy scope %s on vserver %s: %s' % (
362 self.parameters['name'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()
363 )
364
365 def modify_fpolicy_scope(self, modify):
366 """
367 Modify an FPolicy policy scope
368 :return: nothing
369 """
370 if self.use_rest:
371 api = "/private/cli/vserver/fpolicy/policy/scope"
372 query = {'vserver': self.parameters['vserver']}
373 query['policy-name'] = self.parameters['name']
374 dummy, error = self.rest_api.patch(api, modify, query)
375 if error:
376 self.module.fail_json(msg=error)
377
378 else:
379 fpolicy_scope_obj = netapp_utils.zapi.NaElement('fpolicy-policy-scope-modify')
380 fpolicy_scope_obj.add_new_child('policy-name', self.parameters['name'])
381
382 if 'check_extensions_on_directories' in self.parameters:
383 fpolicy_scope_obj.add_new_child(
384 'check-extensions-on-directories', self.na_helper.get_value_for_bool(
385 from_zapi=False, value=self.parameters['check_extensions_on_directories']
386 )
387 )
388
389 if 'is_monitoring_of_objects_with_no_extension_enabled' in self.parameters:
390 fpolicy_scope_obj.add_new_child(
391 'is-monitoring-of-objects-with-no-extension-enabled',
392 self.na_helper.get_value_for_bool(from_zapi=False, value=self.parameters['is_monitoring_of_objects_with_no_extension_enabled'])
393 )
394
395 if 'export_policies_to_exclude' in self.parameters:
396 export_policies_to_exclude_obj = netapp_utils.zapi.NaElement('export-policies-to-exclude')
397 for export_policies_to_exclude in self.parameters['export_policies_to_exclude']:
398 export_policies_to_exclude_obj.add_new_child('string', export_policies_to_exclude)
399 fpolicy_scope_obj.add_child_elem(export_policies_to_exclude_obj)
400
401 if 'export_policies_to_include' in self.parameters:
402 export_policies_to_include_obj = netapp_utils.zapi.NaElement('export-policies-to-include')
403
404 for export_policies_to_include in self.parameters['export_policies_to_include']:
405 export_policies_to_include_obj.add_new_child('string', export_policies_to_include)
406 fpolicy_scope_obj.add_child_elem(export_policies_to_include_obj)
407
408 if 'file_extensions_to_exclude' in self.parameters:
409 file_extensions_to_exclude_obj = netapp_utils.zapi.NaElement('file-extensions-to-exclude')
410
411 for file_extensions_to_exclude in self.parameters['file_extensions_to_exclude']:
412 file_extensions_to_exclude_obj.add_new_child('string', file_extensions_to_exclude)
413 fpolicy_scope_obj.add_child_elem(file_extensions_to_exclude_obj)
414
415 if 'file_extensions_to_include' in self.parameters:
416 file_extensions_to_include_obj = netapp_utils.zapi.NaElement('file-extensions-to-include')
417
418 for file_extensions_to_include in self.parameters['file_extensions_to_include']:
419 file_extensions_to_include_obj.add_new_child('string', file_extensions_to_include)
420 fpolicy_scope_obj.add_child_elem(file_extensions_to_include_obj)
421
422 if 'shares_to_exclude' in self.parameters:
423 shares_to_exclude_obj = netapp_utils.zapi.NaElement('shares-to-exclude')
424
425 for shares_to_exclude in self.parameters['shares_to_exclude']:
426 shares_to_exclude_obj.add_new_child('string', shares_to_exclude)
427 fpolicy_scope_obj.add_child_elem(shares_to_exclude_obj)
428
429 if 'volumes_to_exclude' in self.parameters:
430 volumes_to_exclude_obj = netapp_utils.zapi.NaElement('volumes-to-exclude')
431
432 for volumes_to_exclude in self.parameters['volumes_to_exclude']:
433 volumes_to_exclude_obj.add_new_child('string', volumes_to_exclude)
434 fpolicy_scope_obj.add_child_elem(volumes_to_exclude_obj)
435
436 if 'shares_to_include' in self.parameters:
437 shares_to_include_obj = netapp_utils.zapi.NaElement('shares-to-include')
438
439 for shares_to_include in self.parameters['shares_to_include']:
440 shares_to_include_obj.add_new_child('string', shares_to_include)
441 fpolicy_scope_obj.add_child_elem(shares_to_include_obj)
442
443 if 'volumes_to_include' in self.parameters:
444 volumes_to_include_obj = netapp_utils.zapi.NaElement('volumes-to-include')
445
446 for volumes_to_include in self.parameters['volumes_to_include']:
447 volumes_to_include_obj.add_new_child('string', volumes_to_include)
448 fpolicy_scope_obj.add_child_elem(volumes_to_include_obj)
449
450 try:
451 self.server.invoke_successfully(fpolicy_scope_obj, True)
452 except netapp_utils.zapi.NaApiError as error:
453 self.module.fail_json(msg='Error modifying fPolicy policy scope %s on vserver %s: %s' % (
454 self.parameters['name'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()
455 )
456
457 def delete_fpolicy_scope(self):
458 """
459 Delete an FPolicy policy scope
460 :return: nothing
461 """
462
463 if self.use_rest:
464 api = "/private/cli/vserver/fpolicy/policy/scope"
465 body = {
466 'vserver': self.parameters['vserver'],
467 'policy-name': self.parameters['name']
468 }
469 dummy, error = self.rest_api.delete(api, body)
470 if error:
471 self.module.fail_json(msg=error)
472 else:
473 fpolicy_scope_obj = netapp_utils.zapi.NaElement('fpolicy-policy-scope-delete')
474 fpolicy_scope_obj.add_new_child('policy-name', self.parameters['name'])
475
476 try:
477 self.server.invoke_successfully(fpolicy_scope_obj, True)
478 except netapp_utils.zapi.NaApiError as error:
479 self.module.fail_json(
480 msg='Error deleting fPolicy policy scope %s on vserver %s: %s' % (
481 self.parameters['name'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()
482 )
483
484 def apply(self):
485 current, modify = self.get_fpolicy_scope(), None
486 cd_action = self.na_helper.get_cd_action(current, self.parameters)
487
488 if cd_action is None and self.parameters['state'] == 'present':
489 modify = self.na_helper.get_modified_attributes(current, self.parameters)
490
491 if self.na_helper.changed and not self.module.check_mode:
492 if cd_action == 'create':
493 self.create_fpolicy_scope()
494 elif cd_action == 'delete':
495 self.delete_fpolicy_scope()
496 elif modify:
497 self.modify_fpolicy_scope(modify)
498 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
499 self.module.exit_json(**result)
500
501
502 def main():
503 """
504 Execute action from playbook
505 """
506 command = NetAppOntapFpolicyScope()
507 command.apply()
508
509
510 if __name__ == '__main__':
511 main()