Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/modules/na_santricity_global.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 DOCUMENTATION = """
9 ---
10 module: na_santricity_global
11 short_description: NetApp E-Series manage global settings configuration
12 description:
13 - Allow the user to configure several of the global settings associated with an E-Series storage-system
14 author:
15 - Michael Price (@lmprice)
16 - Nathan Swartz (@swartzn)
17 - Vu Tran (@VuTran007)
18 extends_documentation_fragment:
19 - netapp_eseries.santricity.santricity.santricity_doc
20 options:
21 name:
22 description:
23 - Set the name of the E-Series storage-system
24 - This label/name doesn't have to be unique.
25 - May be up to 30 characters in length.
26 type: str
27 aliases:
28 - label
29 cache_block_size:
30 description:
31 - Size of the cache's block size.
32 - All volumes on the storage system share the same cache space; therefore, the volumes can have only one
33 cache block size.
34 - See M(netapp_eseries.santricity.na_santricity_facts) for available sizes.
35 type: int
36 required: False
37 cache_flush_threshold:
38 description:
39 - This is the percentage threshold of the amount of unwritten data that is allowed to remain on the storage
40 array's cache before flushing.
41 type: int
42 required: False
43 default_host_type:
44 description:
45 - Default host type for the storage system.
46 - Either one of the following names can be specified, Linux DM-MP, VMWare, Windows, Windows Clustered, or a
47 host type index which can be found in M(netapp_eseries.santricity.na_santricity_facts)
48 type: str
49 required: False
50 automatic_load_balancing:
51 description:
52 - Enable automatic load balancing to allow incoming traffic from the hosts to be dynamically managed and
53 balanced across both controllers.
54 - Automatic load balancing requires host connectivity reporting to be enabled.
55 type: str
56 choices:
57 - enabled
58 - disabled
59 required: False
60 host_connectivity_reporting:
61 description:
62 - Enable host connectivity reporting to allow host connections to be monitored for connection and multipath
63 driver problems.
64 - When I(automatic_load_balancing==enabled) then M(netapp_eseries.santricity.host_connectivity_reporting)
65 must be enabled.
66 type: str
67 choices:
68 - enabled
69 - disabled
70 required: False
71 login_banner_message:
72 description:
73 - Text message that appears prior to the login page.
74 - I(login_banner_message=="") will delete any existing banner message.
75 type: str
76 required: False
77 controller_shelf_id:
78 description:
79 - This is the identifier for the drive enclosure containing the controllers.
80 type: int
81 required: false
82 default: 0
83 notes:
84 - Check mode is supported.
85 - This module requires Web Services API v1.3 or newer.
86 """
87
88 EXAMPLES = """
89 - name: Set the storage-system name
90 na_santricity_global:
91 ssid: "1"
92 api_url: "https://192.168.1.100:8443/devmgr/v2"
93 api_username: "admin"
94 api_password: "adminpass"
95 validate_certs: true
96 name: myArrayName
97 cache_block_size: 32768
98 cache_flush_threshold: 80
99 automatic_load_balancing: enabled
100 default_host_type: Linux DM-MP
101 - name: Set the storage-system name
102 na_santricity_global:
103 ssid: "1"
104 api_url: "https://192.168.1.100:8443/devmgr/v2"
105 api_username: "admin"
106 api_password: "adminpass"
107 validate_certs: true
108 name: myOtherArrayName
109 cache_block_size: 8192
110 cache_flush_threshold: 60
111 automatic_load_balancing: disabled
112 default_host_type: 28
113 """
114
115 RETURN = """
116 changed:
117 description: Whether global settings were changed
118 returned: on success
119 type: bool
120 sample: true
121 array_name:
122 description: Current storage array's name
123 returned: on success
124 type: str
125 sample: arrayName
126 automatic_load_balancing:
127 description: Whether automatic load balancing feature has been enabled
128 returned: on success
129 type: str
130 sample: enabled
131 host_connectivity_reporting:
132 description: Whether host connectivity reporting feature has been enabled
133 returned: on success
134 type: str
135 sample: enabled
136 cache_settings:
137 description: Current cache block size and flushing threshold values
138 returned: on success
139 type: dict
140 sample: {"cache_block_size": 32768, "cache_flush_threshold": 80}
141 default_host_type_index:
142 description: Current default host type index
143 returned: on success
144 type: int
145 sample: 28
146 login_banner_message:
147 description: Current banner message
148 returned: on success
149 type: str
150 sample: "Banner message here!"
151 controller_shelf_id:
152 description: Identifier for the drive enclosure containing the controllers.
153 returned: on success
154 type: int
155 sample: 99
156 """
157 import random
158 import sys
159
160 from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule
161 from ansible.module_utils import six
162 from ansible.module_utils._text import to_native
163 try:
164 from ansible.module_utils.ansible_release import __version__ as ansible_version
165 except ImportError:
166 ansible_version = 'unknown'
167
168
169 class NetAppESeriesGlobalSettings(NetAppESeriesModule):
170 MAXIMUM_LOGIN_BANNER_SIZE_BYTES = 5 * 1024
171 LAST_AVAILABLE_CONTROLLER_SHELF_ID = 99
172
173 def __init__(self):
174 version = "02.00.0000.0000"
175 ansible_options = dict(cache_block_size=dict(type="int", required=False),
176 cache_flush_threshold=dict(type="int", required=False),
177 default_host_type=dict(type="str", required=False),
178 automatic_load_balancing=dict(type="str", choices=["enabled", "disabled"], required=False),
179 host_connectivity_reporting=dict(type="str", choices=["enabled", "disabled"], required=False),
180 name=dict(type='str', required=False, aliases=['label']),
181 login_banner_message=dict(type='str', required=False),
182 controller_shelf_id=dict(type="int", required=False, default=0))
183
184 super(NetAppESeriesGlobalSettings, self).__init__(ansible_options=ansible_options,
185 web_services_version=version,
186 supports_check_mode=True)
187 args = self.module.params
188 self.name = args["name"]
189 self.cache_block_size = args["cache_block_size"]
190 self.cache_flush_threshold = args["cache_flush_threshold"]
191 self.host_type_index = args["default_host_type"]
192 self.controller_shelf_id = args["controller_shelf_id"]
193
194 self.login_banner_message = None
195 if args["login_banner_message"] is not None:
196 self.login_banner_message = args["login_banner_message"].rstrip("\n")
197
198 self.autoload_enabled = None
199 if args["automatic_load_balancing"]:
200 self.autoload_enabled = args["automatic_load_balancing"] == "enabled"
201
202 self.host_connectivity_reporting_enabled = None
203 if args["host_connectivity_reporting"]:
204 self.host_connectivity_reporting_enabled = args["host_connectivity_reporting"] == "enabled"
205 elif self.autoload_enabled:
206 self.host_connectivity_reporting_enabled = True
207
208 if self.autoload_enabled and not self.host_connectivity_reporting_enabled:
209 self.module.fail_json(msg="Option automatic_load_balancing requires host_connectivity_reporting to be "
210 "enabled. Array [%s]." % self.ssid)
211
212 self.current_configuration_cache = None
213
214 def get_current_configuration(self, update=False):
215 """Retrieve the current storage array's global configuration."""
216 if self.current_configuration_cache is None or update:
217 self.current_configuration_cache = dict()
218
219 # Get the storage array's capabilities and available options
220 try:
221 rc, capabilities = self.request("storage-systems/%s/capabilities" % self.ssid)
222 self.current_configuration_cache["autoload_capable"] = \
223 "capabilityAutoLoadBalancing" in capabilities["productCapabilities"]
224 self.current_configuration_cache["cache_block_size_options"] = \
225 capabilities["featureParameters"]["cacheBlockSizes"]
226 except Exception as error:
227 self.module.fail_json(msg="Failed to retrieve storage array capabilities. Array [%s]. "
228 "Error [%s]." % (self.ssid, to_native(error)))
229
230 try:
231 rc, host_types = self.request("storage-systems/%s/host-types" % self.ssid)
232 self.current_configuration_cache["host_type_options"] = dict()
233 for host_type in host_types:
234 self.current_configuration_cache["host_type_options"].update({host_type["code"].lower(): host_type["index"]})
235 except Exception as error:
236 self.module.fail_json(msg="Failed to retrieve storage array host options. Array [%s]. "
237 "Error [%s]." % (self.ssid, to_native(error)))
238
239 # Get the current cache settings
240 try:
241 rc, settings = self.request("storage-systems/%s/graph/xpath-filter?query=/sa" % self.ssid)
242 self.current_configuration_cache["cache_settings"] = {
243 "cache_block_size": settings[0]["cache"]["cacheBlkSize"],
244 "cache_flush_threshold": settings[0]["cache"]["demandFlushThreshold"]
245 }
246 self.current_configuration_cache["default_host_type_index"] = settings[0]["defaultHostTypeIndex"]
247 except Exception as error:
248 self.module.fail_json(msg="Failed to retrieve cache settings. Array [%s]. "
249 "Error [%s]." % (self.ssid, to_native(error)))
250
251 try:
252 rc, array_info = self.request("storage-systems/%s" % self.ssid)
253 self.current_configuration_cache["autoload_enabled"] = array_info["autoLoadBalancingEnabled"]
254 self.current_configuration_cache["host_connectivity_reporting_enabled"] = \
255 array_info["hostConnectivityReportingEnabled"]
256 self.current_configuration_cache["name"] = array_info['name']
257 except Exception as error:
258 self.module.fail_json(msg="Failed to determine current configuration. Array [%s]. "
259 "Error [%s]." % (self.ssid, to_native(error)))
260
261 try:
262 rc, login_banner_message = self.request("storage-systems/%s/login-banner?asFile=false" % self.ssid,
263 ignore_errors=True, json_response=False,
264 headers={"Accept": "application/octet-stream", "netapp-client-type": "Ansible-%s" % ansible_version})
265 self.current_configuration_cache["login_banner_message"] = \
266 login_banner_message.decode("utf-8").rstrip("\n")
267 except Exception as error:
268 self.module.fail_json(msg="Failed to determine current login banner message. Array [%s]. "
269 "Error [%s]." % (self.ssid, to_native(error)))
270
271 try:
272 rc, hardware_inventory = self.request("storage-systems/%s/hardware-inventory" % self.ssid)
273 self.current_configuration_cache["controller_shelf_reference"] = \
274 hardware_inventory["trays"][0]["trayRef"]
275 self.current_configuration_cache["controller_shelf_id"] = hardware_inventory["trays"][0]["trayId"]
276 self.current_configuration_cache["used_shelf_ids"] = \
277 [tray["trayId"] for tray in hardware_inventory["trays"]]
278 except Exception as error:
279 self.module.fail_json(msg="Failed to retrieve controller shelf identifier. Array [%s]. "
280 "Error [%s]." % (self.ssid, to_native(error)))
281
282 return self.current_configuration_cache
283
284 def change_cache_block_size_required(self):
285 """Determine whether cache block size change is required."""
286 if self.cache_block_size is None:
287 return False
288
289 current_configuration = self.get_current_configuration()
290 current_available_block_sizes = current_configuration["cache_block_size_options"]
291 if self.cache_block_size not in current_available_block_sizes:
292 self.module.fail_json(msg="Invalid cache block size. Array [%s]. "
293 "Available cache block sizes [%s]." % (self.ssid, current_available_block_sizes))
294
295 return self.cache_block_size != current_configuration["cache_settings"]["cache_block_size"]
296
297 def change_cache_flush_threshold_required(self):
298 """Determine whether cache flush percentage change is required."""
299 if self.cache_flush_threshold is None:
300 return False
301
302 current_configuration = self.get_current_configuration()
303 if self.cache_flush_threshold <= 0 or self.cache_flush_threshold >= 100:
304 self.module.fail_json(msg="Invalid cache flushing threshold, it must be equal to or between 0 and 100. "
305 "Array [%s]" % self.ssid)
306
307 return self.cache_flush_threshold != current_configuration["cache_settings"]["cache_flush_threshold"]
308
309 def change_host_type_required(self):
310 """Determine whether default host type change is required."""
311 if self.host_type_index is None:
312 return False
313
314 current_configuration = self.get_current_configuration()
315 current_available_host_types = current_configuration["host_type_options"]
316 if isinstance(self.host_type_index, str):
317 self.host_type_index = self.host_type_index.lower()
318
319 if self.host_type_index in self.HOST_TYPE_INDEXES.keys():
320 self.host_type_index = self.HOST_TYPE_INDEXES[self.host_type_index]
321 elif self.host_type_index in current_available_host_types.keys():
322 self.host_type_index = current_available_host_types[self.host_type_index]
323
324 if self.host_type_index not in current_available_host_types.values():
325 self.module.fail_json(msg="Invalid host type index! Array [%s]. "
326 "Available host options [%s]." % (self.ssid, current_available_host_types))
327
328 return int(self.host_type_index) != current_configuration["default_host_type_index"]
329
330 def change_autoload_enabled_required(self):
331 """Determine whether automatic load balancing state change is required."""
332 if self.autoload_enabled is None:
333 return False
334
335 change_required = False
336 current_configuration = self.get_current_configuration()
337 if self.autoload_enabled and not current_configuration["autoload_capable"]:
338 self.module.fail_json(msg="Automatic load balancing is not available. Array [%s]." % self.ssid)
339
340 if self.autoload_enabled:
341 if not current_configuration["autoload_enabled"] or \
342 not current_configuration["host_connectivity_reporting_enabled"]:
343 change_required = True
344 elif current_configuration["autoload_enabled"]:
345 change_required = True
346
347 return change_required
348
349 def change_host_connectivity_reporting_enabled_required(self):
350 """Determine whether host connectivity reporting state change is required."""
351 if self.host_connectivity_reporting_enabled is None:
352 return False
353
354 current_configuration = self.get_current_configuration()
355 return self.host_connectivity_reporting_enabled != current_configuration["host_connectivity_reporting_enabled"]
356
357 def change_name_required(self):
358 """Determine whether storage array name change is required."""
359 if self.name is None:
360 return False
361
362 current_configuration = self.get_current_configuration()
363 if self.name and len(self.name) > 30:
364 self.module.fail_json(msg="The provided name is invalid. It must be less than or equal to 30 characters "
365 "in length. Array [%s]" % self.ssid)
366
367 return self.name != current_configuration["name"]
368
369 def change_login_banner_message_required(self):
370 """Determine whether storage array name change is required."""
371 if self.login_banner_message is None:
372 return False
373
374 current_configuration = self.get_current_configuration()
375 if self.login_banner_message and sys.getsizeof(self.login_banner_message) > self.MAXIMUM_LOGIN_BANNER_SIZE_BYTES:
376 self.module.fail_json(msg="The banner message is too long! It must be %s bytes. "
377 "Array [%s]" % (self.MAXIMUM_LOGIN_BANNER_SIZE_BYTES, self.ssid))
378 return self.login_banner_message != current_configuration["login_banner_message"]
379
380 def change_controller_shelf_id_required(self):
381 """Determine whether storage array tray identifier change is required."""
382 current_configuration = self.get_current_configuration()
383 if self.controller_shelf_id is not None and \
384 self.controller_shelf_id != current_configuration["controller_shelf_id"]:
385
386 if self.controller_shelf_id in current_configuration["used_shelf_ids"]:
387 used_shelf_ids = ", ".join([str(id) for id in self.get_current_configuration()["used_shelf_ids"]])
388 self.module.fail_json(msg="The controller_shelf_id is currently being used by another shelf. "
389 "Used Identifiers: [%s]. Array [%s]." % (used_shelf_ids, self.ssid))
390
391 if self.controller_shelf_id < 0 or self.controller_shelf_id > self.LAST_AVAILABLE_CONTROLLER_SHELF_ID:
392 used_shelf_ids = ", ".join([str(id) for id in self.get_current_configuration()["used_shelf_ids"]])
393 self.module.fail_json(msg="The controller_shelf_id must be 0-99 and not already used by another shelf. "
394 "Used Identifiers: [%s]. Array [%s]." % (used_shelf_ids, self.ssid))
395
396 return True
397 return False
398
399 def update_cache_settings(self):
400 """Update cache block size and/or flushing threshold."""
401 current_configuration = self.get_current_configuration()
402 block_size = self.cache_block_size if self.cache_block_size else current_configuration["cache_settings"]["cache_block_size"]
403 threshold = self.cache_flush_threshold if self.cache_flush_threshold else \
404 current_configuration["cache_settings"]["cache_flush_threshold"]
405 try:
406 rc, cache_settings = \
407 self.request("storage-systems/%s/symbol/setSACacheParams?verboseErrorResponse=true" % self.ssid,
408 method="POST",
409 data={"cacheBlkSize": block_size,
410 "demandFlushAmount": threshold,
411 "demandFlushThreshold": threshold})
412 except Exception as error:
413 self.module.fail_json(msg="Failed to set cache settings. Array [%s]. "
414 "Error [%s]." % (self.ssid, to_native(error)))
415
416 def update_host_type(self):
417 """Update default host type."""
418 try:
419 rc, default_host_type = \
420 self.request(
421 "storage-systems/%s/symbol/setStorageArrayProperties?verboseErrorResponse=true" % self.ssid,
422 method="POST",
423 data={"settings": {"defaultHostTypeIndex": self.host_type_index}}
424 )
425 except Exception as error:
426 self.module.fail_json(msg="Failed to set default host type. Array [%s]. "
427 "Error [%s]" % (self.ssid, to_native(error)))
428
429 def update_autoload(self):
430 """Update automatic load balancing state."""
431 current_configuration = self.get_current_configuration()
432 if self.autoload_enabled and not current_configuration["host_connectivity_reporting_enabled"]:
433 try:
434 rc, host_connectivity_reporting = \
435 self.request(
436 "storage-systems/%s/symbol/setHostConnectivityReporting?verboseErrorResponse=true" % self.ssid,
437 method="POST",
438 data={"enableHostConnectivityReporting": self.autoload_enabled}
439 )
440 except Exception as error:
441 self.module.fail_json(msg="Failed to enable host connectivity reporting which is needed for "
442 "automatic load balancing state. Array [%s]. "
443 "Error [%s]." % (self.ssid, to_native(error)))
444
445 try:
446 rc, autoload = \
447 self.request("storage-systems/%s/symbol/setAutoLoadBalancing?verboseErrorResponse=true" % self.ssid,
448 method="POST", data={"enableAutoLoadBalancing": self.autoload_enabled})
449 except Exception as error:
450 self.module.fail_json(msg="Failed to set automatic load balancing state. Array [%s]. "
451 "Error [%s]." % (self.ssid, to_native(error)))
452
453 def update_host_connectivity_reporting_enabled(self):
454 """Update automatic load balancing state."""
455 try:
456 rc, host_connectivity_reporting = \
457 self.request(
458 "storage-systems/%s/symbol/setHostConnectivityReporting?verboseErrorResponse=true" % self.ssid,
459 method="POST",
460 data={"enableHostConnectivityReporting": self.host_connectivity_reporting_enabled}
461 )
462 except Exception as error:
463 self.module.fail_json(msg="Failed to enable host connectivity reporting. Array [%s]. "
464 "Error [%s]." % (self.ssid, to_native(error)))
465
466 def update_name(self):
467 """Update storage array's name."""
468 try:
469 rc, result = \
470 self.request("storage-systems/%s/configuration" % self.ssid, method="POST", data={"name": self.name})
471 except Exception as err:
472 self.module.fail_json(msg="Failed to set the storage array name! Array Id [%s]. "
473 "Error [%s]." % (self.ssid, to_native(err)))
474
475 def update_login_banner_message(self):
476 """Update storage login banner message."""
477 if self.login_banner_message:
478 boundary = "---------------------------" + "".join([str(random.randint(0, 9)) for x in range(27)])
479 data_parts = list()
480 data = None
481
482 if six.PY2: # Generate payload for Python 2
483 newline = "\r\n"
484 data_parts.extend(["--%s" % boundary,
485 'Content-Disposition: form-data; name="file"; filename="banner.txt"',
486 "Content-Type: text/plain",
487 "",
488 self.login_banner_message])
489 data_parts.extend(["--%s--" % boundary, ""])
490 data = newline.join(data_parts)
491
492 else:
493 newline = six.b("\r\n")
494 data_parts.extend([six.b("--%s" % boundary),
495 six.b('Content-Disposition: form-data; name="file"; filename="banner.txt"'),
496 six.b("Content-Type: text/plain"),
497 six.b(""),
498 six.b(self.login_banner_message)])
499 data_parts.extend([six.b("--%s--" % boundary), b""])
500 data = newline.join(data_parts)
501
502 headers = {"Content-Type": "multipart/form-data; boundary=%s" % boundary, "Content-Length": str(len(data))}
503
504 try:
505 rc, result = self.request("storage-systems/%s/login-banner" % self.ssid,
506 method="POST",
507 headers=headers,
508 data=data)
509 except Exception as err:
510 self.module.fail_json(msg="Failed to set the storage system login banner message! Array Id [%s]. "
511 "Error [%s]." % (self.ssid, to_native(err)))
512 else:
513 try:
514 rc, result = self.request("storage-systems/%s/login-banner" % self.ssid, method="DELETE")
515 except Exception as err:
516 self.module.fail_json(msg="Failed to clear the storage system login banner message! Array Id [%s]. "
517 "Error [%s]." % (self.ssid, to_native(err)))
518
519 def update_controller_shelf_id(self):
520 """Update controller shelf tray identifier."""
521 current_configuration = self.get_current_configuration()
522 try:
523 rc, tray = \
524 self.request("storage-systems/%s/symbol/updateTray?verboseErrorResponse=true" % self.ssid,
525 method="POST",
526 data={"ref": current_configuration["controller_shelf_reference"],
527 "trayID": self.controller_shelf_id})
528 except Exception as error:
529 self.module.fail_json(msg="Failed to update controller shelf identifier. Array [%s]. "
530 "Error [%s]." % (self.ssid, to_native(error)))
531
532 def update(self):
533 """Ensure the storage array's global setting are correctly set."""
534 change_required = False
535 if (self.change_autoload_enabled_required() or
536 self.change_cache_block_size_required() or
537 self.change_cache_flush_threshold_required() or
538 self.change_host_type_required() or
539 self.change_name_required() or
540 self.change_host_connectivity_reporting_enabled_required() or
541 self.change_login_banner_message_required() or
542 self.change_controller_shelf_id_required()):
543 change_required = True
544
545 if change_required and not self.module.check_mode:
546 if self.change_autoload_enabled_required():
547 self.update_autoload()
548 if self.change_host_connectivity_reporting_enabled_required():
549 self.update_host_connectivity_reporting_enabled()
550 if self.change_cache_block_size_required() or self.change_cache_flush_threshold_required():
551 self.update_cache_settings()
552 if self.change_host_type_required():
553 self.update_host_type()
554 if self.change_name_required():
555 self.update_name()
556 if self.change_login_banner_message_required():
557 self.update_login_banner_message()
558 if self.change_controller_shelf_id_required():
559 self.update_controller_shelf_id()
560
561 current_configuration = self.get_current_configuration(update=True)
562 automatic_load_balancing = "enabled" if current_configuration["autoload_enabled"] else "disabled"
563 host_connectivity_reporting = "enabled" \
564 if current_configuration["host_connectivity_reporting_enabled"] else "disabled"
565
566 self.module.exit_json(changed=change_required,
567 cache_settings=current_configuration["cache_settings"],
568 default_host_type_index=current_configuration["default_host_type_index"],
569 automatic_load_balancing=automatic_load_balancing,
570 host_connectivity_reporting=host_connectivity_reporting,
571 array_name=current_configuration["name"],
572 login_banner_message=current_configuration["login_banner_message"],
573 controller_shelf_id=current_configuration["controller_shelf_id"])
574
575
576 def main():
577 global_settings = NetAppESeriesGlobalSettings()
578 global_settings.update()
579
580
581 if __name__ == "__main__":
582 main()