| 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_info |
| 12 |
|
| 13 |
short_description: Gather infos about Hetzner Storage Box Snapshots. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather infos about Hetzner Storage Box Snapshots. |
| 17 |
- See the L(Storage Boxes API documentation,https://docs.hetzner.cloud/reference/hetzner#storage-boxes) 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 get. |
| 32 |
- If the ID is invalid, the module will fail. |
| 33 |
type: int |
| 34 |
name: |
| 35 |
description: |
| 36 |
- Name of the Storage Box Snapshot to get. |
| 37 |
type: str |
| 38 |
label_selector: |
| 39 |
description: |
| 40 |
- Label selector to filter the Storage Box Snapshots to get. |
| 41 |
type: str |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- hetzner.hcloud.hcloud |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = """ |
| 48 |
- name: Gather all Storage Box Snapshots |
| 49 |
hetzner.hcloud.storage_box_snapshot_info: |
| 50 |
storage_box: my-storage-box |
| 51 |
register: output |
| 52 |
|
| 53 |
- name: Gather Storage Box Snapshots by label |
| 54 |
hetzner.hcloud.storage_box_snapshot_info: |
| 55 |
storage_box: my-storage-box |
| 56 |
label_selector: key=value |
| 57 |
register: output |
| 58 |
|
| 59 |
- name: Gather Storage Box Snapshot by id |
| 60 |
hetzner.hcloud.storage_box_snapshot_info: |
| 61 |
storage_box: 497436 |
| 62 |
id: 405920 |
| 63 |
register: output |
| 64 |
|
| 65 |
- name: Gather Storage Box Snapshot by name |
| 66 |
hetzner.hcloud.storage_box_snapshot_info: |
| 67 |
storage_box: my-storage-box |
| 68 |
name: 2025-02-12T11-35-19 |
| 69 |
register: output |
| 70 |
|
| 71 |
- name: Print the gathered infos |
| 72 |
debug: |
| 73 |
var: output.hcloud_storage_box_snapshot_info |
| 74 |
""" |
| 75 |
|
| 76 |
RETURN = """ |
| 77 |
hcloud_storage_box_snapshot_info: |
| 78 |
description: List of Storage Box Snapshots. |
| 79 |
returned: always |
| 80 |
type: list |
| 81 |
contains: |
| 82 |
storage_box: |
| 83 |
description: ID of the parent Storage Box. |
| 84 |
returned: always |
| 85 |
type: int |
| 86 |
sample: 497436 |
| 87 |
id: |
| 88 |
description: ID of the Storage Box Snapshot. |
| 89 |
returned: always |
| 90 |
type: int |
| 91 |
sample: 405920 |
| 92 |
name: |
| 93 |
description: Name of the Storage Box Snapshot. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: 2025-02-12T11-35-19 |
| 97 |
description: |
| 98 |
description: Description of the Storage Box Snapshot. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: before app migration |
| 102 |
labels: |
| 103 |
description: User-defined labels (key-value pairs) of the Storage Box Snapshot. |
| 104 |
returned: always |
| 105 |
type: dict |
| 106 |
sample: |
| 107 |
env: prod |
| 108 |
stats: |
| 109 |
description: Statistics of the Storage Box Snapshot. |
| 110 |
returned: always |
| 111 |
type: dict |
| 112 |
contains: |
| 113 |
size: |
| 114 |
description: Current storage requirements of the Snapshot in bytes. |
| 115 |
returned: always |
| 116 |
type: int |
| 117 |
sample: 10485760 |
| 118 |
size_filesystem: |
| 119 |
description: Size of the compressed file system contained in the Snapshot in bytes. |
| 120 |
returned: always |
| 121 |
type: int |
| 122 |
sample: 10485760 |
| 123 |
is_automatic: |
| 124 |
description: Whether the Storage Box Snapshot was created automatically. |
| 125 |
returned: always |
| 126 |
type: bool |
| 127 |
sample: false |
| 128 |
created: |
| 129 |
description: Point in time when the Storage Box Snapshot was created (in RFC3339 format). |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
sample: "2025-12-03T13:47:47Z" |
| 133 |
""" |
| 134 |
|
| 135 |
from ..module_utils import _storage_box, _storage_box_snapshot |
| 136 |
from ..module_utils._base import AnsibleHCloud, AnsibleModule |
| 137 |
from ..module_utils._vendor.hcloud import HCloudException |
| 138 |
from ..module_utils._vendor.hcloud.storage_boxes import ( |
| 139 |
BoundStorageBox, |
| 140 |
BoundStorageBoxSnapshot, |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
class AnsibleStorageBoxSnapshotInfo(AnsibleHCloud): |
| 145 |
represent = "storage_box_snapshots" |
| 146 |
|
| 147 |
storage_box: BoundStorageBox | None = None |
| 148 |
storage_box_snapshots: list[BoundStorageBoxSnapshot] | None = None |
| 149 |
|
| 150 |
def _prepare_result(self): |
| 151 |
result = [] |
| 152 |
|
| 153 |
for o in self.storage_box_snapshots or []: |
| 154 |
if o is not None: |
| 155 |
result.append(_storage_box_snapshot.prepare_result(o)) |
| 156 |
return result |
| 157 |
|
| 158 |
def fetch(self): |
| 159 |
try: |
| 160 |
self.storage_box = _storage_box.get( |
| 161 |
self.client.storage_boxes, |
| 162 |
self.module.params.get("storage_box"), |
| 163 |
) |
| 164 |
|
| 165 |
if (id_ := self.module.params.get("id")) is not None: |
| 166 |
self.storage_box_snapshots = [self.storage_box.get_snapshot_by_id(id_)] |
| 167 |
elif (name := self.module.params.get("name")) is not None: |
| 168 |
self.storage_box_snapshots = [self.storage_box.get_snapshot_by_name(name)] |
| 169 |
else: |
| 170 |
params = {} |
| 171 |
if (value := self.module.params.get("label_selector")) is not None: |
| 172 |
params["label_selector"] = value |
| 173 |
self.storage_box_snapshots = self.storage_box.get_snapshot_all(**params) |
| 174 |
|
| 175 |
except HCloudException as exception: |
| 176 |
self.fail_json_hcloud(exception) |
| 177 |
|
| 178 |
@classmethod |
| 179 |
def define_module(cls): |
| 180 |
return AnsibleModule( |
| 181 |
argument_spec=dict( |
| 182 |
storage_box={"type": "str", "required": True}, |
| 183 |
id={"type": "int"}, |
| 184 |
name={"type": "str"}, |
| 185 |
label_selector={"type": "str"}, |
| 186 |
**super().base_module_arguments(), |
| 187 |
), |
| 188 |
supports_check_mode=True, |
| 189 |
) |
| 190 |
|
| 191 |
|
| 192 |
def main(): |
| 193 |
module = AnsibleStorageBoxSnapshotInfo.define_module() |
| 194 |
o = AnsibleStorageBoxSnapshotInfo(module) |
| 195 |
|
| 196 |
o.fetch() |
| 197 |
result = o.get_result() |
| 198 |
|
| 199 |
# Legacy return value naming pattern |
| 200 |
result["hcloud_storage_box_snapshot_info"] = result.pop(o.represent) |
| 201 |
|
| 202 |
module.exit_json(**result) |
| 203 |
|
| 204 |
|
| 205 |
if __name__ == "__main__": |
| 206 |
main() |