Bug fixes for ontap_export_policy.py (#44194)

* Adding ontap_export_policy.py Module

* fix issues
pull/44385/head
Chris Archibald 6 years ago committed by ansibot
parent 1d0dede9de
commit b83565958e

@ -17,7 +17,7 @@ short_description: Manage NetApp ONTAP export-policy
extends_documentation_fragment: extends_documentation_fragment:
- netapp.na_ontap - netapp.na_ontap
version_added: '2.6' version_added: '2.6'
author: Archana Ganesan (garchana@netapp.com), Suhas Bangalore Shekar (bsuhas@netapp.com) author: NetApp Ansible Team (ng-ansibleteam@netapp.com)
description: description:
- Create or destroy or rename export-policies on ONTAP - Create or destroy or rename export-policies on ONTAP
options: options:
@ -30,9 +30,10 @@ options:
description: description:
- The name of the export-policy to manage. - The name of the export-policy to manage.
required: true required: true
new_name: from_name:
description: description:
- The name of the export-policy to be renamed. - The name of the export-policy to be renamed.
version_added: '2.7'
vserver: vserver:
description: description:
- Name of the vserver to use. - Name of the vserver to use.
@ -50,9 +51,9 @@ EXAMPLES = """
- name: Rename Export Policy - name: Rename Export Policy
na_ontap_export_policy: na_ontap_export_policy:
action: present action: present
name: ansiblePolicyName from_name: ansiblePolicyName
vserver: vs_hack vserver: vs_hack
new_name: newPolicyName name: newPolicyName
hostname: "{{ netapp_hostname }}" hostname: "{{ netapp_hostname }}"
username: "{{ netapp_username }}" username: "{{ netapp_username }}"
password: "{{ netapp_password }}" password: "{{ netapp_password }}"
@ -88,7 +89,7 @@ class NetAppONTAPExportPolicy(object):
self.argument_spec.update(dict( self.argument_spec.update(dict(
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
name=dict(required=True, type='str'), name=dict(required=True, type='str'),
new_name=dict(required=False, type='str', default=None), from_name=dict(required=False, type='str', default=None),
vserver=dict(required=False, type='str') vserver=dict(required=False, type='str')
)) ))
self.module = AnsibleModule( self.module = AnsibleModule(
@ -102,7 +103,7 @@ class NetAppONTAPExportPolicy(object):
# set up state variables # set up state variables
self.state = parameters['state'] self.state = parameters['state']
self.name = parameters['name'] self.name = parameters['name']
self.new_name = parameters['new_name'] self.from_name = parameters['from_name']
self.vserver = parameters['vserver'] self.vserver = parameters['vserver']
if HAS_NETAPP_LIB is False: if HAS_NETAPP_LIB is False:
@ -110,7 +111,7 @@ class NetAppONTAPExportPolicy(object):
else: else:
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.vserver) self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.vserver)
def get_export_policy(self): def get_export_policy(self, name=None):
""" """
Return details about the export-policy Return details about the export-policy
:param: :param:
@ -118,9 +119,11 @@ class NetAppONTAPExportPolicy(object):
:return: Details about the export-policy. None if not found. :return: Details about the export-policy. None if not found.
:rtype: dict :rtype: dict
""" """
if name is None:
name = self.name
export_policy_iter = netapp_utils.zapi.NaElement('export-policy-get-iter') export_policy_iter = netapp_utils.zapi.NaElement('export-policy-get-iter')
export_policy_info = netapp_utils.zapi.NaElement('export-policy-info') export_policy_info = netapp_utils.zapi.NaElement('export-policy-info')
export_policy_info.add_new_child('policy-name', self.name) export_policy_info.add_new_child('policy-name', name)
query = netapp_utils.zapi.NaElement('query') query = netapp_utils.zapi.NaElement('query')
query.add_child_elem(export_policy_info) query.add_child_elem(export_policy_info)
export_policy_iter.add_child_elem(query) export_policy_iter.add_child_elem(query)
@ -169,8 +172,8 @@ class NetAppONTAPExportPolicy(object):
Rename the export-policy. Rename the export-policy.
""" """
export_policy_rename = netapp_utils.zapi.NaElement.create_node_with_children( export_policy_rename = netapp_utils.zapi.NaElement.create_node_with_children(
'export-policy-rename', **{'policy-name': self.name, 'export-policy-rename', **{'policy-name': self.from_name,
'new-policy-name': self.new_name}) 'new-policy-name': self.name})
try: try:
self.server.invoke_successfully(export_policy_rename, self.server.invoke_successfully(export_policy_rename,
enable_tunneling=True) enable_tunneling=True)
@ -190,29 +193,29 @@ class NetAppONTAPExportPolicy(object):
export_policy_details = self.get_export_policy() export_policy_details = self.get_export_policy()
if export_policy_details: if export_policy_details:
export_policy_exists = True export_policy_exists = True
if self.state == 'present': if self.state == 'absent': # delete
if self.new_name is not None:
if self.new_name != self.name and self.new_name != export_policy_details['policy-name']: # rename
changed = True
rename_flag = True
elif self.state == 'absent': # delete
changed = True changed = True
else: else:
if self.state == 'present': # create if self.state == 'present': # create or rename
changed = True if self.from_name is not None:
if self.get_export_policy(self.from_name):
changed = True
rename_flag = True
else:
self.module.fail_json(msg='Error renaming export-policy %s: does not exists' % self.from_name)
else: # create
changed = True
if changed: if changed:
if self.module.check_mode: if self.module.check_mode:
pass pass
else: else:
if self.state == 'present': # execute create if self.state == 'present': # execute create or rename_export_policy
if not export_policy_exists: if rename_flag:
self.rename_export_policy()
else:
self.create_export_policy() self.create_export_policy()
else: # execute rename
if rename_flag:
self.rename_export_policy()
elif self.state == 'absent': # execute delete elif self.state == 'absent': # execute delete
self.delete_export_policy() self.delete_export_policy()
self.module.exit_json(changed=changed) self.module.exit_json(changed=changed)

Loading…
Cancel
Save