Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 """ this is ndmp module
3
4 (c) 2019-2025, NetApp, Inc
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 """
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 'metadata_version': '1.1',
13 'status': ['preview'],
14 'supported_by': 'community'
15 }
16
17
18 DOCUMENTATION = '''
19 ---
20 module: na_ontap_ndmp
21 short_description: NetApp ONTAP NDMP services configuration
22 extends_documentation_fragment:
23 - netapp.ontap.netapp.na_ontap
24 version_added: 2.9.0
25 author: NetApp Ansible Team (@carchi8py) <[email protected]>
26
27 description:
28 - Modify NDMP Services.
29
30 options:
31
32 vserver:
33 description:
34 - Name of the vserver.
35 required: true
36 type: str
37
38 abort_on_disk_error:
39 description:
40 - Enable abort on disk error.
41 type: bool
42
43 authtype:
44 description:
45 - Authentication type.
46 type: list
47 elements: str
48
49 backup_log_enable:
50 description:
51 - Enable backup log.
52 type: bool
53
54 data_port_range:
55 description:
56 - Data port range. Modification not supported for data Vservers.
57 type: str
58
59 debug_enable:
60 description:
61 - Enable debug.
62 type: bool
63
64 debug_filter:
65 description:
66 - Debug filter.
67 type: str
68
69 dump_detailed_stats:
70 description:
71 - Enable logging of VM stats for dump.
72 type: bool
73
74 dump_logical_find:
75 description:
76 - Enable logical find for dump.
77 type: str
78
79 enable:
80 description:
81 - Enable NDMP on vserver.
82 type: bool
83
84 fh_dir_retry_interval:
85 description:
86 - FH throttle value for dir.
87 type: int
88
89 fh_node_retry_interval:
90 description:
91 - FH throttle value for node.
92 type: int
93
94 ignore_ctime_enabled:
95 description:
96 - Ignore ctime.
97 type: bool
98
99 is_secure_control_connection_enabled:
100 description:
101 - Is secure control connection enabled.
102 type: bool
103
104 offset_map_enable:
105 description:
106 - Enable offset map.
107 type: bool
108
109 per_qtree_exclude_enable:
110 description:
111 - Enable per qtree exclusion.
112 type: bool
113
114 preferred_interface_role:
115 description:
116 - Preferred interface role.
117 type: list
118 elements: str
119
120 restore_vm_cache_size:
121 description:
122 - Restore VM file cache size. Value range [4-1024]
123 type: int
124
125 secondary_debug_filter:
126 description:
127 - Secondary debug filter.
128 type: str
129
130 tcpnodelay:
131 description:
132 - Enable TCP nodelay.
133 type: bool
134
135 tcpwinsize:
136 description:
137 - TCP window size.
138 type: int
139
140 ndmp_user:
141 description:
142 - The name of the NDMP user.
143 - This field cannot be specified in a PATCH method.
144 type: str
145 version_added: 23.0.0
146 '''
147
148 EXAMPLES = '''
149 - name: Modify ndmp
150 netapp.ontap.na_ontap_ndmp:
151 vserver: ansible
152 abort_on_disk_error: true
153 authtype: plaintext,challenge
154 backup_log_enable: true
155 data_port_range: 8000-9000
156 debug_enable: true
157 debug_filter: filter
158 dump_detailed_stats: true
159 dump_logical_find: default
160 enable: true
161 fh_dir_retry_interval: 100
162 fh_node_retry_interval: 100
163 ignore_ctime_enabled: true
164 is_secure_control_connection_enabled: true
165 offset_map_enable: true
166 per_qtree_exclude_enable: true
167 preferred_interface_role: node_mgmt,intercluster
168 restore_vm_cache_size: 1000
169 secondary_debug_filter: filter
170 tcpnodelay: true
171 tcpwinsize: 10000
172 hostname: "{{ netapp_hostname }}"
173 username: "{{ netapp_username }}"
174 password: "{{ netapp_password }}"
175 https: true
176
177 - name: generate password - REST
178 netapp.ontap.na_ontap_ndmp:
179 ndmp_user: "ndmp_user"
180 vserver: vs0
181 hostname: "{{ netapp_hostname }}"
182 username: "{{ netapp_username }}"
183 password: "{{ netapp_password }}"
184 https: true
185 '''
186
187 RETURN = '''
188 '''
189
190 import traceback
191 from ansible.module_utils.basic import AnsibleModule
192 from ansible.module_utils._text import to_native
193 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
194 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
195 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
196
197 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
198
199
200 class NetAppONTAPNdmp(object):
201 '''
202 modify vserver cifs security
203 '''
204 def __init__(self):
205 self.use_rest = False
206
207 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
208 self.modifiable_options = dict(
209 abort_on_disk_error=dict(required=False, type='bool'),
210 authtype=dict(required=False, type='list', elements='str'),
211 backup_log_enable=dict(required=False, type='bool'),
212 data_port_range=dict(required=False, type='str'),
213 debug_enable=dict(required=False, type='bool'),
214 debug_filter=dict(required=False, type='str'),
215 dump_detailed_stats=dict(required=False, type='bool'),
216 dump_logical_find=dict(required=False, type='str'),
217 enable=dict(required=False, type='bool'),
218 fh_dir_retry_interval=dict(required=False, type='int'),
219 fh_node_retry_interval=dict(required=False, type='int'),
220 ignore_ctime_enabled=dict(required=False, type='bool'),
221 is_secure_control_connection_enabled=dict(required=False, type='bool'),
222 offset_map_enable=dict(required=False, type='bool'),
223 per_qtree_exclude_enable=dict(required=False, type='bool'),
224 preferred_interface_role=dict(required=False, type='list', elements='str'),
225 restore_vm_cache_size=dict(required=False, type='int'),
226 secondary_debug_filter=dict(required=False, type='str'),
227 tcpnodelay=dict(required=False, type='bool'),
228 tcpwinsize=dict(required=False, type='int')
229 )
230 self.argument_spec.update(dict(
231 vserver=dict(required=True, type='str'),
232 ndmp_user=dict(required=False, type='str'),
233 ))
234
235 self.argument_spec.update(self.modifiable_options)
236
237 self.module = AnsibleModule(
238 argument_spec=self.argument_spec,
239 supports_check_mode=True
240 )
241
242 self.na_helper = NetAppModule()
243 self.parameters = self.na_helper.set_parameters(self.module.params)
244 # API should be used for ONTAP 9.6 or higher, ZAPI for lower version
245 self.rest_api = OntapRestAPI(self.module)
246 unsupported_rest_properties = ['abort_on_disk_error', 'backup_log_enable', 'data_port_range',
247 'debug_enable', 'debug_filter', 'dump_detailed_stats',
248 'dump_logical_find', 'fh_dir_retry_interval', 'fh_node_retry_interval',
249 'ignore_ctime_enabled', 'is_secure_control_connection_enabled',
250 'offset_map_enable', 'per_qtree_exclude_enable', 'preferred_interface_role',
251 'restore_vm_cache_size', 'secondary_debug_filter', 'tcpnodelay', 'tcpwinsize']
252 self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties)
253 if 'ndmp_user' in self.parameters and not self.rest_api.meets_rest_minimum_version(True, 9, 7, 0):
254 self.module.fail_json(msg="Error: ndmp_user %s requires ONTAP 9.7 or later version." % self.parameters['ndmp_user'])
255 if not self.use_rest:
256 if HAS_NETAPP_LIB is False:
257 self.module.fail_json(msg="the python NetApp-Lib module is required")
258 else:
259 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
260
261 def get_ndmp_svm_uuid(self):
262
263 """
264 Get a svm's UUID
265 :return: uuid of the node
266 """
267 params = {'svm.name': self.parameters['vserver']}
268 api = "protocols/ndmp/svms"
269 message, error = self.rest_api.get(api, params)
270 if error is not None:
271 self.module.fail_json(msg=error)
272 if 'records' in message and len(message['records']) == 0:
273 self.module.fail_json(msg='Error fetching uuid for vserver %s: ' % (self.parameters['vserver']))
274 if len(message.keys()) == 0:
275 error = "No information collected from %s: %s" % (api, repr(message))
276 self.module.fail_json(msg=error)
277 elif 'records' not in message:
278 error = "Unexpected response from %s: %s" % (api, repr(message))
279 self.module.fail_json(msg=error)
280 return message['records'][0]['svm']['uuid']
281
282 def ndmp_get_password(self, uuid, ndmp_user):
283 api = '/protocols/ndmp/svms/%s/passwords/%s' % (uuid, ndmp_user)
284 params = {'fields': 'password'}
285 message, error = self.rest_api.get(api, params)
286 if error:
287 self.module.fail_json(msg=error)
288 if message.get("password"):
289 return message["password"]
290 return None
291
292 def ndmp_get_iter(self, uuid=None):
293 """
294 get current vserver ndmp attributes.
295 :return: a dict of ndmp attributes.
296 """
297 if self.use_rest:
298 data = dict()
299 params = {'fields': 'authentication_types,enabled'}
300 api = '/protocols/ndmp/svms/' + uuid
301 message, error = self.rest_api.get(api, params)
302 if message:
303 return {
304 'enable': message.get('enabled'),
305 'authtype': message.get('authentication_types')
306 }
307 if error:
308 self.module.fail_json(msg=error)
309 return data
310 else:
311 ndmp_get = netapp_utils.zapi.NaElement('ndmp-vserver-attributes-get-iter')
312 query = netapp_utils.zapi.NaElement('query')
313 ndmp_info = netapp_utils.zapi.NaElement('ndmp-vserver-attributes-info')
314 ndmp_info.add_new_child('vserver', self.parameters['vserver'])
315 query.add_child_elem(ndmp_info)
316 ndmp_get.add_child_elem(query)
317 ndmp_details = dict()
318 try:
319 result = self.server.invoke_successfully(ndmp_get, enable_tunneling=True)
320 except netapp_utils.zapi.NaApiError as error:
321 self.module.fail_json(msg='Error fetching ndmp from %s: %s'
322 % (self.parameters['vserver'], to_native(error)),
323 exception=traceback.format_exc())
324
325 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) > 0:
326 ndmp_attributes = result.get_child_by_name('attributes-list').get_child_by_name('ndmp-vserver-attributes-info')
327 self.get_ndmp_details(ndmp_details, ndmp_attributes)
328 return ndmp_details
329
330 def get_ndmp_details(self, ndmp_details, ndmp_attributes):
331 """
332 :param ndmp_details: a dict of current ndmp.
333 :param ndmp_attributes: ndmp returned from api call in xml format.
334 :return: None
335 """
336 for option in self.modifiable_options:
337 option_type = self.modifiable_options[option]['type']
338 if option_type == 'bool':
339 ndmp_details[option] = self.str_to_bool(ndmp_attributes.get_child_content(self.attribute_to_name(option)))
340 elif option_type == 'int':
341 ndmp_details[option] = int(ndmp_attributes.get_child_content(self.attribute_to_name(option)))
342 elif option_type == 'list':
343 child_list = ndmp_attributes.get_child_by_name(self.attribute_to_name(option))
344 values = [child.get_content() for child in child_list.get_children()]
345 ndmp_details[option] = values
346 else:
347 ndmp_details[option] = ndmp_attributes.get_child_content(self.attribute_to_name(option))
348
349 def modify_ndmp(self, modify):
350 """
351 :param modify: A list of attributes to modify
352 :return: None
353 """
354 if self.use_rest:
355 ndmp = dict()
356 uuid = self.get_ndmp_svm_uuid()
357 if 'enable' in modify:
358 ndmp['enabled'] = modify['enable']
359 if 'authtype' in modify:
360 ndmp['authentication_types'] = modify['authtype']
361 api = "protocols/ndmp/svms/" + uuid
362 dummy, error = self.rest_api.patch(api, ndmp)
363 if error:
364 self.module.fail_json(msg=error)
365 else:
366
367 ndmp_modify = netapp_utils.zapi.NaElement('ndmp-vserver-attributes-modify')
368 for attribute in modify:
369 if attribute == 'authtype':
370 authtypes = netapp_utils.zapi.NaElement('authtype')
371 types = self.parameters['authtype']
372 for authtype in types:
373 authtypes.add_new_child('ndmpd-authtypes', authtype)
374 ndmp_modify.add_child_elem(authtypes)
375 elif attribute == 'preferred_interface_role':
376 preferred_interface_roles = netapp_utils.zapi.NaElement('preferred-interface-role')
377 roles = self.parameters['preferred_interface_role']
378 for role in roles:
379 preferred_interface_roles.add_new_child('netport-role', role)
380 ndmp_modify.add_child_elem(preferred_interface_roles)
381 else:
382 ndmp_modify.add_new_child(self.attribute_to_name(attribute), str(self.parameters[attribute]))
383 try:
384 self.server.invoke_successfully(ndmp_modify, enable_tunneling=True)
385 except netapp_utils.zapi.NaApiError as exc:
386 self.module.fail_json(msg='Error modifying ndmp on %s: %s'
387 % (self.parameters['vserver'], to_native(exc)),
388 exception=traceback.format_exc())
389
390 @staticmethod
391 def attribute_to_name(attribute):
392 return str.replace(attribute, '_', '-')
393
394 @staticmethod
395 def str_to_bool(value):
396 return value == 'true'
397
398 def apply(self):
399 """Call modify operations."""
400 uuid = None
401 if self.use_rest:
402 # we only have the svm name, we need to the the uuid for the svm
403 uuid = self.get_ndmp_svm_uuid()
404 if 'ndmp_user' in self.parameters:
405 new_password = self.ndmp_get_password(uuid, self.parameters['ndmp_user'])
406 self.module.exit_json(changed=False, password=new_password)
407 current = self.ndmp_get_iter(uuid=uuid)
408 modify = self.na_helper.get_modified_attributes(current, self.parameters)
409 if self.na_helper.changed:
410 if self.module.check_mode:
411 pass
412 else:
413 if modify:
414 self.modify_ndmp(modify)
415 result = netapp_utils.generate_result(self.na_helper.changed, modify=modify)
416 self.module.exit_json(**result)
417
418
419 def main():
420 obj = NetAppONTAPNdmp()
421 obj.apply()
422
423
424 if __name__ == '__main__':
425 main()