Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2020-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 ANSIBLE_METADATA = {
11 'metadata_version': '1.1',
12 'status': ['preview'],
13 'supported_by': 'certified'
14 }
15
16 DOCUMENTATION = """
17
18 module: na_ontap_ntfs_sd
19 author: NetApp Ansible Team (@carchi8py) <[email protected]>
20 short_description: NetApp ONTAP create, delete or modify NTFS security descriptor
21 extends_documentation_fragment:
22 - netapp.ontap.netapp.na_ontap
23 version_added: '20.4.0'
24 description:
25 - Create, modify or destroy NTFS security descriptor
26
27 options:
28 state:
29 description:
30 - Whether the specified NTFS security descriptor should exist or not.
31 choices: ['present', 'absent']
32 default: present
33 type: str
34
35 vserver:
36 description:
37 - Specifies the vserver for the NTFS security descriptor.
38 required: true
39 type: str
40
41 name:
42 description:
43 - Specifies the NTFS security descriptor name. Not modifiable.
44 required: true
45 type: str
46
47 owner:
48 description:
49 - Specifies the owner's SID or domain account of the NTFS security descriptor.
50 - Need to provide the full path of the owner.
51 type: str
52
53 group:
54 description:
55 - Specifies the group's SID or domain account of the NTFS security descriptor.
56 - Need to provide the full path of the group.
57 required: false
58 type: str
59
60 control_flags_raw:
61 description:
62 - Specifies the security descriptor control flags.
63 - 1... .... .... .... = Self Relative
64 - .0.. .... .... .... = RM Control Valid
65 - ..0. .... .... .... = SACL Protected
66 - ...0 .... .... .... = DACL Protected
67 - .... 0... .... .... = SACL Inherited
68 - .... .0.. .... .... = DACL Inherited
69 - .... ..0. .... .... = SACL Inherit Required
70 - .... ...0 .... .... = DACL Inherit Required
71 - .... .... ..0. .... = SACL Defaulted
72 - .... .... ...0 .... = SACL Present
73 - .... .... .... 0... = DACL Defaulted
74 - .... .... .... .1.. = DACL Present
75 - .... .... .... ..0. = Group Defaulted
76 - .... .... .... ...0 = Owner Defaulted
77 - At present only the following flags are honored. Others are ignored.
78 - ..0. .... .... .... = SACL Protected
79 - ...0 .... .... .... = DACL Protected
80 - .... .... ..0. .... = SACL Defaulted
81 - .... .... .... 0... = DACL Defaulted
82 - .... .... .... ..0. = Group Defaulted
83 - .... .... .... ...0 = Owner Defaulted
84 - Convert the 16 bit binary flags and convert to decimal for the input.
85 type: int
86
87 """
88
89 EXAMPLES = """
90 - name: Create NTFS Security Descriptor
91 netapp.ontap.na_ontap_ntfs_sd:
92 state: present
93 vserver: SVM1
94 name: ansible_sd
95 owner: DOMAIN\\Account
96 group: DOMAIN\\Group
97 control_flags_raw: 0
98 hostname: "{{ netapp_hostname }}"
99 username: "{{ netapp_username }}"
100 password: "{{ netapp_password }}"
101
102 - name: Modify NTFS Security Descriptor
103 netapp.ontap.na_ontap_ntfs_sd:
104 state: present
105 vserver: SVM1
106 name: ansible_sd
107 owner: DOMAIN\\Account
108 group: DOMAIN\\Group
109 control_flags_raw: 0
110 hostname: "{{ netapp_hostname }}"
111 username: "{{ netapp_username }}"
112 password: "{{ netapp_password }}"
113
114 - name: Delete NTFS Security Descriptor
115 netapp.ontap.na_ontap_ntfs_sd:
116 state: absent
117 vserver: SVM1
118 name: ansible_sd
119 hostname: "{{ netapp_hostname }}"
120 username: "{{ netapp_username }}"
121 password: "{{ netapp_password }}"
122 """
123
124 RETURN = """
125 """
126
127
128 import traceback
129 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
130 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
131 from ansible.module_utils.basic import AnsibleModule
132 from ansible.module_utils._text import to_native
133
134 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
135
136
137 class NetAppOntapNtfsSd(object):
138 """
139 Creates, Modifies and Destroys a NTFS security descriptor
140 """
141
142 def __init__(self):
143 """
144 Initialize the Ontap NTFS Security Descriptor class
145 """
146 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
147 self.argument_spec.update(dict(
148 state=dict(required=False, choices=['present', 'absent'], default='present'),
149 vserver=dict(required=True, type='str'),
150 name=dict(required=True, type='str'),
151 owner=dict(required=False, type='str'),
152 group=dict(required=False, type='str'),
153 control_flags_raw=dict(required=False, type='int'),
154 ))
155
156 self.module = AnsibleModule(
157 argument_spec=self.argument_spec,
158 supports_check_mode=True,
159 )
160
161 # set up variables
162 self.na_helper = NetAppModule()
163 self.parameters = self.na_helper.set_parameters(self.module.params)
164
165 if HAS_NETAPP_LIB is False:
166 self.module.fail_json(msg='The python NetApp-Lib module is required')
167 else:
168 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
169
170 def get_ntfs_sd(self):
171
172 ntfs_sd_entry, result = None, None
173
174 ntfs_sd_get_iter = netapp_utils.zapi.NaElement('file-directory-security-ntfs-get-iter')
175 ntfs_sd_info = netapp_utils.zapi.NaElement('file-directory-security-ntfs')
176 ntfs_sd_info.add_new_child('vserver', self.parameters['vserver'])
177 ntfs_sd_info.add_new_child('ntfs-sd', self.parameters['name'])
178 query = netapp_utils.zapi.NaElement('query')
179 query.add_child_elem(ntfs_sd_info)
180 ntfs_sd_get_iter.add_child_elem(query)
181 try:
182 result = self.server.invoke_successfully(ntfs_sd_get_iter, True)
183 except netapp_utils.zapi.NaApiError as error:
184 self.module.fail_json(msg='Error fetching NTFS security descriptor %s : %s'
185 % (self.parameters['name'], to_native(error)),
186 exception=traceback.format_exc())
187 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1:
188 attributes_list = result.get_child_by_name('attributes-list')
189 ntfs_sd = attributes_list.get_child_by_name('file-directory-security-ntfs')
190 ntfs_sd_entry = {
191 'vserver': ntfs_sd.get_child_content('vserver'),
192 'name': ntfs_sd.get_child_content('ntfs-sd'),
193 'owner': ntfs_sd.get_child_content('owner'),
194 'group': ntfs_sd.get_child_content('group'),
195 'control_flags_raw': ntfs_sd.get_child_content('control-flags-raw'),
196 }
197 if ntfs_sd_entry.get('control_flags_raw'):
198 ntfs_sd_entry['control_flags_raw'] = int(ntfs_sd_entry['control_flags_raw'])
199 return ntfs_sd_entry
200 return None
201
202 def add_ntfs_sd(self):
203 """
204 Adds a new NTFS security descriptor
205 """
206
207 ntfs_sd_obj = netapp_utils.zapi.NaElement("file-directory-security-ntfs-create")
208 ntfs_sd_obj.add_new_child("ntfs-sd", self.parameters['name'])
209
210 if self.parameters.get('control_flags_raw') is not None:
211 ntfs_sd_obj.add_new_child("control-flags-raw", str(self.parameters['control_flags_raw']))
212
213 if self.parameters.get('owner'):
214 ntfs_sd_obj.add_new_child("owner", self.parameters['owner'])
215
216 if self.parameters.get('group'):
217 ntfs_sd_obj.add_new_child("group", self.parameters['group'])
218
219 try:
220 self.server.invoke_successfully(ntfs_sd_obj, True)
221 except netapp_utils.zapi.NaApiError as error:
222 self.module.fail_json(
223 msg='Error creating NTFS security descriptor %s: %s' % (self.parameters['name'], to_native(error)),
224 exception=traceback.format_exc())
225
226 def remove_ntfs_sd(self):
227 """
228 Deletes a NTFS security descriptor
229 """
230 ntfs_sd_obj = netapp_utils.zapi.NaElement("file-directory-security-ntfs-delete")
231 ntfs_sd_obj.add_new_child("ntfs-sd", self.parameters['name'])
232 try:
233 self.server.invoke_successfully(ntfs_sd_obj, True)
234 except netapp_utils.zapi.NaApiError as error:
235 self.module.fail_json(msg='Error deleting NTFS security descriptor %s: %s' % (self.parameters['name'], to_native(error)),
236 exception=traceback.format_exc())
237
238 def modify_ntfs_sd(self):
239 """
240 Modifies a NTFS security descriptor
241 """
242
243 ntfs_sd_obj = netapp_utils.zapi.NaElement("file-directory-security-ntfs-modify")
244 ntfs_sd_obj.add_new_child("ntfs-sd", self.parameters['name'])
245
246 if self.parameters.get('control_flags_raw') is not None:
247 ntfs_sd_obj.add_new_child('control-flags-raw', str(self.parameters['control_flags_raw']))
248
249 if self.parameters.get('owner'):
250 ntfs_sd_obj.add_new_child('owner', self.parameters['owner'])
251
252 if self.parameters.get('group'):
253 ntfs_sd_obj.add_new_child('group', self.parameters['group'])
254
255 try:
256 self.server.invoke_successfully(ntfs_sd_obj, True)
257 except netapp_utils.zapi.NaApiError as error:
258 self.module.fail_json(
259 msg='Error modifying NTFS security descriptor %s: %s' % (self.parameters['name'], to_native(error)),
260 exception=traceback.format_exc())
261
262 def apply(self):
263 current, modify = self.get_ntfs_sd(), None
264 cd_action = self.na_helper.get_cd_action(current, self.parameters)
265 if cd_action is None and self.parameters['state'] == 'present':
266 modify = self.na_helper.get_modified_attributes(current, self.parameters)
267 if self.na_helper.changed and not self.module.check_mode:
268 if cd_action == 'create':
269 self.add_ntfs_sd()
270 elif cd_action == 'delete':
271 self.remove_ntfs_sd()
272 elif modify:
273 self.modify_ntfs_sd()
274 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
275 self.module.exit_json(**result)
276
277
278 def main():
279 """
280 Creates, deletes and modifies NTFS secudity descriptor
281 """
282 obj = NetAppOntapNtfsSd()
283 obj.apply()
284
285
286 if __name__ == '__main__':
287 main()