Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2019-2026, 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
10 DOCUMENTATION = '''
11
12 module: na_ontap_lun_copy
13
14 short_description: NetApp ONTAP copy LUNs
15 extends_documentation_fragment:
16 - netapp.ontap.netapp.na_ontap
17 version_added: 2.8.0
18 author: NetApp Ansible Team (@carchi8py) <[email protected]>
19
20 description:
21 - Copy LUNs on NetApp ONTAP.
22
23 options:
24
25 state:
26 description:
27 - Whether the specified LUN should exist or not.
28 choices: ['present']
29 type: str
30 default: present
31
32 destination_vserver:
33 description:
34 - the name of the Vserver that will host the new LUN.
35 required: true
36 type: str
37 aliases: ['vserver']
38
39 destination_path:
40 description:
41 - Specifies the full path to the new LUN.
42 required: true
43 type: str
44
45 source_path:
46 description:
47 - Specifies the full path to the source LUN.
48 required: true
49 type: str
50
51 source_vserver:
52 description:
53 - Specifies the name of the vserver hosting the LUN to be copied.
54 - If not provided, C(destination_vserver) value is set as default.
55 - with REST, this option value must match C(destination_vserver) when present.
56 type: str
57
58 lambda_config:
59 description:
60 - Configuration parameters for AWS Lambda proxy functionality.
61 - These option and suboptions are only supported with REST.
62 type: dict
63 version_added: 23.4.0
64 suboptions:
65 function_name:
66 description:
67 - The name of the AWS Lambda function to invoke.
68 type: str
69 required: true
70 aws_region:
71 description:
72 - The name of the AWS region.
73 type: str
74 required: true
75 aws_profile:
76 description:
77 - The name of the AWS profile to use for authentication.
78 type: str
79
80 notes:
81 - supports ZAPI and REST. REST requires ONTAP 9.10.1 or later.
82 - supports check mode.
83 - REST supports intra-Vserver lun copy only.
84 - This module is not compatible with ASA r2 systems.
85 - Supports AWS Lambda proxy functionality when using REST. See the README file for examples.
86 '''
87 EXAMPLES = """
88 - name: Copy LUN
89 netapp.ontap.na_ontap_lun_copy:
90 destination_vserver: ansible
91 destination_path: /vol/test/test_copy_dest_dest_new
92 source_path: /vol/test/test_copy_1
93 source_vserver: ansible
94 hostname: "{{ netapp_hostname }}"
95 username: "{{ netapp_username }}"
96 password: "{{ netapp_password }}"
97 """
98
99 RETURN = """
100
101 """
102
103 import traceback
104 from ansible.module_utils.basic import AnsibleModule
105 from ansible.module_utils._text import to_native
106 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
107 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
108 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, rest_ontap_personality
109
110
111 class NetAppOntapLUNCopy:
112
113 def __init__(self):
114 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
115 self.argument_spec.update(dict(
116 state=dict(required=False, type='str', choices=['present'], default='present'),
117 destination_vserver=dict(required=True, type='str', aliases=['vserver']),
118 destination_path=dict(required=True, type='str'),
119 source_path=dict(required=True, type='str'),
120 source_vserver=dict(required=False, type='str'),
121 ))
122
123 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
124
125 self.module = AnsibleModule(
126 argument_spec=self.argument_spec,
127 supports_check_mode=True,
128 required_if=[
129 ['use_lambda', True, ('lambda_config',)]
130 ],
131 )
132
133 self.na_helper = NetAppModule()
134 self.parameters = self.na_helper.set_parameters(self.module.params)
135 self.rest_api = netapp_utils.OntapRestAPI(self.module)
136 self.use_rest = self.rest_api.is_rest()
137 # if source_vserver not present, set destination_vserver value for intra-vserver copy operation.
138 if not self.parameters.get('source_vserver'):
139 self.parameters['source_vserver'] = self.parameters['destination_vserver']
140 if self.use_rest and not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 10, 1):
141 msg = 'REST requires ONTAP 9.10.1 or later for na_ontap_lun_copy'
142 self.use_rest = self.na_helper.fall_back_to_zapi(self.module, msg, self.parameters)
143 self.asa_r2_system = False
144 if self.use_rest:
145 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 16, 0):
146 self.asa_r2_system = rest_ontap_personality.is_asa_r2_system(self.rest_api, self.module)
147 if self.asa_r2_system:
148 self.module.fail_json(msg='na_ontap_lun_copy operation is not compatible with ASA r2 systems.')
149 if not self.use_rest:
150 if self.parameters.get('use_lambda'):
151 self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.")
152 if not netapp_utils.has_netapp_lib():
153 self.module.fail_json(msg=netapp_utils.netapp_lib_is_required())
154 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module,
155 vserver=self.parameters['destination_vserver'])
156
157 def get_lun(self):
158 """
159 Check if the LUN exists
160
161 :return: true is it exists, false otherwise
162 :rtype: bool
163 """
164
165 if self.use_rest:
166 return self.get_lun_rest()
167 return_value = False
168 lun_info = netapp_utils.zapi.NaElement('lun-get-iter')
169 query_details = netapp_utils.zapi.NaElement('lun-info')
170
171 query_details.add_new_child('path', self.parameters['destination_path'])
172 query_details.add_new_child('vserver', self.parameters['destination_vserver'])
173
174 query = netapp_utils.zapi.NaElement('query')
175 query.add_child_elem(query_details)
176
177 lun_info.add_child_elem(query)
178 try:
179 result = self.server.invoke_successfully(lun_info, True)
180
181 except netapp_utils.zapi.NaApiError as e:
182 self.module.fail_json(msg="Error getting lun info %s for verver %s: %s" %
183 (self.parameters['destination_path'], self.parameters['destination_vserver'], to_native(e)),
184 exception=traceback.format_exc())
185
186 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1:
187 return_value = True
188 return return_value
189
190 def copy_lun(self):
191 """
192 Copy LUN with requested path and vserver
193 """
194 if self.use_rest:
195 return self.copy_lun_rest()
196 lun_copy = netapp_utils.zapi.NaElement.create_node_with_children(
197 'lun-copy-start', **{'source-vserver': self.parameters['source_vserver']})
198 path_obj = netapp_utils.zapi.NaElement('paths')
199 pair = netapp_utils.zapi.NaElement('lun-path-pair')
200 pair.add_new_child('destination-path', self.parameters['destination_path'])
201 pair.add_new_child('source-path', self.parameters['source_path'])
202 path_obj.add_child_elem(pair)
203 lun_copy.add_child_elem(path_obj)
204
205 try:
206 self.server.invoke_successfully(lun_copy, enable_tunneling=True)
207 except netapp_utils.zapi.NaApiError as e:
208 self.module.fail_json(msg="Error copying lun from %s to vserver %s: %s" %
209 (self.parameters['source_vserver'], self.parameters['destination_vserver'], to_native(e)),
210 exception=traceback.format_exc())
211
212 def get_lun_rest(self):
213 api = 'storage/luns'
214 params = {
215 'svm.name': self.parameters['destination_vserver'],
216 'name': self.parameters['destination_path']
217 }
218 record, error = rest_generic.get_one_record(self.rest_api, api, params)
219 if error:
220 self.module.fail_json(msg="Error getting lun info %s for verver %s: %s" %
221 (self.parameters['destination_path'], self.parameters['destination_vserver'], to_native(error)))
222 return True if record else False
223
224 def copy_lun_rest(self):
225 api = 'storage/luns'
226 body = {
227 'copy': {'source': {'name': self.parameters['source_path']}},
228 'name': self.parameters['destination_path'],
229 'svm.name': self.parameters['destination_vserver']
230 }
231 dummy, error = rest_generic.post_async(self.rest_api, api, body)
232 if error:
233 self.module.fail_json(msg="Error copying lun from %s to vserver %s: %s" %
234 (self.parameters['source_vserver'], self.parameters['destination_vserver'], to_native(error)))
235
236 def apply(self):
237 if self.get_lun(): # lun already exists at destination
238 changed = False
239 else:
240 if self.use_rest and self.parameters['source_vserver'] != self.parameters['destination_vserver']:
241 self.module.fail_json(msg="Error: REST does not supports inter-Vserver lun copy.")
242 changed = True
243 if not self.module.check_mode:
244 # need to copy lun
245 if self.parameters['state'] == 'present':
246 self.copy_lun()
247
248 self.module.exit_json(changed=changed)
249
250
251 def main():
252 v = NetAppOntapLUNCopy()
253 v.apply()
254
255
256 if __name__ == '__main__':
257 main()