| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2025, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: storage_box_info |
| 12 |
|
| 13 |
short_description: Gather infos about Hetzner Storage Boxes. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather infos about Hetzner Storage Boxes. |
| 17 |
- See the L(Storage Boxes API documentation,https://docs.hetzner.cloud/reference/hetzner#storage-boxes) for more details. |
| 18 |
|
| 19 |
author: |
| 20 |
- Jonas Lammler (@jooola) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- ID of the Storage Box to get. |
| 26 |
- If the ID is invalid, the module will fail. |
| 27 |
type: int |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the Storage Box to get. |
| 31 |
type: str |
| 32 |
label_selector: |
| 33 |
description: |
| 34 |
- Label selector to filter the Storage Boxes to get. |
| 35 |
type: str |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- hetzner.hcloud.hcloud |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = """ |
| 42 |
- name: Gather all Storage Boxes |
| 43 |
hetzner.hcloud.storage_box_info: |
| 44 |
register: output |
| 45 |
- name: Print the gathered infos |
| 46 |
debug: |
| 47 |
var: output.hcloud_storage_box_info |
| 48 |
|
| 49 |
- name: Gather Storage Boxes by label |
| 50 |
hetzner.hcloud.storage_box_info: |
| 51 |
label_selector: env=prod |
| 52 |
register: output |
| 53 |
- name: Print the gathered infos |
| 54 |
debug: |
| 55 |
var: output.hcloud_storage_box_info |
| 56 |
|
| 57 |
- name: Gather a Storage Box by name |
| 58 |
hetzner.hcloud.storage_box_info: |
| 59 |
name: backups |
| 60 |
register: output |
| 61 |
- name: Print the gathered infos |
| 62 |
debug: |
| 63 |
var: output.hcloud_storage_box_info[0] |
| 64 |
|
| 65 |
- name: Gather a Storage Box by id |
| 66 |
hetzner.hcloud.storage_box_info: |
| 67 |
name: 12345 |
| 68 |
register: output |
| 69 |
- name: Print the gathered infos |
| 70 |
debug: |
| 71 |
var: output.hcloud_storage_box_info[0] |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = """ |
| 75 |
hcloud_storage_box_info: |
| 76 |
description: List of Storage Boxes. |
| 77 |
returned: always |
| 78 |
type: list |
| 79 |
elements: dict |
| 80 |
contains: |
| 81 |
id: |
| 82 |
description: ID of the Storage Box. |
| 83 |
returned: always |
| 84 |
type: int |
| 85 |
sample: 1937415 |
| 86 |
name: |
| 87 |
description: Name of the Storage Box. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: my-storage-box |
| 91 |
storage_box_type: |
| 92 |
description: Name of the Storage Box Type. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
sample: bx11 |
| 96 |
location: |
| 97 |
description: Name of the Location of the Storage Box. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
sample: fsn1 |
| 101 |
labels: |
| 102 |
description: User-defined labels (key-value pairs) of the Storage Box. |
| 103 |
returned: always |
| 104 |
type: dict |
| 105 |
sample: |
| 106 |
env: prod |
| 107 |
delete_protection: |
| 108 |
description: Protect the Storage Box from deletion. |
| 109 |
returned: always |
| 110 |
type: bool |
| 111 |
sample: false |
| 112 |
access_settings: |
| 113 |
description: Access settings of the Storage Box. |
| 114 |
returned: always |
| 115 |
type: dict |
| 116 |
contains: |
| 117 |
reachable_externally: |
| 118 |
description: Whether access from outside the Hetzner network is allowed. |
| 119 |
returned: always |
| 120 |
type: bool |
| 121 |
sample: false |
| 122 |
samba_enabled: |
| 123 |
description: Whether the Samba subsystem is enabled. |
| 124 |
returned: always |
| 125 |
type: bool |
| 126 |
sample: false |
| 127 |
ssh_enabled: |
| 128 |
description: Whether the SSH subsystem is enabled. |
| 129 |
returned: always |
| 130 |
type: bool |
| 131 |
sample: true |
| 132 |
webdav_enabled: |
| 133 |
description: Whether the WebDAV subsystem is enabled. |
| 134 |
returned: always |
| 135 |
type: bool |
| 136 |
sample: false |
| 137 |
zfs_enabled: |
| 138 |
description: Whether the ZFS snapshot folder is visible. |
| 139 |
returned: always |
| 140 |
type: bool |
| 141 |
sample: false |
| 142 |
username: |
| 143 |
description: User name of the Storage Box. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: u505337 |
| 147 |
server: |
| 148 |
description: FQDN of the Storage Box. |
| 149 |
returned: always |
| 150 |
type: str |
| 151 |
sample: u505337.your-storagebox.de |
| 152 |
system: |
| 153 |
description: Host system of the Storage Box. |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: HEL1-BX136 |
| 157 |
status: |
| 158 |
description: Status of the Storage Box. |
| 159 |
returned: always |
| 160 |
type: str |
| 161 |
sample: active |
| 162 |
stats: |
| 163 |
description: Statistics of the Storage Box. |
| 164 |
returned: always |
| 165 |
type: dict |
| 166 |
contains: |
| 167 |
size: |
| 168 |
description: Current disk usage in bytes. |
| 169 |
returned: always |
| 170 |
type: int |
| 171 |
sample: 10485760 |
| 172 |
size_data: |
| 173 |
description: Current disk usage for data in bytes. |
| 174 |
returned: always |
| 175 |
type: int |
| 176 |
sample: 10485760 |
| 177 |
size_snapshots: |
| 178 |
description: Current disk usage for snapshots in bytes. |
| 179 |
returned: always |
| 180 |
type: int |
| 181 |
sample: 10485760 |
| 182 |
""" |
| 183 |
|
| 184 |
from ansible.module_utils.basic import AnsibleModule |
| 185 |
|
| 186 |
from ..module_utils import _storage_box |
| 187 |
from ..module_utils._base import AnsibleHCloud |
| 188 |
from ..module_utils._vendor.hcloud import HCloudException |
| 189 |
from ..module_utils._vendor.hcloud.storage_boxes import ( |
| 190 |
BoundStorageBox, |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
class AnsibleStorageBox(AnsibleHCloud): |
| 195 |
represent = "storage_box" |
| 196 |
|
| 197 |
storage_box: list[BoundStorageBox] | None = None |
| 198 |
|
| 199 |
def _prepare_result(self): |
| 200 |
result = [] |
| 201 |
for o in self.storage_box or []: |
| 202 |
if o is not None: |
| 203 |
result.append(_storage_box.prepare_result(o)) |
| 204 |
|
| 205 |
return result |
| 206 |
|
| 207 |
def fetch(self): |
| 208 |
try: |
| 209 |
if (id_ := self.module.params.get("id")) is not None: |
| 210 |
self.storage_box = [self.client.storage_boxes.get_by_id(id_)] |
| 211 |
elif (name := self.module.params.get("name")) is not None: |
| 212 |
self.storage_box = [self.client.storage_boxes.get_by_name(name)] |
| 213 |
else: |
| 214 |
params = {} |
| 215 |
if (label_selector := self.module.params.get("label_selector")) is not None: |
| 216 |
params["label_selector"] = label_selector |
| 217 |
self.storage_box = self.client.storage_boxes.get_all(**params) |
| 218 |
|
| 219 |
except HCloudException as exception: |
| 220 |
self.fail_json_hcloud(exception) |
| 221 |
|
| 222 |
@classmethod |
| 223 |
def define_module(cls): |
| 224 |
return AnsibleModule( |
| 225 |
argument_spec=dict( |
| 226 |
id={"type": "int"}, |
| 227 |
name={"type": "str"}, |
| 228 |
label_selector={"type": "str"}, |
| 229 |
**super().base_module_arguments(), |
| 230 |
), |
| 231 |
supports_check_mode=True, |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
def main(): |
| 236 |
module = AnsibleStorageBox.define_module() |
| 237 |
o = AnsibleStorageBox(module) |
| 238 |
|
| 239 |
o.fetch() |
| 240 |
result = o.get_result() |
| 241 |
|
| 242 |
# Legacy return value naming pattern |
| 243 |
result["hcloud_storage_box_info"] = result.pop(o.represent) |
| 244 |
|
| 245 |
module.exit_json(**result) |
| 246 |
|
| 247 |
|
| 248 |
if __name__ == "__main__": |
| 249 |
main() |