| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway database backups management module |
| 4 |
# |
| 5 |
# Copyright (C) 2020 Guillaume Rodriguez ([email protected]). |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: scaleway_database_backup |
| 14 |
short_description: Scaleway database backups management module |
| 15 |
version_added: 1.2.0 |
| 16 |
author: Guillaume Rodriguez (@guillaume_ro_fr) |
| 17 |
description: |
| 18 |
- This module manages database backups on Scaleway account U(https://developer.scaleway.com). |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._scaleway |
| 21 |
- community.general._attributes |
| 22 |
- community.general._scaleway.actiongroup_scaleway |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
action_group: |
| 29 |
version_added: 11.3.0 |
| 30 |
|
| 31 |
options: |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Indicate desired state of the database backup. |
| 35 |
- V(present) creates a backup. |
| 36 |
- V(absent) deletes the backup. |
| 37 |
- V(exported) creates a download link for the backup. |
| 38 |
- V(restored) restores the backup to a new database. |
| 39 |
type: str |
| 40 |
default: present |
| 41 |
choices: |
| 42 |
- present |
| 43 |
- absent |
| 44 |
- exported |
| 45 |
- restored |
| 46 |
|
| 47 |
region: |
| 48 |
description: |
| 49 |
- Scaleway region to use (for example V(fr-par)). |
| 50 |
type: str |
| 51 |
required: true |
| 52 |
choices: |
| 53 |
- fr-par |
| 54 |
- nl-ams |
| 55 |
- pl-waw |
| 56 |
|
| 57 |
id: |
| 58 |
description: |
| 59 |
- UUID used to identify the database backup. |
| 60 |
- Required for V(absent), V(exported) and V(restored) states. |
| 61 |
type: str |
| 62 |
|
| 63 |
name: |
| 64 |
description: |
| 65 |
- Name used to identify the database backup. |
| 66 |
- Required for V(present) state. |
| 67 |
- Ignored when O(state=absent), O(state=exported) or O(state=restored). |
| 68 |
type: str |
| 69 |
|
| 70 |
database_name: |
| 71 |
description: |
| 72 |
- Name used to identify the database. |
| 73 |
- Required for V(present) and V(restored) states. |
| 74 |
- Ignored when O(state=absent) or O(state=exported). |
| 75 |
type: str |
| 76 |
|
| 77 |
instance_id: |
| 78 |
description: |
| 79 |
- UUID of the instance associated to the database backup. |
| 80 |
- Required for V(present) and V(restored) states. |
| 81 |
- Ignored when O(state=absent) or O(state=exported). |
| 82 |
type: str |
| 83 |
|
| 84 |
expires_at: |
| 85 |
description: |
| 86 |
- Expiration datetime of the database backup (ISO 8601 format). |
| 87 |
- Ignored when O(state=absent), O(state=exported) or O(state=restored). |
| 88 |
type: str |
| 89 |
|
| 90 |
wait: |
| 91 |
description: |
| 92 |
- Wait for the instance to reach its desired state before returning. |
| 93 |
type: bool |
| 94 |
default: false |
| 95 |
|
| 96 |
wait_timeout: |
| 97 |
description: |
| 98 |
- Time to wait for the backup to reach the expected state. |
| 99 |
type: int |
| 100 |
default: 300 |
| 101 |
|
| 102 |
wait_sleep_time: |
| 103 |
description: |
| 104 |
- Time to wait before every attempt to check the state of the backup. |
| 105 |
type: int |
| 106 |
default: 3 |
| 107 |
""" |
| 108 |
|
| 109 |
EXAMPLES = r""" |
| 110 |
- name: Create a backup |
| 111 |
community.general.scaleway_database_backup: |
| 112 |
name: 'my_backup' |
| 113 |
state: present |
| 114 |
region: 'fr-par' |
| 115 |
database_name: 'my-database' |
| 116 |
instance_id: '50968a80-2909-4e5c-b1af-a2e19860dddb' |
| 117 |
|
| 118 |
- name: Export a backup |
| 119 |
community.general.scaleway_database_backup: |
| 120 |
id: '6ef1125a-037e-494f-a911-6d9c49a51691' |
| 121 |
state: exported |
| 122 |
region: 'fr-par' |
| 123 |
|
| 124 |
- name: Restore a backup |
| 125 |
community.general.scaleway_database_backup: |
| 126 |
id: '6ef1125a-037e-494f-a911-6d9c49a51691' |
| 127 |
state: restored |
| 128 |
region: 'fr-par' |
| 129 |
database_name: 'my-new-database' |
| 130 |
instance_id: '50968a80-2909-4e5c-b1af-a2e19860dddb' |
| 131 |
|
| 132 |
- name: Remove a backup |
| 133 |
community.general.scaleway_database_backup: |
| 134 |
id: '6ef1125a-037e-494f-a911-6d9c49a51691' |
| 135 |
state: absent |
| 136 |
region: 'fr-par' |
| 137 |
""" |
| 138 |
|
| 139 |
RETURN = r""" |
| 140 |
metadata: |
| 141 |
description: Backup metadata. |
| 142 |
returned: when O(state=present), O(state=exported), or O(state=restored) |
| 143 |
type: dict |
| 144 |
sample: |
| 145 |
{ |
| 146 |
"metadata": { |
| 147 |
"created_at": "2020-08-06T12:42:05.631049Z", |
| 148 |
"database_name": "my-database", |
| 149 |
"download_url": null, |
| 150 |
"download_url_expires_at": null, |
| 151 |
"expires_at": null, |
| 152 |
"id": "a15297bd-0c4a-4b4f-8fbb-b36a35b7eb07", |
| 153 |
"instance_id": "617be32e-6497-4ed7-b4c7-0ee5a81edf49", |
| 154 |
"instance_name": "my-instance", |
| 155 |
"name": "backup_name", |
| 156 |
"region": "fr-par", |
| 157 |
"size": 600000, |
| 158 |
"status": "ready", |
| 159 |
"updated_at": "2020-08-06T12:42:10.581649Z" |
| 160 |
} |
| 161 |
} |
| 162 |
""" |
| 163 |
|
| 164 |
import datetime |
| 165 |
import time |
| 166 |
|
| 167 |
from ansible.module_utils.basic import AnsibleModule |
| 168 |
|
| 169 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 170 |
now, |
| 171 |
) |
| 172 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 173 |
SCALEWAY_REGIONS, |
| 174 |
Scaleway, |
| 175 |
scaleway_argument_spec, |
| 176 |
) |
| 177 |
|
| 178 |
stable_states = ( |
| 179 |
"ready", |
| 180 |
"deleting", |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
def wait_to_complete_state_transition(module, account_api, backup=None): |
| 185 |
wait_timeout = module.params["wait_timeout"] |
| 186 |
wait_sleep_time = module.params["wait_sleep_time"] |
| 187 |
|
| 188 |
if backup is None or backup["status"] in stable_states: |
| 189 |
return backup |
| 190 |
|
| 191 |
start = now() |
| 192 |
end = start + datetime.timedelta(seconds=wait_timeout) |
| 193 |
while now() < end: |
| 194 |
module.debug("We are going to wait for the backup to finish its transition") |
| 195 |
|
| 196 |
response = account_api.get(f"/rdb/v1/regions/{module.params.get('region')}/backups/{backup['id']}") |
| 197 |
if not response.ok: |
| 198 |
module.fail_json(msg=f"Error getting backup [{response.status_code}: {response.json}]") |
| 199 |
break |
| 200 |
response_json = response.json |
| 201 |
|
| 202 |
if response_json["status"] in stable_states: |
| 203 |
module.debug("It seems that the backup is not in transition anymore.") |
| 204 |
module.debug(f"Backup in state: {response_json['status']}") |
| 205 |
return response_json |
| 206 |
time.sleep(wait_sleep_time) |
| 207 |
else: |
| 208 |
module.fail_json(msg="Backup takes too long to finish its transition") |
| 209 |
|
| 210 |
|
| 211 |
def present_strategy(module, account_api, backup): |
| 212 |
name = module.params["name"] |
| 213 |
database_name = module.params["database_name"] |
| 214 |
instance_id = module.params["instance_id"] |
| 215 |
expiration_date = module.params["expires_at"] |
| 216 |
|
| 217 |
if backup is not None: |
| 218 |
if (backup["name"] == name or name is None) and ( |
| 219 |
backup["expires_at"] == expiration_date or expiration_date is None |
| 220 |
): |
| 221 |
wait_to_complete_state_transition(module, account_api, backup) |
| 222 |
module.exit_json(changed=False) |
| 223 |
|
| 224 |
if module.check_mode: |
| 225 |
module.exit_json(changed=True) |
| 226 |
|
| 227 |
payload = {} |
| 228 |
if name is not None: |
| 229 |
payload["name"] = name |
| 230 |
if expiration_date is not None: |
| 231 |
payload["expires_at"] = expiration_date |
| 232 |
|
| 233 |
response = account_api.patch(f"/rdb/v1/regions/{module.params.get('region')}/backups/{backup['id']}", payload) |
| 234 |
if response.ok: |
| 235 |
result = wait_to_complete_state_transition(module, account_api, response.json) |
| 236 |
module.exit_json(changed=True, metadata=result) |
| 237 |
|
| 238 |
module.fail_json(msg=f"Error modifying backup [{response.status_code}: {response.json}]") |
| 239 |
|
| 240 |
if module.check_mode: |
| 241 |
module.exit_json(changed=True) |
| 242 |
|
| 243 |
payload = {"name": name, "database_name": database_name, "instance_id": instance_id} |
| 244 |
if expiration_date is not None: |
| 245 |
payload["expires_at"] = expiration_date |
| 246 |
|
| 247 |
response = account_api.post(f"/rdb/v1/regions/{module.params.get('region')}/backups", payload) |
| 248 |
|
| 249 |
if response.ok: |
| 250 |
result = wait_to_complete_state_transition(module, account_api, response.json) |
| 251 |
module.exit_json(changed=True, metadata=result) |
| 252 |
|
| 253 |
module.fail_json(msg=f"Error creating backup [{response.status_code}: {response.json}]") |
| 254 |
|
| 255 |
|
| 256 |
def absent_strategy(module, account_api, backup): |
| 257 |
if backup is None: |
| 258 |
module.exit_json(changed=False) |
| 259 |
|
| 260 |
if module.check_mode: |
| 261 |
module.exit_json(changed=True) |
| 262 |
|
| 263 |
response = account_api.delete(f"/rdb/v1/regions/{module.params.get('region')}/backups/{backup['id']}") |
| 264 |
if response.ok: |
| 265 |
result = wait_to_complete_state_transition(module, account_api, response.json) |
| 266 |
module.exit_json(changed=True, metadata=result) |
| 267 |
|
| 268 |
module.fail_json(msg=f"Error deleting backup [{response.status_code}: {response.json}]") |
| 269 |
|
| 270 |
|
| 271 |
def exported_strategy(module, account_api, backup): |
| 272 |
if backup is None: |
| 273 |
module.fail_json(msg=f'Backup "{module.params["id"]}" not found') |
| 274 |
|
| 275 |
if backup["download_url"] is not None: |
| 276 |
module.exit_json(changed=False, metadata=backup) |
| 277 |
|
| 278 |
if module.check_mode: |
| 279 |
module.exit_json(changed=True) |
| 280 |
|
| 281 |
backup = wait_to_complete_state_transition(module, account_api, backup) |
| 282 |
response = account_api.post(f"/rdb/v1/regions/{module.params.get('region')}/backups/{backup['id']}/export", {}) |
| 283 |
|
| 284 |
if response.ok: |
| 285 |
result = wait_to_complete_state_transition(module, account_api, response.json) |
| 286 |
module.exit_json(changed=True, metadata=result) |
| 287 |
|
| 288 |
module.fail_json(msg=f"Error exporting backup [{response.status_code}: {response.json}]") |
| 289 |
|
| 290 |
|
| 291 |
def restored_strategy(module, account_api, backup): |
| 292 |
if backup is None: |
| 293 |
module.fail_json(msg=f'Backup "{module.params["id"]}" not found') |
| 294 |
|
| 295 |
database_name = module.params["database_name"] |
| 296 |
instance_id = module.params["instance_id"] |
| 297 |
|
| 298 |
if module.check_mode: |
| 299 |
module.exit_json(changed=True) |
| 300 |
|
| 301 |
backup = wait_to_complete_state_transition(module, account_api, backup) |
| 302 |
|
| 303 |
payload = {"database_name": database_name, "instance_id": instance_id} |
| 304 |
response = account_api.post( |
| 305 |
f"/rdb/v1/regions/{module.params.get('region')}/backups/{backup['id']}/restore", payload |
| 306 |
) |
| 307 |
|
| 308 |
if response.ok: |
| 309 |
result = wait_to_complete_state_transition(module, account_api, response.json) |
| 310 |
module.exit_json(changed=True, metadata=result) |
| 311 |
|
| 312 |
module.fail_json(msg=f"Error restoring backup [{response.status_code}: {response.json}]") |
| 313 |
|
| 314 |
|
| 315 |
state_strategy = { |
| 316 |
"present": present_strategy, |
| 317 |
"absent": absent_strategy, |
| 318 |
"exported": exported_strategy, |
| 319 |
"restored": restored_strategy, |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
def core(module): |
| 324 |
state = module.params["state"] |
| 325 |
backup_id = module.params["id"] |
| 326 |
|
| 327 |
account_api = Scaleway(module) |
| 328 |
|
| 329 |
if backup_id is None: |
| 330 |
backup_by_id = None |
| 331 |
else: |
| 332 |
response = account_api.get(f"/rdb/v1/regions/{module.params.get('region')}/backups/{backup_id}") |
| 333 |
status_code = response.status_code |
| 334 |
backup_json = response.json |
| 335 |
backup_by_id = None |
| 336 |
if status_code == 404: |
| 337 |
backup_by_id = None |
| 338 |
elif response.ok: |
| 339 |
backup_by_id = backup_json |
| 340 |
else: |
| 341 |
module.fail_json(msg=f"Error getting backup [{status_code}: {response.json['message']}]") |
| 342 |
|
| 343 |
state_strategy[state](module, account_api, backup_by_id) |
| 344 |
|
| 345 |
|
| 346 |
def main(): |
| 347 |
argument_spec = scaleway_argument_spec() |
| 348 |
argument_spec.update( |
| 349 |
dict( |
| 350 |
state=dict(default="present", choices=["absent", "present", "exported", "restored"]), |
| 351 |
region=dict(required=True, choices=SCALEWAY_REGIONS), |
| 352 |
id=dict(), |
| 353 |
name=dict(type="str"), |
| 354 |
database_name=dict(), |
| 355 |
instance_id=dict(), |
| 356 |
expires_at=dict(), |
| 357 |
wait=dict(type="bool", default=False), |
| 358 |
wait_timeout=dict(type="int", default=300), |
| 359 |
wait_sleep_time=dict(type="int", default=3), |
| 360 |
) |
| 361 |
) |
| 362 |
module = AnsibleModule( |
| 363 |
argument_spec=argument_spec, |
| 364 |
supports_check_mode=True, |
| 365 |
required_together=[ |
| 366 |
["database_name", "instance_id"], |
| 367 |
], |
| 368 |
required_if=[ |
| 369 |
["state", "present", ["name", "database_name", "instance_id"]], |
| 370 |
["state", "absent", ["id"]], |
| 371 |
["state", "exported", ["id"]], |
| 372 |
["state", "restored", ["id", "database_name", "instance_id"]], |
| 373 |
], |
| 374 |
) |
| 375 |
|
| 376 |
core(module) |
| 377 |
|
| 378 |
|
| 379 |
if __name__ == "__main__": |
| 380 |
main() |