| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2025 Matthias Hurdebise <[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_info |
| 14 |
short_description: Query the snapshots for a storage box |
| 15 |
version_added: 2.4.0 |
| 16 |
author: |
| 17 |
- Matthias Hurdebise (@matthiashurdebise) |
| 18 |
description: |
| 19 |
- Query the snapshots for 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 |
- community.hrobot.attributes.idempotent_not_modify_state |
| 29 |
- community.hrobot.attributes.info_module |
| 30 |
|
| 31 |
options: |
| 32 |
hetzner_token: |
| 33 |
version_added: 2.5.0 |
| 34 |
storagebox_id: |
| 35 |
description: |
| 36 |
- The ID of the storage box to query. |
| 37 |
type: int |
| 38 |
required: true |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = r""" |
| 42 |
--- |
| 43 |
- name: Query the snapshots |
| 44 |
community.hrobot.storagebox_snapshot_info: |
| 45 |
hetzner_user: foo |
| 46 |
hetzner_password: bar |
| 47 |
id: 123 |
| 48 |
register: result |
| 49 |
|
| 50 |
- name: Output data |
| 51 |
ansible.builtin.debug: |
| 52 |
msg: "Timestamp of the first snapshot : {{ result.snapshots[0].timestamp }}" |
| 53 |
""" |
| 54 |
|
| 55 |
RETURN = r""" |
| 56 |
snapshots: |
| 57 |
description: |
| 58 |
- The storage box's info. |
| 59 |
- All date and time parameters are in UTC. |
| 60 |
returned: success |
| 61 |
type: list |
| 62 |
elements: dict |
| 63 |
contains: |
| 64 |
name: |
| 65 |
description: |
| 66 |
- The snapshot name. |
| 67 |
type: str |
| 68 |
sample: "2025-01-21T12-40-38" |
| 69 |
returned: success |
| 70 |
timestamp: |
| 71 |
description: |
| 72 |
- The timestamp of snapshot in UTC. |
| 73 |
- Note that this is copied from RV(snapshots[].created) in case O(hetzner_token) is specified. |
| 74 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 75 |
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). |
| 76 |
type: str |
| 77 |
sample: "2025-01-21T13:40:38+00:00" |
| 78 |
returned: success |
| 79 |
size: |
| 80 |
description: |
| 81 |
- The Snapshot size in MB. |
| 82 |
- Note that this is copied from RV(snapshots[].stats.size) in case O(hetzner_token) is specified. |
| 83 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 84 |
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). |
| 85 |
type: int |
| 86 |
sample: 400 |
| 87 |
returned: success |
| 88 |
filesystem_size: |
| 89 |
description: |
| 90 |
- The size of the Storage Box at creation time of the snapshot in MB. |
| 91 |
- Note that this is computed from RV(snapshots[].stats.size_filesystem) in case O(hetzner_token) is specified. |
| 92 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 93 |
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). |
| 94 |
type: int |
| 95 |
sample: 12345 |
| 96 |
returned: success |
| 97 |
automatic: |
| 98 |
description: |
| 99 |
- Whether the snapshot was created automatically. |
| 100 |
- Note that this is computed from RV(snapshots[].is_automatic) in case O(hetzner_token) is specified. |
| 101 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 102 |
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). |
| 103 |
type: bool |
| 104 |
sample: false |
| 105 |
returned: success |
| 106 |
comment: |
| 107 |
description: |
| 108 |
- The comment for the snapshot. |
| 109 |
- Note that this is copied from RV(snapshots[].description) in case O(hetzner_token) is specified. |
| 110 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 111 |
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). |
| 112 |
type: str |
| 113 |
sample: "This is a snapshot" |
| 114 |
returned: success |
| 115 |
id: |
| 116 |
description: |
| 117 |
- The snapshot's ID. |
| 118 |
type: int |
| 119 |
sample: 1 |
| 120 |
returned: success and O(hetzner_token) is specified |
| 121 |
version_added: 2.5.0 |
| 122 |
stats: |
| 123 |
description: |
| 124 |
- Statistics about the snapshot. |
| 125 |
type: dict |
| 126 |
returned: success and O(hetzner_token) is specified |
| 127 |
version_added: 2.5.0 |
| 128 |
contains: |
| 129 |
size: |
| 130 |
description: |
| 131 |
- Total size of the snapshot in bytes. |
| 132 |
type: int |
| 133 |
sample: 2097152 |
| 134 |
size_filesystem: |
| 135 |
description: |
| 136 |
- Actual size of the snapshot on the filesystem in bytes (after deduplication). |
| 137 |
type: int |
| 138 |
sample: 1048576 |
| 139 |
is_automatic: |
| 140 |
description: |
| 141 |
- Whether the snapshot was created automatically. |
| 142 |
type: bool |
| 143 |
sample: true |
| 144 |
returned: success and O(hetzner_token) is specified |
| 145 |
version_added: 2.5.0 |
| 146 |
description: |
| 147 |
description: |
| 148 |
- The snapshot's description (used to be called comment in the Robot API). |
| 149 |
type: int |
| 150 |
sample: my-description |
| 151 |
returned: success and O(hetzner_token) is specified |
| 152 |
version_added: 2.5.0 |
| 153 |
created: |
| 154 |
description: |
| 155 |
- The creation timestamp of snapshot in UTC, in ISO-8601 format. |
| 156 |
type: int |
| 157 |
sample: "2016-01-30T23:55:00+00:00" |
| 158 |
returned: success and O(hetzner_token) is specified |
| 159 |
version_added: 2.5.0 |
| 160 |
storage_box: |
| 161 |
description: |
| 162 |
- ID of the associated storage box. |
| 163 |
type: int |
| 164 |
sample: 42 |
| 165 |
returned: success and O(hetzner_token) is specified |
| 166 |
version_added: 2.5.0 |
| 167 |
labels: |
| 168 |
description: |
| 169 |
- User-defined labels for the snapshot. |
| 170 |
type: dict |
| 171 |
returned: success and O(hetzner_token) is specified |
| 172 |
version_added: 2.5.0 |
| 173 |
""" |
| 174 |
|
| 175 |
from ansible.module_utils.basic import AnsibleModule |
| 176 |
|
| 177 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 178 |
BASE_URL, |
| 179 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 180 |
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
| 181 |
fetch_url_json, |
| 182 |
) |
| 183 |
|
| 184 |
from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
| 185 |
API_BASE_URL, |
| 186 |
API_DEFAULT_ARGUMENT_SPEC, |
| 187 |
_API_DEFAULT_ARGUMENT_SPEC_COMPAT, |
| 188 |
api_fetch_url_json, |
| 189 |
) |
| 190 |
|
| 191 |
from ansible_collections.community.hrobot.plugins.module_utils._tagging import ( |
| 192 |
deprecate_value, |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
def adjust_legacy(snapshot): |
| 197 |
result = dict(snapshot) |
| 198 |
result["timestamp"] = deprecate_value( |
| 199 |
result["created"], |
| 200 |
"The return value `timestamp` is deprecated; use `created` instead.", |
| 201 |
version="3.0.0", |
| 202 |
) |
| 203 |
result["size"] = deprecate_value( |
| 204 |
result["stats"]["size"] // (1024 * 1024), |
| 205 |
"The return value `size` is deprecated; use `stats.size / (1024*1024)` instead.", |
| 206 |
version="3.0.0", |
| 207 |
) |
| 208 |
result["filesystem_size"] = deprecate_value( |
| 209 |
result["stats"]["size_filesystem"] // (1024 * 1024), |
| 210 |
"The return value `filesystem_size` is deprecated; use `stats.size_filesystem / (1024*1024)` instead.", |
| 211 |
version="3.0.0", |
| 212 |
) |
| 213 |
result["automatic"] = deprecate_value( |
| 214 |
result["is_automatic"], |
| 215 |
"The return value `automatic` is deprecated; use `is_automatic` instead.", |
| 216 |
version="3.0.0", |
| 217 |
) |
| 218 |
result["comment"] = deprecate_value( |
| 219 |
result["description"], |
| 220 |
"The return value `comment` is deprecated; use `description` instead.", |
| 221 |
version="3.0.0", |
| 222 |
) |
| 223 |
return result |
| 224 |
|
| 225 |
|
| 226 |
def main(): |
| 227 |
argument_spec = dict( |
| 228 |
storagebox_id=dict(type='int', required=True), |
| 229 |
) |
| 230 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 231 |
argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED) |
| 232 |
argument_spec.update(API_DEFAULT_ARGUMENT_SPEC) |
| 233 |
argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT) |
| 234 |
module = AnsibleModule( |
| 235 |
argument_spec=argument_spec, |
| 236 |
supports_check_mode=True, |
| 237 |
) |
| 238 |
|
| 239 |
storagebox_id = module.params['storagebox_id'] |
| 240 |
|
| 241 |
if module.params["hetzner_user"] is not None: |
| 242 |
module.deprecate( |
| 243 |
"The hetzner_token parameter will be required from community.hrobot 3.0.0 on.", |
| 244 |
collection_name="community.hrobot", |
| 245 |
version="3.0.0", |
| 246 |
) |
| 247 |
# DEPRECATED: old API |
| 248 |
url = "{0}/storagebox/{1}/snapshot".format(BASE_URL, storagebox_id) |
| 249 |
result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND']) |
| 250 |
if error: |
| 251 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 252 |
|
| 253 |
module.exit_json( |
| 254 |
changed=False, |
| 255 |
snapshots=[item['snapshot'] for item in result], |
| 256 |
) |
| 257 |
|
| 258 |
else: |
| 259 |
# NEW API! |
| 260 |
|
| 261 |
url = "{0}/v1/storage_boxes/{1}/snapshots".format(API_BASE_URL, storagebox_id) |
| 262 |
result, dummy, error = api_fetch_url_json(module, url, accept_errors=['not_found']) |
| 263 |
if error: |
| 264 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 265 |
|
| 266 |
module.exit_json( |
| 267 |
changed=False, |
| 268 |
snapshots=[adjust_legacy(item) for item in result['snapshots']], |
| 269 |
) |
| 270 |
|
| 271 |
|
| 272 |
if __name__ == '__main__': # pragma: no cover |
| 273 |
main() # pragma: no cover |