Debian Codegraph @

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

raw
1 # Copyright (C) 2018 IBM CORPORATION
2 # Author(s): Tzur Eliyahu <[email protected]>
3 #
4 # Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
5 # SPDX-License-Identifier: BSD-2-Clause
6
7 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
8 # Do not use this from other collections or standalone plugins/modules!
9
10 from __future__ import annotations
11
12 import traceback
13 import typing as t
14 from functools import wraps
15
16 from ansible.module_utils.basic import missing_required_lib
17
18 if t.TYPE_CHECKING:
19 from ansible.module_utils.basic import AnsibleModule
20
21 PYXCLI_INSTALLED = True
22 PYXCLI_IMP_ERR = None
23 try:
24 from pyxcli import client, errors
25 except ImportError:
26 PYXCLI_IMP_ERR = traceback.format_exc()
27 PYXCLI_INSTALLED = False
28
29 AVAILABLE_PYXCLI_FIELDS = [
30 "pool",
31 "size",
32 "snapshot_size",
33 "domain",
34 "perf_class",
35 "vol",
36 "iscsi_chap_name",
37 "iscsi_chap_secret",
38 "cluster",
39 "host",
40 "lun",
41 "override",
42 "fcaddress",
43 "iscsi_name",
44 "max_dms",
45 "max_cgs",
46 "ldap_id",
47 "max_mirrors",
48 "max_pools",
49 "max_volumes",
50 "hard_capacity",
51 "soft_capacity",
52 ]
53
54
55 def xcli_wrapper(func):
56 """Catch xcli errors and return a proper message"""
57
58 @wraps(func)
59 def wrapper(module: AnsibleModule, *args, **kwargs):
60 try:
61 return func(module, *args, **kwargs)
62 except errors.CommandExecutionError as e:
63 module.fail_json(msg=f"{e}")
64
65 return wrapper
66
67
68 @xcli_wrapper
69 def connect_ssl(module: AnsibleModule):
70 endpoints = module.params["endpoints"]
71 username = module.params["username"]
72 password = module.params["password"]
73 if not (username and password and endpoints):
74 module.fail_json(msg="Username, password or endpoints arguments are missing from the module arguments")
75
76 try:
77 return client.XCLIClient.connect_multiendpoint_ssl(username, password, endpoints)
78 except errors.CommandFailedConnectionError as e:
79 module.fail_json(msg=f"Connection with Spectrum Accelerate system has failed: {e}.")
80
81
82 def spectrum_accelerate_spec() -> dict[str, t.Any]:
83 """Return arguments spec for AnsibleModule"""
84 return dict(
85 endpoints=dict(required=True),
86 username=dict(required=True),
87 password=dict(no_log=True, required=True),
88 )
89
90
91 @xcli_wrapper
92 def execute_pyxcli_command(module: AnsibleModule, xcli_command, xcli_client):
93 pyxcli_args = build_pyxcli_command(module.params)
94 getattr(xcli_client.cmd, xcli_command)(**(pyxcli_args))
95 return True
96
97
98 def build_pyxcli_command(fields):
99 """Builds the args for pyxcli using the exact args from ansible"""
100 pyxcli_args = {}
101 for field in fields:
102 if not fields[field]:
103 continue
104 if field in AVAILABLE_PYXCLI_FIELDS and fields[field] != "":
105 pyxcli_args[field] = fields[field]
106 return pyxcli_args
107
108
109 def is_pyxcli_installed(module: AnsibleModule) -> None:
110 if not PYXCLI_INSTALLED:
111 module.fail_json(msg=missing_required_lib("pyxcli"), exception=PYXCLI_IMP_ERR)