| 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_info |
| 12 |
|
| 13 |
short_description: Gather infos about Hetzner Storage Box Subaccounts. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather infos about Hetzner Storage Box Subaccounts. |
| 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 get. |
| 32 |
- If the ID is invalid, the module will fail. |
| 33 |
type: int |
| 34 |
name: |
| 35 |
description: |
| 36 |
- Name of the Storage Box Subaccount to get. |
| 37 |
type: str |
| 38 |
label_selector: |
| 39 |
description: |
| 40 |
- Label selector to filter the Storage Box Subaccounts to get. |
| 41 |
type: str |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- hetzner.hcloud.hcloud |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = """ |
| 48 |
- name: Gather all Storage Box Subaccounts |
| 49 |
hetzner.hcloud.storage_box_subaccount_info: |
| 50 |
register: output |
| 51 |
|
| 52 |
- name: Gather Storage Box Subaccounts by label |
| 53 |
hetzner.hcloud.storage_box_subaccount_info: |
| 54 |
label_selector: env=prod |
| 55 |
register: output |
| 56 |
|
| 57 |
- name: Gather a Storage Box Subaccount by name |
| 58 |
hetzner.hcloud.storage_box_subaccount_info: |
| 59 |
name: subaccount1 |
| 60 |
register: output |
| 61 |
|
| 62 |
- name: Gather a Storage Box Subaccount by id |
| 63 |
hetzner.hcloud.storage_box_subaccount_info: |
| 64 |
name: 12345 |
| 65 |
register: output |
| 66 |
|
| 67 |
- name: Print the gathered infos |
| 68 |
debug: |
| 69 |
var: output.hcloud_storage_box_subaccount_info |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = """ |
| 73 |
hcloud_storage_box_subaccount_info: |
| 74 |
description: List of Storage Box Subaccounts. |
| 75 |
returned: always |
| 76 |
type: list |
| 77 |
elements: dict |
| 78 |
contains: |
| 79 |
storage_box: |
| 80 |
description: ID of the parent Storage Box. |
| 81 |
returned: always |
| 82 |
type: int |
| 83 |
sample: 514605 |
| 84 |
id: |
| 85 |
description: ID of the Storage Box Subaccount. |
| 86 |
returned: always |
| 87 |
type: int |
| 88 |
sample: 158045 |
| 89 |
name: |
| 90 |
description: Name of the Storage Box Subaccount. |
| 91 |
returned: always |
| 92 |
type: str |
| 93 |
sample: subaccount1 |
| 94 |
description: |
| 95 |
description: Description of the Storage Box Subaccount. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: backups from subaccount1 |
| 99 |
home_directory: |
| 100 |
description: Home directory of the Storage Box Subaccount. |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: backups/subaccount1 |
| 104 |
username: |
| 105 |
description: Username of the Storage Box Subaccount. |
| 106 |
returned: always |
| 107 |
type: str |
| 108 |
sample: u514605-sub1 |
| 109 |
server: |
| 110 |
description: FQDN of the Storage Box Subaccount. |
| 111 |
returned: always |
| 112 |
type: str |
| 113 |
sample: u514605-sub1.your-storagebox.de |
| 114 |
access_settings: |
| 115 |
description: Access settings of the Storage Box Subaccount. |
| 116 |
returned: always |
| 117 |
type: dict |
| 118 |
contains: |
| 119 |
reachable_externally: |
| 120 |
description: Whether access from outside the Hetzner network is allowed. |
| 121 |
returned: always |
| 122 |
type: bool |
| 123 |
sample: false |
| 124 |
samba_enabled: |
| 125 |
description: Whether the Samba subsystem is enabled. |
| 126 |
returned: always |
| 127 |
type: bool |
| 128 |
sample: false |
| 129 |
ssh_enabled: |
| 130 |
description: Whether the SSH subsystem is enabled. |
| 131 |
returned: always |
| 132 |
type: bool |
| 133 |
sample: true |
| 134 |
webdav_enabled: |
| 135 |
description: Whether the WebDAV subsystem is enabled. |
| 136 |
returned: always |
| 137 |
type: bool |
| 138 |
sample: false |
| 139 |
readonly: |
| 140 |
description: Whether the Subaccount is read-only. |
| 141 |
returned: always |
| 142 |
type: bool |
| 143 |
sample: false |
| 144 |
labels: |
| 145 |
description: User-defined labels (key-value pairs) of the Storage Box Subaccount. |
| 146 |
returned: always |
| 147 |
type: dict |
| 148 |
sample: |
| 149 |
env: prod |
| 150 |
created: |
| 151 |
description: Point in time when the Storage Box Subaccount was created (in RFC3339 format). |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: "2025-12-03T13:47:47Z" |
| 155 |
""" |
| 156 |
|
| 157 |
from ansible.module_utils.basic import AnsibleModule |
| 158 |
|
| 159 |
from ..module_utils import _storage_box, _storage_box_subaccount |
| 160 |
from ..module_utils._base import AnsibleHCloud |
| 161 |
from ..module_utils._vendor.hcloud import HCloudException |
| 162 |
from ..module_utils._vendor.hcloud.storage_boxes import ( |
| 163 |
BoundStorageBox, |
| 164 |
BoundStorageBoxSubaccount, |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
class AnsibleStorageBoxSubaccountInfo(AnsibleHCloud): |
| 169 |
represent = "storage_box_subaccounts" |
| 170 |
|
| 171 |
storage_box: BoundStorageBox | None = None |
| 172 |
storage_box_subaccounts: list[BoundStorageBoxSubaccount] | None = None |
| 173 |
|
| 174 |
def _prepare_result(self): |
| 175 |
result = [] |
| 176 |
|
| 177 |
for o in self.storage_box_subaccounts or []: |
| 178 |
if o is not None: |
| 179 |
result.append(_storage_box_subaccount.prepare_result(o)) |
| 180 |
return result |
| 181 |
|
| 182 |
def fetch(self): |
| 183 |
try: |
| 184 |
self.storage_box = _storage_box.get( |
| 185 |
self.client.storage_boxes, |
| 186 |
self.module.params.get("storage_box"), |
| 187 |
) |
| 188 |
|
| 189 |
if (value := self.module.params.get("id")) is not None: |
| 190 |
self.storage_box_subaccounts = [self.storage_box.get_subaccount_by_id(value)] |
| 191 |
elif (value := self.module.params.get("name")) is not None: |
| 192 |
self.storage_box_subaccounts = [self.storage_box.get_subaccount_by_name(value)] |
| 193 |
|
| 194 |
# Backward compatible upgrade from label based name |
| 195 |
if not self.storage_box_subaccounts or self.storage_box_subaccounts[0] is None: |
| 196 |
self.storage_box_subaccounts = [_storage_box_subaccount.get_by_label_name(self.storage_box, value)] |
| 197 |
else: |
| 198 |
params = {} |
| 199 |
if (value := self.module.params.get("label_selector")) is not None: |
| 200 |
params["label_selector"] = value |
| 201 |
self.storage_box_subaccounts = self.storage_box.get_subaccount_all(**params) |
| 202 |
|
| 203 |
except HCloudException as exception: |
| 204 |
self.fail_json_hcloud(exception) |
| 205 |
|
| 206 |
@classmethod |
| 207 |
def define_module(cls): |
| 208 |
return AnsibleModule( |
| 209 |
argument_spec=dict( |
| 210 |
storage_box={"type": "str", "required": True}, |
| 211 |
id={"type": "int"}, |
| 212 |
name={"type": "str"}, |
| 213 |
label_selector={"type": "str"}, |
| 214 |
**super().base_module_arguments(), |
| 215 |
), |
| 216 |
supports_check_mode=True, |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def main(): |
| 221 |
module = AnsibleStorageBoxSubaccountInfo.define_module() |
| 222 |
o = AnsibleStorageBoxSubaccountInfo(module) |
| 223 |
|
| 224 |
o.fetch() |
| 225 |
result = o.get_result() |
| 226 |
|
| 227 |
# Legacy return value naming pattern |
| 228 |
result["hcloud_storage_box_subaccount_info"] = result.pop(o.represent) |
| 229 |
|
| 230 |
module.exit_json(**result) |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
main() |