Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/lookup/santricity_storage_pool.py

raw symbols SCIP
1 # (c) 2024, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import (absolute_import, division, print_function)
5 __metaclass__ = type
6
7 DOCUMENTATION = """
8 name: santricity_storage_pool
9 author:
10 - Nathan Swartz (@swartzn)
11 - Vu Tran (@VuTran007)
12 short_description: Storage pool information
13 description:
14 - Retrieves storage pool information from the inventory
15 options:
16 state:
17 description:
18 - Define the state of storage pool.
19 choices:
20 - absent
21 - present
22 type: str
23 """
24
25 EXAMPLES = r"""
26 - name: set facts for storage pool to be absent
27 ansible.builtin.set_fact:
28 absent_storage_pool: "{{ lookup('netapp_eseries.santricity.santricity_storage_pool', hostvars[inventory_hostname], state='absent') }}"
29 """
30
31 import re
32 from ansible.plugins.lookup import LookupBase
33 from ansible.errors import AnsibleError
34 from itertools import product
35
36
37 class LookupModule(LookupBase):
38
39 # pylint: disable=arguments-renamed
40 def run(self, inventory, state, **kwargs):
41 if isinstance(inventory, list):
42 inventory = inventory[0]
43
44 if ("eseries_storage_pool_configuration" not in inventory or not isinstance(inventory["eseries_storage_pool_configuration"], list) or
45 len(inventory["eseries_storage_pool_configuration"]) == 0):
46 return list()
47
48 sp_list = list()
49 for sp_info in inventory["eseries_storage_pool_configuration"]:
50
51 if not isinstance(sp_info, dict) or "name" not in sp_info:
52 raise AnsibleError("eseries_storage_pool_configuration must contain a list of dictionaries containing the necessary information.")
53
54 for sp in patternize(sp_info["name"], inventory):
55 if (("eseries_remove_all_configuration_state" in inventory and inventory["eseries_remove_all_configuration_state"] == "absent") or
56 ("state" in sp_info and sp_info["state"] == "absent") or
57 ("state" not in sp_info and "eseries_storage_pool_state" in inventory and inventory["eseries_storage_pool_state"] == "absent")):
58 sp_options = {"state": "absent"}
59 else:
60 sp_options = {"state": "present"}
61
62 for option in sp_info.keys():
63 sp_options.update({option: sp_info[option]})
64
65 sp_options.update({"name": sp})
66
67 if sp_options["state"] == state:
68 sp_list.append(sp_options)
69
70 return sp_list
71
72
73 def patternize(pattern, inventory, storage_pool=None):
74 """Generate list of strings determined by a pattern"""
75 if storage_pool:
76 pattern = pattern.replace("[pool]", storage_pool)
77
78 if inventory:
79 inventory_tokens = re.findall(r"\[[a-zA-Z0-9_]*\]", pattern)
80 for token in inventory_tokens:
81 pattern = pattern.replace(token, str(inventory[token[1:-1]]))
82
83 tokens = re.findall(r"\[[0-9]-[0-9]\]|\[[a-z]-[a-z]\]|\[[A-Z]-[A-Z]\]", pattern)
84 segments = "%s".join(re.split(r"\[[0-9]-[0-9]\]|\[[a-z]-[a-z]\]|\[[A-Z]-[A-Z]\]", pattern))
85
86 if len(tokens) == 0:
87 return [pattern]
88
89 combinations = []
90 for token in tokens:
91 start, stop = token[1:-1].split("-")
92
93 try:
94 start = int(start)
95 stop = int(stop)
96 combinations.append([str(number) for number in range(start, stop + 1)])
97 except ValueError:
98 combinations.append([chr(number) for number in range(ord(start), ord(stop) + 1)])
99
100 return [segments % subset for subset in list(product(*combinations))]