| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: na_santricity_lun_mapping |
| 12 |
author: |
| 13 |
- Kevin Hulquest (@hulquest) |
| 14 |
- Nathan Swartz (@ndswartz) |
| 15 |
short_description: NetApp E-Series manage lun mappings |
| 16 |
description: |
| 17 |
- Create, delete, or modify mappings between a volume and a targeted host/host+ group. |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp_eseries.santricity.santricity.santricity_doc |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Present will ensure the mapping exists, absent will remove the mapping. |
| 24 |
type: str |
| 25 |
required: False |
| 26 |
choices: ["present", "absent"] |
| 27 |
default: "present" |
| 28 |
target: |
| 29 |
description: |
| 30 |
- The name of host or hostgroup you wish to assign to the mapping |
| 31 |
- If omitted, the default hostgroup is used. |
| 32 |
- If the supplied I(volume_name) is associated with a different target, it will be updated to what is supplied here. |
| 33 |
type: str |
| 34 |
required: False |
| 35 |
volume_name: |
| 36 |
description: |
| 37 |
- The name of the volume you wish to include in the mapping. |
| 38 |
- Use ACCESS_VOLUME to reference the in-band access management volume. |
| 39 |
type: str |
| 40 |
required: True |
| 41 |
aliases: |
| 42 |
- volume |
| 43 |
lun: |
| 44 |
description: |
| 45 |
- The LUN value you wish to give the mapping. |
| 46 |
- If the supplied I(volume_name) is associated with a different LUN, it will be updated to what is supplied here. |
| 47 |
- LUN value will be determine by the storage-system when not specified. |
| 48 |
type: int |
| 49 |
required: false |
| 50 |
''' |
| 51 |
|
| 52 |
EXAMPLES = ''' |
| 53 |
--- |
| 54 |
- name: Map volume1 to the host target host1 |
| 55 |
na_santricity_lun_mapping: |
| 56 |
ssid: "1" |
| 57 |
api_url: "https://192.168.1.100:8443/devmgr/v2" |
| 58 |
api_username: "admin" |
| 59 |
api_password: "adminpass" |
| 60 |
validate_certs: true |
| 61 |
state: present |
| 62 |
target: host1 |
| 63 |
volume: volume1 |
| 64 |
- name: Delete the lun mapping between volume1 and host1 |
| 65 |
na_santricity_lun_mapping: |
| 66 |
ssid: "1" |
| 67 |
api_url: "https://192.168.1.100:8443/devmgr/v2" |
| 68 |
api_username: "admin" |
| 69 |
api_password: "adminpass" |
| 70 |
validate_certs: true |
| 71 |
state: absent |
| 72 |
target: host1 |
| 73 |
volume: volume1 |
| 74 |
''' |
| 75 |
RETURN = ''' |
| 76 |
msg: |
| 77 |
description: success of the module |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: Lun mapping is complete |
| 81 |
''' |
| 82 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule |
| 83 |
from ansible.module_utils._text import to_native |
| 84 |
|
| 85 |
|
| 86 |
class NetAppESeriesLunMapping(NetAppESeriesModule): |
| 87 |
def __init__(self): |
| 88 |
ansible_options = dict(state=dict(required=False, choices=["present", "absent"], default="present"), |
| 89 |
target=dict(required=False, default=None), |
| 90 |
volume_name=dict(required=True, aliases=["volume"]), |
| 91 |
lun=dict(type="int", required=False)) |
| 92 |
|
| 93 |
super(NetAppESeriesLunMapping, self).__init__(ansible_options=ansible_options, |
| 94 |
web_services_version="02.00.0000.0000", |
| 95 |
supports_check_mode=True) |
| 96 |
|
| 97 |
args = self.module.params |
| 98 |
self.state = args["state"] == "present" |
| 99 |
self.target = args["target"] if args["target"] else "DEFAULT_HOSTGROUP" |
| 100 |
self.volume = args["volume_name"] if args["volume_name"] != "ACCESS_VOLUME" else "Access" |
| 101 |
self.lun = args["lun"] |
| 102 |
self.check_mode = self.module.check_mode |
| 103 |
self.mapping_info = None |
| 104 |
|
| 105 |
if not self.url.endswith('/'): |
| 106 |
self.url += '/' |
| 107 |
|
| 108 |
def update_mapping_info(self): |
| 109 |
"""Collect the current state of the storage array.""" |
| 110 |
response = None |
| 111 |
try: |
| 112 |
rc, response = self.request("storage-systems/%s/graph" % self.ssid) |
| 113 |
except Exception as error: |
| 114 |
self.module.fail_json(msg="Failed to retrieve storage array graph. Id [%s]. Error [%s]" % (self.ssid, to_native(error))) |
| 115 |
|
| 116 |
|
| 117 |
target_reference = {} |
| 118 |
target_name = {} |
| 119 |
target_type = {} |
| 120 |
|
| 121 |
for host in response["storagePoolBundle"]["host"]: |
| 122 |
target_reference.update({host["hostRef"]: host["name"]}) |
| 123 |
target_name.update({host["name"]: host["hostRef"]}) |
| 124 |
target_type.update({host["name"]: "host"}) |
| 125 |
|
| 126 |
for cluster in response["storagePoolBundle"]["cluster"]: |
| 127 |
|
| 128 |
|
| 129 |
if cluster["name"] == self.target and self.target in target_name.keys(): |
| 130 |
self.module.fail_json(msg="Ambiguous target type: target name is used for both host and group targets! Id [%s]" % self.ssid) |
| 131 |
|
| 132 |
target_reference.update({cluster["clusterRef"]: cluster["name"]}) |
| 133 |
target_name.update({cluster["name"]: cluster["clusterRef"]}) |
| 134 |
target_type.update({cluster["name"]: "group"}) |
| 135 |
|
| 136 |
target_reference.update({"0000000000000000000000000000000000000000": "DEFAULT_HOSTGROUP"}) |
| 137 |
target_name.update({"DEFAULT_HOSTGROUP": "0000000000000000000000000000000000000000"}) |
| 138 |
target_type.update({"DEFAULT_HOSTGROUP": "group"}) |
| 139 |
|
| 140 |
volume_reference = {} |
| 141 |
volume_name = {} |
| 142 |
lun_name = {} |
| 143 |
for volume in response["volume"]: |
| 144 |
volume_reference.update({volume["volumeRef"]: volume["name"]}) |
| 145 |
volume_name.update({volume["name"]: volume["volumeRef"]}) |
| 146 |
if volume["listOfMappings"]: |
| 147 |
lun_name.update({volume["name"]: volume["listOfMappings"][0]["lun"]}) |
| 148 |
for volume in response["highLevelVolBundle"]["thinVolume"]: |
| 149 |
volume_reference.update({volume["volumeRef"]: volume["name"]}) |
| 150 |
volume_name.update({volume["name"]: volume["volumeRef"]}) |
| 151 |
if volume["listOfMappings"]: |
| 152 |
lun_name.update({volume["name"]: volume["listOfMappings"][0]["lun"]}) |
| 153 |
|
| 154 |
volume_name.update({response["sa"]["accessVolume"]["name"]: response["sa"]["accessVolume"]["accessVolumeRef"]}) |
| 155 |
volume_reference.update({response["sa"]["accessVolume"]["accessVolumeRef"]: response["sa"]["accessVolume"]["name"]}) |
| 156 |
|
| 157 |
|
| 158 |
self.mapping_info = dict(lun_mapping=[dict(volume_reference=mapping["volumeRef"], |
| 159 |
map_reference=mapping["mapRef"], |
| 160 |
lun_mapping_reference=mapping["lunMappingRef"], |
| 161 |
lun=mapping["lun"] |
| 162 |
) for mapping in response["storagePoolBundle"]["lunMapping"]], |
| 163 |
volume_by_reference=volume_reference, |
| 164 |
volume_by_name=volume_name, |
| 165 |
lun_by_name=lun_name, |
| 166 |
target_by_reference=target_reference, |
| 167 |
target_by_name=target_name, |
| 168 |
target_type_by_name=target_type) |
| 169 |
|
| 170 |
def get_lun_mapping(self): |
| 171 |
"""Find the matching lun mapping reference. |
| 172 |
|
| 173 |
Returns: tuple(bool, int, int): contains volume match, volume mapping reference and mapping lun |
| 174 |
""" |
| 175 |
target_match = False |
| 176 |
reference = None |
| 177 |
lun = None |
| 178 |
|
| 179 |
self.update_mapping_info() |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if self.lun and any((self.lun == lun_mapping["lun"] and |
| 184 |
self.target == self.mapping_info["target_by_reference"][lun_mapping["map_reference"]] and |
| 185 |
self.volume != self.mapping_info["volume_by_reference"][lun_mapping["volume_reference"]] |
| 186 |
) for lun_mapping in self.mapping_info["lun_mapping"]): |
| 187 |
self.module.fail_json(msg="Option lun value is already in use for target! Array Id [%s]." % self.ssid) |
| 188 |
|
| 189 |
|
| 190 |
if self.state: |
| 191 |
if self.volume not in self.mapping_info["volume_by_name"].keys(): |
| 192 |
self.module.fail_json(msg="Volume does not exist. Id [%s]." % self.ssid) |
| 193 |
if self.target and self.target not in self.mapping_info["target_by_name"].keys(): |
| 194 |
self.module.fail_json(msg="Target does not exist. Id [%s'." % self.ssid) |
| 195 |
|
| 196 |
for lun_mapping in self.mapping_info["lun_mapping"]: |
| 197 |
|
| 198 |
|
| 199 |
if lun_mapping["volume_reference"] == self.mapping_info["volume_by_name"][self.volume]: |
| 200 |
reference = lun_mapping["lun_mapping_reference"] |
| 201 |
lun = lun_mapping["lun"] |
| 202 |
|
| 203 |
|
| 204 |
if (lun_mapping["map_reference"] in self.mapping_info["target_by_reference"].keys() and |
| 205 |
self.mapping_info["target_by_reference"][lun_mapping["map_reference"]] == self.target and |
| 206 |
(self.lun is None or lun == self.lun)): |
| 207 |
target_match = True |
| 208 |
|
| 209 |
return target_match, reference, lun |
| 210 |
|
| 211 |
def update(self): |
| 212 |
"""Execute the changes the require changes on the storage array.""" |
| 213 |
target_match, lun_reference, lun = self.get_lun_mapping() |
| 214 |
update = (self.state and not target_match) or (not self.state and lun_reference) |
| 215 |
|
| 216 |
if update and not self.check_mode: |
| 217 |
try: |
| 218 |
if self.state: |
| 219 |
body = dict() |
| 220 |
target = None if not self.target else self.mapping_info["target_by_name"][self.target] |
| 221 |
if target: |
| 222 |
body.update(dict(targetId=target)) |
| 223 |
if self.lun is not None: |
| 224 |
body.update(dict(lun=self.lun)) |
| 225 |
|
| 226 |
if lun_reference: |
| 227 |
|
| 228 |
rc, response = self.request("storage-systems/%s/volume-mappings/%s/move" % (self.ssid, lun_reference), method="POST", data=body) |
| 229 |
else: |
| 230 |
body.update(dict(mappableObjectId=self.mapping_info["volume_by_name"][self.volume])) |
| 231 |
rc, response = self.request("storage-systems/%s/volume-mappings" % self.ssid, method="POST", data=body) |
| 232 |
|
| 233 |
else: |
| 234 |
rc, response = self.request("storage-systems/%s/volume-mappings/%s" % (self.ssid, lun_reference), method="DELETE") |
| 235 |
except Exception as error: |
| 236 |
self.module.fail_json(msg="Failed to update storage array lun mapping. Id [%s]. Error [%s]" % (self.ssid, to_native(error))) |
| 237 |
|
| 238 |
self.module.exit_json(msg="Lun mapping is complete.", changed=update) |
| 239 |
|
| 240 |
|
| 241 |
def main(): |
| 242 |
mapping = NetAppESeriesLunMapping() |
| 243 |
mapping.update() |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == "__main__": |
| 247 |
main() |