Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2020-2025, NetApp, Inc
4 # GNU General Public License v3.0+
5 # (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
11 ANSIBLE_METADATA = {'metadata_version': '1.1',
12 'status': ['preview'],
13 'supported_by': 'certified'}
14
15
16 DOCUMENTATION = '''
17
18 module: na_ontap_volume_snaplock
19
20 short_description: NetApp ONTAP manage volume snaplock retention.
21 extends_documentation_fragment:
22 - netapp.ontap.netapp.na_ontap_zapi
23 version_added: '20.2.0'
24 author: NetApp Ansible Team (@carchi8py) <[email protected]>
25 description:
26 - Modifies the snaplock retention of volumes on NetApp ONTAP.
27 options:
28 name:
29 description:
30 - The name of the volume to manage.
31 type: str
32 required: true
33
34 vserver:
35 description:
36 - Name of the vserver to use.
37 type: str
38 required: true
39
40 default_retention_period:
41 description:
42 - Specifies the default retention period that will be applied.
43 - The format is "<number> <units>" for example "10 days", the following units are valid
44 - "seconds"
45 - "minutes"
46 - "hours"
47 - "days"
48 - "months"
49 - "years"
50 - If this option is specified as "max", then maximum_retention_period will be used as the default retention period.
51 type: str
52
53 autocommit_period:
54 description:
55 - Specifies the autocommit-period for the snaplock volume.
56 - The format is "<number> <units>" for example "8 hours", the following units are valid
57 - "seconds"
58 - "minutes"
59 - "hours"
60 - "days"
61 - "months"
62 - "years"
63 type: str
64
65 is_volume_append_mode_enabled:
66 description:
67 - Specifies if the volume append mode must be enabled or disabled.
68 - It can be modified only when the volume is not mounted and does not have any data or Snapshot copy.
69 - Volume append mode is not supported on SnapLock audit log volumes.
70 - When it is enabled, all files created with write permissions on the volume will be WORM appendable files by default.
71 - All WORM appendable files not modified for a period greater than the autocommit period of the volume are also committed to WORM read-only state.
72 type: bool
73
74 maximum_retention_period:
75 description:
76 - Specifies the allowed maximum retention period that will be applied.
77 - The format is "<number> <units>" for example "2 years", the following units are valid
78 - "seconds"
79 - "minutes"
80 - "hours"
81 - "days"
82 - "months"
83 - "years"
84 type: str
85
86 minimum_retention_period:
87 description:
88 - Specifies the allowed minimum retention period that will be applied.
89 - The format is "<number> <units>" for example "1 days", the following units are valid
90 - "seconds"
91 - "minutes"
92 - "hours"
93 - "days"
94 - "months"
95 - "years"
96 type: str
97
98 notes:
99 - supports ZAPI only.
100 - for REST, snaplock is supported in na_ontap_volume starting with 21.18.0.
101 '''
102
103 EXAMPLES = """
104 - name: Set volume snaplock
105 netapp.ontap.na_ontap_volume_snaplock:
106 vserver: ansibleSVM
107 name: ansibleVolume
108 default_retention_period: "5 days"
109 minimum_retention_period: "0 years"
110 maximum_retention_period: "10 days"
111 is_volume_append_mode_enabled: false
112 hostname: "{{ netapp_hostname }}"
113 username: "{{ netapp_username }}"
114 password: "{{ netapp_password }}"
115 """
116
117 RETURN = """
118 """
119
120 import traceback
121
122 from ansible.module_utils.basic import AnsibleModule
123 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
124 from ansible.module_utils._text import to_native
125 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
126
127 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
128
129
130 class NetAppOntapVolumeSnaplock(object):
131 '''Class with volume operations'''
132
133 def __init__(self):
134 '''Initialize module parameters'''
135
136 self.argument_spec = netapp_utils.na_ontap_zapi_only_spec()
137 self.argument_spec.update(dict(
138 name=dict(required=True, type='str'),
139 vserver=dict(required=True, type='str'),
140 default_retention_period=dict(required=False, type='str'),
141 maximum_retention_period=dict(required=False, type='str'),
142 minimum_retention_period=dict(required=False, type='str'),
143 autocommit_period=dict(required=False, type='str'),
144 is_volume_append_mode_enabled=dict(required=False, type='bool'),
145 ))
146 self.module = AnsibleModule(
147 argument_spec=self.argument_spec,
148 supports_check_mode=True
149 )
150 self.na_helper = NetAppModule()
151 self.parameters = self.na_helper.set_parameters(self.module.params)
152 self.module.warn('The module only supports ZAPI; refer to netapp.ontap.na_ontap_volume module for RESTful equivalent.')
153
154 if HAS_NETAPP_LIB is False:
155 self.module.fail_json(msg="the python NetApp-Lib module is required")
156 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
157
158 def get_volume_snaplock_attrs(self):
159 """
160 Return volume-get-snaplock-attrs query results
161 :param vol_name: name of the volume
162 :return: dict of the volume snaplock attrs
163 """
164 volume_snaplock = netapp_utils.zapi.NaElement('volume-get-snaplock-attrs')
165 volume_snaplock.add_new_child('volume', self.parameters['name'])
166
167 try:
168 result = self.server.invoke_successfully(volume_snaplock, True)
169 except netapp_utils.zapi.NaApiError as error:
170 self.module.fail_json(msg='Error fetching snaplock attributes for volume %s : %s'
171 % (self.parameters['name'], to_native(error)),
172 exception=traceback.format_exc())
173
174 return_value = None
175
176 if result.get_child_by_name('snaplock-attrs'):
177 volume_snaplock_attributes = result['snaplock-attrs']['snaplock-attrs-info']
178 return_value = {
179 'autocommit_period': volume_snaplock_attributes['autocommit-period'],
180 'default_retention_period': volume_snaplock_attributes['default-retention-period'],
181 'is_volume_append_mode_enabled': self.na_helper.get_value_for_bool(True, volume_snaplock_attributes['is-volume-append-mode-enabled']),
182 'maximum_retention_period': volume_snaplock_attributes['maximum-retention-period'],
183 'minimum_retention_period': volume_snaplock_attributes['minimum-retention-period'],
184 }
185 return return_value
186
187 def set_volume_snaplock_attrs(self, modify):
188 '''Set ONTAP volume snaplock attributes'''
189 volume_snaplock_obj = netapp_utils.zapi.NaElement('volume-set-snaplock-attrs')
190 volume_snaplock_obj.add_new_child('volume', self.parameters['name'])
191 if modify.get('autocommit_period') is not None:
192 volume_snaplock_obj.add_new_child('autocommit-period', self.parameters['autocommit_period'])
193 if modify.get('default_retention_period') is not None:
194 volume_snaplock_obj.add_new_child('default-retention-period', self.parameters['default_retention_period'])
195 if modify.get('is_volume_append_mode_enabled') is not None:
196 volume_snaplock_obj.add_new_child('is-volume-append-mode-enabled',
197 self.na_helper.get_value_for_bool(False, self.parameters['is_volume_append_mode_enabled']))
198 if modify.get('maximum_retention_period') is not None:
199 volume_snaplock_obj.add_new_child('maximum-retention-period', self.parameters['maximum_retention_period'])
200 if modify.get('minimum_retention_period') is not None:
201 volume_snaplock_obj.add_new_child('minimum-retention-period', self.parameters['minimum_retention_period'])
202 try:
203 self.server.invoke_successfully(volume_snaplock_obj, True)
204 except netapp_utils.zapi.NaApiError as error:
205 self.module.fail_json(msg='Error setting snaplock attributes for volume %s : %s'
206 % (self.parameters['name'], to_native(error)),
207 exception=traceback.format_exc())
208
209 def apply(self):
210 current, modify = self.get_volume_snaplock_attrs(), None
211 modify = self.na_helper.get_modified_attributes(current, self.parameters)
212
213 if self.na_helper.changed and not self.module.check_mode:
214 self.set_volume_snaplock_attrs(modify)
215 result = netapp_utils.generate_result(self.na_helper.changed, modify=modify)
216 self.module.exit_json(**result)
217
218
219 def main():
220 '''Set volume snaplock attributes from playbook'''
221 obj = NetAppOntapVolumeSnaplock()
222 obj.apply()
223
224
225 if __name__ == '__main__':
226 main()