mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
New parameters and bugfixes for na_ontap_volume (#49511)
* add changes * Fixed documentation * Review comment
This commit is contained in:
parent
916bf5acc7
commit
c62a6988b1
2 changed files with 578 additions and 35 deletions
|
@ -21,7 +21,6 @@ extends_documentation_fragment:
|
|||
- netapp.na_ontap
|
||||
version_added: '2.6'
|
||||
author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com>
|
||||
|
||||
description:
|
||||
- Create or destroy or modify volumes on NetApp ONTAP.
|
||||
|
||||
|
@ -86,6 +85,7 @@ options:
|
|||
junction_path:
|
||||
description:
|
||||
- Junction path of the volume.
|
||||
- To unmount, use junction path C('').
|
||||
|
||||
space_guarantee:
|
||||
description:
|
||||
|
@ -114,6 +114,19 @@ options:
|
|||
- Allows a storage efficiency policy to be set on volume creation.
|
||||
version_added: '2.7'
|
||||
|
||||
unix_permissions:
|
||||
description:
|
||||
- Unix permission bits in octal or symbolic format.
|
||||
- For example, 0 is equivalent to ------------, 777 is equivalent to ---rwxrwxrwx,both formats are accepted.
|
||||
- The valid octal value ranges between 0 and 777 inclusive.
|
||||
version_added: '2.8'
|
||||
|
||||
snapshot_policy:
|
||||
description:
|
||||
- The name of the snapshot policy.
|
||||
- the default policy name is 'default'.
|
||||
version_added: '2.8'
|
||||
|
||||
'''
|
||||
|
||||
EXAMPLES = """
|
||||
|
@ -202,6 +215,8 @@ class NetAppOntapVolume(object):
|
|||
default='mixed'),
|
||||
encrypt=dict(required=False, type='bool', default=False),
|
||||
efficiency_policy=dict(required=False, type='str'),
|
||||
unix_permissions=dict(required=False, type='str'),
|
||||
snapshot_policy=dict(required=False, type='str')
|
||||
))
|
||||
self.module = AnsibleModule(
|
||||
argument_spec=self.argument_spec,
|
||||
|
@ -249,7 +264,6 @@ class NetAppOntapVolume(object):
|
|||
Return details about the volume
|
||||
:param:
|
||||
name : Name of the volume
|
||||
|
||||
:return: Details about the volume. None if not found.
|
||||
:rtype: dict
|
||||
"""
|
||||
|
@ -260,45 +274,42 @@ class NetAppOntapVolume(object):
|
|||
if volume_get_iter.get_child_by_name('num-records') and \
|
||||
int(volume_get_iter.get_child_content('num-records')) > 0:
|
||||
|
||||
volume_attributes = volume_get_iter.get_child_by_name(
|
||||
'attributes-list').get_child_by_name(
|
||||
'volume-attributes')
|
||||
# Get volume's current size
|
||||
volume_space_attributes = volume_attributes.get_child_by_name(
|
||||
'volume-space-attributes')
|
||||
current_size = int(volume_space_attributes.get_child_content('size'))
|
||||
|
||||
volume_attributes = volume_get_iter['attributes-list']['volume-attributes']
|
||||
volume_space_attributes = volume_attributes['volume-space-attributes']
|
||||
volume_state_attributes = volume_attributes['volume-state-attributes']
|
||||
volume_id_attributes = volume_attributes['volume-id-attributes']
|
||||
volume_export_attributes = volume_attributes['volume-export-attributes']
|
||||
volume_security_unix_attributes = volume_attributes['volume-security-attributes']['volume-security-unix-attributes']
|
||||
volume_snapshot_attributes = volume_attributes['volume-snapshot-attributes']
|
||||
# Get volume's state (online/offline)
|
||||
volume_state_attributes = volume_attributes.get_child_by_name(
|
||||
'volume-state-attributes')
|
||||
current_state = volume_state_attributes.get_child_content('state')
|
||||
volume_id_attributes = volume_attributes.get_child_by_name(
|
||||
'volume-id-attributes')
|
||||
aggregate_name = volume_id_attributes.get_child_content(
|
||||
'containing-aggregate-name')
|
||||
volume_export_attributes = volume_attributes.get_child_by_name(
|
||||
'volume-export-attributes')
|
||||
policy = volume_export_attributes.get_child_content('policy')
|
||||
space_guarantee = volume_space_attributes.get_child_content(
|
||||
'space-guarantee')
|
||||
|
||||
current_state = volume_state_attributes['state']
|
||||
is_online = (current_state == "online")
|
||||
|
||||
return_value = {
|
||||
'name': vol_name,
|
||||
'size': current_size,
|
||||
'size': int(volume_space_attributes['size']),
|
||||
'is_online': is_online,
|
||||
'aggregate_name': aggregate_name,
|
||||
'policy': policy,
|
||||
'space_guarantee': space_guarantee,
|
||||
'policy': volume_export_attributes['policy'],
|
||||
'space_guarantee': volume_space_attributes['space-guarantee'],
|
||||
'unix_permissions': volume_security_unix_attributes['permissions'],
|
||||
'snapshot_policy': volume_snapshot_attributes['snapshot-policy']
|
||||
|
||||
}
|
||||
if volume_id_attributes.get_child_by_name('containing-aggregate-name'):
|
||||
return_value['aggregate_name'] = volume_id_attributes['containing-aggregate-name']
|
||||
else:
|
||||
return_value['aggregate_name'] = None
|
||||
if volume_id_attributes.get_child_by_name('junction-path'):
|
||||
return_value['junction_path'] = volume_id_attributes['junction-path']
|
||||
else:
|
||||
return_value['junction_path'] = ''
|
||||
|
||||
return return_value
|
||||
|
||||
def create_volume(self):
|
||||
'''Create ONTAP volume'''
|
||||
if self.parameters.get('aggregate_name') is None:
|
||||
self.module.fail_json(msg='Error provisioning volume %s: \
|
||||
aggregate_name is required'
|
||||
self.module.fail_json(msg='Error provisioning volume %s: aggregate_name is required'
|
||||
% self.parameters['name'])
|
||||
options = {'volume': self.parameters['name'],
|
||||
'containing-aggr-name': self.parameters['aggregate_name'],
|
||||
|
@ -315,6 +326,10 @@ class NetAppOntapVolume(object):
|
|||
options['space-reserve'] = self.parameters['space_guarantee']
|
||||
if self.parameters.get('volume_security_style'):
|
||||
options['volume-security-style'] = self.parameters['volume_security_style']
|
||||
if self.parameters.get('unix_permissions'):
|
||||
options['unix-permissions'] = self.parameters['unix_permissions']
|
||||
if self.parameters.get('snapshot_policy'):
|
||||
options['snapshot-policy'] = self.parameters['snapshot_policy']
|
||||
volume_create = netapp_utils.zapi.NaElement.create_node_with_children('volume-create', **options)
|
||||
try:
|
||||
self.server.invoke_successfully(volume_create,
|
||||
|
@ -426,7 +441,7 @@ class NetAppOntapVolume(object):
|
|||
% (self.parameters['name'], state, to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
|
||||
def volume_modify_policy_space(self):
|
||||
def volume_modify_attributes(self):
|
||||
"""
|
||||
modify volume parameter 'policy' or 'space_guarantee'
|
||||
"""
|
||||
|
@ -445,6 +460,19 @@ class NetAppOntapVolume(object):
|
|||
vol_space_attributes.add_new_child(
|
||||
'space-guarantee', self.parameters['space_guarantee'])
|
||||
vol_mod_attributes.add_child_elem(vol_space_attributes)
|
||||
if self.parameters.get('unix_permissions'):
|
||||
vol_unix_permissions_attributes = netapp_utils.zapi.NaElement(
|
||||
'volume-security-unix-attributes')
|
||||
vol_unix_permissions_attributes.add_new_child('permissions', self.parameters['unix_permissions'])
|
||||
vol_security_attributes = netapp_utils.zapi.NaElement(
|
||||
'volume-security-attributes')
|
||||
vol_security_attributes.add_child_elem(vol_unix_permissions_attributes)
|
||||
vol_mod_attributes.add_child_elem(vol_security_attributes)
|
||||
if self.parameters.get('snapshot_policy'):
|
||||
vol_snapshot_policy_attributes = netapp_utils.zapi.NaElement(
|
||||
'volume-snapshot-attributes')
|
||||
vol_snapshot_policy_attributes.add_new_child('snapshot-policy', self.parameters['snapshot_policy'])
|
||||
vol_mod_attributes.add_child_elem(vol_snapshot_policy_attributes)
|
||||
attributes.add_child_elem(vol_mod_attributes)
|
||||
query = netapp_utils.zapi.NaElement('query')
|
||||
vol_query_attributes = netapp_utils.zapi.NaElement('volume-attributes')
|
||||
|
@ -457,7 +485,7 @@ class NetAppOntapVolume(object):
|
|||
try:
|
||||
result = self.server.invoke_successfully(vol_mod_iter, enable_tunneling=True)
|
||||
failures = result.get_child_by_name('failure-list')
|
||||
# handle error if modify space or policy parameter fails
|
||||
# handle error if modify space, policy, or unix-permissions parameter fails
|
||||
if failures is not None and failures.get_child_by_name('volume-modify-iter-info') is not None:
|
||||
error_msg = failures.get_child_by_name('volume-modify-iter-info').get_child_content('error-message')
|
||||
self.module.fail_json(msg="Error modifying volume %s: %s"
|
||||
|
@ -469,16 +497,88 @@ class NetAppOntapVolume(object):
|
|||
% (self.parameters['name'], to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
|
||||
def volume_mount(self):
|
||||
"""
|
||||
Mount an existing volume in specified junction_path
|
||||
:return: None
|
||||
"""
|
||||
vol_mount = netapp_utils.zapi.NaElement('volume-mount')
|
||||
vol_mount.add_new_child('volume-name', self.parameters['name'])
|
||||
vol_mount.add_new_child('junction-path', self.parameters['junction_path'])
|
||||
try:
|
||||
self.server.invoke_successfully(vol_mount, enable_tunneling=True)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg='Error mounting volume %s on path %s: %s'
|
||||
% (self.parameters['name'], self.parameters['junction_path'],
|
||||
to_native(error)), exception=traceback.format_exc())
|
||||
|
||||
def volume_unmount(self):
|
||||
"""
|
||||
Unmount an existing volume
|
||||
:return: None
|
||||
"""
|
||||
vol_unmount = netapp_utils.zapi.NaElement.create_node_with_children(
|
||||
'volume-unmount', **{'volume-name': self.parameters['name']})
|
||||
try:
|
||||
self.server.invoke_successfully(vol_unmount, enable_tunneling=True)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg='Error unmounting volume %s: %s'
|
||||
% (self.parameters['name'], to_native(error)), exception=traceback.format_exc())
|
||||
|
||||
def modify_volume(self, modify):
|
||||
for attribute in modify.keys():
|
||||
if attribute == 'size':
|
||||
self.resize_volume()
|
||||
elif attribute == 'is_online':
|
||||
if attribute == 'is_online':
|
||||
self.change_volume_state()
|
||||
elif attribute == 'aggregate_name':
|
||||
if attribute == 'aggregate_name':
|
||||
self.move_volume()
|
||||
else:
|
||||
self.volume_modify_policy_space()
|
||||
if attribute in ['space_guarantee', 'policy', 'unix_permissions', 'snapshot_policy']:
|
||||
self.volume_modify_attributes()
|
||||
if attribute == 'junction_path':
|
||||
if modify.get('junction_path') == '':
|
||||
self.volume_unmount()
|
||||
else:
|
||||
self.volume_mount()
|
||||
|
||||
def compare_chmod_value(self, current):
|
||||
"""
|
||||
compare current unix_permissions to desire unix_permissions.
|
||||
:return: True if the same, False it not the same or desire unix_permissions is not valid.
|
||||
"""
|
||||
desire = self.parameters
|
||||
if current is None:
|
||||
return False
|
||||
octal_value = ''
|
||||
unix_permissions = desire['unix_permissions']
|
||||
if unix_permissions.isdigit():
|
||||
return int(current['unix_permissions']) == int(unix_permissions)
|
||||
else:
|
||||
if len(unix_permissions) != 12:
|
||||
return False
|
||||
if unix_permissions[:3] != '---':
|
||||
return False
|
||||
for i in range(3, len(unix_permissions), 3):
|
||||
if unix_permissions[i] not in ['r', '-'] or unix_permissions[i + 1] not in ['w', '-']\
|
||||
or unix_permissions[i + 2] not in ['x', '-']:
|
||||
return False
|
||||
group_permission = self.char_to_octal(unix_permissions[i:i + 3])
|
||||
octal_value += str(group_permission)
|
||||
return int(current['unix_permissions']) == int(octal_value)
|
||||
|
||||
def char_to_octal(self, chars):
|
||||
"""
|
||||
:param chars: Characters to be converted into octal values.
|
||||
:return: octal value of the individual group permission.
|
||||
"""
|
||||
total = 0
|
||||
if chars[0] == 'r':
|
||||
total += 4
|
||||
if chars[1] == 'w':
|
||||
total += 2
|
||||
if chars[2] == 'x':
|
||||
total += 1
|
||||
return total
|
||||
|
||||
def apply(self):
|
||||
'''Call create/modify/delete operations'''
|
||||
|
@ -489,6 +589,11 @@ class NetAppOntapVolume(object):
|
|||
rename = self.na_helper.is_rename_action(self.get_volume(self.parameters['from_name']), current)
|
||||
else:
|
||||
cd_action = self.na_helper.get_cd_action(current, self.parameters)
|
||||
if self.parameters.get('unix_permissions'):
|
||||
# current stores unix_permissions' numeric value.
|
||||
# unix_permission in self.parameter can be either numeric or character.
|
||||
if self.compare_chmod_value(current):
|
||||
del self.parameters['unix_permissions']
|
||||
modify = self.na_helper.get_modified_attributes(current, self.parameters)
|
||||
if self.na_helper.changed:
|
||||
if self.module.check_mode:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue