mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Resolve issues with NetApp E-Series LunMapping module (#28294)
* Refactor E-Series LunMapping module to use module_utils Refactor the NetApp E-Series module to utlize the common module_utils and doc_fragments. * Remove the default LUN number By providing a default LUN number, it interferes with the ability of the API to determine the appropriate LUN value. * Fix ignored validate_certs parameter The validate_certs parameter was not being provided to the underlying requests method. This patch resolves the issue by passing the value to all relevant HTTP requests.
This commit is contained in:
parent
530988666a
commit
8515db8588
1 changed files with 50 additions and 96 deletions
|
@ -4,13 +4,13 @@
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
'status': ['preview'],
|
'status': ['preview'],
|
||||||
'supported_by': 'community'}
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: netapp_e_lun_mapping
|
module: netapp_e_lun_mapping
|
||||||
|
@ -19,16 +19,9 @@ short_description: Create or Remove LUN Mappings
|
||||||
description:
|
description:
|
||||||
- Allows for the creation and removal of volume to host mappings for NetApp E-series storage arrays.
|
- Allows for the creation and removal of volume to host mappings for NetApp E-series storage arrays.
|
||||||
version_added: "2.2"
|
version_added: "2.2"
|
||||||
|
extends_documentation_fragment:
|
||||||
|
- netapp.eseries
|
||||||
options:
|
options:
|
||||||
validate_certs:
|
|
||||||
required: false
|
|
||||||
default: true
|
|
||||||
description:
|
|
||||||
- Should https certificates be validated?
|
|
||||||
ssid:
|
|
||||||
description:
|
|
||||||
- "The storage system array identifier."
|
|
||||||
required: False
|
|
||||||
lun:
|
lun:
|
||||||
description:
|
description:
|
||||||
- The LUN number you wish to give the mapping
|
- The LUN number you wish to give the mapping
|
||||||
|
@ -57,19 +50,6 @@ options:
|
||||||
- All parameters I(lun), I(target), I(target_type) and I(volume_name) must still be supplied.
|
- All parameters I(lun), I(target), I(target_type) and I(volume_name) must still be supplied.
|
||||||
required: True
|
required: True
|
||||||
choices: ["present", "absent"]
|
choices: ["present", "absent"]
|
||||||
api_url:
|
|
||||||
description:
|
|
||||||
- "The full API url. Example: http://ENDPOINT:8080/devmgr/v2"
|
|
||||||
- This can optionally be set via an environment variable, API_URL
|
|
||||||
required: False
|
|
||||||
api_username:
|
|
||||||
description:
|
|
||||||
- The username used to authenticate against the API. This can optionally be set via an environment variable, API_USERNAME
|
|
||||||
required: False
|
|
||||||
api_password:
|
|
||||||
description:
|
|
||||||
- The password used to authenticate against the API. This can optionally be set via an environment variable, API_PASSWORD
|
|
||||||
required: False
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
|
@ -96,11 +76,9 @@ msg:
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from ansible.module_utils.api import basic_auth_argument_spec
|
from ansible.module_utils.api import basic_auth_argument_spec
|
||||||
from ansible.module_utils.pycompat24 import get_exception
|
|
||||||
from ansible.module_utils.urls import open_url
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.six.moves.urllib.error import HTTPError
|
from ansible.module_utils.netapp import request, eseries_host_argument_spec
|
||||||
|
from ansible.module_utils.pycompat24 import get_exception
|
||||||
|
|
||||||
HEADERS = {
|
HEADERS = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
@ -108,45 +86,14 @@ HEADERS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def request(url, data=None, headers=None, method='GET', use_proxy=True,
|
def get_host_and_group_map(module, ssid, api_url, user, pwd, validate_certs):
|
||||||
force=False, last_mod_time=None, timeout=10, validate_certs=True,
|
|
||||||
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False):
|
|
||||||
try:
|
|
||||||
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy,
|
|
||||||
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs,
|
|
||||||
url_username=url_username, url_password=url_password, http_agent=http_agent,
|
|
||||||
force_basic_auth=force_basic_auth)
|
|
||||||
except HTTPError:
|
|
||||||
err = get_exception()
|
|
||||||
r = err.fp
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_data = r.read()
|
|
||||||
if raw_data:
|
|
||||||
data = json.loads(raw_data)
|
|
||||||
else:
|
|
||||||
raw_data = None
|
|
||||||
except:
|
|
||||||
if ignore_errors:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise Exception(raw_data)
|
|
||||||
|
|
||||||
resp_code = r.getcode()
|
|
||||||
|
|
||||||
if resp_code >= 400 and not ignore_errors:
|
|
||||||
raise Exception(resp_code, data)
|
|
||||||
else:
|
|
||||||
return resp_code, data
|
|
||||||
|
|
||||||
|
|
||||||
def get_host_and_group_map(module, ssid, api_url, user, pwd):
|
|
||||||
mapping = dict(host=dict(), group=dict())
|
mapping = dict(host=dict(), group=dict())
|
||||||
|
|
||||||
hostgroups = 'storage-systems/%s/host-groups' % ssid
|
hostgroups = 'storage-systems/%s/host-groups' % ssid
|
||||||
groups_url = api_url + hostgroups
|
groups_url = api_url + hostgroups
|
||||||
try:
|
try:
|
||||||
hg_rc, hg_data = request(groups_url, headers=HEADERS, url_username=user, url_password=pwd)
|
hg_rc, hg_data = request(groups_url, headers=HEADERS, url_username=user, url_password=pwd,
|
||||||
|
validate_certs=validate_certs)
|
||||||
except:
|
except:
|
||||||
err = get_exception()
|
err = get_exception()
|
||||||
module.fail_json(msg="Failed to get host groups. Id [%s]. Error [%s]" % (ssid, str(err)))
|
module.fail_json(msg="Failed to get host groups. Id [%s]. Error [%s]" % (ssid, str(err)))
|
||||||
|
@ -157,7 +104,8 @@ def get_host_and_group_map(module, ssid, api_url, user, pwd):
|
||||||
hosts = 'storage-systems/%s/hosts' % ssid
|
hosts = 'storage-systems/%s/hosts' % ssid
|
||||||
hosts_url = api_url + hosts
|
hosts_url = api_url + hosts
|
||||||
try:
|
try:
|
||||||
h_rc, h_data = request(hosts_url, headers=HEADERS, url_username=user, url_password=pwd)
|
h_rc, h_data = request(hosts_url, headers=HEADERS, url_username=user, url_password=pwd,
|
||||||
|
validate_certs=validate_certs)
|
||||||
except:
|
except:
|
||||||
err = get_exception()
|
err = get_exception()
|
||||||
module.fail_json(msg="Failed to get hosts. Id [%s]. Error [%s]" % (ssid, str(err)))
|
module.fail_json(msg="Failed to get hosts. Id [%s]. Error [%s]" % (ssid, str(err)))
|
||||||
|
@ -186,22 +134,22 @@ def get_volume_id(module, data, ssid, name, api_url, user, pwd):
|
||||||
module.fail_json(msg="No volume with the name: %s, was found" % (name))
|
module.fail_json(msg="No volume with the name: %s, was found" % (name))
|
||||||
|
|
||||||
|
|
||||||
def get_hostgroups(module, ssid, api_url, user, pwd):
|
def get_hostgroups(module, ssid, api_url, user, pwd, validate_certs):
|
||||||
groups = "storage-systems/%s/host-groups" % ssid
|
groups = "storage-systems/%s/host-groups" % ssid
|
||||||
url = api_url + groups
|
url = api_url + groups
|
||||||
try:
|
try:
|
||||||
rc, data = request(url, headers=HEADERS, url_username=user, url_password=pwd)
|
rc, data = request(url, headers=HEADERS, url_username=user, url_password=pwd, validate_certs=validate_certs)
|
||||||
return data
|
return data
|
||||||
except Exception:
|
except Exception:
|
||||||
module.fail_json(msg="There was an issue with connecting, please check that your"
|
module.fail_json(msg="There was an issue with connecting, please check that your"
|
||||||
"endpoint is properly defined and your credentials are correct")
|
"endpoint is properly defined and your credentials are correct")
|
||||||
|
|
||||||
|
|
||||||
def get_volumes(module, ssid, api_url, user, pwd, mappable):
|
def get_volumes(module, ssid, api_url, user, pwd, mappable, validate_certs):
|
||||||
volumes = 'storage-systems/%s/%s' % (ssid, mappable)
|
volumes = 'storage-systems/%s/%s' % (ssid, mappable)
|
||||||
url = api_url + volumes
|
url = api_url + volumes
|
||||||
try:
|
try:
|
||||||
rc, data = request(url, url_username=user, url_password=pwd)
|
rc, data = request(url, url_username=user, url_password=pwd, validate_certs=validate_certs)
|
||||||
except Exception:
|
except Exception:
|
||||||
err = get_exception()
|
err = get_exception()
|
||||||
module.fail_json(
|
module.fail_json(
|
||||||
|
@ -209,10 +157,10 @@ def get_volumes(module, ssid, api_url, user, pwd, mappable):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_lun_mappings(ssid, api_url, user, pwd, get_all=None):
|
def get_lun_mappings(ssid, api_url, user, pwd, validate_certs, get_all=None):
|
||||||
mappings = 'storage-systems/%s/volume-mappings' % ssid
|
mappings = 'storage-systems/%s/volume-mappings' % ssid
|
||||||
url = api_url + mappings
|
url = api_url + mappings
|
||||||
rc, data = request(url, url_username=user, url_password=pwd)
|
rc, data = request(url, url_username=user, url_password=pwd, validate_certs=validate_certs)
|
||||||
|
|
||||||
if not get_all:
|
if not get_all:
|
||||||
remove_keys = ('ssid', 'perms', 'lunMappingRef', 'type', 'id')
|
remove_keys = ('ssid', 'perms', 'lunMappingRef', 'type', 'id')
|
||||||
|
@ -224,20 +172,27 @@ def get_lun_mappings(ssid, api_url, user, pwd, get_all=None):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def create_mapping(module, ssid, lun_map, vol_name, api_url, user, pwd):
|
def create_mapping(module, ssid, lun_map, vol_name, api_url, user, pwd, validate_certs):
|
||||||
mappings = 'storage-systems/%s/volume-mappings' % ssid
|
mappings = 'storage-systems/%s/volume-mappings' % ssid
|
||||||
url = api_url + mappings
|
url = api_url + mappings
|
||||||
post_body = json.dumps(dict(
|
|
||||||
mappableObjectId=lun_map['volumeRef'],
|
if lun_map is not None:
|
||||||
targetId=lun_map['mapRef'],
|
post_body = json.dumps(dict(
|
||||||
lun=lun_map['lun']
|
mappableObjectId=lun_map['volumeRef'],
|
||||||
))
|
targetId=lun_map['mapRef'],
|
||||||
|
lun=lun_map['lun']
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
post_body = json.dumps(dict(
|
||||||
|
mappableObjectId=lun_map['volumeRef'],
|
||||||
|
targetId=lun_map['mapRef'],
|
||||||
|
))
|
||||||
|
|
||||||
rc, data = request(url, data=post_body, method='POST', url_username=user, url_password=pwd, headers=HEADERS,
|
rc, data = request(url, data=post_body, method='POST', url_username=user, url_password=pwd, headers=HEADERS,
|
||||||
ignore_errors=True)
|
ignore_errors=True, validate_certs=validate_certs)
|
||||||
|
|
||||||
if rc == 422:
|
if rc == 422 and lun_map['lun'] is not None:
|
||||||
data = move_lun(module, ssid, lun_map, vol_name, api_url, user, pwd)
|
data = move_lun(module, ssid, lun_map, vol_name, api_url, user, pwd, validate_certs)
|
||||||
# module.fail_json(msg="The volume you specified '%s' is already "
|
# module.fail_json(msg="The volume you specified '%s' is already "
|
||||||
# "part of a different LUN mapping. If you "
|
# "part of a different LUN mapping. If you "
|
||||||
# "want to move it to a different host or "
|
# "want to move it to a different host or "
|
||||||
|
@ -246,17 +201,18 @@ def create_mapping(module, ssid, lun_map, vol_name, api_url, user, pwd):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def move_lun(module, ssid, lun_map, vol_name, api_url, user, pwd):
|
def move_lun(module, ssid, lun_map, vol_name, api_url, user, pwd, validate_certs):
|
||||||
lun_id = get_lun_id(module, ssid, lun_map, api_url, user, pwd)
|
lun_id = get_lun_id(module, ssid, lun_map, api_url, user, pwd, validate_certs)
|
||||||
move_lun = "storage-systems/%s/volume-mappings/%s/move" % (ssid, lun_id)
|
move_lun = "storage-systems/%s/volume-mappings/%s/move" % (ssid, lun_id)
|
||||||
url = api_url + move_lun
|
url = api_url + move_lun
|
||||||
post_body = json.dumps(dict(targetId=lun_map['mapRef'], lun=lun_map['lun']))
|
post_body = json.dumps(dict(targetId=lun_map['mapRef'], lun=lun_map['lun']))
|
||||||
rc, data = request(url, data=post_body, method='POST', url_username=user, url_password=pwd, headers=HEADERS)
|
rc, data = request(url, data=post_body, method='POST', url_username=user, url_password=pwd, headers=HEADERS,
|
||||||
|
validate_certs=validate_certs)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_lun_id(module, ssid, lun_mapping, api_url, user, pwd):
|
def get_lun_id(module, ssid, lun_mapping, api_url, user, pwd, validate_certs):
|
||||||
data = get_lun_mappings(ssid, api_url, user, pwd, get_all=True)
|
data = get_lun_mappings(ssid, api_url, user, pwd, validate_certs, get_all=True)
|
||||||
|
|
||||||
for lun_map in data:
|
for lun_map in data:
|
||||||
if lun_map['volumeRef'] == lun_mapping['volumeRef']:
|
if lun_map['volumeRef'] == lun_mapping['volumeRef']:
|
||||||
|
@ -265,25 +221,22 @@ def get_lun_id(module, ssid, lun_mapping, api_url, user, pwd):
|
||||||
module.fail_json(msg="No LUN map found.")
|
module.fail_json(msg="No LUN map found.")
|
||||||
|
|
||||||
|
|
||||||
def remove_mapping(module, ssid, lun_mapping, api_url, user, pwd):
|
def remove_mapping(module, ssid, lun_mapping, api_url, user, pwd, validate_certs):
|
||||||
lun_id = get_lun_id(module, ssid, lun_mapping, api_url, user, pwd)
|
lun_id = get_lun_id(module, ssid, lun_mapping, api_url, user, pwd)
|
||||||
lun_del = "storage-systems/%s/volume-mappings/%s" % (ssid, lun_id)
|
lun_del = "storage-systems/%s/volume-mappings/%s" % (ssid, lun_id)
|
||||||
url = api_url + lun_del
|
url = api_url + lun_del
|
||||||
rc, data = request(url, method='DELETE', url_username=user, url_password=pwd, headers=HEADERS)
|
rc, data = request(url, method='DELETE', url_username=user, url_password=pwd, headers=HEADERS,
|
||||||
|
validate_certs=validate_certs)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = basic_auth_argument_spec()
|
argument_spec = eseries_host_argument_spec()
|
||||||
argument_spec.update(dict(
|
argument_spec.update(dict(
|
||||||
api_username=dict(type='str', required=True),
|
|
||||||
api_password=dict(type='str', required=True, no_log=True),
|
|
||||||
api_url=dict(type='str', required=True),
|
|
||||||
state=dict(required=True, choices=['present', 'absent']),
|
state=dict(required=True, choices=['present', 'absent']),
|
||||||
target=dict(required=False, default=None),
|
target=dict(required=False, default=None),
|
||||||
target_type=dict(required=False, choices=['host', 'group']),
|
target_type=dict(required=False, choices=['host', 'group']),
|
||||||
lun=dict(required=False, type='int', default=0),
|
lun=dict(required=False, type='int'),
|
||||||
ssid=dict(required=False),
|
|
||||||
volume_name=dict(required=True),
|
volume_name=dict(required=True),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -294,6 +247,7 @@ def main():
|
||||||
target_type = module.params['target_type']
|
target_type = module.params['target_type']
|
||||||
lun = module.params['lun']
|
lun = module.params['lun']
|
||||||
ssid = module.params['ssid']
|
ssid = module.params['ssid']
|
||||||
|
validate_certs = module.params['validate_certs']
|
||||||
vol_name = module.params['volume_name']
|
vol_name = module.params['volume_name']
|
||||||
user = module.params['api_username']
|
user = module.params['api_username']
|
||||||
pwd = module.params['api_password']
|
pwd = module.params['api_password']
|
||||||
|
@ -302,8 +256,8 @@ def main():
|
||||||
if not api_url.endswith('/'):
|
if not api_url.endswith('/'):
|
||||||
api_url += '/'
|
api_url += '/'
|
||||||
|
|
||||||
volume_map = get_volumes(module, ssid, api_url, user, pwd, "volumes")
|
volume_map = get_volumes(module, ssid, api_url, user, pwd, "volumes", validate_certs)
|
||||||
thin_volume_map = get_volumes(module, ssid, api_url, user, pwd, "thin-volumes")
|
thin_volume_map = get_volumes(module, ssid, api_url, user, pwd, "thin-volumes", validate_certs)
|
||||||
volref = None
|
volref = None
|
||||||
|
|
||||||
for vol in volume_map:
|
for vol in volume_map:
|
||||||
|
@ -318,7 +272,7 @@ def main():
|
||||||
if not volref:
|
if not volref:
|
||||||
module.fail_json(changed=False, msg="No volume with the name %s was found" % vol_name)
|
module.fail_json(changed=False, msg="No volume with the name %s was found" % vol_name)
|
||||||
|
|
||||||
host_and_group_mapping = get_host_and_group_map(module, ssid, api_url, user, pwd)
|
host_and_group_mapping = get_host_and_group_map(module, ssid, api_url, user, pwd, validate_certs)
|
||||||
|
|
||||||
desired_lun_mapping = dict(
|
desired_lun_mapping = dict(
|
||||||
mapRef=host_and_group_mapping[target_type][target],
|
mapRef=host_and_group_mapping[target_type][target],
|
||||||
|
@ -326,18 +280,18 @@ def main():
|
||||||
volumeRef=volref
|
volumeRef=volref
|
||||||
)
|
)
|
||||||
|
|
||||||
lun_mappings = get_lun_mappings(ssid, api_url, user, pwd)
|
lun_mappings = get_lun_mappings(ssid, api_url, user, pwd, validate_certs)
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if desired_lun_mapping in lun_mappings:
|
if desired_lun_mapping in lun_mappings:
|
||||||
module.exit_json(changed=False, msg="Mapping exists")
|
module.exit_json(changed=False, msg="Mapping exists")
|
||||||
else:
|
else:
|
||||||
result = create_mapping(module, ssid, desired_lun_mapping, vol_name, api_url, user, pwd)
|
result = create_mapping(module, ssid, desired_lun_mapping, vol_name, api_url, user, pwd, validate_certs)
|
||||||
module.exit_json(changed=True, **result)
|
module.exit_json(changed=True, **result)
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
if desired_lun_mapping in lun_mappings:
|
if desired_lun_mapping in lun_mappings:
|
||||||
result = remove_mapping(module, ssid, desired_lun_mapping, api_url, user, pwd)
|
result = remove_mapping(module, ssid, desired_lun_mapping, api_url, user, pwd, validate_certs)
|
||||||
module.exit_json(changed=True, msg="Mapping removed")
|
module.exit_json(changed=True, msg="Mapping removed")
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=False, msg="Mapping absent")
|
module.exit_json(changed=False, msg="Mapping absent")
|
||||||
|
|
Loading…
Add table
Reference in a new issue