Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/lookup/santricity_lun_mapping.py

raw symbols SCIP
1 # (c) 2024, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import (absolute_import, division, print_function)
5 __metaclass__ = type
6
7 DOCUMENTATION = """
8 name: santricity_lun_mapping
9 author:
10 - Nathan Swartz (@swartzn)
11 - Vu Tran (@VuTran007)
12 short_description: NetApp E-Series manage lun mappings
13 description:
14 - Create, delete, or modify mappings between a volume and a targeted host/host+ group.
15 options:
16 array_facts:
17 description:
18 - E-Series storage array facts
19 - Run na_santricity_facts prior to calling
20 required: True
21 type: raw
22 volumes:
23 description:
24 - Volume information returned from santricity_volume lookup plugin which expands
25 type: raw
26 """
27
28 from ansible.plugins.lookup import LookupBase
29 from ansible.errors import AnsibleError
30
31
32 class LookupModule(LookupBase):
33
34 # pylint: disable=arguments-renamed
35 def run(self, array_facts, volumes, **kwargs):
36 if isinstance(array_facts, list):
37 array_facts = array_facts[0]
38
39 if isinstance(volumes, dict): # This means that there is only one volume and volumes was stripped of its list
40 volumes = [volumes]
41
42 if "storage_array_facts" not in array_facts.keys():
43 # Don't throw exceptions unless you want run to terminate!!!
44 # raise AnsibleError("Storage array information not available. Collect facts using na_santricity_facts module.")
45 return list()
46
47 # Remove any absent volumes
48 volumes = [vol for vol in volumes if "state" not in vol or vol["state"] == "present"]
49
50 self.array_facts = array_facts["storage_array_facts"]
51 self.luns_by_target = self.array_facts["netapp_luns_by_target"]
52 self.access_volume_lun = self.array_facts["netapp_default_hostgroup_access_volume_lun"]
53
54 # Search for volumes that have a specified host or host group initiator
55 mapping_info = list()
56 for volume in volumes:
57 if "host" in volume.keys():
58
59 # host initiator is already mapped on the storage system
60 if volume["host"] in self.luns_by_target:
61
62 used_luns = [lun for name, lun in self.luns_by_target[volume["host"]]]
63 for host_group in self.array_facts["netapp_host_groups"]:
64 if volume["host"] == host_group["name"]: # target is an existing host group
65 for host in host_group["hosts"]:
66 used_luns.extend([lun for name, lun in self.luns_by_target[host]])
67 break
68 elif volume["host"] in host_group["hosts"]: # target is an existing host in the host group.
69 used_luns.extend([lun for name, lun in self.luns_by_target[host_group["name"]]])
70 break
71
72 for name, lun in self.luns_by_target[volume["host"]]:
73
74 # Check whether volume is mapped to the expected host
75 if name == volume["name"]:
76 # Check whether lun option differs from existing lun
77 if "lun" in volume and volume["lun"] != lun:
78 self.change_volume_mapping_lun(volume["name"], volume["host"], volume["lun"])
79 lun = volume["lun"]
80
81 if lun in used_luns:
82 raise AnsibleError("Volume [%s] cannot be mapped to host or host group [%s] using lun number %s!"
83 % (name, volume["host"], lun))
84
85 mapping_info.append({"volume": volume["name"], "target": volume["host"], "lun": lun})
86 break
87
88 # Volume has not been mapped to host initiator
89 else:
90
91 # Check whether lun option has been used
92 if "lun" in volume:
93 if volume["lun"] in used_luns:
94 for target in self.array_facts["netapp_luns_by_target"].keys():
95 for mapped_volume, mapped_lun in [entry for entry in self.array_facts["netapp_luns_by_target"][target] if entry]:
96 if volume["lun"] == mapped_lun:
97 if volume["name"] != mapped_volume:
98 raise AnsibleError("Volume [%s] cannot be mapped to host or host group [%s] using lun number %s!"
99 % (volume["name"], volume["host"], volume["lun"]))
100 else: # volume is being remapped with the same lun number
101 self.remove_volume_mapping(mapped_volume, target)
102 lun = volume["lun"]
103 else:
104 lun = self.next_available_lun(used_luns)
105
106 mapping_info.append({"volume": volume["name"], "target": volume["host"], "lun": lun})
107 self.add_volume_mapping(volume["name"], volume["host"], lun)
108
109 else:
110 raise AnsibleError("The host or host group [%s] is not defined!" % volume["host"])
111 else:
112 mapping_info.append({"volume": volume["name"]})
113
114 return mapping_info
115
116 def next_available_lun(self, used_luns):
117 """Find next available lun numbers."""
118 if self.access_volume_lun is not None:
119 used_luns.append(self.access_volume_lun)
120
121 lun = 1
122 while lun in used_luns:
123 lun += 1
124
125 return lun
126
127 def add_volume_mapping(self, name, host, lun):
128 """Add volume mapping to record table (luns_by_target)."""
129 # Find associated group and the groups hosts
130 for host_group in self.array_facts["netapp_host_groups"]:
131
132 if host == host_group["name"]:
133 # add to group
134 self.luns_by_target[host].append([name, lun])
135
136 # add to hosts
137 for hostgroup_host in host_group["hosts"]:
138 self.luns_by_target[hostgroup_host].append([name, lun])
139
140 break
141 else:
142 self.luns_by_target[host].append([name, lun])
143
144 def remove_volume_mapping(self, name, host):
145 """remove volume mapping to record table (luns_by_target)."""
146 # Find associated group and the groups hosts
147 for host_group in self.array_facts["netapp_host_groups"]:
148 if host == host_group["name"]:
149 # add to group
150 for entry in self.luns_by_target[host_group["name"]]:
151 if entry[0] == name:
152 del entry
153 # add to hosts
154 for hostgroup_host in host_group["hosts"]:
155 for entry in self.luns_by_target[hostgroup_host]:
156 if entry[0] == name:
157 del entry
158 break
159 else:
160 for index, entry in enumerate(self.luns_by_target[host]):
161 if entry[0] == name:
162 self.luns_by_target[host].pop(index)
163
164 def change_volume_mapping_lun(self, name, host, lun):
165 """remove volume mapping to record table (luns_by_target)."""
166 self.remove_volume_mapping(name, host)
167 self.add_volume_mapping(name, host, lun)