| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2025 Victor LEFEBVRE <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: storagebox_subaccount_info |
| 15 |
short_description: Query the subaccounts for a storage box |
| 16 |
version_added: 2.4.0 |
| 17 |
author: |
| 18 |
- Victor LEFEBVRE (@vic1707) |
| 19 |
description: |
| 20 |
- Query the subaccounts for a storage box. |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.hrobot.api._robot_compat_shim_deprecation # must come before api and robot |
| 23 |
- community.hrobot.api |
| 24 |
- community.hrobot.robot |
| 25 |
- community.hrobot.attributes |
| 26 |
- community.hrobot.attributes._actiongroup_robot_and_api_deprecation # must come before the other two! |
| 27 |
- community.hrobot.attributes.actiongroup_api |
| 28 |
- community.hrobot.attributes.actiongroup_robot |
| 29 |
- community.hrobot.attributes.idempotent_not_modify_state |
| 30 |
- community.hrobot.attributes.info_module |
| 31 |
|
| 32 |
options: |
| 33 |
hetzner_token: |
| 34 |
version_added: 2.5.0 |
| 35 |
storagebox_id: |
| 36 |
description: |
| 37 |
- The ID of the storage box to query. |
| 38 |
type: int |
| 39 |
required: true |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = r""" |
| 43 |
--- |
| 44 |
- name: Query the subaccounts |
| 45 |
community.hrobot.storagebox_subaccount_info: |
| 46 |
hetzner_user: foo |
| 47 |
hetzner_password: bar |
| 48 |
storage_box_id: 123 |
| 49 |
register: result |
| 50 |
|
| 51 |
- name: Output data |
| 52 |
ansible.builtin.debug: |
| 53 |
msg: "Username of the first subaccount: {{ result.subaccounts[0].username }}" |
| 54 |
""" |
| 55 |
|
| 56 |
RETURN = r""" |
| 57 |
subaccounts: |
| 58 |
description: |
| 59 |
- The storage box's info. |
| 60 |
- All date and time parameters are in UTC. |
| 61 |
returned: success |
| 62 |
type: list |
| 63 |
elements: dict |
| 64 |
contains: |
| 65 |
username: |
| 66 |
description: |
| 67 |
- Username of the sub-account. |
| 68 |
type: str |
| 69 |
sample: "u2342-sub1" |
| 70 |
returned: success |
| 71 |
accountid: |
| 72 |
description: |
| 73 |
- Username of the main user. |
| 74 |
- Not supported by the new Hetzner API. |
| 75 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 76 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 77 |
type: str |
| 78 |
sample: "u2342" |
| 79 |
returned: success and if O(hetzner_token) is not specified |
| 80 |
server: |
| 81 |
description: |
| 82 |
- Server on which the sub-account resides. |
| 83 |
type: str |
| 84 |
sample: "sb1234.your-storagebox.de" |
| 85 |
returned: success |
| 86 |
homedirectory: |
| 87 |
description: |
| 88 |
- Homedirectory of the sub-account. |
| 89 |
- Note that this is copied from RV(subaccounts[].home_directory) in case O(hetzner_token) is specified. |
| 90 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 91 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 92 |
type: str |
| 93 |
sample: "/home/u2342-sub1" |
| 94 |
returned: success |
| 95 |
samba: |
| 96 |
description: |
| 97 |
- Status of Samba support. |
| 98 |
- Note that this is copied from RV(subaccounts[].access_settings.samba_enabled) in case O(hetzner_token) is specified. |
| 99 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 100 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 101 |
type: bool |
| 102 |
sample: true |
| 103 |
returned: success |
| 104 |
ssh: |
| 105 |
description: |
| 106 |
- Status of SSH support. |
| 107 |
- Note that this is copied from RV(subaccounts[].access_settings.ssh_enabled) in case O(hetzner_token) is specified. |
| 108 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 109 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 110 |
type: bool |
| 111 |
sample: true |
| 112 |
returned: success |
| 113 |
external_reachability: |
| 114 |
description: |
| 115 |
- Status of external reachability. |
| 116 |
- Note that this is copied from RV(subaccounts[].access_settings.reachable_externally) in case O(hetzner_token) is specified. |
| 117 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 118 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 119 |
type: bool |
| 120 |
sample: false |
| 121 |
returned: success |
| 122 |
webdav: |
| 123 |
description: |
| 124 |
- Status of WebDAV support. |
| 125 |
- Note that this is copied from RV(subaccounts[].access_settings.webdav_enabled) in case O(hetzner_token) is specified. |
| 126 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 127 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 128 |
type: bool |
| 129 |
sample: true |
| 130 |
returned: success |
| 131 |
readonly: |
| 132 |
description: |
| 133 |
- Indicates if the sub-account is in readonly mode. |
| 134 |
- Note that this is copied from RV(subaccounts[].access_settings.readonly) in case O(hetzner_token) is specified. |
| 135 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 136 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 137 |
type: bool |
| 138 |
sample: false |
| 139 |
returned: success |
| 140 |
createtime: |
| 141 |
description: |
| 142 |
- Timestamp when the sub-account was created. |
| 143 |
- Note that this is copied from RV(subaccounts[].created) in case O(hetzner_token) is specified. |
| 144 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 145 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 146 |
type: str |
| 147 |
sample: "2023-08-25T14:23:05Z" |
| 148 |
returned: success |
| 149 |
comment: |
| 150 |
description: |
| 151 |
- Custom comment for the sub-account. |
| 152 |
- Note that this is copied from RV(subaccounts[].description) in case O(hetzner_token) is specified. |
| 153 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 154 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 155 |
type: str |
| 156 |
sample: "This is a subaccount" |
| 157 |
returned: success |
| 158 |
id: |
| 159 |
description: |
| 160 |
- The subaccount's ID. |
| 161 |
type: int |
| 162 |
sample: 42 |
| 163 |
version_added: 2.5.0 |
| 164 |
home_directory: |
| 165 |
description: |
| 166 |
- Home directory of the subaccount. |
| 167 |
type: str |
| 168 |
sample: "my_backups/host01.my.company" |
| 169 |
version_added: 2.5.0 |
| 170 |
access_settings: |
| 171 |
description: |
| 172 |
- Access settings of the subaccount. |
| 173 |
type: dict |
| 174 |
version_added: 2.5.0 |
| 175 |
contains: |
| 176 |
samba_enabled: |
| 177 |
description: |
| 178 |
- Whether the subaccount can be accessed through SAMBA. |
| 179 |
type: bool |
| 180 |
sample: false |
| 181 |
ssh_enabled: |
| 182 |
description: |
| 183 |
- Whether the subaccount can be accessed through SSH. |
| 184 |
type: bool |
| 185 |
sample: true |
| 186 |
webdav_enabled: |
| 187 |
description: |
| 188 |
- Whether the subaccount can be accessed through WebDAV. |
| 189 |
type: bool |
| 190 |
sample: false |
| 191 |
reachable_externally: |
| 192 |
description: |
| 193 |
- Whether the subaccount is reachable from outside Hetzner's network. |
| 194 |
type: bool |
| 195 |
sample: true |
| 196 |
readonly: |
| 197 |
description: |
| 198 |
- Whether the subaccount is read-only. |
| 199 |
type: bool |
| 200 |
sample: false |
| 201 |
description: |
| 202 |
description: |
| 203 |
- A user-defined description for the subaccount. |
| 204 |
type: str |
| 205 |
sample: "host01 backup" |
| 206 |
version_added: 2.5.0 |
| 207 |
created: |
| 208 |
description: |
| 209 |
- Creation timestamp in ISO-8601 format. |
| 210 |
type: str |
| 211 |
sample: "2025-02-22:00:02.000Z" |
| 212 |
version_added: 2.5.0 |
| 213 |
labels: |
| 214 |
description: |
| 215 |
- User-defined labels for the subaccount. |
| 216 |
type: dict |
| 217 |
version_added: 2.5.0 |
| 218 |
storage_box: |
| 219 |
description: |
| 220 |
- The associated storage box's ID. |
| 221 |
type: int |
| 222 |
sample: 42 |
| 223 |
version_added: 2.5.0 |
| 224 |
""" |
| 225 |
|
| 226 |
from ansible.module_utils.basic import AnsibleModule |
| 227 |
|
| 228 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 229 |
BASE_URL, |
| 230 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 231 |
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
| 232 |
fetch_url_json, |
| 233 |
) |
| 234 |
|
| 235 |
from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
| 236 |
API_BASE_URL, |
| 237 |
API_DEFAULT_ARGUMENT_SPEC, |
| 238 |
_API_DEFAULT_ARGUMENT_SPEC_COMPAT, |
| 239 |
api_fetch_url_json, |
| 240 |
) |
| 241 |
|
| 242 |
from ansible_collections.community.hrobot.plugins.module_utils._tagging import ( |
| 243 |
deprecate_value, |
| 244 |
) |
| 245 |
|
| 246 |
|
| 247 |
def adjust_legacy(subaccount): |
| 248 |
result = dict(subaccount) |
| 249 |
result['homedirectory'] = deprecate_value( |
| 250 |
subaccount['home_directory'], |
| 251 |
"The return value `homedirectory` is deprecated; use `home_directory` instead.", |
| 252 |
version="3.0.0", |
| 253 |
) |
| 254 |
result['samba'] = deprecate_value( |
| 255 |
subaccount['access_settings']['samba_enabled'], |
| 256 |
"The return value `samba` is deprecated; use `access_settings.samba_enabled` instead.", |
| 257 |
version="3.0.0", |
| 258 |
) |
| 259 |
result['ssh'] = deprecate_value( |
| 260 |
subaccount['access_settings']['ssh_enabled'], |
| 261 |
"The return value `ssh` is deprecated; use `access_settings.ssh_enabled` instead.", |
| 262 |
version="3.0.0", |
| 263 |
) |
| 264 |
result['webdav'] = deprecate_value( |
| 265 |
subaccount['access_settings']['webdav_enabled'], |
| 266 |
"The return value `webdav` is deprecated; use `access_settings.webdav_enabled` instead.", |
| 267 |
version="3.0.0", |
| 268 |
) |
| 269 |
result['external_reachability'] = deprecate_value( |
| 270 |
subaccount['access_settings']['reachable_externally'], |
| 271 |
"The return value `external_reachability` is deprecated; use `access_settings.reachable_externally` instead.", |
| 272 |
version="3.0.0", |
| 273 |
) |
| 274 |
result['readonly'] = deprecate_value( |
| 275 |
subaccount['access_settings']['readonly'], |
| 276 |
"The return value `readonly` is deprecated; use `access_settings.readonly` instead.", |
| 277 |
version="3.0.0", |
| 278 |
) |
| 279 |
result['createtime'] = deprecate_value( |
| 280 |
subaccount['created'], |
| 281 |
"The return value `createtime` is deprecated; use `created` instead.", |
| 282 |
version="3.0.0", |
| 283 |
) |
| 284 |
result['comment'] = deprecate_value( |
| 285 |
subaccount['description'], |
| 286 |
"The return value `comment` is deprecated; use `description` instead.", |
| 287 |
version="3.0.0", |
| 288 |
) |
| 289 |
return result |
| 290 |
|
| 291 |
|
| 292 |
def main(): |
| 293 |
argument_spec = dict( |
| 294 |
storagebox_id=dict(type="int", required=True), |
| 295 |
) |
| 296 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 297 |
argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED) |
| 298 |
argument_spec.update(API_DEFAULT_ARGUMENT_SPEC) |
| 299 |
argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT) |
| 300 |
module = AnsibleModule( |
| 301 |
argument_spec=argument_spec, |
| 302 |
supports_check_mode=True, |
| 303 |
) |
| 304 |
|
| 305 |
storagebox_id = module.params["storagebox_id"] |
| 306 |
|
| 307 |
if module.params["hetzner_user"] is not None: |
| 308 |
module.deprecate( |
| 309 |
"The hetzner_token parameter will be required from community.hrobot 3.0.0 on.", |
| 310 |
collection_name="community.hrobot", |
| 311 |
version="3.0.0", |
| 312 |
) |
| 313 |
# DEPRECATED: old API |
| 314 |
|
| 315 |
url = "{0}/storagebox/{1}/subaccount".format(BASE_URL, storagebox_id) |
| 316 |
result, error = fetch_url_json(module, url, accept_errors=["STORAGEBOX_NOT_FOUND"]) |
| 317 |
if error: |
| 318 |
module.fail_json( |
| 319 |
msg="Storagebox with ID {0} does not exist".format(storagebox_id) |
| 320 |
) |
| 321 |
|
| 322 |
module.exit_json( |
| 323 |
changed=False, |
| 324 |
subaccounts=[item["subaccount"] for item in result], |
| 325 |
) |
| 326 |
|
| 327 |
else: |
| 328 |
# NEW API! |
| 329 |
|
| 330 |
url = "{0}/v1/storage_boxes/{1}/subaccounts".format(API_BASE_URL, storagebox_id) |
| 331 |
result, dummy, error = api_fetch_url_json(module, url, accept_errors=['not_found']) |
| 332 |
if error: |
| 333 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 334 |
|
| 335 |
module.exit_json( |
| 336 |
changed=False, |
| 337 |
subaccounts=[adjust_legacy(item) for item in result['subaccounts']], |
| 338 |
) |
| 339 |
|
| 340 |
|
| 341 |
if __name__ == "__main__": # pragma: no cover |
| 342 |
main() # pragma: no cover |