Debian Codegraph @

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

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