| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2025 Felix Fontein <[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 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: storagebox |
| 14 |
short_description: Modify a storage box's basic configuration |
| 15 |
version_added: 2.1.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Modify a storage box's basic configuration. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.hrobot.api._robot_compat_shim_deprecation # must come before api and robot |
| 22 |
- community.hrobot.api |
| 23 |
- community.hrobot.robot |
| 24 |
- community.hrobot.attributes |
| 25 |
- community.hrobot.attributes._actiongroup_robot_and_api_deprecation # must come before the other two! |
| 26 |
- community.hrobot.attributes.actiongroup_api |
| 27 |
- community.hrobot.attributes.actiongroup_robot |
| 28 |
attributes: |
| 29 |
check_mode: |
| 30 |
support: full |
| 31 |
diff_mode: |
| 32 |
support: full |
| 33 |
idempotent: |
| 34 |
support: full |
| 35 |
|
| 36 |
options: |
| 37 |
hetzner_token: |
| 38 |
version_added: 2.5.0 |
| 39 |
id: |
| 40 |
description: |
| 41 |
- The ID of the storage box to modify. |
| 42 |
type: int |
| 43 |
required: true |
| 44 |
name: |
| 45 |
description: |
| 46 |
- The name of the storage box. |
| 47 |
type: str |
| 48 |
samba: |
| 49 |
description: |
| 50 |
- Whether the storage box is accessible through SAMBA. |
| 51 |
type: bool |
| 52 |
webdav: |
| 53 |
description: |
| 54 |
- Whether the storage box is accessible through WebDAV. |
| 55 |
type: bool |
| 56 |
ssh: |
| 57 |
description: |
| 58 |
- Whether the storage box is accessible through SSH. |
| 59 |
type: bool |
| 60 |
external_reachability: |
| 61 |
description: |
| 62 |
- Whether the storage box is externally reachable. |
| 63 |
type: bool |
| 64 |
zfs: |
| 65 |
description: |
| 66 |
- Whether the ZFS directory is visible. |
| 67 |
type: bool |
| 68 |
""" |
| 69 |
|
| 70 |
EXAMPLES = r""" |
| 71 |
--- |
| 72 |
- name: Setup storagebox |
| 73 |
community.hrobot.storagebox: |
| 74 |
hetzner_user: foo |
| 75 |
hetzner_password: bar |
| 76 |
name: "My storage box" |
| 77 |
ssh: true |
| 78 |
samba: false |
| 79 |
webdav: false |
| 80 |
external_reachability: false |
| 81 |
zfs: false |
| 82 |
""" |
| 83 |
|
| 84 |
RETURN = r""" |
| 85 |
name: |
| 86 |
description: |
| 87 |
- The storage box's name. |
| 88 |
type: str |
| 89 |
sample: Backup Server 1 |
| 90 |
returned: success |
| 91 |
webdav: |
| 92 |
description: |
| 93 |
- Whether WebDAV is active. |
| 94 |
type: bool |
| 95 |
sample: true |
| 96 |
returned: success |
| 97 |
samba: |
| 98 |
description: |
| 99 |
- Whether SAMBA is active. |
| 100 |
type: bool |
| 101 |
sample: true |
| 102 |
returned: success |
| 103 |
ssh: |
| 104 |
description: |
| 105 |
- Whether SSH is active. |
| 106 |
type: bool |
| 107 |
sample: true |
| 108 |
returned: success |
| 109 |
external_reachability: |
| 110 |
description: |
| 111 |
- Whether the storage box is reachable externally. |
| 112 |
type: bool |
| 113 |
sample: true |
| 114 |
returned: success |
| 115 |
zfs: |
| 116 |
description: |
| 117 |
- Shows whether the ZFS directory is visible. |
| 118 |
type: bool |
| 119 |
sample: false |
| 120 |
returned: success |
| 121 |
""" |
| 122 |
|
| 123 |
from ansible.module_utils.basic import AnsibleModule |
| 124 |
from ansible.module_utils.common.text.converters import to_native |
| 125 |
|
| 126 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 127 |
BASE_URL, |
| 128 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 129 |
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
| 130 |
fetch_url_json, |
| 131 |
) |
| 132 |
|
| 133 |
from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
| 134 |
API_BASE_URL, |
| 135 |
API_DEFAULT_ARGUMENT_SPEC, |
| 136 |
_API_DEFAULT_ARGUMENT_SPEC_COMPAT, |
| 137 |
ApplyActionError, |
| 138 |
api_apply_action, |
| 139 |
api_fetch_url_json, |
| 140 |
) |
| 141 |
|
| 142 |
try: |
| 143 |
from urllib.parse import urlencode |
| 144 |
except ImportError: |
| 145 |
# Python 2.x fallback: |
| 146 |
from urllib import urlencode |
| 147 |
|
| 148 |
|
| 149 |
PARAMETERS_LEGACY = { |
| 150 |
'name': ('name', 'storagebox_name'), |
| 151 |
'webdav': ('webdav', 'webdav'), |
| 152 |
'samba': ('samba', 'samba'), |
| 153 |
'ssh': ('ssh', 'ssh'), |
| 154 |
'external_reachability': ('external_reachability', 'external_reachability'), |
| 155 |
'zfs': ('zfs', 'zfs'), |
| 156 |
} |
| 157 |
|
| 158 |
UPDATE_PARAMETERS = { |
| 159 |
'name': ('name', ['name'], 'name'), |
| 160 |
} |
| 161 |
|
| 162 |
ACTION_PARAMETERS = { |
| 163 |
'webdav': ('webdav', ['access_settings', 'webdav_enabled'], 'webdav_enabled'), |
| 164 |
'samba': ('samba', ['access_settings', 'samba_enabled'], 'samba_enabled'), |
| 165 |
'ssh': ('ssh', ['access_settings', 'ssh_enabled'], 'ssh_enabled'), |
| 166 |
'external_reachability': ('external_reachability', ['access_settings', 'reachable_externally'], 'reachable_externally'), |
| 167 |
'zfs': ('zfs', ['access_settings', 'zfs_enabled'], 'zfs_enabled'), |
| 168 |
} |
| 169 |
|
| 170 |
PARAMETERS = dict(UPDATE_PARAMETERS) |
| 171 |
PARAMETERS.update(ACTION_PARAMETERS) |
| 172 |
|
| 173 |
|
| 174 |
def extract_legacy(result): |
| 175 |
sb = result['storagebox'] |
| 176 |
return {key: sb.get(key) for key, dummy in PARAMETERS_LEGACY.values()} |
| 177 |
|
| 178 |
|
| 179 |
def extract(result): |
| 180 |
sb = result['storage_box'] |
| 181 |
|
| 182 |
def get(keys): |
| 183 |
value = sb |
| 184 |
for key in keys: |
| 185 |
value = value[key] |
| 186 |
return value |
| 187 |
|
| 188 |
return {data_key: get(keys) for data_key, keys, dummy2 in PARAMETERS.values()} |
| 189 |
|
| 190 |
|
| 191 |
def main(): |
| 192 |
argument_spec = dict( |
| 193 |
id=dict(type='int', required=True), |
| 194 |
name=dict(type='str'), |
| 195 |
samba=dict(type='bool'), |
| 196 |
webdav=dict(type='bool'), |
| 197 |
ssh=dict(type='bool'), |
| 198 |
external_reachability=dict(type='bool'), |
| 199 |
zfs=dict(type='bool'), |
| 200 |
) |
| 201 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 202 |
argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED) |
| 203 |
argument_spec.update(API_DEFAULT_ARGUMENT_SPEC) |
| 204 |
argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT) |
| 205 |
module = AnsibleModule( |
| 206 |
argument_spec=argument_spec, |
| 207 |
supports_check_mode=True, |
| 208 |
required_together=[("hetzner_user", "hetzner_password")], |
| 209 |
required_one_of=[("hetzner_user", "hetzner_token")], |
| 210 |
mutually_exclusive=[("hetzner_user", "hetzner_token")], |
| 211 |
) |
| 212 |
|
| 213 |
storagebox_id = module.params['id'] |
| 214 |
before = {} |
| 215 |
after = {} |
| 216 |
changes = {} |
| 217 |
|
| 218 |
if module.params["hetzner_user"] is not None: |
| 219 |
module.deprecate( |
| 220 |
"The hetzner_token parameter will be required from community.hrobot 3.0.0 on.", |
| 221 |
collection_name="community.hrobot", |
| 222 |
version="3.0.0", |
| 223 |
) |
| 224 |
# DEPRECATED: old API |
| 225 |
url = "{0}/storagebox/{1}".format(BASE_URL, storagebox_id) |
| 226 |
result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND']) |
| 227 |
if error: |
| 228 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 229 |
|
| 230 |
before = extract_legacy(result) |
| 231 |
after = dict(before) |
| 232 |
|
| 233 |
for option_name, (data_name, change_name) in PARAMETERS_LEGACY.items(): |
| 234 |
value = module.params[option_name] |
| 235 |
if value is not None: |
| 236 |
if before[data_name] != value: |
| 237 |
after[data_name] = value |
| 238 |
if isinstance(value, bool): |
| 239 |
changes[change_name] = str(value).lower() |
| 240 |
else: |
| 241 |
changes[change_name] = value |
| 242 |
|
| 243 |
if changes and not module.check_mode: |
| 244 |
headers = {"Content-type": "application/x-www-form-urlencoded"} |
| 245 |
result, error = fetch_url_json( |
| 246 |
module, |
| 247 |
url, |
| 248 |
data=urlencode(changes), |
| 249 |
headers=headers, |
| 250 |
method='POST', |
| 251 |
accept_errors=['INVALID_INPUT'], |
| 252 |
) |
| 253 |
if error: |
| 254 |
invalid = result['error'].get('invalid') or [] |
| 255 |
module.fail_json(msg='The values to update were invalid ({0})'.format(', '.join(invalid))) |
| 256 |
after = extract_legacy(result) |
| 257 |
|
| 258 |
else: |
| 259 |
# NEW API! |
| 260 |
url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id) |
| 261 |
result, dummy, error = api_fetch_url_json(module, url, accept_errors=['not_found']) |
| 262 |
if error: |
| 263 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 264 |
|
| 265 |
before = extract(result) |
| 266 |
after = dict(before) |
| 267 |
|
| 268 |
update = {} |
| 269 |
for option_name, (data_name, dummy, change_name) in UPDATE_PARAMETERS.items(): |
| 270 |
value = module.params[option_name] |
| 271 |
if value is not None: |
| 272 |
if before[data_name] != value: |
| 273 |
after[data_name] = value |
| 274 |
changes[change_name] = value |
| 275 |
update[change_name] = value |
| 276 |
|
| 277 |
action = {} |
| 278 |
update_after_update = {} |
| 279 |
for option_name, (data_name, dummy, change_name) in ACTION_PARAMETERS.items(): |
| 280 |
value = module.params[option_name] |
| 281 |
if value is not None: |
| 282 |
if before[data_name] != value: |
| 283 |
after[data_name] = value |
| 284 |
update_after_update[data_name] = value |
| 285 |
changes[change_name] = value |
| 286 |
action[change_name] = value |
| 287 |
|
| 288 |
if update and not module.check_mode: |
| 289 |
headers = {"Content-type": "application/json"} |
| 290 |
result, dummy, error = api_fetch_url_json( |
| 291 |
module, |
| 292 |
url, |
| 293 |
data=module.jsonify(update), |
| 294 |
headers=headers, |
| 295 |
method='PUT', |
| 296 |
accept_errors=['invalid_input'], |
| 297 |
) |
| 298 |
if error: |
| 299 |
details = result['error'].get('details') or {} |
| 300 |
fields = details.get("fields") or [] |
| 301 |
details_str = ", ".join(['{0}: {1}'.format(to_native(field["name"]), to_native(field["message"])) for field in fields]) |
| 302 |
module.fail_json(msg='The values to update were invalid ({0})'.format(details_str or "no details")) |
| 303 |
after = extract(result) |
| 304 |
|
| 305 |
if action and not module.check_mode: |
| 306 |
after.update(update_after_update) |
| 307 |
action_url = "{0}/actions/update_access_settings".format(url) |
| 308 |
try: |
| 309 |
api_apply_action( |
| 310 |
module, |
| 311 |
action_url, |
| 312 |
action, |
| 313 |
lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id), |
| 314 |
check_done_delay=1, |
| 315 |
check_done_timeout=60, |
| 316 |
) |
| 317 |
except ApplyActionError as exc: |
| 318 |
module.fail_json(msg='Error while updating access settings: {0}'.format(exc)) |
| 319 |
|
| 320 |
result = dict(after) |
| 321 |
result['changed'] = bool(changes) |
| 322 |
result['diff'] = { |
| 323 |
'before': before, |
| 324 |
'after': after, |
| 325 |
} |
| 326 |
module.exit_json(**result) |
| 327 |
|
| 328 |
|
| 329 |
if __name__ == '__main__': # pragma: no cover |
| 330 |
main() # pragma: no cover |