Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/modules/na_santricity_hostgroup.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # (c) 2024, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8
9 DOCUMENTATION = """
10 ---
11 module: na_santricity_hostgroup
12 short_description: NetApp E-Series manage array host groups
13 author:
14 - Kevin Hulquest (@hulquest)
15 - Nathan Swartz (@swartzn)
16 - Vu Tran (@VuTran007)
17 description: Create, update or destroy host groups on a NetApp E-Series storage array.
18 extends_documentation_fragment:
19 - netapp_eseries.santricity.santricity.santricity_doc
20 options:
21 state:
22 description:
23 - Whether the specified host group should exist or not.
24 type: str
25 choices: ["present", "absent"]
26 default: present
27 name:
28 description:
29 - Name of the host group to manage
30 type: str
31 required: true
32 hosts:
33 description:
34 - List of host names/labels to add to the group
35 type: list
36 elements: str
37 required: false
38 """
39 EXAMPLES = """
40 - name: Configure Hostgroup
41 na_santricity_hostgroup:
42 ssid: "1"
43 api_url: "https://192.168.1.100:8443/devmgr/v2"
44 api_username: "admin"
45 api_password: "adminpass"
46 validate_certs: true
47 state: present
48 name: example_hostgroup
49 hosts:
50 - host01
51 - host02
52 """
53 RETURN = """
54 clusterRef:
55 description: The unique identification value for this object. Other objects may use this reference value to refer to the cluster.
56 returned: always except when state is absent
57 type: str
58 sample: "3233343536373839303132333100000000000000"
59 confirmLUNMappingCreation:
60 description: If true, indicates that creation of LUN-to-volume mappings should require careful confirmation from the end-user, since such a mapping
61 will alter the volume access rights of other clusters, in addition to this one.
62 returned: always
63 type: bool
64 sample: false
65 hosts:
66 description: A list of the hosts that are part of the host group after all operations.
67 returned: always except when state is absent
68 type: list
69 sample: ["HostA","HostB"]
70 id:
71 description: The id number of the hostgroup
72 returned: always except when state is absent
73 type: str
74 sample: "3233343536373839303132333100000000000000"
75 isSAControlled:
76 description: If true, indicates that I/O accesses from this cluster are subject to the storage array's default LUN-to-volume mappings. If false,
77 indicates that I/O accesses from the cluster are subject to cluster-specific LUN-to-volume mappings.
78 returned: always except when state is absent
79 type: bool
80 sample: false
81 label:
82 description: The user-assigned, descriptive label string for the cluster.
83 returned: always
84 type: str
85 sample: "MyHostGroup"
86 name:
87 description: same as label
88 returned: always except when state is absent
89 type: str
90 sample: "MyHostGroup"
91 protectionInformationCapableAccessMethod:
92 description: This field is true if the host has a PI capable access method.
93 returned: always except when state is absent
94 type: bool
95 sample: true
96 """
97 from ansible.module_utils._text import to_native
98 from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule
99
100
101 class NetAppESeriesHostGroup(NetAppESeriesModule):
102 EXPANSION_TIMEOUT_SEC = 10
103 DEFAULT_DISK_POOL_MINIMUM_DISK_COUNT = 11
104
105 def __init__(self):
106 version = "02.00.0000.0000"
107 ansible_options = dict(
108 state=dict(choices=["present", "absent"], type="str", default="present"),
109 name=dict(required=True, type="str"),
110 hosts=dict(required=False, type="list", elements="str"))
111 super(NetAppESeriesHostGroup, self).__init__(ansible_options=ansible_options,
112 web_services_version=version,
113 supports_check_mode=True)
114
115 args = self.module.params
116 self.state = args["state"]
117 self.name = args["name"]
118 self.hosts_list = args["hosts"]
119
120 self.current_host_group = None
121 self.hosts_cache = None
122
123 @property
124 def hosts(self):
125 """Retrieve a list of host reference identifiers should be associated with the host group."""
126 if self.hosts_cache is None:
127 self.hosts_cache = []
128 existing_hosts = []
129
130 if self.hosts_list:
131 try:
132 rc, existing_hosts = self.request("storage-systems/%s/hosts" % self.ssid)
133 except Exception as error:
134 self.module.fail_json(msg="Failed to retrieve hosts information. Array id [%s]. Error[%s]."
135 % (self.ssid, to_native(error)))
136
137 for host in self.hosts_list:
138 for existing_host in existing_hosts:
139 if host in existing_host["id"] or host.lower() in existing_host["name"].lower():
140 self.hosts_cache.append(existing_host["id"])
141 break
142 else:
143 self.module.fail_json(msg="Expected host does not exist. Array id [%s]. Host [%s]." % (self.ssid, host))
144 self.hosts_cache.sort()
145 return self.hosts_cache
146
147 @property
148 def host_groups(self):
149 """Retrieve a list of existing host groups."""
150 host_groups = []
151 hosts = []
152 try:
153 rc, host_groups = self.request("storage-systems/%s/host-groups" % self.ssid)
154 rc, hosts = self.request("storage-systems/%s/hosts" % self.ssid)
155 except Exception as error:
156 self.module.fail_json(msg="Failed to retrieve host group information. Array id [%s]. Error[%s]."
157 % (self.ssid, to_native(error)))
158
159 host_groups = [{"id": group["clusterRef"], "name": group["name"]} for group in host_groups]
160 for group in host_groups:
161 hosts_ids = []
162 for host in hosts:
163 if group["id"] == host["clusterRef"]:
164 hosts_ids.append(host["hostRef"])
165 group.update({"hosts": hosts_ids})
166
167 return host_groups
168
169 @property
170 def current_hosts_in_host_group(self):
171 """Retrieve the current hosts associated with the current hostgroup."""
172 current_hosts = []
173 for group in self.host_groups:
174 if group["name"] == self.name:
175 current_hosts = group["hosts"]
176 break
177
178 return current_hosts
179
180 def unassign_hosts(self, host_list=None):
181 """Unassign hosts from host group."""
182 if host_list is None:
183 host_list = self.current_host_group["hosts"]
184
185 for host_id in host_list:
186 try:
187 rc, resp = self.request("storage-systems/%s/hosts/%s/move" % (self.ssid, host_id),
188 method="POST", data={"group": "0000000000000000000000000000000000000000"})
189 except Exception as error:
190 self.module.fail_json(msg="Failed to unassign hosts from host group. Array id [%s]. Host id [%s]."
191 " Error[%s]." % (self.ssid, host_id, to_native(error)))
192
193 def delete_host_group(self, unassign_hosts=True):
194 """Delete host group"""
195 if unassign_hosts:
196 self.unassign_hosts()
197
198 try:
199 rc, resp = self.request("storage-systems/%s/host-groups/%s" % (self.ssid, self.current_host_group["id"]), method="DELETE")
200 except Exception as error:
201 self.module.fail_json(msg="Failed to delete host group. Array id [%s]. Error[%s]." % (self.ssid, to_native(error)))
202
203 def create_host_group(self):
204 """Create host group."""
205 data = {"name": self.name, "hosts": self.hosts}
206
207 response = None
208 try:
209 rc, response = self.request("storage-systems/%s/host-groups" % self.ssid, method="POST", data=data)
210 except Exception as error:
211 self.module.fail_json(msg="Failed to create host group. Array id [%s]. Error[%s]." % (self.ssid, to_native(error)))
212
213 return response
214
215 def update_host_group(self):
216 """Update host group."""
217 data = {"name": self.name, "hosts": self.hosts}
218
219 # unassign hosts that should not be part of the hostgroup
220 desired_host_ids = self.hosts
221 for host in self.current_hosts_in_host_group:
222 if host not in desired_host_ids:
223 self.unassign_hosts([host])
224
225 update_response = None
226 try:
227 rc, update_response = self.request("storage-systems/%s/host-groups/%s" % (self.ssid, self.current_host_group["id"]), method="POST", data=data)
228 except Exception as error:
229 self.module.fail_json(msg="Failed to create host group. Array id [%s]. Error[%s]." % (self.ssid, to_native(error)))
230
231 return update_response
232
233 def apply(self):
234 """Apply desired host group state to the storage array."""
235 changes_required = False
236
237 # Search for existing host group match
238 for group in self.host_groups:
239 if group["name"] == self.name:
240 self.current_host_group = group
241 self.current_host_group["hosts"].sort()
242 break
243
244 # Determine whether changes are required
245 if self.state == "present":
246 if self.current_host_group:
247 if self.hosts and self.hosts != self.current_host_group["hosts"]:
248 changes_required = True
249 else:
250 if not self.name:
251 self.module.fail_json(msg="The option name must be supplied when creating a new host group. Array id [%s]." % self.ssid)
252 changes_required = True
253
254 elif self.current_host_group:
255 changes_required = True
256
257 # Apply any necessary changes
258 msg = ""
259 if changes_required and not self.module.check_mode:
260 msg = "No changes required."
261 if self.state == "present":
262 if self.current_host_group:
263 if self.hosts != self.current_host_group["hosts"]:
264 msg = self.update_host_group()
265 else:
266 msg = self.create_host_group()
267
268 elif self.current_host_group:
269 self.delete_host_group()
270 msg = "Host group deleted. Array Id [%s]. Host group [%s]." % (self.ssid, self.current_host_group["name"])
271
272 self.module.exit_json(msg=msg, changed=changes_required)
273
274
275 def main():
276 hostgroup = NetAppESeriesHostGroup()
277 hostgroup.apply()
278
279
280 if __name__ == "__main__":
281 main()