| 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_snapshot |
| 14 |
short_description: Create, update, or delete a snapshot of a storage box |
| 15 |
version_added: 2.3.0 |
| 16 |
author: |
| 17 |
- Matthias Hurdebise (@matthiashurdebise) |
| 18 |
description: |
| 19 |
- Create, update comment, or delete a snapshot of a storage box. |
| 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: none |
| 33 |
idempotent: |
| 34 |
support: partial |
| 35 |
details: |
| 36 |
- This module is not idempotent when creating a snapshot. |
| 37 |
options: |
| 38 |
hetzner_token: |
| 39 |
version_added: 2.5.0 |
| 40 |
storagebox_id: |
| 41 |
description: |
| 42 |
- The ID of the storage box to snapshot. |
| 43 |
type: int |
| 44 |
required: true |
| 45 |
snapshot_name: |
| 46 |
description: |
| 47 |
- The name of the snapshot to comment or delete. |
| 48 |
- The snapshot name is automatically generated and should not be specified when creating a snapshot. |
| 49 |
- Required when setting O(state) to V(absent), or when O(snapshot_comment) is specified. |
| 50 |
type: str |
| 51 |
state: |
| 52 |
description: |
| 53 |
- The state of the snapshot. |
| 54 |
type: str |
| 55 |
default: present |
| 56 |
choices: |
| 57 |
- present |
| 58 |
- absent |
| 59 |
snapshot_comment: |
| 60 |
description: |
| 61 |
- The comment to set for the snapshot. |
| 62 |
type: str |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
--- |
| 67 |
- name: Create a snapshot |
| 68 |
community.hrobot.storagebox_snapshot: |
| 69 |
storagebox_id: 12345 |
| 70 |
# The snapshot name is automatically generated and should not be specified. |
| 71 |
|
| 72 |
- name: Delete a snapshot |
| 73 |
community.hrobot.storagebox_snapshot: |
| 74 |
storagebox_id: 12345 |
| 75 |
snapshot_name: "2025-01-21T12-40-38" |
| 76 |
state: absent |
| 77 |
|
| 78 |
- name: Update snapshot comment |
| 79 |
community.hrobot.storagebox_snapshot: |
| 80 |
storagebox_id: 12345 |
| 81 |
snapshot_name: "2025-01-21T12-40-38" |
| 82 |
snapshot_comment: "This is an updated comment" |
| 83 |
""" |
| 84 |
|
| 85 |
RETURN = r""" |
| 86 |
snapshot: |
| 87 |
description: |
| 88 |
- The snapshot that was created or updated. |
| 89 |
returned: success and O(state=present) |
| 90 |
type: dict |
| 91 |
contains: |
| 92 |
name: |
| 93 |
description: The name of the snapshot. |
| 94 |
type: str |
| 95 |
sample: "2025-01-21T12-40-38" |
| 96 |
timestamp: |
| 97 |
description: Timestamp of snapshot in UTC |
| 98 |
type: str |
| 99 |
sample: "2025-01-21T12:40:38+00:00" |
| 100 |
size: |
| 101 |
description: The size of the snapshot in MB. |
| 102 |
type: int |
| 103 |
sample: 400 |
| 104 |
""" |
| 105 |
|
| 106 |
from ansible.module_utils.basic import AnsibleModule |
| 107 |
|
| 108 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 109 |
BASE_URL, |
| 110 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 111 |
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
| 112 |
fetch_url_json, |
| 113 |
) |
| 114 |
|
| 115 |
from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
| 116 |
API_BASE_URL, |
| 117 |
API_DEFAULT_ARGUMENT_SPEC, |
| 118 |
_API_DEFAULT_ARGUMENT_SPEC_COMPAT, |
| 119 |
ApplyActionError, |
| 120 |
api_apply_action, |
| 121 |
api_fetch_url_json, |
| 122 |
) |
| 123 |
|
| 124 |
try: |
| 125 |
from urllib.parse import urlencode |
| 126 |
except ImportError: |
| 127 |
# Python 2.x fallback: |
| 128 |
from urllib import urlencode |
| 129 |
|
| 130 |
|
| 131 |
def legacy_handle_errors(module, error, storagebox_id=None, snapshot_name=None): |
| 132 |
error_messages = { |
| 133 |
"STORAGEBOX_NOT_FOUND": "Storagebox with ID {0} does not exist".format(storagebox_id), |
| 134 |
"SNAPSHOT_NOT_FOUND": "Snapshot with name {0} does not exist".format(snapshot_name), |
| 135 |
"SNAPSHOT_LIMIT_EXCEEDED": "Snapshot limit exceeded", |
| 136 |
} |
| 137 |
module.fail_json(msg=error_messages.get(error, error)) |
| 138 |
|
| 139 |
|
| 140 |
def extract_legacy(snapshot): |
| 141 |
return { |
| 142 |
'id': snapshot['id'], |
| 143 |
'name': snapshot['name'], |
| 144 |
'comment': snapshot['description'], |
| 145 |
'timestamp': snapshot['created'], |
| 146 |
'size': snapshot['stats']['size'] // (1024 * 1024), |
| 147 |
'filesystem_size': snapshot['stats']['size_filesystem'] // (1024 * 1024), |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
def main(): |
| 152 |
argument_spec = dict( |
| 153 |
storagebox_id=dict(type='int', required=True), |
| 154 |
snapshot_name=dict(type='str'), |
| 155 |
state=dict(type='str', default="present", |
| 156 |
choices=['present', 'absent']), |
| 157 |
snapshot_comment=dict(type='str') |
| 158 |
) |
| 159 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 160 |
argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED) |
| 161 |
argument_spec.update(API_DEFAULT_ARGUMENT_SPEC) |
| 162 |
argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT) |
| 163 |
|
| 164 |
module = AnsibleModule( |
| 165 |
argument_spec=argument_spec, |
| 166 |
required_if=[["state", "absent", ["snapshot_name"]]], |
| 167 |
supports_check_mode=True |
| 168 |
) |
| 169 |
|
| 170 |
storagebox_id = module.params['storagebox_id'] |
| 171 |
state = module.params['state'] |
| 172 |
snapshot_name = module.params['snapshot_name'] |
| 173 |
snapshot_comment = module.params['snapshot_comment'] |
| 174 |
|
| 175 |
if module.params["hetzner_user"] is not None: |
| 176 |
module.deprecate( |
| 177 |
"The hetzner_token parameter will be required from community.hrobot 3.0.0 on.", |
| 178 |
collection_name="community.hrobot", |
| 179 |
version="3.0.0", |
| 180 |
) |
| 181 |
# DEPRECATED: old API |
| 182 |
|
| 183 |
# Create snapshot |
| 184 |
if state == 'present' and not snapshot_name: |
| 185 |
if module.check_mode: |
| 186 |
module.exit_json(changed=True) |
| 187 |
snapshot = legacy_create_snapshot(module, storagebox_id) |
| 188 |
|
| 189 |
# Add the comment if provided |
| 190 |
if snapshot_comment is not None: |
| 191 |
legacy_update_snapshot_comment(module, storagebox_id, snapshot['name'], snapshot_comment) |
| 192 |
snapshot['comment'] = snapshot_comment |
| 193 |
|
| 194 |
module.exit_json(changed=True, snapshot=snapshot) |
| 195 |
|
| 196 |
# Update snapshot comment |
| 197 |
elif state == 'present' and snapshot_name: |
| 198 |
if snapshot_comment is None: |
| 199 |
module.fail_json(msg="snapshot_comment is required when updating a snapshot") |
| 200 |
|
| 201 |
snapshots = legacy_fetch_snapshots(module=module, storagebox_id=storagebox_id) |
| 202 |
snapshot = legacy_get_snapshot_by_name(snapshots, snapshot_name) |
| 203 |
if not snapshot: |
| 204 |
legacy_handle_errors(module, "SNAPSHOT_NOT_FOUND", snapshot_name=snapshot_name) |
| 205 |
if snapshot_comment != snapshot['comment']: |
| 206 |
if not module.check_mode: |
| 207 |
legacy_update_snapshot_comment(module, storagebox_id, snapshot_name, snapshot_comment) |
| 208 |
module.exit_json(changed=True, snapshot=snapshot) |
| 209 |
else: |
| 210 |
module.exit_json(changed=False, snapshot=snapshot) |
| 211 |
|
| 212 |
# Delete snapshot |
| 213 |
else: |
| 214 |
snapshots = legacy_fetch_snapshots(module=module, storagebox_id=storagebox_id) |
| 215 |
snapshot = legacy_get_snapshot_by_name(snapshots, snapshot_name) |
| 216 |
if snapshot: |
| 217 |
if not module.check_mode: |
| 218 |
legacy_delete_snapshot(module, storagebox_id, snapshot_name) |
| 219 |
module.exit_json(changed=True) |
| 220 |
else: |
| 221 |
module.exit_json(changed=False) |
| 222 |
|
| 223 |
else: |
| 224 |
# NEW API! |
| 225 |
|
| 226 |
# Create snapshot |
| 227 |
if state == 'present' and not snapshot_name: |
| 228 |
if module.check_mode: |
| 229 |
module.exit_json(changed=True) |
| 230 |
action_url = "{0}/v1/storage_boxes/{1}/snapshots".format(API_BASE_URL, storagebox_id) |
| 231 |
action = {} |
| 232 |
if snapshot_comment: |
| 233 |
action["description"] = snapshot_comment |
| 234 |
try: |
| 235 |
extracted_ids, error = api_apply_action( |
| 236 |
module, |
| 237 |
action_url, |
| 238 |
action, |
| 239 |
lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id), |
| 240 |
check_done_delay=1, |
| 241 |
check_done_timeout=120, |
| 242 |
accept_errors=["not_found"], |
| 243 |
) |
| 244 |
except ApplyActionError as exc: |
| 245 |
module.fail_json(msg='Error while creating snapshot: {0}'.format(exc)) |
| 246 |
|
| 247 |
if error == "not_found": |
| 248 |
module.fail_json(msg="Storagebox with ID {0} does not exist".format(storagebox_id)) |
| 249 |
|
| 250 |
new_snapshot_id = extracted_ids["storage_box_snapshot"] |
| 251 |
# Retrieve created snapshot |
| 252 |
url = "{0}/v1/storage_boxes/{1}/snapshots/{2}".format(API_BASE_URL, storagebox_id, new_snapshot_id) |
| 253 |
snapshot = api_fetch_url_json(module, url, method='GET')[0]["snapshot"] |
| 254 |
|
| 255 |
module.exit_json(changed=True, snapshot=extract_legacy(snapshot)) |
| 256 |
|
| 257 |
# Update snapshot comment |
| 258 |
elif state == 'present' and snapshot_name: |
| 259 |
if snapshot_comment is None: |
| 260 |
module.fail_json(msg="snapshot_comment is required when updating a snapshot") |
| 261 |
|
| 262 |
snapshot = find_snapshot(module, storagebox_id, snapshot_name) |
| 263 |
if not snapshot: |
| 264 |
module.fail_json(msg="Snapshot with name {0} does not exist".format(snapshot_name)) |
| 265 |
if snapshot_comment == snapshot['description']: |
| 266 |
module.exit_json(changed=False, snapshot=extract_legacy(snapshot)) |
| 267 |
if not module.check_mode: |
| 268 |
url = "{0}/v1/storage_boxes/{1}/snapshots/{2}".format(API_BASE_URL, storagebox_id, snapshot['id']) |
| 269 |
headers = {"Content-type": "application/json"} |
| 270 |
result, dummy, dummy2 = api_fetch_url_json( |
| 271 |
module, |
| 272 |
url, |
| 273 |
method='PUT', |
| 274 |
data=module.jsonify({"description": snapshot_comment}), |
| 275 |
headers=headers, |
| 276 |
) |
| 277 |
snapshot = result["snapshot"] |
| 278 |
else: |
| 279 |
snapshot['description'] = snapshot_comment |
| 280 |
module.exit_json(changed=True, snapshot=extract_legacy(snapshot)) |
| 281 |
|
| 282 |
# Delete snapshot |
| 283 |
else: |
| 284 |
snapshot = find_snapshot(module, storagebox_id, snapshot_name) |
| 285 |
if not snapshot: |
| 286 |
module.exit_json(changed=False) |
| 287 |
if not module.check_mode: |
| 288 |
action_url = "{0}/v1/storage_boxes/{1}/snapshots/{2}".format(API_BASE_URL, storagebox_id, snapshot['id']) |
| 289 |
try: |
| 290 |
api_apply_action( |
| 291 |
module, |
| 292 |
action_url, |
| 293 |
None, |
| 294 |
lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id), |
| 295 |
method='DELETE', |
| 296 |
check_done_delay=1, |
| 297 |
check_done_timeout=120, |
| 298 |
) |
| 299 |
except ApplyActionError as exc: |
| 300 |
module.fail_json(msg='Error while deleting snapshot: {0}'.format(exc)) |
| 301 |
module.exit_json(changed=True) |
| 302 |
|
| 303 |
|
| 304 |
def legacy_delete_snapshot(module, storagebox_id, snapshot_name): |
| 305 |
url = "{0}/storagebox/{1}/snapshot/{2}".format(BASE_URL, storagebox_id, snapshot_name) |
| 306 |
fetch_url_json(module, url, method="DELETE", allow_empty_result=True) |
| 307 |
|
| 308 |
|
| 309 |
def legacy_update_snapshot_comment(module, storagebox_id, snapshot_name, snapshot_comment): |
| 310 |
url = "{0}/storagebox/{1}/snapshot/{2}/comment".format(BASE_URL, storagebox_id, snapshot_name) |
| 311 |
headers = {"Content-type": "application/x-www-form-urlencoded"} |
| 312 |
fetch_url_json( |
| 313 |
module, url, method="POST", data=urlencode({"comment": snapshot_comment}), headers=headers, allow_empty_result=True, |
| 314 |
) |
| 315 |
|
| 316 |
|
| 317 |
def legacy_create_snapshot(module, storagebox_id): |
| 318 |
url = "{0}/storagebox/{1}/snapshot".format(BASE_URL, storagebox_id) |
| 319 |
result, error = fetch_url_json( |
| 320 |
module, url, method="POST", accept_errors=["STORAGEBOX_NOT_FOUND", "SNAPSHOT_LIMIT_EXCEEDED"], |
| 321 |
) |
| 322 |
if error: |
| 323 |
legacy_handle_errors(module, error, storagebox_id) |
| 324 |
return result['snapshot'] |
| 325 |
|
| 326 |
|
| 327 |
def legacy_get_snapshot_by_name(snapshots, name): |
| 328 |
for snapshot in snapshots: |
| 329 |
if snapshot['name'] == name: |
| 330 |
return snapshot |
| 331 |
return None |
| 332 |
|
| 333 |
|
| 334 |
def legacy_fetch_snapshots(module, storagebox_id): |
| 335 |
url = "{0}/storagebox/{1}/snapshot".format(BASE_URL, storagebox_id) |
| 336 |
result, error = fetch_url_json(module, url, method="GET", accept_errors=["STORAGEBOX_NOT_FOUND"]) |
| 337 |
if error: |
| 338 |
legacy_handle_errors(module, error, storagebox_id) |
| 339 |
return [item['snapshot'] for item in result] |
| 340 |
|
| 341 |
|
| 342 |
def find_snapshot(module, storagebox_id, snapshot_name): |
| 343 |
url = "{0}/v1/storage_boxes/{1}/snapshots?{2}".format(API_BASE_URL, storagebox_id, urlencode({'name': snapshot_name})) |
| 344 |
result, dummy, error = api_fetch_url_json(module, url, accept_errors=['not_found']) |
| 345 |
if error: |
| 346 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 347 |
snapshots = [snapshot for snapshot in result["snapshots"] if snapshot['name'] == snapshot_name] |
| 348 |
if len(snapshots) > 1: |
| 349 |
module.fail_json(msg='Found {0} snapshots with name {1!r}'.format(len(snapshots), snapshot_name)) |
| 350 |
return snapshots[0] if snapshots else None |
| 351 |
|
| 352 |
|
| 353 |
if __name__ == '__main__': # pragma: no cover |
| 354 |
main() # pragma: no cover |