| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2019-2026, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
''' |
| 8 |
na_ontap_volume_autosize |
| 9 |
''' |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 15 |
'status': ['preview'], |
| 16 |
'supported_by': 'community'} |
| 17 |
|
| 18 |
DOCUMENTATION = ''' |
| 19 |
module: na_ontap_volume_autosize |
| 20 |
short_description: NetApp ONTAP manage volume autosize |
| 21 |
extends_documentation_fragment: |
| 22 |
- netapp.ontap.netapp.na_ontap |
| 23 |
version_added: 2.9.0 |
| 24 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 25 |
description: |
| 26 |
- Modify Volume AutoSize |
| 27 |
options: |
| 28 |
volume: |
| 29 |
description: |
| 30 |
- The name of the flexible volume for which we want to set autosize. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
|
| 34 |
mode: |
| 35 |
description: |
| 36 |
- Specify the flexible volume's autosize mode of operation. |
| 37 |
type: str |
| 38 |
choices: ['grow', 'grow_shrink', 'off'] |
| 39 |
|
| 40 |
vserver: |
| 41 |
description: |
| 42 |
- Name of the vserver to use. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
|
| 46 |
grow_threshold_percent: |
| 47 |
description: |
| 48 |
- Specifies the percentage of the flexible volume's capacity at which autogrow is initiated. |
| 49 |
- The default grow threshold varies from 85% to 98%, depending on the volume size. |
| 50 |
- It is an error for the grow threshold to be less than or equal to the shrink threshold. |
| 51 |
- Range between 0 and 100 |
| 52 |
type: int |
| 53 |
|
| 54 |
increment_size: |
| 55 |
description: |
| 56 |
- Specify the flexible volume's increment size using the following format < number > [k|m|g|t] |
| 57 |
- The amount is the absolute size to set. |
| 58 |
- The trailing 'k', 'm', 'g', and 't' indicates the desired units, namely 'kilobytes', 'megabytes', 'gigabytes', and 'terabytes' (respectively). |
| 59 |
- Not supported with REST. |
| 60 |
type: str |
| 61 |
|
| 62 |
maximum_size: |
| 63 |
description: |
| 64 |
- Specify the flexible volume's maximum allowed size using the following format < number > [k|m|g|t] |
| 65 |
- The amount is the absolute size to set. |
| 66 |
- The trailing 'k', 'm', 'g', and 't' indicates the desired units, namely 'kilobytes', 'megabytes', 'gigabytes', and 'terabytes' (respectively). |
| 67 |
- The default value is 20% greater than the volume size at the time autosize was enabled. |
| 68 |
- It is an error for the maximum volume size to be less than the current volume size. |
| 69 |
- It is also an error for the maximum size to be less than or equal to the minimum size. |
| 70 |
type: str |
| 71 |
|
| 72 |
minimum_size: |
| 73 |
description: |
| 74 |
- Specify the flexible volume's minimum allowed size using the following format < number > [k|m|g|t] The amount is the absolute size to set. |
| 75 |
- The trailing 'k', 'm', 'g', and 't' indicates the desired units, namely 'kilobytes', 'megabytes', 'gigabytes', and 'terabytes' (respectively). |
| 76 |
- The default value is the size of the volume at the time the 'grow_shrink' mode was enabled. |
| 77 |
- It is an error for the minimum size to be greater than or equal to the maximum size. |
| 78 |
type: str |
| 79 |
|
| 80 |
reset: |
| 81 |
description: |
| 82 |
- "Sets the values of maximum_size, increment_size, minimum_size, grow_threshold_percent, shrink_threshold_percent and mode to their defaults" |
| 83 |
- If reset paramater is present system will always perform reset action, so idempotency is not supported. |
| 84 |
- Not supported with REST. |
| 85 |
type: bool |
| 86 |
|
| 87 |
shrink_threshold_percent: |
| 88 |
description: |
| 89 |
- Specifies the percentage of the flexible volume's capacity at which autoshrink is initiated. |
| 90 |
- The default shrink theshold is 50%. It is an error for the shrink threshold to be greater than or equal to the grow threshold. |
| 91 |
- Range between 0 and 100 |
| 92 |
type: int |
| 93 |
|
| 94 |
lambda_config: |
| 95 |
description: |
| 96 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 97 |
- These option and suboptions are only supported with REST. |
| 98 |
type: dict |
| 99 |
version_added: 23.4.0 |
| 100 |
suboptions: |
| 101 |
function_name: |
| 102 |
description: |
| 103 |
- The name of the AWS Lambda function to invoke. |
| 104 |
type: str |
| 105 |
required: true |
| 106 |
aws_region: |
| 107 |
description: |
| 108 |
- The name of the AWS region. |
| 109 |
type: str |
| 110 |
required: true |
| 111 |
aws_profile: |
| 112 |
description: |
| 113 |
- The name of the AWS profile to use for authentication. |
| 114 |
type: str |
| 115 |
|
| 116 |
notes: |
| 117 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 118 |
''' |
| 119 |
|
| 120 |
EXAMPLES = """ |
| 121 |
- name: Modify volume autosize |
| 122 |
netapp.ontap.na_ontap_volume_autosize: |
| 123 |
hostname: "{{ netapp_hostname }}" |
| 124 |
username: "{{ netapp_username }}" |
| 125 |
password: "{{ netapp_password }}" |
| 126 |
volume: ansibleVolumesize12 |
| 127 |
mode: grow |
| 128 |
grow_threshold_percent: 99 |
| 129 |
increment_size: 50m |
| 130 |
maximum_size: 10g |
| 131 |
minimum_size: 21m |
| 132 |
shrink_threshold_percent: 40 |
| 133 |
vserver: ansible_vserver |
| 134 |
|
| 135 |
- name: Reset volume autosize |
| 136 |
netapp.ontap.na_ontap_volume_autosize: |
| 137 |
hostname: "{{ netapp_hostname }}" |
| 138 |
username: "{{ netapp_username }}" |
| 139 |
password: "{{ netapp_password }}" |
| 140 |
volume: ansibleVolumesize12 |
| 141 |
reset: true |
| 142 |
vserver: ansible_vserver |
| 143 |
""" |
| 144 |
|
| 145 |
RETURN = """ |
| 146 |
""" |
| 147 |
import copy |
| 148 |
import traceback |
| 149 |
from ansible.module_utils.basic import AnsibleModule |
| 150 |
from ansible.module_utils._text import to_native |
| 151 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 152 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 153 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 154 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 155 |
|
| 156 |
|
| 157 |
class NetAppOntapVolumeAutosize: |
| 158 |
''' volume autosize configuration ''' |
| 159 |
def __init__(self): |
| 160 |
self.use_rest = False |
| 161 |
# Volume_autosize returns KB and not B like Volume so values are shifted down 1 |
| 162 |
self._size_unit_map = dict( |
| 163 |
k=1, |
| 164 |
m=1024, |
| 165 |
g=1024 ** 2, |
| 166 |
t=1024 ** 3, |
| 167 |
) |
| 168 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 169 |
self.argument_spec.update(dict( |
| 170 |
volume=dict(required=True, type="str"), |
| 171 |
mode=dict(required=False, choices=['grow', 'grow_shrink', 'off']), |
| 172 |
vserver=dict(required=True, type='str'), |
| 173 |
grow_threshold_percent=dict(required=False, type='int'), |
| 174 |
increment_size=dict(required=False, type='str'), |
| 175 |
maximum_size=dict(required=False, type='str'), |
| 176 |
minimum_size=dict(required=False, type='str'), |
| 177 |
reset=dict(required=False, type='bool'), |
| 178 |
shrink_threshold_percent=dict(required=False, type='int') |
| 179 |
)) |
| 180 |
|
| 181 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 182 |
|
| 183 |
self.module = AnsibleModule( |
| 184 |
argument_spec=self.argument_spec, |
| 185 |
supports_check_mode=True, |
| 186 |
mutually_exclusive=[ |
| 187 |
['reset', 'maximum_size'], |
| 188 |
['reset', 'increment_size'], |
| 189 |
['reset', 'minimum_size'], |
| 190 |
['reset', 'grow_threshold_percent'], |
| 191 |
['reset', 'shrink_threshold_percent'], |
| 192 |
['reset', 'mode'] |
| 193 |
], |
| 194 |
required_if=[['use_lambda', True, ('lambda_config',)]], |
| 195 |
) |
| 196 |
self.na_helper = NetAppModule() |
| 197 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 198 |
# API should be used for ONTAP 9.6 or higher, ZAPI for lower version |
| 199 |
self.rest_api = OntapRestAPI(self.module) |
| 200 |
if self.rest_api.is_rest(): |
| 201 |
self.use_rest = True |
| 202 |
# increment size and reset are not supported with rest api |
| 203 |
if self.parameters.get('increment_size'): |
| 204 |
self.module.fail_json(msg="Rest API does not support increment size, please switch to ZAPI") |
| 205 |
if self.parameters.get('reset'): |
| 206 |
self.module.fail_json(msg="Rest API does not support reset, please switch to ZAPI") |
| 207 |
else: |
| 208 |
if self.parameters.get('use_lambda'): |
| 209 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 210 |
if not netapp_utils.has_netapp_lib(): |
| 211 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 212 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 213 |
|
| 214 |
def get_volume_autosize(self): |
| 215 |
""" |
| 216 |
Get volume_autosize information from the ONTAP system |
| 217 |
:return: |
| 218 |
""" |
| 219 |
if self.use_rest: |
| 220 |
query = { |
| 221 |
'name': self.parameters['volume'], |
| 222 |
'svm.name': self.parameters['vserver'], |
| 223 |
'fields': 'autosize,uuid' |
| 224 |
} |
| 225 |
api = 'storage/volumes' |
| 226 |
response, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 227 |
if error is not None: |
| 228 |
self.module.fail_json(msg='Error fetching volume autosize info for %s: %s' % (self.parameters['volume'], error)) |
| 229 |
if response: |
| 230 |
return self._create_get_volume_return(response['autosize'], response['uuid']) |
| 231 |
self.module.fail_json(msg='Error fetching volume autosize info for %s: volume not found for vserver %s.' |
| 232 |
% (self.parameters['volume'], self.parameters['vserver'])) |
| 233 |
else: |
| 234 |
volume_autosize_info = netapp_utils.zapi.NaElement('volume-autosize-get') |
| 235 |
volume_autosize_info.add_new_child('volume', self.parameters['volume']) |
| 236 |
try: |
| 237 |
result = self.server.invoke_successfully(volume_autosize_info, True) |
| 238 |
except netapp_utils.zapi.NaApiError as error: |
| 239 |
self.module.fail_json(msg='Error fetching volume autosize info for %s: %s.' % (self.parameters['volume'], to_native(error)), |
| 240 |
exception=traceback.format_exc()) |
| 241 |
return self._create_get_volume_return(result) |
| 242 |
|
| 243 |
def _create_get_volume_return(self, results, uuid=None): |
| 244 |
""" |
| 245 |
Create a return value from volume-autosize-get info file |
| 246 |
:param results: |
| 247 |
:return: |
| 248 |
""" |
| 249 |
return_value = {} |
| 250 |
if self.use_rest: |
| 251 |
return_value['uuid'] = uuid |
| 252 |
if 'mode' in results: |
| 253 |
return_value['mode'] = results['mode'] |
| 254 |
if 'grow_threshold' in results: |
| 255 |
return_value['grow_threshold_percent'] = results['grow_threshold'] |
| 256 |
if 'maximum' in results: |
| 257 |
return_value['maximum_size'] = results['maximum'] |
| 258 |
if 'minimum' in results: |
| 259 |
return_value['minimum_size'] = results['minimum'] |
| 260 |
if 'shrink_threshold' in results: |
| 261 |
return_value['shrink_threshold_percent'] = results['shrink_threshold'] |
| 262 |
else: |
| 263 |
if results.get_child_by_name('mode'): |
| 264 |
return_value['mode'] = results.get_child_content('mode') |
| 265 |
if results.get_child_by_name('grow-threshold-percent'): |
| 266 |
return_value['grow_threshold_percent'] = int(results.get_child_content('grow-threshold-percent')) |
| 267 |
if results.get_child_by_name('increment-size'): |
| 268 |
return_value['increment_size'] = results.get_child_content('increment-size') |
| 269 |
if results.get_child_by_name('maximum-size'): |
| 270 |
return_value['maximum_size'] = results.get_child_content('maximum-size') |
| 271 |
if results.get_child_by_name('minimum-size'): |
| 272 |
return_value['minimum_size'] = results.get_child_content('minimum-size') |
| 273 |
if results.get_child_by_name('shrink-threshold-percent'): |
| 274 |
return_value['shrink_threshold_percent'] = int(results.get_child_content('shrink-threshold-percent')) |
| 275 |
if not return_value: |
| 276 |
return_value = None |
| 277 |
return return_value |
| 278 |
|
| 279 |
def modify_volume_autosize(self, uuid): |
| 280 |
""" |
| 281 |
Modify a Volumes autosize |
| 282 |
:return: |
| 283 |
""" |
| 284 |
if self.use_rest: |
| 285 |
autosize = {} |
| 286 |
if self.parameters.get('mode'): |
| 287 |
autosize['mode'] = self.parameters['mode'] |
| 288 |
if self.parameters.get('grow_threshold_percent'): |
| 289 |
autosize['grow_threshold'] = self.parameters['grow_threshold_percent'] |
| 290 |
if self.parameters.get('maximum_size'): |
| 291 |
autosize['maximum'] = self.parameters['maximum_size'] |
| 292 |
if self.parameters.get('minimum_size'): |
| 293 |
autosize['minimum'] = self.parameters['minimum_size'] |
| 294 |
if self.parameters.get('shrink_threshold_percent'): |
| 295 |
autosize['shrink_threshold'] = self.parameters['shrink_threshold_percent'] |
| 296 |
if not autosize: |
| 297 |
return |
| 298 |
api = 'storage/volumes' |
| 299 |
body = {'autosize': autosize} |
| 300 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 301 |
if error is not None: |
| 302 |
self.module.fail_json(msg="Error modifying volume autosize for %s: %s" % (self.parameters["volume"], error)) |
| 303 |
|
| 304 |
else: |
| 305 |
volume_autosize_info = netapp_utils.zapi.NaElement('volume-autosize-set') |
| 306 |
volume_autosize_info.add_new_child('volume', self.parameters['volume']) |
| 307 |
if self.parameters.get('mode'): |
| 308 |
volume_autosize_info.add_new_child('mode', self.parameters['mode']) |
| 309 |
if self.parameters.get('grow_threshold_percent'): |
| 310 |
volume_autosize_info.add_new_child('grow-threshold-percent', str(self.parameters['grow_threshold_percent'])) |
| 311 |
if self.parameters.get('increment_size'): |
| 312 |
volume_autosize_info.add_new_child('increment-size', self.parameters['increment_size']) |
| 313 |
if self.parameters.get('reset') is not None: |
| 314 |
volume_autosize_info.add_new_child('reset', str(self.parameters['reset'])) |
| 315 |
if self.parameters.get('maximum_size'): |
| 316 |
volume_autosize_info.add_new_child('maximum-size', self.parameters['maximum_size']) |
| 317 |
if self.parameters.get('minimum_size'): |
| 318 |
volume_autosize_info.add_new_child('minimum-size', self.parameters['minimum_size']) |
| 319 |
if self.parameters.get('shrink_threshold_percent'): |
| 320 |
volume_autosize_info.add_new_child('shrink-threshold-percent', str(self.parameters['shrink_threshold_percent'])) |
| 321 |
try: |
| 322 |
self.server.invoke_successfully(volume_autosize_info, True) |
| 323 |
except netapp_utils.zapi.NaApiError as error: |
| 324 |
self.module.fail_json(msg="Error modifying volume autosize for %s: %s." % (self.parameters["volume"], to_native(error)), |
| 325 |
exception=traceback.format_exc()) |
| 326 |
|
| 327 |
def modify_to_kb(self, converted_parameters): |
| 328 |
""" |
| 329 |
Save a coverted parameter |
| 330 |
:param converted_parameters: Dic of all parameters |
| 331 |
:return: |
| 332 |
""" |
| 333 |
for attr in ['maximum_size', 'minimum_size', 'increment_size']: |
| 334 |
if converted_parameters.get(attr) is not None: |
| 335 |
if self.use_rest: |
| 336 |
converted_parameters[attr] = self.convert_to_byte(attr, converted_parameters) |
| 337 |
else: |
| 338 |
converted_parameters[attr] = str(self.convert_to_kb(attr, converted_parameters)) |
| 339 |
return converted_parameters |
| 340 |
|
| 341 |
def convert_to_kb(self, variable, converted_parameters): |
| 342 |
""" |
| 343 |
Convert a number 10m in to its correct KB size |
| 344 |
:param variable: the Parameter we are going to covert |
| 345 |
:param converted_parameters: Dic of all parameters |
| 346 |
:return: |
| 347 |
""" |
| 348 |
value = converted_parameters.get(variable) |
| 349 |
if len(value) < 2: |
| 350 |
self.module.fail_json(msg="%s must start with a number, and must end with a k, m, g or t, found '%s'." % (variable, value)) |
| 351 |
if value[-1] not in ['k', 'm', 'g', 't']: |
| 352 |
self.module.fail_json(msg="%s must end with a k, m, g or t, found %s in %s." % (variable, value[-1], value)) |
| 353 |
try: |
| 354 |
digits = int(value[:-1]) |
| 355 |
except ValueError: |
| 356 |
self.module.fail_json(msg="%s must start with a number, found %s in %s." % (variable, value[:-1], value)) |
| 357 |
return self._size_unit_map[value[-1]] * digits |
| 358 |
|
| 359 |
def convert_to_byte(self, variable, converted_parameters): |
| 360 |
return self.convert_to_kb(variable, converted_parameters) * 1024 |
| 361 |
|
| 362 |
def apply(self): |
| 363 |
current = self.get_volume_autosize() |
| 364 |
converted_parameters = copy.deepcopy(self.parameters) |
| 365 |
converted_parameters = self.modify_to_kb(converted_parameters) |
| 366 |
self.na_helper.get_modified_attributes(current, converted_parameters) |
| 367 |
if self.parameters.get('reset') is True: |
| 368 |
self.na_helper.changed = True |
| 369 |
if self.na_helper.changed and not self.module.check_mode: |
| 370 |
uuid = current.get('uuid') if current else None |
| 371 |
self.modify_volume_autosize(uuid=uuid) |
| 372 |
|
| 373 |
self.module.exit_json(changed=self.na_helper.changed) |
| 374 |
|
| 375 |
|
| 376 |
def main(): |
| 377 |
""" |
| 378 |
Apply volume autosize operations from playbook |
| 379 |
:return: |
| 380 |
""" |
| 381 |
obj = NetAppOntapVolumeAutosize() |
| 382 |
obj.apply() |
| 383 |
|
| 384 |
|
| 385 |
if __name__ == '__main__': |
| 386 |
main() |