Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_vexata.py

raw
1 #
2 # Copyright (c) 2019, Sandeep Kasargod <[email protected]>
3 # Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
4 # SPDX-License-Identifier: BSD-2-Clause
5
6 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
7 # Do not use this from other collections or standalone plugins/modules!
8
9 from __future__ import annotations
10
11 import typing as t
12
13 HAS_VEXATAPI = True
14 try:
15 from vexatapi.vexata_api_proxy import VexataAPIProxy
16 except ImportError:
17 HAS_VEXATAPI = False
18
19 from ansible.module_utils.basic import env_fallback
20
21 if t.TYPE_CHECKING:
22 from ansible.module_utils.basic import AnsibleModule
23
24
25 VXOS_VERSION = None
26
27
28 def get_version(iocs_json) -> tuple[int, ...]:
29 if not iocs_json:
30 raise Exception("Invalid IOC json")
31 active = next((x for x in iocs_json if x["mgmtRole"]), None)
32 if active is None:
33 raise Exception("Unable to detect active IOC")
34 ver = active["swVersion"]
35 if ver[0] != "v":
36 raise Exception("Illegal version string")
37 ver = ver[1 : ver.find("-")]
38 ver = map(int, ver.split("."))
39 return tuple(ver)
40
41
42 def get_array(module: AnsibleModule):
43 """Return storage array object or fail"""
44 global VXOS_VERSION
45 array = module.params["array"]
46 user = module.params.get("user", None)
47 password = module.params.get("password", None)
48 validate = module.params.get("validate_certs")
49
50 if not HAS_VEXATAPI:
51 module.fail_json(msg="vexatapi library is required for this module. To install, use `pip install vexatapi`")
52
53 if user and password:
54 system = VexataAPIProxy(array, user, password, verify_cert=validate)
55 else:
56 module.fail_json(
57 msg="The user/password are required to be passed in to "
58 "the module as arguments or by setting the "
59 "VEXATA_USER and VEXATA_PASSWORD environment variables."
60 )
61 try:
62 if system.test_connection():
63 VXOS_VERSION = get_version(system.iocs())
64 return system
65 else:
66 module.fail_json(msg="Test connection to array failed.")
67 except Exception as e:
68 module.fail_json(msg=f"Vexata API access failed: {e}")
69
70
71 def argument_spec() -> dict[str, t.Any]:
72 """Return standard base dictionary used for the argument_spec argument in AnsibleModule"""
73 return dict(
74 array=dict(type="str", required=True),
75 user=dict(type="str", fallback=(env_fallback, ["VEXATA_USER"])),
76 password=dict(type="str", no_log=True, fallback=(env_fallback, ["VEXATA_PASSWORD"])),
77 validate_certs=dict(type="bool", required=False, default=False),
78 )
79
80
81 def required_together() -> list[list[str]]:
82 """Return the default list used for the required_together argument to AnsibleModule"""
83 return [["user", "password"]]
84
85
86 def size_to_MiB(size: str) -> int:
87 """Convert a '<integer>[MGT]' string to MiB, return -1 on error."""
88 quant = size[:-1]
89 exponent = size[-1]
90 if not quant.isdigit() or exponent not in "MGT":
91 return -1
92 quant_int = int(quant)
93 if exponent == "G":
94 quant_int <<= 10
95 elif exponent == "T":
96 quant_int <<= 20
97 return quant_int