Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_storage_info.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright Tristan Le Guern (@tleguern) <tleguern at bouledef.eu>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 DOCUMENTATION = r"""
9 module: proxmox_storage_info
10 short_description: Retrieve information about one or more Proxmox VE storages
11 description:
12 - Retrieve information about one or more Proxmox VE storages.
13 options:
14 storage:
15 description:
16 - Only return information on a specific storage.
17 aliases: ['name']
18 type: str
19 type:
20 description:
21 - Filter on a specific storage type.
22 type: str
23 author: Tristan Le Guern (@tleguern)
24 extends_documentation_fragment:
25 - community.proxmox.proxmox.actiongroup_proxmox
26 - community.proxmox.proxmox.documentation
27 - community.proxmox.attributes
28 - community.proxmox.attributes.info_module
29 notes:
30 - Storage specific options can be returned by this module, please look at the documentation at U(https://pve.proxmox.com/wiki/Storage).
31 """
32
33
34 EXAMPLES = r"""
35 - name: List existing storages
36 community.proxmox.proxmox_storage_info:
37 register: proxmox_storages
38
39 - name: List NFS storages only
40 community.proxmox.proxmox_storage_info:
41 type: nfs
42 register: proxmox_storages_nfs
43
44 - name: Retrieve information about the lvm2 storage
45 community.proxmox.proxmox_storage_info:
46 storage: lvm2
47 register: proxmox_storage_lvm
48 """
49
50
51 RETURN = r"""
52 proxmox_storages:
53 description: List of storage pools.
54 returned: on success
55 type: list
56 elements: dict
57 contains:
58 content:
59 description: Proxmox content types available in this storage.
60 returned: on success
61 type: list
62 elements: str
63 digest:
64 description: Storage's digest.
65 returned: on success
66 type: str
67 nodes:
68 description: List of nodes associated to this storage.
69 returned: on success, if storage is not local
70 type: list
71 elements: str
72 path:
73 description: Physical path to this storage.
74 returned: on success
75 type: str
76 prune-backups:
77 description: Backup retention options.
78 returned: on success
79 type: list
80 elements: dict
81 shared:
82 description: Is this storage shared.
83 returned: on success
84 type: bool
85 storage:
86 description: Storage name.
87 returned: on success
88 type: str
89 type:
90 description: Storage type.
91 returned: on success
92 type: str
93 """
94
95
96 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
97 ProxmoxAnsible,
98 create_proxmox_module,
99 proxmox_to_ansible_bool,
100 )
101
102
103 def module_args():
104 return dict(
105 storage=dict(type="str", aliases=["name"]),
106 type=dict(type="str"),
107 )
108
109
110 def module_options():
111 return dict(mutually_exclusive=[("storage", "type")])
112
113
114 class ProxmoxStorageInfoAnsible(ProxmoxAnsible):
115 def get_storage(self, storage):
116 try:
117 storage = self.proxmox_api.storage.get(storage)
118 except Exception:
119 self.module.fail_json(msg=f"Storage '{storage}' does not exist")
120 return ProxmoxStorage(storage)
121
122 def get_storages(self, storagetype=None):
123 storages = self.proxmox_api.storage.get(type=storagetype)
124 storages = [ProxmoxStorage(storage) for storage in storages]
125 return storages
126
127
128 class ProxmoxStorage:
129 def __init__(self, storage):
130 self.storage = storage
131 # Convert proxmox representation of lists, dicts and boolean for easier
132 # manipulation within ansible.
133 if "shared" in self.storage:
134 self.storage["shared"] = proxmox_to_ansible_bool(self.storage["shared"])
135 if "content" in self.storage:
136 self.storage["content"] = self.storage["content"].split(",")
137 if "nodes" in self.storage:
138 self.storage["nodes"] = self.storage["nodes"].split(",")
139 if "prune-backups" in storage:
140 options = storage["prune-backups"].split(",")
141 self.storage["prune-backups"] = dict()
142 for option in options:
143 k, v = option.split("=")
144 self.storage["prune-backups"][k] = v
145
146
147 def main():
148 module = create_proxmox_module(module_args(), **module_options())
149 proxmox = ProxmoxStorageInfoAnsible(module)
150
151 result = dict(changed=False)
152
153 storage = module.params["storage"]
154 storagetype = module.params["type"]
155
156 if storage:
157 storages = [proxmox.get_storage(storage)]
158 else:
159 storages = proxmox.get_storages(storagetype)
160 result["proxmox_storages"] = [storage.storage for storage in storages]
161
162 module.exit_json(**result)
163
164
165 if __name__ == "__main__":
166 main()