| 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 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
--- |
| 10 |
module: backup_vault_info |
| 11 |
version_added: 6.0.0 |
| 12 |
short_description: Describe AWS Backup Vaults |
| 13 |
description: |
| 14 |
- Lists info about Backup Vault configuration. |
| 15 |
author: |
| 16 |
- Gomathi Selvi Srinivasan (@GomathiselviS) |
| 17 |
options: |
| 18 |
backup_vault_names: |
| 19 |
type: list |
| 20 |
elements: str |
| 21 |
default: [] |
| 22 |
description: |
| 23 |
- Specifies a list of vault names. |
| 24 |
- If an empty list is specified, information for the backup vaults in the current region is returned. |
| 25 |
|
| 26 |
extends_documentation_fragment: |
| 27 |
- amazon.aws.common.modules |
| 28 |
- amazon.aws.region.modules |
| 29 |
- amazon.aws.boto3 |
| 30 |
""" |
| 31 |
|
| 32 |
EXAMPLES = r""" |
| 33 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 34 |
|
| 35 |
# Gather information about all backup vaults |
| 36 |
- amazon.aws.backup_vault_info |
| 37 |
|
| 38 |
# Gather information about a particular backup vault |
| 39 |
- amazon.aws.backup_vault_info: |
| 40 |
backup vault_names: |
| 41 |
- "arn:aws:backup_vault:us-east-2:123456789012:backup_vault/defaultvault" |
| 42 |
""" |
| 43 |
|
| 44 |
RETURN = r""" |
| 45 |
backup_vaults: |
| 46 |
description: List of backup vault objects. Each element consists of a dict with all the information related to that backup vault. |
| 47 |
type: list |
| 48 |
elements: dict |
| 49 |
returned: always |
| 50 |
contains: |
| 51 |
backup_vault_name: |
| 52 |
description: Name of the backup vault. |
| 53 |
type: str |
| 54 |
sample: "default vault" |
| 55 |
backup_vault_arn: |
| 56 |
description: ARN of the backup vault. |
| 57 |
type: str |
| 58 |
sample: "arn:aws:backup:us-west-2:111122223333:vault/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 59 |
encryption_key_arn: |
| 60 |
description: The server-side encryption key that is used to protect the backups. |
| 61 |
type: str |
| 62 |
sample: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 63 |
creation_date: |
| 64 |
description: The date and time a backup vault is created, in Unix format and Coordinated Universal Time (UTC). |
| 65 |
type: str |
| 66 |
sample: "1516925490.087 (represents Friday, January 26, 2018 12:11:30.087 AM)." |
| 67 |
creator_request_id: |
| 68 |
description: |
| 69 |
- A unique string that identifies the request and allows failed requests to be retried without the risk of running the operation twice. |
| 70 |
type: str |
| 71 |
number_of_recovery_points: |
| 72 |
description: The number of recovery points that are stored in a backup vault. |
| 73 |
type: int |
| 74 |
locked: |
| 75 |
description: |
| 76 |
- Indicates whether Backup Vault Lock is currently protecting the backup vault. |
| 77 |
- True means that Vault Lock causes delete or update operations on the recovery points stored in the vault to fail. |
| 78 |
type: bool |
| 79 |
sample: true |
| 80 |
min_retention_days: |
| 81 |
description: |
| 82 |
- The minimum retention period that the vault retains its recovery points. |
| 83 |
- If this parameter is not specified, Vault Lock does not enforce a minimum retention period. |
| 84 |
type: int |
| 85 |
sample: 120 |
| 86 |
max_retention_days: |
| 87 |
description: |
| 88 |
- The maximum retention period that the vault retains its recovery points. |
| 89 |
- If this parameter is not specified, Vault Lock does not enforce a maximum retention period (allowing indefinite storage). |
| 90 |
type: int |
| 91 |
sample: 123 |
| 92 |
lock_date: |
| 93 |
description: The date and time when Backup Vault Lock configuration cannot be changed or deleted. |
| 94 |
type: str |
| 95 |
sample: "1516925490.087 represents Friday, January 26, 2018 12:11:30.087 AM." |
| 96 |
|
| 97 |
""" |
| 98 |
|
| 99 |
try: |
| 100 |
import botocore |
| 101 |
except ImportError: |
| 102 |
pass # Handled by AnsibleAWSModule |
| 103 |
|
| 104 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 105 |
|
| 106 |
from ansible_collections.amazon.aws.plugins.module_utils.backup import get_backup_resource_tags |
| 107 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 108 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 109 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 110 |
|
| 111 |
|
| 112 |
def get_backup_vaults(connection, module): |
| 113 |
all_backup_vaults = [] |
| 114 |
try: |
| 115 |
result = connection.get_paginator("list_backup_vaults") |
| 116 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 117 |
module.fail_json_aws(e, msg="Failed to get the backup vaults.") |
| 118 |
for backup_vault in result.paginate(): |
| 119 |
all_backup_vaults.extend(list_backup_vaults(backup_vault)) |
| 120 |
return all_backup_vaults |
| 121 |
|
| 122 |
|
| 123 |
def list_backup_vaults(backup_vault_dict): |
| 124 |
return [x["BackupVaultName"] for x in backup_vault_dict["BackupVaultList"]] |
| 125 |
|
| 126 |
|
| 127 |
def get_backup_vault_detail(connection, module): |
| 128 |
output = [] |
| 129 |
result = {} |
| 130 |
backup_vault_name_list = module.params.get("backup_vault_names") |
| 131 |
if not backup_vault_name_list: |
| 132 |
backup_vault_name_list = get_backup_vaults(connection, module) |
| 133 |
for name in backup_vault_name_list: |
| 134 |
try: |
| 135 |
output.append(connection.describe_backup_vault(BackupVaultName=name, aws_retry=True)) |
| 136 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 137 |
module.fail_json_aws(e, msg=f"Failed to describe vault {name}") |
| 138 |
# Turn the boto3 result in to ansible_friendly_snaked_names |
| 139 |
snaked_backup_vault = [] |
| 140 |
for backup_vault in output: |
| 141 |
try: |
| 142 |
resource = backup_vault.get("BackupVaultArn", None) |
| 143 |
tag_dict = get_backup_resource_tags(module, connection, resource) |
| 144 |
backup_vault.update({"tags": tag_dict}) |
| 145 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 146 |
module.warn(f"Failed to get the backup vault tags - {e}") |
| 147 |
snaked_backup_vault.append(camel_dict_to_snake_dict(backup_vault)) |
| 148 |
|
| 149 |
# Turn the boto3 result in to ansible friendly tag dictionary |
| 150 |
for v in snaked_backup_vault: |
| 151 |
if "tags_list" in v: |
| 152 |
v["tags"] = boto3_tag_list_to_ansible_dict(v["tags_list"], "key", "value") |
| 153 |
del v["tags_list"] |
| 154 |
if "response_metadata" in v: |
| 155 |
del v["response_metadata"] |
| 156 |
result["backup_vaults"] = snaked_backup_vault |
| 157 |
return result |
| 158 |
|
| 159 |
|
| 160 |
def main(): |
| 161 |
argument_spec = dict( |
| 162 |
backup_vault_names=dict(type="list", elements="str", default=[]), |
| 163 |
) |
| 164 |
|
| 165 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 166 |
|
| 167 |
try: |
| 168 |
connection = module.client("backup", retry_decorator=AWSRetry.jittered_backoff()) |
| 169 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 170 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 171 |
result = get_backup_vault_detail(connection, module) |
| 172 |
module.exit_json(**result) |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
main() |