| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: backup_restore_job_info |
| 10 |
version_added: 6.0.0 |
| 11 |
short_description: List information about backup restore jobs |
| 12 |
description: |
| 13 |
- List detailed information about AWS Backup restore jobs initiated to restore a saved resource. |
| 14 |
author: |
| 15 |
- Mandar Vijay Kulkarni (@mandar242) |
| 16 |
options: |
| 17 |
account_id: |
| 18 |
description: |
| 19 |
- The account ID to list the restore jobs from. |
| 20 |
required: false |
| 21 |
type: str |
| 22 |
status: |
| 23 |
description: |
| 24 |
- Status of restore jobs to filter the result based on job status. |
| 25 |
required: false |
| 26 |
choices: ['PENDING', 'RUNNING', 'COMPLETED', 'ABORTED', 'FAILED'] |
| 27 |
type: str |
| 28 |
created_before: |
| 29 |
description: |
| 30 |
- Specified date to filter result based on the restore job creation datetime. |
| 31 |
- If specified, only the restore jobs created before the specified datetime will be returned. |
| 32 |
- The date must be in Unix format and Coordinated Universal Time (UTC), example "2023-02-25T00:05:36.309Z". |
| 33 |
required: false |
| 34 |
type: str |
| 35 |
created_after: |
| 36 |
description: |
| 37 |
- Specified date to filter result based on the restore job creation datetime. |
| 38 |
- If specified, only the restore jobs created after the specified datetime will be returned. |
| 39 |
- The date must be in Unix format and Coordinated Universal Time (UTC), example "2023-02-25T00:05:36.309Z". |
| 40 |
required: false |
| 41 |
type: str |
| 42 |
completed_before: |
| 43 |
description: |
| 44 |
- Specified date to filter result based on the restore job completion datetime. |
| 45 |
- If specified, only the restore jobs created before the specified datetime will be returned. |
| 46 |
- The date must be in Unix format and Coordinated Universal Time (UTC), example "2023-02-25T00:05:36.309Z". |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
completed_after: |
| 50 |
description: |
| 51 |
- Specified date to filter result based on the restore job completion datetime. |
| 52 |
- If specified, only the restore jobs created after the specified datetime will be returned. |
| 53 |
- The date must be in Unix format and Coordinated Universal Time (UTC), example "2023-02-25T00:05:36.309Z". |
| 54 |
required: false |
| 55 |
type: str |
| 56 |
restore_job_id: |
| 57 |
description: |
| 58 |
- ID of the restore job to get information about. |
| 59 |
- This parameter is mutually exlusive with all other parameters. |
| 60 |
required: false |
| 61 |
type: str |
| 62 |
extends_documentation_fragment: |
| 63 |
- amazon.aws.common.modules |
| 64 |
- amazon.aws.region.modules |
| 65 |
- amazon.aws.boto3 |
| 66 |
""" |
| 67 |
|
| 68 |
EXAMPLES = r""" |
| 69 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 70 |
|
| 71 |
- name: List all restore jobs |
| 72 |
amazon.aws.backup_restore_job_info: |
| 73 |
|
| 74 |
- name: List specific restore job's info by job ID |
| 75 |
amazon.aws.backup_restore_job_info: |
| 76 |
restore_job_id: "52BEE289-xxxx-xxxx-xxxx-47DCAA2E7ACD" |
| 77 |
|
| 78 |
- name: List restore jobs based on Account ID |
| 79 |
amazon.aws.backup_restore_job_info: |
| 80 |
account_id: xx1234567890 |
| 81 |
|
| 82 |
- name: List restore jobs based on status and created_before time |
| 83 |
amazon.aws.backup_restore_job_info: |
| 84 |
status: completed |
| 85 |
created_before: "2023-02-25T00:05:36.309Z" |
| 86 |
""" |
| 87 |
|
| 88 |
RETURN = r""" |
| 89 |
restore_jobs: |
| 90 |
returned: always |
| 91 |
description: |
| 92 |
- restore jobs that match the provided filters. |
| 93 |
- Each element consists of a dict with details related to that restore job. |
| 94 |
type: list |
| 95 |
elements: dict |
| 96 |
contains: |
| 97 |
account_id: |
| 98 |
description: |
| 99 |
- The account ID that owns the restore job. |
| 100 |
type: str |
| 101 |
returned: if restore job exists |
| 102 |
sample: "123456789012" |
| 103 |
created_resource_arn: |
| 104 |
description: |
| 105 |
- An Amazon Resource Name (ARN) that uniquely identifies a resource whose recovery point is being restored. |
| 106 |
- The format of the ARN depends on the resource type of the backed-up resource. |
| 107 |
type: str |
| 108 |
returned: if restore job exists |
| 109 |
sample: "arn:aws:ec2:us-east-2:xxxxxxxxxx..." |
| 110 |
creation_date: |
| 111 |
description: |
| 112 |
- The date and time that a restore job is created, in Unix format and Coordinated Universal Time (UTC). |
| 113 |
type: str |
| 114 |
returned: if restore job exists |
| 115 |
sample: "2023-03-13T15:53:07.172000-07:00" |
| 116 |
iam_role_arn: |
| 117 |
description: |
| 118 |
- The IAM role ARN used to create the target recovery point. |
| 119 |
type: str |
| 120 |
returned: if restore job exists |
| 121 |
sample: "arn:aws:ec2:us-east-2:xxxxxxxxxx..." |
| 122 |
percent_done: |
| 123 |
description: |
| 124 |
- The estimated percentage that is complete of a job at the time the job status was queried. |
| 125 |
type: str |
| 126 |
returned: if restore job exists |
| 127 |
sample: "0.00%" |
| 128 |
recovery_point_arn: |
| 129 |
description: |
| 130 |
- An ARN that uniquely identifies a recovery point. |
| 131 |
type: str |
| 132 |
returned: if restore job exists |
| 133 |
sample: "arn:aws:ec2:us-east-2:xxxxxxxxxx..." |
| 134 |
restore_job_id: |
| 135 |
description: |
| 136 |
- The ID of the job that restores a recovery point. |
| 137 |
type: str |
| 138 |
returned: if restore job exists |
| 139 |
sample: "AAAA1234-1D1D-1234-3F8E-1EB111EEEE00" |
| 140 |
status: |
| 141 |
description: |
| 142 |
- The state of the job initiated by Backup to restore a recovery point. |
| 143 |
type: str |
| 144 |
returned: if restore job exists |
| 145 |
sample: "COMPLETED" |
| 146 |
""" |
| 147 |
|
| 148 |
try: |
| 149 |
import botocore |
| 150 |
except ImportError: |
| 151 |
pass # caught by AnsibleAWSModule |
| 152 |
|
| 153 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 154 |
|
| 155 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 156 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 157 |
|
| 158 |
|
| 159 |
def build_request_args(account_id, status, created_before, created_after, completed_before, completed_after): |
| 160 |
request_args = { |
| 161 |
"ByAccountId": account_id if account_id else "", |
| 162 |
"ByStatus": status if status else "", |
| 163 |
"ByCreatedBefore": created_before if created_before else "", |
| 164 |
"ByCreatedAfter": created_after if created_after else "", |
| 165 |
"ByCompleteBefore": completed_before if completed_before else "", |
| 166 |
"ByCompleteAfter": completed_after if completed_after else "", |
| 167 |
} |
| 168 |
|
| 169 |
request_args = {k: v for k, v in request_args.items() if v} |
| 170 |
|
| 171 |
return request_args |
| 172 |
|
| 173 |
|
| 174 |
def _describe_restore_job(connection, module, restore_job_id): |
| 175 |
try: |
| 176 |
response = connection.describe_restore_job(RestoreJobId=restore_job_id) |
| 177 |
response.pop("ResponseMetadata", None) |
| 178 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 179 |
module.fail_json_aws(e, msg=f"Failed to describe restore job with ID: {restore_job_id}") |
| 180 |
|
| 181 |
return [camel_dict_to_snake_dict(response)] |
| 182 |
|
| 183 |
|
| 184 |
@AWSRetry.jittered_backoff() |
| 185 |
def _list_restore_jobs(connection, **params): |
| 186 |
paginator = connection.get_paginator("list_restore_jobs") |
| 187 |
return paginator.paginate(**params).build_full_result() |
| 188 |
|
| 189 |
|
| 190 |
def list_restore_jobs(connection, module, request_args): |
| 191 |
try: |
| 192 |
response = _list_restore_jobs(connection, **request_args) |
| 193 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 194 |
module.fail_json_aws(e, msg="Failed to list restore jobs") |
| 195 |
|
| 196 |
return [camel_dict_to_snake_dict(restore_job) for restore_job in response["RestoreJobs"]] |
| 197 |
|
| 198 |
|
| 199 |
def main(): |
| 200 |
argument_spec = dict( |
| 201 |
account_id=dict(required=False, type="str"), |
| 202 |
status=dict(required=False, type="str", choices=["PENDING", "RUNNING", "COMPLETED", "ABORTED", "FAILED"]), |
| 203 |
created_before=dict(required=False, type="str"), |
| 204 |
created_after=dict(required=False, type="str"), |
| 205 |
completed_before=dict(required=False, type="str"), |
| 206 |
completed_after=dict(required=False, type="str"), |
| 207 |
restore_job_id=dict(required=False, type="str"), |
| 208 |
) |
| 209 |
|
| 210 |
module = AnsibleAWSModule( |
| 211 |
argument_spec=argument_spec, |
| 212 |
supports_check_mode=True, |
| 213 |
) |
| 214 |
|
| 215 |
backup_client = module.client("backup") |
| 216 |
|
| 217 |
request_args = build_request_args( |
| 218 |
account_id=module.params["account_id"], |
| 219 |
status=module.params["status"], |
| 220 |
created_before=module.params["created_before"], |
| 221 |
created_after=module.params["created_after"], |
| 222 |
completed_before=module.params["completed_before"], |
| 223 |
completed_after=module.params["completed_after"], |
| 224 |
) |
| 225 |
|
| 226 |
if module.params.get("restore_job_id"): |
| 227 |
restore_jobs = _describe_restore_job(backup_client, module, module.params.get("restore_job_id")) |
| 228 |
else: |
| 229 |
restore_jobs = list_restore_jobs(backup_client, module, request_args) |
| 230 |
|
| 231 |
module.exit_json(changed=False, restore_jobs=restore_jobs) |
| 232 |
|
| 233 |
|
| 234 |
if __name__ == "__main__": |
| 235 |
main() |