Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_storage_unit_snapshot
12 short_description: NetApp ONTAP ASA r2 storage unit snapshot
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap_rest
15 version_added: 23.0.0
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Create/modify/delete storage unit snapshot.
19 options:
20 state:
21 description:
22 - Specifies whether the specified storage unit should exist or not.
23 choices: ['present', 'absent']
24 type: str
25 default: present
26
27 name:
28 description:
29 - Specifies the name of the snapshot.
30 type: str
31 required: true
32
33 from_name:
34 description:
35 - Specifies the name of the snapshot to be renamed.
36 type: str
37
38 expiry_time:
39 description:
40 - Specifies the expiry time for the snapshot. Example, 2025-04-09T07:30:00-04:00.
41 - Snapshots with an expiry time set are not allowed to be deleted until the retention time is reached.
42 type: str
43
44 snapmirror_label:
45 description:
46 - Specifies label for SnapMirror operations.
47 type: str
48
49 storage_unit:
50 description:
51 - Specifies the storage unit in which the snapshot is located.
52 type: str
53 required: true
54
55 vserver:
56 description:
57 - Specifies the SVM in which the storage unit is located.
58 type: str
59 required: true
60
61 comment:
62 description:
63 - A comment associated with the snapshot.
64 type: str
65
66 notes:
67 - Only supported with REST and requires ONTAP 9.16.1 or later.
68 - Only suppored with ASA r2 systems.
69 """
70
71 EXAMPLES = """
72 - name: Create a snapshot for LUN storage unit
73 netapp.ontap.na_ontap_storage_unit_snapshot:
74 state: present
75 name: lun1_snap1
76 vserver: ansibleSVM
77 storage_unit: lun1
78 expiry_time: 2025-04-09T07:30:00-04:00
79 snapmirror_label: my_label
80 comment: "snapshot for lun1"
81 hostname: "{{ netapp_hostname }}"
82 username: "{{ netapp_username }}"
83 password: "{{ netapp_password }}"
84 https: true
85 validate_certs: "{{ validate_certs }}"
86 use_rest: always
87
88 - name: Update a snapshot
89 netapp.ontap.na_ontap_storage_unit_snapshot:
90 name: lun1_snap1
91 vserver: ansibleSVM
92 storage_unit: lun1
93 expiry_time: 2025-04-09T08:30:00-04:00
94 snapmirror_label: another_label
95 comment: "updated expiry date, label"
96 hostname: "{{ netapp_hostname }}"
97 username: "{{ netapp_username }}"
98 password: "{{ netapp_password }}"
99 https: true
100 validate_certs: "{{ validate_certs }}"
101 use_rest: always
102
103 - name: Rename a storage unit snapshot
104 netapp.ontap.na_ontap_storage_unit_snapshot:
105 name: lun1_snapshot1
106 from_name: lun1_snap1
107 vserver: ansibleSVM
108 storage_unit: lun1
109 expiry_time: 2025-04-09T08:30:00-04:00
110 snapmirror_label: another_label
111 comment: "snapshot for lun1"
112 hostname: "{{ netapp_hostname }}"
113 username: "{{ netapp_username }}"
114 password: "{{ netapp_password }}"
115 https: true
116 validate_certs: "{{ validate_certs }}"
117 use_rest: always
118
119 - name: Delete a snapshot
120 netapp.ontap.na_ontap_mav_approval_group:
121 state: absent
122 name: lun1_snapshot1
123 hostname: "{{ netapp_hostname }}"
124 username: "{{ netapp_username }}"
125 password: "{{ netapp_password }}"
126 https: true
127 validate_certs: "{{ validate_certs }}"
128 use_rest: always
129 """
130
131 RETURN = """
132
133 """
134
135 import traceback
136 from ansible.module_utils.basic import AnsibleModule
137 from ansible.module_utils._text import to_native
138 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
139 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
140 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
141 from ansible_collections.netapp.ontap.plugins.module_utils import rest_ontap_personality
142
143
144 class NetAppOntapStorageUnitSnapshot:
145 def __init__(self):
146 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
147 self.argument_spec.update(dict(
148 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
149 name=dict(required=True, type='str'),
150 from_name=dict(required=False, type='str'),
151 expiry_time=dict(required=False, type='str'),
152 snapmirror_label=dict(required=False, type='str'),
153 storage_unit=dict(required=True, type='str'),
154 vserver=dict(required=True, type='str'),
155 comment=dict(required=False, type='str'),
156 ))
157
158 self.module = AnsibleModule(
159 argument_spec=self.argument_spec,
160 supports_check_mode=True
161 )
162
163 self.storage_unit_uuid, self.snapshot_uuid = None, None
164 self.na_helper = NetAppModule(self.module)
165 self.parameters = self.na_helper.check_and_set_parameters(self.module)
166
167 self.rest_api = netapp_utils.OntapRestAPI(self.module)
168 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_storage_unit_snapshot:', 9, 16, 1)
169 asa_r2_system = self.is_asa_r2_system()
170 if not asa_r2_system:
171 self.module.fail_json(msg="na_ontap_storage_unit_snapshot module is only supported with ASA r2 systems.")
172
173 def is_asa_r2_system(self):
174 ''' checks if the given host is a ASA r2 system or not '''
175 return rest_ontap_personality.is_asa_r2_system(self.rest_api, self.module)
176
177 def get_storage_unit(self):
178 """ Retrieves storage unit by name """
179 api = 'storage/storage-units'
180 params = {
181 'name': self.parameters['storage_unit'],
182 'svm.name': self.parameters['vserver'],
183 'fields': 'name,uuid,type'
184 }
185 record, error = rest_generic.get_one_record(self.rest_api, api, params)
186 if error:
187 self.module.fail_json(msg="Error while fetching storage unit named %s: %s" % (self.parameters['storage_unit'], to_native(error)),
188 exception=traceback.format_exc())
189 if record:
190 self.storage_unit_uuid = record.get('uuid')
191 return {
192 'name': record.get('name'),
193 'type': record.get('type')
194 }
195 return None
196
197 def get_storage_unit_snapshot(self, name=None):
198 """ Retrieves storage unit snapshot by name """
199 api = 'storage/storage-units/%s/snapshots' % self.storage_unit_uuid
200 fields = 'name,uuid,comment,expiry_time,snapmirror_label,'
201 params = {
202 'name': name if name else self.parameters['name'],
203 'svm.name': self.parameters['vserver'],
204 'fields': fields
205 }
206 record, error = rest_generic.get_one_record(self.rest_api, api, params)
207 if error:
208 self.module.fail_json(msg="Error while fetching storage unit snapshot named %s: %s" % (self.parameters['name'], to_native(error)),
209 exception=traceback.format_exc())
210 if record:
211 self.snapshot_uuid = record.get('uuid')
212 return {
213 'name': record.get('name'),
214 'uuid': record.get('uuid'),
215 'expiry_time': record.get('expiry_time'),
216 'snapmirror_label': record.get('snapmirror_label'),
217 'comment': record.get('comment')
218 }
219 return None
220
221 def create_storage_unit_snapshot(self):
222 """ Create a storage unit snapshot """
223 api = 'storage/storage-units/%s/snapshots' % self.storage_unit_uuid
224 body = {
225 'name': self.parameters['name'],
226 'svm.name': self.parameters['vserver'],
227 'storage_unit.name': self.parameters['storage_unit'],
228 }
229 if 'comment' in self.parameters:
230 body['comment'] = self.parameters['comment']
231 if 'expiry_time' in self.parameters:
232 body['expiry_time'] = self.parameters['expiry_time']
233 if 'snapmirror_label' in self.parameters:
234 body['snapmirror_label'] = self.parameters['snapmirror_label']
235
236 dummy, error = rest_generic.post_async(self.rest_api, api, body)
237 if error:
238 self.module.fail_json(msg="Error while creating storage unit snapshot %s: %s" % (self.parameters['name'], to_native(error)),
239 exception=traceback.format_exc())
240
241 def modify_storage_unit_snapshot(self, modify, rename=False):
242 """ Modify storage unit snapshot """
243 api = 'storage/storage-units/%s/snapshots/%s' % (self.storage_unit_uuid, self.snapshot_uuid)
244 body = {'name': self.parameters['name']} if rename else {}
245 if 'comment' in modify:
246 body['comment'] = modify['comment']
247 if 'snapmirror_label' in modify:
248 body['snapmirror_label'] = modify['snapmirror_label']
249 if 'expiry_time' in modify:
250 body['expiry_time'] = modify['expiry_time']
251
252 dummy, error = rest_generic.patch_async(self.rest_api, api, uuid_or_name=None, body=body)
253 if error:
254 self.module.fail_json(msg="Error while modifying storage unit snapshot %s: %s." % (self.parameters['name'], to_native(error)),
255 exception=traceback.format_exc())
256
257 def delete_storage_unit_snapshot(self):
258 """ Delete a storage unit snapshot """
259 api = 'storage/storage-units/%s/snapshots/%s' % (self.storage_unit_uuid, self.snapshot_uuid)
260 dummy, error = rest_generic.delete_async(self.rest_api, api, uuid=None)
261 if error:
262 self.module.fail_json(msg="Error while deleting storage unit snapshot %s: %s." % (self.parameters['name'], to_native(error)),
263 exception=traceback.format_exc())
264
265 def apply(self):
266 storage_unit = self.get_storage_unit()
267 current = self.get_storage_unit_snapshot()
268 modify = None
269 rename = False
270 cd_action = self.na_helper.get_cd_action(current, self.parameters)
271 if cd_action == 'create' and 'from_name' in self.parameters:
272 # create by renaming existing snapshot, if it exists
273 old_snapshot = self.get_storage_unit_snapshot(self.parameters['from_name'])
274 snapshot_rename = self.na_helper.is_rename_action(old_snapshot, current)
275 if snapshot_rename is None:
276 self.module.fail_json(msg="Error renaming storage unit snapshot: %s does not exist" % self.parameters['from_name'])
277 if snapshot_rename:
278 current = old_snapshot
279 self.snapshot_uuid = current.get('uuid')
280 rename = True
281 cd_action = None
282 if cd_action is None and self.parameters['state'] == 'present':
283 modify = self.na_helper.get_modified_attributes(current, self.parameters)
284
285 if self.na_helper.changed and not self.module.check_mode:
286 if cd_action == 'create':
287 self.create_storage_unit_snapshot()
288 elif cd_action == 'delete':
289 self.delete_storage_unit_snapshot()
290 elif modify:
291 self.modify_storage_unit_snapshot(modify, rename=rename)
292
293 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
294 self.module.exit_json(**result)
295
296
297 def main():
298 snapshot = NetAppOntapStorageUnitSnapshot()
299 snapshot.apply()
300
301
302 if __name__ == '__main__':
303 main()