| 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_subaccount |
| 12 |
|
| 13 |
short_description: Create and manage Storage Box Subaccounts in Hetzner. |
| 14 |
|
| 15 |
description: |
| 16 |
- Create, update and delete Storage Box Subaccounts in Hetzner. |
| 17 |
- See the L(Storage Box Subaccounts API documentation,https://docs.hetzner.cloud/reference/hetzner#storage-box-subaccounts) for more details. |
| 18 |
|
| 19 |
author: |
| 20 |
- Jonas Lammler (@jooola) |
| 21 |
|
| 22 |
options: |
| 23 |
storage_box: |
| 24 |
description: |
| 25 |
- ID or Name of the parent Storage Box. |
| 26 |
- Using the ID is preferred, to reduce the amount of API requests. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
id: |
| 30 |
description: |
| 31 |
- ID of the Storage Box Subaccount to manage. |
| 32 |
- Required if no Storage Box Subaccount O(name) is given. |
| 33 |
- If the ID is invalid, the module will fail. |
| 34 |
type: int |
| 35 |
name: |
| 36 |
description: |
| 37 |
- Name of the Storage Box Subaccount to manage. |
| 38 |
- Required if no Storage Box Subaccount O(id) is given. |
| 39 |
- Required if the Storage Box Subaccount does not exist. |
| 40 |
type: str |
| 41 |
password: |
| 42 |
description: |
| 43 |
- Password for the Storage Box Subaccount. |
| 44 |
- Required if the Storage Box Subaccount does not exist or when O(state=reset_password). |
| 45 |
type: str |
| 46 |
home_directory: |
| 47 |
description: |
| 48 |
- Home directory of the Storage Box Subaccount. |
| 49 |
- Required if the Storage Box Subaccount does not exist. |
| 50 |
type: str |
| 51 |
access_settings: |
| 52 |
description: |
| 53 |
- Access settings of the Storage Box Subaccount. |
| 54 |
type: dict |
| 55 |
suboptions: |
| 56 |
reachable_externally: |
| 57 |
description: |
| 58 |
- Whether access from outside the Hetzner network is allowed. |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
samba_enabled: |
| 62 |
description: |
| 63 |
- Whether the Samba subsystem is enabled. |
| 64 |
type: bool |
| 65 |
default: false |
| 66 |
ssh_enabled: |
| 67 |
description: |
| 68 |
- Whether the SSH subsystem is enabled. |
| 69 |
type: bool |
| 70 |
default: false |
| 71 |
webdav_enabled: |
| 72 |
description: |
| 73 |
- Whether the WebDAV subsystem is enabled. |
| 74 |
type: bool |
| 75 |
default: false |
| 76 |
readonly: |
| 77 |
description: |
| 78 |
- Whether the Subaccount is read-only. |
| 79 |
type: bool |
| 80 |
default: false |
| 81 |
description: |
| 82 |
description: |
| 83 |
- Description of the Storage Box Subaccount. |
| 84 |
type: str |
| 85 |
labels: |
| 86 |
description: |
| 87 |
- User-defined labels (key-value pairs) for the Storage Box Subaccount. |
| 88 |
type: dict |
| 89 |
state: |
| 90 |
description: |
| 91 |
- State of the Storage Box Subaccount. |
| 92 |
- C(reset_password) is not idempotent. |
| 93 |
default: present |
| 94 |
choices: [absent, present, reset_password] |
| 95 |
type: str |
| 96 |
|
| 97 |
extends_documentation_fragment: |
| 98 |
- hetzner.hcloud.hcloud |
| 99 |
""" |
| 100 |
|
| 101 |
EXAMPLES = """ |
| 102 |
- name: Create a Storage Box Subaccount |
| 103 |
hetzner.hcloud.storage_box_subaccount: |
| 104 |
storage_box: my-storage-box |
| 105 |
name: subaccount1 |
| 106 |
home_directory: backups/subaccount1 |
| 107 |
password: secret |
| 108 |
access_settings: |
| 109 |
reachable_externally: false |
| 110 |
ssh_enabled: true |
| 111 |
samba_enabled: false |
| 112 |
webdav_enabled: false |
| 113 |
readonly: false |
| 114 |
labels: |
| 115 |
env: prod |
| 116 |
state: present |
| 117 |
|
| 118 |
- name: Reset a Storage Box Subaccount password |
| 119 |
hetzner.hcloud.storage_box_subaccount: |
| 120 |
storage_box: my-storage-box |
| 121 |
name: subaccount1 |
| 122 |
password: secret |
| 123 |
state: reset_password |
| 124 |
|
| 125 |
- name: Delete a Storage Box Subaccount by name |
| 126 |
hetzner.hcloud.storage_box_subaccount: |
| 127 |
storage_box: my-storage-box |
| 128 |
name: subaccount1 |
| 129 |
state: absent |
| 130 |
|
| 131 |
- name: Delete a Storage Box Subaccount by id |
| 132 |
hetzner.hcloud.storage_box_subaccount: |
| 133 |
storage_box: 497436 |
| 134 |
id: 158045 |
| 135 |
state: absent |
| 136 |
""" |
| 137 |
|
| 138 |
RETURN = """ |
| 139 |
hcloud_storage_box_subaccount: |
| 140 |
description: Details about the Storage Box Subaccount. |
| 141 |
returned: always |
| 142 |
type: dict |
| 143 |
contains: |
| 144 |
storage_box: |
| 145 |
description: ID of the parent Storage Box. |
| 146 |
returned: always |
| 147 |
type: int |
| 148 |
sample: 497436 |
| 149 |
id: |
| 150 |
description: ID of the Storage Box Subaccount. |
| 151 |
returned: always |
| 152 |
type: int |
| 153 |
sample: 158045 |
| 154 |
name: |
| 155 |
description: Name of the Storage Box Subaccount. |
| 156 |
returned: always |
| 157 |
type: str |
| 158 |
sample: subaccount1 |
| 159 |
description: |
| 160 |
description: Description of the Storage Box Subaccount. |
| 161 |
returned: always |
| 162 |
type: str |
| 163 |
sample: backups from subaccount1 |
| 164 |
home_directory: |
| 165 |
description: Home directory of the Storage Box Subaccount. |
| 166 |
returned: always |
| 167 |
type: str |
| 168 |
sample: backups/subaccount1 |
| 169 |
username: |
| 170 |
description: Username of the Storage Box Subaccount. |
| 171 |
returned: always |
| 172 |
type: str |
| 173 |
sample: u514605-sub1 |
| 174 |
server: |
| 175 |
description: FQDN of the Storage Box Subaccount. |
| 176 |
returned: always |
| 177 |
type: str |
| 178 |
sample: u514605-sub1.your-storagebox.de |
| 179 |
access_settings: |
| 180 |
description: Access settings of the Storage Box Subaccount. |
| 181 |
returned: always |
| 182 |
type: dict |
| 183 |
contains: |
| 184 |
reachable_externally: |
| 185 |
description: Whether access from outside the Hetzner network is allowed. |
| 186 |
returned: always |
| 187 |
type: bool |
| 188 |
sample: false |
| 189 |
samba_enabled: |
| 190 |
description: Whether the Samba subsystem is enabled. |
| 191 |
returned: always |
| 192 |
type: bool |
| 193 |
sample: false |
| 194 |
ssh_enabled: |
| 195 |
description: Whether the SSH subsystem is enabled. |
| 196 |
returned: always |
| 197 |
type: bool |
| 198 |
sample: true |
| 199 |
webdav_enabled: |
| 200 |
description: Whether the WebDAV subsystem is enabled. |
| 201 |
returned: always |
| 202 |
type: bool |
| 203 |
sample: false |
| 204 |
readonly: |
| 205 |
description: Whether the Subaccount is read-only. |
| 206 |
returned: always |
| 207 |
type: bool |
| 208 |
sample: false |
| 209 |
labels: |
| 210 |
description: User-defined labels (key-value pairs) of the Storage Box Subaccount. |
| 211 |
returned: always |
| 212 |
type: dict |
| 213 |
sample: |
| 214 |
env: prod |
| 215 |
created: |
| 216 |
description: Point in time when the Storage Box Subaccount was created (in RFC3339 format). |
| 217 |
returned: always |
| 218 |
type: str |
| 219 |
sample: "2025-12-03T13:47:47Z" |
| 220 |
""" |
| 221 |
|
| 222 |
|
| 223 |
from ..module_utils import _storage_box, _storage_box_subaccount |
| 224 |
from ..module_utils._base import AnsibleHCloud, AnsibleModule |
| 225 |
from ..module_utils._client import client_resource_not_found |
| 226 |
from ..module_utils._vendor.hcloud import HCloudException |
| 227 |
from ..module_utils._vendor.hcloud.storage_boxes import ( |
| 228 |
BoundStorageBox, |
| 229 |
BoundStorageBoxSubaccount, |
| 230 |
StorageBoxSubaccountAccessSettings, |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
class AnsibleStorageBoxSubaccount(AnsibleHCloud): |
| 235 |
represent = "storage_box_subaccount" |
| 236 |
|
| 237 |
storage_box: BoundStorageBox | None = None |
| 238 |
storage_box_subaccount: BoundStorageBoxSubaccount | None = None |
| 239 |
|
| 240 |
def _prepare_result(self): |
| 241 |
if self.storage_box_subaccount is None: |
| 242 |
return {} |
| 243 |
return _storage_box_subaccount.prepare_result(self.storage_box_subaccount) |
| 244 |
|
| 245 |
def _fetch(self): |
| 246 |
self.storage_box = _storage_box.get(self.client.storage_boxes, self.module.params.get("storage_box")) |
| 247 |
|
| 248 |
if (value := self.module.params.get("id")) is not None: |
| 249 |
self.storage_box_subaccount = self.storage_box.get_subaccount_by_id(value) |
| 250 |
elif (value := self.module.params.get("name")) is not None: |
| 251 |
self.storage_box_subaccount = self.storage_box.get_subaccount_by_name(value) |
| 252 |
|
| 253 |
# Backward compatible upgrade from label based name |
| 254 |
if self.storage_box_subaccount is None: |
| 255 |
self.storage_box_subaccount = _storage_box_subaccount.get_by_label_name(self.storage_box, value) |
| 256 |
|
| 257 |
def _create(self): |
| 258 |
self.fail_on_invalid_params( |
| 259 |
required=["name", "home_directory", "password"], |
| 260 |
) |
| 261 |
params = { |
| 262 |
"name": self.module.params.get("name"), |
| 263 |
"home_directory": self.module.params.get("home_directory"), |
| 264 |
"password": self.module.params.get("password"), |
| 265 |
} |
| 266 |
|
| 267 |
if (value := self.module.params.get("description")) is not None: |
| 268 |
params["description"] = value |
| 269 |
|
| 270 |
if (value := self.module.params.get("labels")) is not None: |
| 271 |
params["labels"] = value |
| 272 |
|
| 273 |
if (value := self.module.params.get("access_settings")) is not None: |
| 274 |
params["access_settings"] = StorageBoxSubaccountAccessSettings.from_dict(value) |
| 275 |
|
| 276 |
if not self.module.check_mode: |
| 277 |
resp = self.storage_box.create_subaccount(**params) |
| 278 |
self.storage_box_subaccount = resp.subaccount |
| 279 |
resp.action.wait_until_finished() |
| 280 |
|
| 281 |
self.storage_box_subaccount.reload() |
| 282 |
|
| 283 |
self._mark_as_changed() |
| 284 |
|
| 285 |
def _update(self): |
| 286 |
need_reload = False |
| 287 |
|
| 288 |
if (value := self.module.params.get("home_directory")) is not None: |
| 289 |
if self.storage_box_subaccount.home_directory != value: |
| 290 |
if not self.module.check_mode: |
| 291 |
action = self.storage_box_subaccount.change_home_directory(value) |
| 292 |
action.wait_until_finished() |
| 293 |
need_reload = True |
| 294 |
self._mark_as_changed() |
| 295 |
|
| 296 |
if (value := self.module.params.get("access_settings")) is not None: |
| 297 |
access_settings = StorageBoxSubaccountAccessSettings.from_dict(value) |
| 298 |
if self.storage_box_subaccount.access_settings.to_payload() != access_settings.to_payload(): |
| 299 |
if not self.module.check_mode: |
| 300 |
action = self.storage_box_subaccount.update_access_settings(access_settings) |
| 301 |
action.wait_until_finished() |
| 302 |
need_reload = True |
| 303 |
self._mark_as_changed() |
| 304 |
|
| 305 |
params = {} |
| 306 |
if (value := self.module.params.get("name")) is not None: |
| 307 |
if value != self.storage_box_subaccount.name: |
| 308 |
params["name"] = value |
| 309 |
self._mark_as_changed() |
| 310 |
|
| 311 |
if (value := self.module.params.get("description")) is not None: |
| 312 |
if value != self.storage_box_subaccount.description: |
| 313 |
params["description"] = value |
| 314 |
self._mark_as_changed() |
| 315 |
|
| 316 |
if (value := self.module.params.get("labels")) is not None: |
| 317 |
if value != self.storage_box_subaccount.labels: |
| 318 |
params["labels"] = value |
| 319 |
self._mark_as_changed() |
| 320 |
|
| 321 |
# Update only if params holds changes or actions were triggered |
| 322 |
if params or need_reload: |
| 323 |
if not self.module.check_mode: |
| 324 |
self.storage_box_subaccount = self.storage_box_subaccount.update(**params) |
| 325 |
|
| 326 |
def _delete(self): |
| 327 |
if not self.module.check_mode: |
| 328 |
resp = self.storage_box_subaccount.delete() |
| 329 |
resp.action.wait_until_finished() |
| 330 |
|
| 331 |
self.storage_box_subaccount = None |
| 332 |
self._mark_as_changed() |
| 333 |
|
| 334 |
def present(self): |
| 335 |
try: |
| 336 |
self._fetch() |
| 337 |
if self.storage_box_subaccount is None: |
| 338 |
self._create() |
| 339 |
else: |
| 340 |
self._update() |
| 341 |
|
| 342 |
except HCloudException as exception: |
| 343 |
self.fail_json_hcloud(exception) |
| 344 |
|
| 345 |
def absent(self): |
| 346 |
try: |
| 347 |
self._fetch() |
| 348 |
if self.storage_box_subaccount is None: |
| 349 |
return |
| 350 |
self._delete() |
| 351 |
|
| 352 |
except HCloudException as exception: |
| 353 |
self.fail_json_hcloud(exception) |
| 354 |
|
| 355 |
def reset_password(self): |
| 356 |
self.fail_on_invalid_params( |
| 357 |
required=["password"], |
| 358 |
) |
| 359 |
try: |
| 360 |
self._fetch() |
| 361 |
if self.storage_box_subaccount is None: |
| 362 |
raise client_resource_not_found( |
| 363 |
"storage box", |
| 364 |
self.module.params.get("id") or self.module.params.get("name"), |
| 365 |
) |
| 366 |
if not self.module.check_mode: |
| 367 |
action = self.storage_box_subaccount.reset_password(self.module.params.get("password")) |
| 368 |
action.wait_until_finished() |
| 369 |
|
| 370 |
self._mark_as_changed() |
| 371 |
|
| 372 |
except HCloudException as exception: |
| 373 |
self.fail_json_hcloud(exception) |
| 374 |
|
| 375 |
@classmethod |
| 376 |
def define_module(cls): |
| 377 |
return AnsibleModule( |
| 378 |
argument_spec=dict( |
| 379 |
storage_box={"type": "str", "required": True}, |
| 380 |
id={"type": "int"}, |
| 381 |
name={"type": "str"}, |
| 382 |
home_directory={"type": "str"}, |
| 383 |
password={"type": "str", "no_log": True}, |
| 384 |
description={"type": "str"}, |
| 385 |
labels={"type": "dict"}, |
| 386 |
access_settings={ |
| 387 |
"type": "dict", |
| 388 |
"options": dict( |
| 389 |
reachable_externally={"type": "bool", "default": False}, |
| 390 |
samba_enabled={"type": "bool", "default": False}, |
| 391 |
ssh_enabled={"type": "bool", "default": False}, |
| 392 |
webdav_enabled={"type": "bool", "default": False}, |
| 393 |
readonly={"type": "bool", "default": False}, |
| 394 |
), |
| 395 |
}, |
| 396 |
state={ |
| 397 |
"choices": ["absent", "present", "reset_password"], |
| 398 |
"default": "present", |
| 399 |
}, |
| 400 |
**super().base_module_arguments(), |
| 401 |
), |
| 402 |
required_one_of=[["id", "name"]], |
| 403 |
supports_check_mode=True, |
| 404 |
) |
| 405 |
|
| 406 |
|
| 407 |
def main(): |
| 408 |
module = AnsibleStorageBoxSubaccount.define_module() |
| 409 |
o = AnsibleStorageBoxSubaccount(module) |
| 410 |
|
| 411 |
match module.params.get("state"): |
| 412 |
case "reset_password": |
| 413 |
o.reset_password() |
| 414 |
case "absent": |
| 415 |
o.absent() |
| 416 |
case _: |
| 417 |
o.present() |
| 418 |
|
| 419 |
result = o.get_result() |
| 420 |
|
| 421 |
# Legacy return value naming pattern |
| 422 |
result["hcloud_storage_box_subaccount"] = result.pop(o.represent) |
| 423 |
|
| 424 |
module.exit_json(**result) |
| 425 |
|
| 426 |
|
| 427 |
if __name__ == "__main__": |
| 428 |
main() |