| 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_fdspt |
| 11 |
short_description: NetApp ONTAP create, delete or modify File Directory security policy tasks |
| 12 |
extends_documentation_fragment: |
| 13 |
- netapp.ontap.netapp.na_ontap_rest |
| 14 |
version_added: 21.8.0 |
| 15 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 16 |
|
| 17 |
description: |
| 18 |
- Create, modify or remove file directory security policy tasks. |
| 19 |
|
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Whether the specified Policy Task should exist or not. |
| 24 |
choices: ['present', 'absent'] |
| 25 |
default: present |
| 26 |
type: str |
| 27 |
|
| 28 |
name: |
| 29 |
description: |
| 30 |
- Specifies the name of the policy the task will be associated with. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
|
| 34 |
vserver: |
| 35 |
description: |
| 36 |
- Specifies the vserver for the File Directory security policy. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
|
| 40 |
access_control: |
| 41 |
description: |
| 42 |
- Specifies access control of the task. |
| 43 |
choices: ['file_directory', 'slag'] |
| 44 |
type: str |
| 45 |
|
| 46 |
ntfs_mode: |
| 47 |
description: |
| 48 |
- Specifies NTFS propagation mode. |
| 49 |
choices: ['propagate', 'ignore', 'replace'] |
| 50 |
type: str |
| 51 |
|
| 52 |
ntfs_sd: |
| 53 |
description: |
| 54 |
- Specifies the NTFS security descriptor name. |
| 55 |
type: list |
| 56 |
elements: str |
| 57 |
|
| 58 |
path: |
| 59 |
description: |
| 60 |
- Specifies the file or folder path of the task. In case of SLAG this path specify the volume or qtree mounted path. |
| 61 |
required: true |
| 62 |
type: str |
| 63 |
|
| 64 |
security_type: |
| 65 |
description: |
| 66 |
- Specifies the type of security. If not specified ONTAP will default to ntfs. |
| 67 |
choices: ['ntfs', 'nfsv4'] |
| 68 |
type: str |
| 69 |
|
| 70 |
index_num: |
| 71 |
description: |
| 72 |
- Specifies the index number of a task. Tasks are applied in order. A task with a larger index value is applied after a task with a lower \ |
| 73 |
index number. If you do not specify this optional parameter, new tasks are applied to the end of the index list. |
| 74 |
type: int |
| 75 |
|
| 76 |
notes: |
| 77 |
- check_mode is supported for this module. |
| 78 |
""" |
| 79 |
|
| 80 |
EXAMPLES = """ |
| 81 |
- name: Create File Directory Security Policy Task |
| 82 |
netapp.ontap.na_ontap_na_ontap_fdspt: |
| 83 |
state: present |
| 84 |
name: "ansible_pl" |
| 85 |
access_control: "file_directory" |
| 86 |
ntfs_sd: "ansible1_sd" |
| 87 |
ntfs_mode: "replace" |
| 88 |
security_type: "ntfs" |
| 89 |
path: "/volume1" |
| 90 |
username: "{{ netapp_username }}" |
| 91 |
password: "{{ netapp_password }}" |
| 92 |
hostname: "{{ netapp_hostname }}" |
| 93 |
|
| 94 |
- name: Modify File Directory Security Policy Task |
| 95 |
netapp.ontap.na_ontap_na_ontap_fdspt: |
| 96 |
state: present |
| 97 |
name: "ansible_pl" |
| 98 |
access_control: "file_directory" |
| 99 |
path: "/volume1" |
| 100 |
ntfs_sd: "ansible1_sd" |
| 101 |
ntfs_mode: "replace" |
| 102 |
security_type: "ntfs" |
| 103 |
username: "{{ netapp_username }}" |
| 104 |
password: "{{ netapp_password }}" |
| 105 |
hostname: "{{ netapp_hostname }}" |
| 106 |
|
| 107 |
- name: Remove File Directory Security Policy Task |
| 108 |
netapp.ontap.na_ontap_na_ontap_fdspt: |
| 109 |
state: absent |
| 110 |
vserver: "SVM1" |
| 111 |
name: "ansible_pl" |
| 112 |
access_control: "file_directory" |
| 113 |
path: "/volume1" |
| 114 |
username: "{{ netapp_username }}" |
| 115 |
password: "{{ netapp_password }}" |
| 116 |
hostname: "{{ netapp_hostname }}" |
| 117 |
""" |
| 118 |
|
| 119 |
RETURN = """ |
| 120 |
|
| 121 |
""" |
| 122 |
|
| 123 |
from ansible.module_utils.basic import AnsibleModule |
| 124 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 125 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 126 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 127 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 128 |
|
| 129 |
|
| 130 |
class NetAppOntapFDSPT(): |
| 131 |
""" |
| 132 |
Creates, Modifies and removes a File Directory Security Policy Tasks |
| 133 |
""" |
| 134 |
def __init__(self): |
| 135 |
""" |
| 136 |
Initialize the Ontap File Directory Security Policy Tasks class |
| 137 |
""" |
| 138 |
|
| 139 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 140 |
self.argument_spec.update(dict( |
| 141 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 142 |
vserver=dict(required=True, type='str'), |
| 143 |
name=dict(required=True, type='str'), |
| 144 |
path=dict(required=True, type='str'), |
| 145 |
access_control=dict(required=False, choices=['file_directory', 'slag'], type='str'), |
| 146 |
ntfs_sd=dict(required=False, type='list', elements='str'), |
| 147 |
ntfs_mode=dict(required=False, choices=['propagate', 'ignore', 'replace'], type='str'), |
| 148 |
security_type=dict(required=False, choices=['ntfs', 'nfsv4'], type='str'), |
| 149 |
index_num=dict(required=False, type='int') |
| 150 |
)) |
| 151 |
|
| 152 |
self.module = AnsibleModule( |
| 153 |
argument_spec=self.argument_spec, |
| 154 |
supports_check_mode=True |
| 155 |
) |
| 156 |
|
| 157 |
# set up variables |
| 158 |
self.na_helper = NetAppModule() |
| 159 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 160 |
|
| 161 |
self.rest_api = OntapRestAPI(self.module) |
| 162 |
self.use_rest = self.rest_api.is_rest() |
| 163 |
|
| 164 |
if not self.use_rest: |
| 165 |
self.module.fail_json(msg=self.rest_api.requires_ontap_version('na_ontap_fdspt', '9.6')) |
| 166 |
|
| 167 |
def get_fdspt(self): |
| 168 |
""" |
| 169 |
Get File Directory Security Policy Task |
| 170 |
""" |
| 171 |
api = "private/cli/vserver/security/file-directory/policy/task" |
| 172 |
query = { |
| 173 |
'policy-name': self.parameters['name'], |
| 174 |
'path': self.parameters['path'], |
| 175 |
'fields': 'vserver,ntfs-mode,ntfs-sd,security-type,access-control,index-num' |
| 176 |
} |
| 177 |
|
| 178 |
message, error = self.rest_api.get(api, query) |
| 179 |
records, error = rrh.check_for_0_or_1_records(api, message, error) |
| 180 |
|
| 181 |
if error: |
| 182 |
self.module.fail_json(msg=error) |
| 183 |
if records: |
| 184 |
if 'ntfs_sd' not in records: # ntfs_sd is not included in the response if there is not an associated value. Required for modify |
| 185 |
records['ntfs_sd'] = [] |
| 186 |
|
| 187 |
return records if records else None |
| 188 |
|
| 189 |
def add_fdspt(self): |
| 190 |
""" |
| 191 |
Adds a new File Directory Security Policy Task |
| 192 |
""" |
| 193 |
api = "private/cli/vserver/security/file-directory/policy/task/add" |
| 194 |
body = { |
| 195 |
'policy-name': self.parameters['name'], |
| 196 |
'vserver': self.parameters['vserver'], |
| 197 |
'path': self.parameters['path'] |
| 198 |
} |
| 199 |
|
| 200 |
for i in ('ntfs_mode', 'ntfs_sd', 'security_type', 'access_control', 'index_num'): |
| 201 |
if i in self.parameters: |
| 202 |
body[i.replace('_', '-')] = self.parameters[i] |
| 203 |
|
| 204 |
dummy, error = self.rest_api.post(api, body) |
| 205 |
if error: |
| 206 |
self.module.fail_json(msg=error) |
| 207 |
|
| 208 |
def remove_fdspt(self): |
| 209 |
""" |
| 210 |
Deletes a File Directory Security Policy Task |
| 211 |
""" |
| 212 |
api = "private/cli/vserver/security/file-directory/policy/task/remove" |
| 213 |
body = { |
| 214 |
'policy-name': self.parameters['name'], |
| 215 |
'vserver': self.parameters['vserver'], |
| 216 |
'path': self.parameters['path'] |
| 217 |
} |
| 218 |
|
| 219 |
dummy, error = self.rest_api.delete(api, body) |
| 220 |
if error: |
| 221 |
self.module.fail_json(msg=error) |
| 222 |
|
| 223 |
def modify_fdspt(self): |
| 224 |
""" |
| 225 |
Modifies a File Directory Security Policy Task |
| 226 |
""" |
| 227 |
# Modify endpoint is not functional. |
| 228 |
self.remove_fdspt() |
| 229 |
self.add_fdspt() |
| 230 |
|
| 231 |
def apply(self): |
| 232 |
current, modify = self.get_fdspt(), None |
| 233 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 234 |
if cd_action is None and self.parameters['state'] == 'present': |
| 235 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 236 |
|
| 237 |
if self.na_helper.changed and not self.module.check_mode: |
| 238 |
if cd_action == 'create': |
| 239 |
self.add_fdspt() |
| 240 |
elif cd_action == 'delete': |
| 241 |
self.remove_fdspt() |
| 242 |
elif modify: |
| 243 |
self.modify_fdspt() |
| 244 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 245 |
self.module.exit_json(**result) |
| 246 |
|
| 247 |
|
| 248 |
def main(): |
| 249 |
""" |
| 250 |
Creates, deletes and modifies File Directory Security Policy Tasks |
| 251 |
""" |
| 252 |
obj = NetAppOntapFDSPT() |
| 253 |
obj.apply() |
| 254 |
|
| 255 |
|
| 256 |
if __name__ == '__main__': |
| 257 |
main() |