| 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_snapshot |
| 12 |
|
| 13 |
short_description: Create and manage Storage Box Snapshots in Hetzner. |
| 14 |
|
| 15 |
description: |
| 16 |
- Create, update and delete Storage Box Snapshots in Hetzner. |
| 17 |
- See the L(Storage Box Snapshots API documentation,https://docs.hetzner.cloud/reference/hetzner#storage-box-snapshots) 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 Snapshot to manage. |
| 32 |
- Required when updating or deleting and if no Storage Box Snapshot 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 Snapshot to manage. |
| 38 |
- Storage Box Snapshot names are defined by the API and cannot be changed. |
| 39 |
- Required when updating or deleting and if no Storage Box Snapshot O(id) is given. |
| 40 |
type: str |
| 41 |
description: |
| 42 |
description: |
| 43 |
- Description of the Storage Box Snapshot. |
| 44 |
type: str |
| 45 |
labels: |
| 46 |
description: |
| 47 |
- User-defined labels (key-value pairs) for the Storage Box Snapshot. |
| 48 |
type: dict |
| 49 |
state: |
| 50 |
description: |
| 51 |
- State of the Storage Box Snapshot. |
| 52 |
default: present |
| 53 |
choices: [absent, present] |
| 54 |
type: str |
| 55 |
|
| 56 |
extends_documentation_fragment: |
| 57 |
- hetzner.hcloud.hcloud |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = """ |
| 61 |
- name: Create a Storage Box Snapshot |
| 62 |
hetzner.hcloud.storage_box_snapshot: |
| 63 |
storage_box: my-storage-box |
| 64 |
description: before app migration |
| 65 |
labels: |
| 66 |
env: prod |
| 67 |
state: present |
| 68 |
|
| 69 |
- name: Delete a Storage Box Snapshot by name |
| 70 |
hetzner.hcloud.storage_box_snapshot: |
| 71 |
storage_box: my-storage-box |
| 72 |
name: 2025-12-03T13-47-47 |
| 73 |
state: absent |
| 74 |
|
| 75 |
- name: Delete a Storage Box Snapshot by id |
| 76 |
hetzner.hcloud.storage_box_snapshot: |
| 77 |
storage_box: 497436 |
| 78 |
id: 405920 |
| 79 |
state: absent |
| 80 |
""" |
| 81 |
|
| 82 |
RETURN = """ |
| 83 |
hcloud_storage_box_snapshot: |
| 84 |
description: Details about the Storage Box Snapshot. |
| 85 |
returned: always |
| 86 |
type: dict |
| 87 |
contains: |
| 88 |
storage_box: |
| 89 |
description: ID of the parent Storage Box. |
| 90 |
returned: always |
| 91 |
type: int |
| 92 |
sample: 497436 |
| 93 |
id: |
| 94 |
description: ID of the Storage Box Snapshot. |
| 95 |
returned: always |
| 96 |
type: int |
| 97 |
sample: 405920 |
| 98 |
name: |
| 99 |
description: Name of the Storage Box Snapshot. |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: 2025-02-12T11-35-19 |
| 103 |
description: |
| 104 |
description: Description of the Storage Box Snapshot. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: before app migration |
| 108 |
labels: |
| 109 |
description: User-defined labels (key-value pairs) of the Storage Box Snapshot. |
| 110 |
returned: always |
| 111 |
type: dict |
| 112 |
sample: |
| 113 |
env: prod |
| 114 |
stats: |
| 115 |
description: Statistics of the Storage Box Snapshot. |
| 116 |
returned: always |
| 117 |
type: dict |
| 118 |
contains: |
| 119 |
size: |
| 120 |
description: Current storage requirements of the Snapshot in bytes. |
| 121 |
returned: always |
| 122 |
type: int |
| 123 |
sample: 10485760 |
| 124 |
size_filesystem: |
| 125 |
description: Size of the compressed file system contained in the Snapshot in bytes. |
| 126 |
returned: always |
| 127 |
type: int |
| 128 |
sample: 10485760 |
| 129 |
is_automatic: |
| 130 |
description: Whether the Storage Box Snapshot was created automatically. |
| 131 |
returned: always |
| 132 |
type: bool |
| 133 |
sample: false |
| 134 |
created: |
| 135 |
description: Point in time when the Storage Box Snapshot was created (in RFC3339 format). |
| 136 |
returned: always |
| 137 |
type: str |
| 138 |
sample: "2025-12-03T13:47:47Z" |
| 139 |
""" |
| 140 |
|
| 141 |
from ..module_utils import _storage_box, _storage_box_snapshot |
| 142 |
from ..module_utils._base import AnsibleHCloud, AnsibleModule |
| 143 |
from ..module_utils._vendor.hcloud import HCloudException |
| 144 |
from ..module_utils._vendor.hcloud.storage_boxes import ( |
| 145 |
BoundStorageBox, |
| 146 |
BoundStorageBoxSnapshot, |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
class AnsibleStorageBoxSnapshot(AnsibleHCloud): |
| 151 |
represent = "storage_box_snapshot" |
| 152 |
|
| 153 |
storage_box: BoundStorageBox | None = None |
| 154 |
storage_box_snapshot: BoundStorageBoxSnapshot | None = None |
| 155 |
|
| 156 |
def _prepare_result(self): |
| 157 |
if self.storage_box_snapshot is None: |
| 158 |
return {} |
| 159 |
return _storage_box_snapshot.prepare_result(self.storage_box_snapshot) |
| 160 |
|
| 161 |
def _fetch(self): |
| 162 |
self.storage_box = _storage_box.get(self.client.storage_boxes, self.module.params.get("storage_box")) |
| 163 |
|
| 164 |
if (value := self.module.params.get("id")) is not None: |
| 165 |
self.storage_box_snapshot = self.storage_box.get_snapshot_by_id(value) |
| 166 |
elif (value := self.module.params.get("name")) is not None: |
| 167 |
self.storage_box_snapshot = self.storage_box.get_snapshot_by_name(value) |
| 168 |
|
| 169 |
def _create(self): |
| 170 |
params = {} |
| 171 |
|
| 172 |
if (value := self.module.params.get("description")) is not None: |
| 173 |
params["description"] = value |
| 174 |
|
| 175 |
if (value := self.module.params.get("labels")) is not None: |
| 176 |
params["labels"] = value |
| 177 |
|
| 178 |
if not self.module.check_mode: |
| 179 |
resp = self.storage_box.create_snapshot(**params) |
| 180 |
self.storage_box_snapshot = resp.snapshot |
| 181 |
resp.action.wait_until_finished() |
| 182 |
|
| 183 |
self.storage_box_snapshot.reload() |
| 184 |
|
| 185 |
self._mark_as_changed() |
| 186 |
|
| 187 |
def _update(self): |
| 188 |
self.fail_on_invalid_params( |
| 189 |
required_one_of=[["id", "name"]], |
| 190 |
) |
| 191 |
|
| 192 |
params = {} |
| 193 |
if (value := self.module.params.get("description")) is not None: |
| 194 |
if value != self.storage_box_snapshot.description: |
| 195 |
params["description"] = value |
| 196 |
self._mark_as_changed() |
| 197 |
|
| 198 |
if (value := self.module.params.get("labels")) is not None: |
| 199 |
if value != self.storage_box_snapshot.labels: |
| 200 |
params["labels"] = value |
| 201 |
self._mark_as_changed() |
| 202 |
|
| 203 |
# Update only if params holds changes |
| 204 |
if params: |
| 205 |
if not self.module.check_mode: |
| 206 |
self.storage_box_snapshot = self.storage_box_snapshot.update(**params) |
| 207 |
|
| 208 |
def _delete(self): |
| 209 |
if not self.module.check_mode: |
| 210 |
resp = self.storage_box_snapshot.delete() |
| 211 |
resp.action.wait_until_finished() |
| 212 |
|
| 213 |
self.storage_box_snapshot = None |
| 214 |
self._mark_as_changed() |
| 215 |
|
| 216 |
def present(self): |
| 217 |
try: |
| 218 |
self._fetch() |
| 219 |
if self.storage_box_snapshot is None: |
| 220 |
self._create() |
| 221 |
else: |
| 222 |
self._update() |
| 223 |
|
| 224 |
except HCloudException as exception: |
| 225 |
self.fail_json_hcloud(exception) |
| 226 |
|
| 227 |
def absent(self): |
| 228 |
try: |
| 229 |
self._fetch() |
| 230 |
if self.storage_box_snapshot is None: |
| 231 |
return |
| 232 |
self._delete() |
| 233 |
|
| 234 |
except HCloudException as exception: |
| 235 |
self.fail_json_hcloud(exception) |
| 236 |
|
| 237 |
@classmethod |
| 238 |
def define_module(cls): |
| 239 |
return AnsibleModule( |
| 240 |
argument_spec=dict( |
| 241 |
storage_box={"type": "str", "required": True}, |
| 242 |
id={"type": "int"}, |
| 243 |
name={"type": "str"}, |
| 244 |
description={"type": "str"}, |
| 245 |
labels={"type": "dict"}, |
| 246 |
state={ |
| 247 |
"choices": ["absent", "present"], |
| 248 |
"default": "present", |
| 249 |
}, |
| 250 |
**super().base_module_arguments(), |
| 251 |
), |
| 252 |
supports_check_mode=True, |
| 253 |
) |
| 254 |
|
| 255 |
|
| 256 |
def main(): |
| 257 |
module = AnsibleStorageBoxSnapshot.define_module() |
| 258 |
o = AnsibleStorageBoxSnapshot(module) |
| 259 |
|
| 260 |
match module.params.get("state"): |
| 261 |
case "absent": |
| 262 |
o.absent() |
| 263 |
case _: |
| 264 |
o.present() |
| 265 |
|
| 266 |
result = o.get_result() |
| 267 |
|
| 268 |
# Legacy return value naming pattern |
| 269 |
result["hcloud_storage_box_snapshot"] = result.pop(o.represent) |
| 270 |
|
| 271 |
module.exit_json(**result) |
| 272 |
|
| 273 |
|
| 274 |
if __name__ == "__main__": |
| 275 |
main() |