| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
--- |
| 10 |
module: backup_plan |
| 11 |
version_added: 6.0.0 |
| 12 |
short_description: Manage AWS Backup Plans |
| 13 |
description: |
| 14 |
- Creates, updates, or deletes AWS Backup Plans. |
| 15 |
- For more information see the AWS documentation for Backup plans U(https://docs.aws.amazon.com/aws-backup/latest/devguide/about-backup-plans.html). |
| 16 |
author: |
| 17 |
- Kristof Imre Szabo (@krisek) |
| 18 |
- Alina Buzachis (@alinabuzachis) |
| 19 |
- Helen Bailey (@hakbailey) |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Create/update or delete a backup plan. |
| 24 |
type: str |
| 25 |
default: present |
| 26 |
choices: ['present', 'absent'] |
| 27 |
backup_plan_name: |
| 28 |
description: |
| 29 |
- The display name of a backup plan. Must contain 1 to 50 alphanumeric or '-_.' characters. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
aliases: ['name'] |
| 33 |
rules: |
| 34 |
description: |
| 35 |
- An array of BackupRule objects, each of which specifies a scheduled task that is used to back up a selection of resources. |
| 36 |
- Required when O(state=present). |
| 37 |
type: list |
| 38 |
elements: dict |
| 39 |
suboptions: |
| 40 |
rule_name: |
| 41 |
description: Name of the rule. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
target_backup_vault_name: |
| 45 |
description: Name of the Backup Vault this rule should target. |
| 46 |
type: str |
| 47 |
required: true |
| 48 |
schedule_expression: |
| 49 |
description: A CRON expression in UTC specifying when Backup initiates a backup |
| 50 |
job. AWS default is used if not supplied. |
| 51 |
type: str |
| 52 |
default: 'cron(0 5 ? * * *)' |
| 53 |
start_window_minutes: |
| 54 |
description: |
| 55 |
- A value in minutes after a backup is scheduled before a job will be |
| 56 |
canceled if it doesn't start successfully. If this value is included, it |
| 57 |
must be at least 60 minutes to avoid errors. |
| 58 |
- AWS default if not supplied is V(480). |
| 59 |
type: int |
| 60 |
default: 480 |
| 61 |
completion_window_minutes: |
| 62 |
description: |
| 63 |
- A value in minutes after a backup job is successfully started before it |
| 64 |
must be completed or it will be canceled by Backup. |
| 65 |
- AWS default if not supplied is V(10080). |
| 66 |
type: int |
| 67 |
default: 10080 |
| 68 |
lifecycle: |
| 69 |
description: |
| 70 |
- The lifecycle defines when a protected resource is transitioned to cold |
| 71 |
storage and when it expires. Backup will transition and expire backups |
| 72 |
automatically according to the lifecycle that you define. |
| 73 |
- Backups transitioned to cold storage must be stored in cold storage for a |
| 74 |
minimum of 90 days. Therefore, the "retention" setting must be 90 days |
| 75 |
greater than the "transition to cold after days" setting. The "transition |
| 76 |
to cold after days" setting cannot be changed after a backup has been |
| 77 |
transitioned to cold. |
| 78 |
type: dict |
| 79 |
suboptions: |
| 80 |
move_to_cold_storage_after_days: |
| 81 |
description: Specifies the number of days after creation that a recovery point is moved to cold storage. |
| 82 |
type: int |
| 83 |
delete_after_days: |
| 84 |
description: Specifies the number of days after creation that a recovery |
| 85 |
point is deleted. Must be greater than V(90) days plus |
| 86 |
O(rules.lifecycle.move_to_cold_storage_after_days). |
| 87 |
type: int |
| 88 |
recovery_point_tags: |
| 89 |
description: To help organize your resources, you can assign your own metadata to the resources that you create. |
| 90 |
type: dict |
| 91 |
copy_actions: |
| 92 |
description: An array of copy_action objects, which contains the details of the copy operation. |
| 93 |
type: list |
| 94 |
elements: dict |
| 95 |
suboptions: |
| 96 |
destination_backup_vault_arn: |
| 97 |
description: An Amazon Resource Name (ARN) that uniquely identifies the destination backup vault for the copied backup. |
| 98 |
type: str |
| 99 |
required: true |
| 100 |
lifecycle: |
| 101 |
description: |
| 102 |
- Contains an array of Transition objects specifying how long in days |
| 103 |
before a recovery point transitions to cold storage or is deleted. |
| 104 |
- Backups transitioned to cold storage must be stored in cold storage for |
| 105 |
a minimum of 90 days. Therefore, on the console, the "retention" |
| 106 |
setting must be 90 days greater than the "transition to cold after |
| 107 |
days" setting. The "transition to cold after days" setting cannot be |
| 108 |
changed after a backup has been transitioned to cold. |
| 109 |
type: dict |
| 110 |
suboptions: |
| 111 |
move_to_cold_storage_after_days: |
| 112 |
description: Specifies the number of days after creation that a |
| 113 |
recovery point is moved to cold storage. |
| 114 |
type: int |
| 115 |
delete_after_days: |
| 116 |
description: Specifies the number of days after creation that a |
| 117 |
recovery point is deleted. Must be greater than V(90) days plus |
| 118 |
O(rules.copy_actions.lifecycle.move_to_cold_storage_after_days). |
| 119 |
type: int |
| 120 |
enable_continuous_backup: |
| 121 |
description: |
| 122 |
- Specifies whether Backup creates continuous backups. True causes Backup to |
| 123 |
create continuous backups capable of point-in-time restore (PITR). False |
| 124 |
(or not specified) causes Backup to create snapshot backups. |
| 125 |
- AWS default if not supplied is V(false). |
| 126 |
type: bool |
| 127 |
default: false |
| 128 |
schedule_expression_timezone: |
| 129 |
description: |
| 130 |
- This is the timezone in which the schedule expression is set. |
| 131 |
- By default, ScheduleExpressions are in UTC. You can modify this to a specified timezone. |
| 132 |
type: str |
| 133 |
default: "Etc/UTC" |
| 134 |
required: false |
| 135 |
version_added: 7.3.0 |
| 136 |
advanced_backup_settings: |
| 137 |
description: |
| 138 |
- Specifies a list of advanced backup settings for each resource type. |
| 139 |
- These settings are only available for Windows Volume Shadow Copy Service (VSS) backup jobs. |
| 140 |
required: false |
| 141 |
type: list |
| 142 |
elements: dict |
| 143 |
suboptions: |
| 144 |
resource_type: |
| 145 |
description: |
| 146 |
- Specifies an object containing resource type and backup options. |
| 147 |
- The only supported resource type is Amazon EC2 instances with Windows Volume Shadow Copy Service (VSS). |
| 148 |
type: str |
| 149 |
choices: ['EC2'] |
| 150 |
backup_options: |
| 151 |
description: |
| 152 |
- Specifies the backup option for a selected resource. |
| 153 |
- This option is only available for Windows VSS backup jobs. |
| 154 |
type: dict |
| 155 |
suboptions: |
| 156 |
WindowsVSS: |
| 157 |
description: Enable or disable WindowsVSS backup option. |
| 158 |
type: str |
| 159 |
choices: ['enabled', 'disabled'] |
| 160 |
creator_request_id: |
| 161 |
description: Identifies the request and allows failed requests to be retried |
| 162 |
without the risk of running the operation twice. If the request includes a |
| 163 |
CreatorRequestId that matches an existing backup plan, that plan is returned. |
| 164 |
type: str |
| 165 |
tags: |
| 166 |
description: To help organize your resources, you can assign your own metadata to |
| 167 |
the resources that you create. Each tag is a key-value pair. The specified tags |
| 168 |
are assigned to all backups created with this plan. |
| 169 |
type: dict |
| 170 |
aliases: ['resource_tags', 'backup_plan_tags'] |
| 171 |
|
| 172 |
extends_documentation_fragment: |
| 173 |
- amazon.aws.common.modules |
| 174 |
- amazon.aws.region.modules |
| 175 |
- amazon.aws.boto3 |
| 176 |
- amazon.aws.tags |
| 177 |
""" |
| 178 |
|
| 179 |
EXAMPLES = r""" |
| 180 |
- name: Create an AWSbackup plan |
| 181 |
amazon.aws.backup_plan: |
| 182 |
state: present |
| 183 |
backup_plan_name: elastic |
| 184 |
rules: |
| 185 |
- rule_name: daily |
| 186 |
advanced_backup_settings: |
| 187 |
- resource_type: "EC2" |
| 188 |
backup_options: |
| 189 |
WindowsVSS: enabled |
| 190 |
target_backup_vault_name: "{{ backup_vault_name }}" |
| 191 |
schedule_expression: 'cron(0 5 ? * * *)' |
| 192 |
start_window_minutes: 60 |
| 193 |
completion_window_minutes: 1440 |
| 194 |
- name: Delete an AWS Backup plan |
| 195 |
amazon.aws.backup_plan: |
| 196 |
backup_plan_name: elastic |
| 197 |
state: absent |
| 198 |
""" |
| 199 |
|
| 200 |
RETURN = r""" |
| 201 |
exists: |
| 202 |
description: Whether the resource exists. |
| 203 |
returned: always |
| 204 |
type: bool |
| 205 |
sample: true |
| 206 |
backup_plan_arn: |
| 207 |
description: ARN of the backup plan. |
| 208 |
returned: always |
| 209 |
type: str |
| 210 |
sample: arn:aws:backup:eu-central-1:111122223333:backup-plan:1111f877-1ecf-4d79-9718-a861cd09df3b |
| 211 |
backup_plan_id: |
| 212 |
description: ID of the backup plan. |
| 213 |
returned: always |
| 214 |
type: str |
| 215 |
sample: "1111f877-1ecf-4d79-9718-a861cd09df3b" |
| 216 |
backup_plan_name: |
| 217 |
description: Name of the backup plan. |
| 218 |
returned: always |
| 219 |
type: str |
| 220 |
sample: elastic |
| 221 |
creation_date: |
| 222 |
description: Creation date of the backup plan. |
| 223 |
returned: on create/update |
| 224 |
type: str |
| 225 |
sample: '2023-01-24T10:08:03.193000+01:00' |
| 226 |
deletion_date: |
| 227 |
description: Date the backup plan was deleted. |
| 228 |
returned: on delete |
| 229 |
type: str |
| 230 |
sample: '2023-05-05T16:24:51.987000-04:00' |
| 231 |
version_id: |
| 232 |
description: Version ID of the backup plan. |
| 233 |
returned: always |
| 234 |
type: str |
| 235 |
sample: ODM3MjVjNjItYWFkOC00NjExLWIwZTYtZDNiNGI5M2I0ZTY1 |
| 236 |
backup_plan: |
| 237 |
description: Backup plan details. |
| 238 |
returned: on create/update |
| 239 |
type: dict |
| 240 |
contains: |
| 241 |
backup_plan_name: |
| 242 |
description: The display name of a backup plan. |
| 243 |
returned: always |
| 244 |
type: str |
| 245 |
sample: elastic |
| 246 |
rules: |
| 247 |
description: |
| 248 |
- An array of BackupRule objects, each of which specifies a scheduled task that is used to back up a selection of resources. |
| 249 |
returned: always |
| 250 |
type: list |
| 251 |
elements: dict |
| 252 |
contains: |
| 253 |
rule_name: |
| 254 |
description: A display name for a backup rule. |
| 255 |
returned: always |
| 256 |
type: str |
| 257 |
sample: "daily" |
| 258 |
target_backup_vault_name: |
| 259 |
description: The name of a logical container where backups are stored. |
| 260 |
returned: always |
| 261 |
type: str |
| 262 |
sample: "09da67966fd5-backup-vault" |
| 263 |
schedule_expression: |
| 264 |
description: A cron expression in UTC specifying when Backup initiates a backup job. |
| 265 |
returned: always |
| 266 |
type: str |
| 267 |
sample: "cron(0 5 ? * * *)" |
| 268 |
start_window_minutes: |
| 269 |
description: |
| 270 |
- A value in minutes after a backup is scheduled before a job will be canceled if it |
| 271 |
doesn't start successfully. |
| 272 |
type: int |
| 273 |
returned: always |
| 274 |
sample: 480 |
| 275 |
completion_window_minutes: |
| 276 |
description: |
| 277 |
- A value in minutes after a backup job is successfully started before it must be |
| 278 |
completed or it will be canceled by Backup. |
| 279 |
type: int |
| 280 |
returned: always |
| 281 |
sample: 10080 |
| 282 |
lifecycle: |
| 283 |
description: |
| 284 |
- Defines when a protected resource is transitioned to cold storage and when it expires. |
| 285 |
type: dict |
| 286 |
returned: when configured. |
| 287 |
sample: { |
| 288 |
"delete_after_days": 100, |
| 289 |
"move_to_cold_storage_after_days": 10 |
| 290 |
} |
| 291 |
recovery_point_tags: |
| 292 |
description: |
| 293 |
- An array of key-value pair strings that are assigned to resources that are associated with |
| 294 |
this rule when restored from backup. |
| 295 |
type: dict |
| 296 |
returned: when configured. |
| 297 |
sample: { |
| 298 |
"Tagkey1": "TagValue1", |
| 299 |
"Tagkey2": "TagValue2" |
| 300 |
} |
| 301 |
rule_id: |
| 302 |
description: |
| 303 |
- Uniquely identifies a rule that is used to schedule the backup of a selection of resources. |
| 304 |
type: str |
| 305 |
returned: always |
| 306 |
sample: "973621ef-d863-41ef-b5c3-9e943a64ad0c" |
| 307 |
copy_actions: |
| 308 |
description: An array of CopyAction objects, which contains the details of the copy operation. |
| 309 |
returned: when configured. |
| 310 |
type: list |
| 311 |
elements: dict |
| 312 |
sample: [ |
| 313 |
{ |
| 314 |
"destination_backup_vault_arn": "arn:aws:backup:us-west-2:123456789012:backup-vault:my-test-vault", |
| 315 |
"lifecycle": { |
| 316 |
"delete_after_days": 100, |
| 317 |
"move_to_cold_storage_after_days": 10 |
| 318 |
} |
| 319 |
} |
| 320 |
] |
| 321 |
enable_continous_backup: |
| 322 |
description: Specifies whether Backup creates continuous backups. |
| 323 |
type: bool |
| 324 |
returned: always |
| 325 |
sample: false |
| 326 |
schedule_expression_timezone: |
| 327 |
description: |
| 328 |
- This is the timezone in which the schedule expression is set. |
| 329 |
type: str |
| 330 |
returned: when configured |
| 331 |
sample: "Etc/UTC" |
| 332 |
version_added: 7.3.0 |
| 333 |
advanced_backup_settings: |
| 334 |
description: Advanced backup settings of the backup plan. |
| 335 |
returned: when configured. |
| 336 |
type: list |
| 337 |
elements: dict |
| 338 |
sample: [ |
| 339 |
{ |
| 340 |
"backup_options": { |
| 341 |
"windows_vss": "enabled" |
| 342 |
}, |
| 343 |
"resource_type": "EC2" |
| 344 |
} |
| 345 |
] |
| 346 |
contains: |
| 347 |
resource_type: |
| 348 |
description: Resource type of the advanced settings. |
| 349 |
type: str |
| 350 |
sample: "EC2" |
| 351 |
backup_options: |
| 352 |
description: Backup options of the advanced settings. |
| 353 |
type: dict |
| 354 |
sample: { |
| 355 |
"windows_vss": "enabled" |
| 356 |
} |
| 357 |
tags: |
| 358 |
description: Tags of the backup plan. |
| 359 |
returned: on create/update |
| 360 |
type: str |
| 361 |
sample: { |
| 362 |
"TagKey1": "TagValue1", |
| 363 |
"TagKey2": "TagValue2" |
| 364 |
} |
| 365 |
""" |
| 366 |
|
| 367 |
import json |
| 368 |
from datetime import datetime |
| 369 |
from typing import Optional |
| 370 |
|
| 371 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 372 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 373 |
|
| 374 |
from ansible_collections.amazon.aws.plugins.module_utils.backup import get_plan_details |
| 375 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 376 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 377 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import scrub_none_parameters |
| 378 |
|
| 379 |
try: |
| 380 |
from botocore.exceptions import BotoCoreError |
| 381 |
from botocore.exceptions import ClientError |
| 382 |
except ImportError: |
| 383 |
pass # Handled by AnsibleAWSModule |
| 384 |
|
| 385 |
ARGUMENT_SPEC = dict( |
| 386 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 387 |
backup_plan_name=dict(required=True, type="str", aliases=["name"]), |
| 388 |
rules=dict( |
| 389 |
type="list", |
| 390 |
elements="dict", |
| 391 |
options=dict( |
| 392 |
rule_name=dict(required=True, type="str"), |
| 393 |
target_backup_vault_name=dict(required=True, type="str"), |
| 394 |
schedule_expression=dict(type="str", default="cron(0 5 ? * * *)"), |
| 395 |
start_window_minutes=dict(type="int", default=480), |
| 396 |
completion_window_minutes=dict(type="int", default=10080), |
| 397 |
schedule_expression_timezone=dict(type="str", default="Etc/UTC"), |
| 398 |
lifecycle=dict( |
| 399 |
type="dict", |
| 400 |
options=dict( |
| 401 |
move_to_cold_storage_after_days=dict(type="int"), |
| 402 |
delete_after_days=dict(type="int"), |
| 403 |
), |
| 404 |
), |
| 405 |
recovery_point_tags=dict(type="dict"), |
| 406 |
copy_actions=dict( |
| 407 |
type="list", |
| 408 |
elements="dict", |
| 409 |
options=dict( |
| 410 |
destination_backup_vault_arn=dict(required=True, type="str"), |
| 411 |
lifecycle=dict( |
| 412 |
type="dict", |
| 413 |
options=dict( |
| 414 |
move_to_cold_storage_after_days=dict(type="int"), |
| 415 |
delete_after_days=dict(type="int"), |
| 416 |
), |
| 417 |
), |
| 418 |
), |
| 419 |
), |
| 420 |
enable_continuous_backup=dict(type="bool", default=False), |
| 421 |
), |
| 422 |
), |
| 423 |
advanced_backup_settings=dict( |
| 424 |
type="list", |
| 425 |
elements="dict", |
| 426 |
options=dict( |
| 427 |
resource_type=dict(type="str", choices=["EC2"]), |
| 428 |
backup_options=dict( |
| 429 |
type="dict", |
| 430 |
options=dict(WindowsVSS=dict(type="str", choices=["enabled", "disabled"])), |
| 431 |
), |
| 432 |
), |
| 433 |
), |
| 434 |
creator_request_id=dict(type="str"), |
| 435 |
tags=dict(type="dict", aliases=["backup_plan_tags", "resource_tags"]), |
| 436 |
purge_tags=dict(default=True, type="bool"), |
| 437 |
) |
| 438 |
|
| 439 |
REQUIRED_IF = [ |
| 440 |
("state", "present", ["backup_plan_name", "rules"]), |
| 441 |
("state", "absent", ["backup_plan_name"]), |
| 442 |
] |
| 443 |
|
| 444 |
SUPPORTS_CHECK_MODE = True |
| 445 |
|
| 446 |
|
| 447 |
def format_client_params( |
| 448 |
module: AnsibleAWSModule, |
| 449 |
plan: dict, |
| 450 |
tags: Optional[dict] = None, |
| 451 |
backup_plan_id: Optional[str] = None, |
| 452 |
operation: Optional[str] = None, |
| 453 |
) -> dict: |
| 454 |
""" |
| 455 |
Formats plan details to match boto3 backup client param expectations. |
| 456 |
|
| 457 |
module : AnsibleAWSModule object |
| 458 |
plan: Dict of plan details including name, rules, and advanced settings |
| 459 |
tags: Dict of plan tags |
| 460 |
backup_plan_id: ID of backup plan to update, only needed for update operation |
| 461 |
operation: Operation to add specific params for, either create or update |
| 462 |
""" |
| 463 |
params = { |
| 464 |
"BackupPlan": snake_dict_to_camel_dict( |
| 465 |
{k: v for k, v in plan.items() if v != "backup_plan_name"}, |
| 466 |
capitalize_first=True, |
| 467 |
) |
| 468 |
} |
| 469 |
|
| 470 |
if operation == "create": # Add create-specific params |
| 471 |
if tags: |
| 472 |
params["BackupPlanTags"] = tags |
| 473 |
creator_request_id = module.params["creator_request_id"] |
| 474 |
if creator_request_id: |
| 475 |
params["CreatorRequestId"] = creator_request_id |
| 476 |
|
| 477 |
elif operation == "update": # Add update-specific params |
| 478 |
params["BackupPlanId"] = backup_plan_id |
| 479 |
|
| 480 |
return params |
| 481 |
|
| 482 |
|
| 483 |
def format_check_mode_response(plan_name: str, plan: dict, tags: dict, delete: bool = False) -> dict: |
| 484 |
""" |
| 485 |
Formats plan details in check mode to match result expectations. |
| 486 |
|
| 487 |
plan_name: Name of backup plan |
| 488 |
plan: Dict of plan details including name, rules, and advanced settings |
| 489 |
tags: Optional dict of plan tags |
| 490 |
delete: Whether the response is for a delete action |
| 491 |
""" |
| 492 |
timestamp = datetime.now().isoformat() |
| 493 |
if delete: |
| 494 |
return { |
| 495 |
"backup_plan_name": plan_name, |
| 496 |
"backup_plan_id": "", |
| 497 |
"backup_plan_arn": "", |
| 498 |
"deletion_date": timestamp, |
| 499 |
"version_id": "", |
| 500 |
} |
| 501 |
else: |
| 502 |
return { |
| 503 |
"backup_plan_name": plan_name, |
| 504 |
"backup_plan_id": "", |
| 505 |
"backup_plan_arn": "", |
| 506 |
"creation_date": timestamp, |
| 507 |
"version_id": "", |
| 508 |
"backup_plan": { |
| 509 |
"backup_plan_name": plan_name, |
| 510 |
"rules": plan["rules"], |
| 511 |
"advanced_backup_settings": plan["advanced_backup_settings"], |
| 512 |
"tags": tags, |
| 513 |
}, |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
def create_backup_plan(module: AnsibleAWSModule, client, create_params: dict) -> dict: |
| 518 |
""" |
| 519 |
Creates a backup plan. |
| 520 |
|
| 521 |
module : AnsibleAWSModule object |
| 522 |
client : boto3 backup client connection object |
| 523 |
create_params : The boto3 backup client parameters to create a backup plan |
| 524 |
""" |
| 525 |
try: |
| 526 |
response = client.create_backup_plan(**create_params) |
| 527 |
except ( |
| 528 |
BotoCoreError, |
| 529 |
ClientError, |
| 530 |
) as err: |
| 531 |
module.fail_json_aws(err, msg="Failed to create backup plan {err}") |
| 532 |
return response |
| 533 |
|
| 534 |
|
| 535 |
def plan_update_needed(existing_plan: dict, new_plan: dict) -> bool: |
| 536 |
""" |
| 537 |
Determines whether existing and new plan rules/settings match. |
| 538 |
|
| 539 |
existing_plan: Dict of existing plan details including rules and advanced settings, |
| 540 |
in snake-case format |
| 541 |
new_plan: Dict of existing plan details including rules and advanced settings, in |
| 542 |
snake-case format |
| 543 |
""" |
| 544 |
update_needed = False |
| 545 |
|
| 546 |
# Check whether rules match |
| 547 |
existing_rules = json.dumps( |
| 548 |
[{key: val for key, val in rule.items() if key != "rule_id"} for rule in existing_plan["backup_plan"]["rules"]], |
| 549 |
sort_keys=True, |
| 550 |
) |
| 551 |
new_rules = json.dumps(new_plan["rules"], sort_keys=True) |
| 552 |
if not existing_rules or existing_rules != new_rules: |
| 553 |
update_needed = True |
| 554 |
|
| 555 |
# Check whether advanced backup settings match |
| 556 |
existing_advanced_backup_settings = json.dumps( |
| 557 |
existing_plan["backup_plan"].get("advanced_backup_settings", []), |
| 558 |
sort_keys=True, |
| 559 |
) |
| 560 |
new_advanced_backup_settings = json.dumps(new_plan.get("advanced_backup_settings", []), sort_keys=True) |
| 561 |
if existing_advanced_backup_settings != new_advanced_backup_settings: |
| 562 |
update_needed = True |
| 563 |
|
| 564 |
return update_needed |
| 565 |
|
| 566 |
|
| 567 |
def update_backup_plan(module: AnsibleAWSModule, client, update_params: dict) -> dict: |
| 568 |
""" |
| 569 |
Updates a backup plan. |
| 570 |
|
| 571 |
module : AnsibleAWSModule object |
| 572 |
client : boto3 backup client connection object |
| 573 |
update_params : The boto3 backup client parameters to update a backup plan |
| 574 |
""" |
| 575 |
try: |
| 576 |
response = client.update_backup_plan(**update_params) |
| 577 |
except ( |
| 578 |
BotoCoreError, |
| 579 |
ClientError, |
| 580 |
) as err: |
| 581 |
module.fail_json_aws(err, msg="Failed to update backup plan {err}") |
| 582 |
return response |
| 583 |
|
| 584 |
|
| 585 |
def tag_backup_plan( |
| 586 |
module: AnsibleAWSModule, |
| 587 |
client, |
| 588 |
new_tags: Optional[dict], |
| 589 |
plan_arn: str, |
| 590 |
current_tags: Optional[dict] = None, |
| 591 |
): |
| 592 |
""" |
| 593 |
Creates, updates, and/or removes tags on a Backup Plan resource. |
| 594 |
|
| 595 |
module : AnsibleAWSModule object |
| 596 |
client : boto3 client connection object |
| 597 |
new_tags : Dict of tags converted from ansible_dict to boto3 list of dicts |
| 598 |
plan_arn : The ARN of the Backup Plan to operate on |
| 599 |
curr_tags : Dict of the current tags on resource, if any |
| 600 |
""" |
| 601 |
|
| 602 |
if not new_tags and not current_tags: |
| 603 |
return False |
| 604 |
|
| 605 |
if module.check_mode: |
| 606 |
return True |
| 607 |
|
| 608 |
new_tags = new_tags or {} |
| 609 |
current_tags = current_tags or {} |
| 610 |
tags_to_add, tags_to_remove = compare_aws_tags(current_tags, new_tags, purge_tags=module.params["purge_tags"]) |
| 611 |
|
| 612 |
if not tags_to_add and not tags_to_remove: |
| 613 |
return False |
| 614 |
|
| 615 |
if tags_to_remove: |
| 616 |
try: |
| 617 |
client.untag_resource(ResourceArn=plan_arn, TagKeyList=tags_to_remove) |
| 618 |
except (BotoCoreError, ClientError) as err: |
| 619 |
module.fail_json_aws(err, msg="Failed to remove tags from the plan") |
| 620 |
|
| 621 |
if tags_to_add: |
| 622 |
try: |
| 623 |
client.tag_resource(ResourceArn=plan_arn, Tags=tags_to_add) |
| 624 |
except (BotoCoreError, ClientError) as err: |
| 625 |
module.fail_json_aws(err, msg="Failed to add tags to the plan") |
| 626 |
|
| 627 |
return True |
| 628 |
|
| 629 |
|
| 630 |
def delete_backup_plan(module: AnsibleAWSModule, client, backup_plan_id: str) -> dict: |
| 631 |
""" |
| 632 |
Deletes a Backup Plan |
| 633 |
|
| 634 |
module : AnsibleAWSModule object |
| 635 |
client : boto3 backup client connection object |
| 636 |
backup_plan_id : ID (*not* name or ARN) of Backup plan to delete |
| 637 |
""" |
| 638 |
try: |
| 639 |
response = client.delete_backup_plan(BackupPlanId=backup_plan_id) |
| 640 |
except (BotoCoreError, ClientError) as err: |
| 641 |
module.fail_json_aws(err, msg="Failed to delete the Backup Plan") |
| 642 |
return response |
| 643 |
|
| 644 |
|
| 645 |
def main(): |
| 646 |
module = AnsibleAWSModule( |
| 647 |
argument_spec=ARGUMENT_SPEC, |
| 648 |
required_if=REQUIRED_IF, |
| 649 |
supports_check_mode=SUPPORTS_CHECK_MODE, |
| 650 |
) |
| 651 |
|
| 652 |
# Set initial result values |
| 653 |
result = dict(changed=False, exists=False) |
| 654 |
|
| 655 |
# Get supplied params from module |
| 656 |
client = module.client("backup") |
| 657 |
state = module.params["state"] |
| 658 |
plan_name = module.params["backup_plan_name"] |
| 659 |
|
| 660 |
plan = { |
| 661 |
"backup_plan_name": module.params["backup_plan_name"], |
| 662 |
"rules": [scrub_none_parameters(rule) for rule in module.params["rules"] or []], |
| 663 |
"advanced_backup_settings": [ |
| 664 |
scrub_none_parameters(setting) for setting in module.params["advanced_backup_settings"] or [] |
| 665 |
], |
| 666 |
} |
| 667 |
|
| 668 |
tags = module.params["tags"] |
| 669 |
|
| 670 |
# Get existing backup plan details and ID if present |
| 671 |
existing_plan = get_plan_details(module, client, plan_name) |
| 672 |
if existing_plan: |
| 673 |
existing_plan_id = existing_plan[0]["backup_plan_id"] |
| 674 |
existing_plan = existing_plan[0] |
| 675 |
else: |
| 676 |
existing_plan = existing_plan_id = None |
| 677 |
|
| 678 |
if state == "present": # Create or update plan |
| 679 |
if existing_plan_id is None: # Plan does not exist, create it |
| 680 |
if module.check_mode: # Use supplied params as result data in check mode |
| 681 |
backup_plan = format_check_mode_response(plan_name, plan, tags) |
| 682 |
else: |
| 683 |
client_params = format_client_params(module, plan, tags=tags, operation="create") |
| 684 |
response = create_backup_plan(module, client, client_params) |
| 685 |
backup_plan = get_plan_details(module, client, plan_name)[0] |
| 686 |
result["exists"] = True |
| 687 |
result["changed"] = True |
| 688 |
result.update(backup_plan) |
| 689 |
|
| 690 |
else: # Plan exists, update as needed |
| 691 |
result["exists"] = True |
| 692 |
if plan_update_needed(existing_plan, plan): |
| 693 |
if not module.check_mode: |
| 694 |
client_params = format_client_params( |
| 695 |
module, |
| 696 |
plan, |
| 697 |
backup_plan_id=existing_plan_id, |
| 698 |
operation="update", |
| 699 |
) |
| 700 |
update_backup_plan(module, client, client_params) |
| 701 |
result["changed"] = True |
| 702 |
if tag_backup_plan( |
| 703 |
module, |
| 704 |
client, |
| 705 |
tags, |
| 706 |
existing_plan["backup_plan_arn"], |
| 707 |
existing_plan["tags"], |
| 708 |
): |
| 709 |
result["changed"] = True |
| 710 |
if module.check_mode: |
| 711 |
backup_plan = format_check_mode_response(plan_name, plan, tags) |
| 712 |
else: |
| 713 |
backup_plan = get_plan_details(module, client, plan_name)[0] |
| 714 |
result.update(backup_plan) |
| 715 |
|
| 716 |
elif state == "absent": # Delete plan |
| 717 |
if existing_plan_id is None: # Plan does not exist, can't delete it |
| 718 |
module.debug(msg=f"Backup plan {plan_name} not found.") |
| 719 |
else: # Plan exists, delete it |
| 720 |
if module.check_mode: |
| 721 |
response = format_check_mode_response(plan_name, existing_plan, tags, True) |
| 722 |
else: |
| 723 |
response = delete_backup_plan(module, client, existing_plan_id) |
| 724 |
result["changed"] = True |
| 725 |
result["exists"] = False |
| 726 |
result.update(camel_dict_to_snake_dict(response)) |
| 727 |
|
| 728 |
module.exit_json(**result) |
| 729 |
|
| 730 |
|
| 731 |
if __name__ == "__main__": |
| 732 |
main() |