
    Vh++                         d Z dZdZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ 	 dd	lmZ dd
lmZ d ZddZd Zd Zd Zedk(  r e        yy# e$ r Y %w xY w)a  
---
module: backup_vault
version_added: 6.0.0
short_description: Manage AWS Backup Vaults
description:
  - Creates, deletes, or lists Backup Vault configuration.
author:
  - Gomathi Selvi Srinivasan (@GomathiselviS)
options:
  state:
    description:
      - Add or remove Backup Vault configuration.
    type: str
    choices: ['present', 'absent']
    default: present
  backup_vault_name:
    description:
      - Name for the Backup Vault.
      - Names are  unique to the account used to create them and the Amazon Web Services Region where they are created.
      - They consist of letters, numbers, and hyphens.
    type: str
    required: true
  encryption_key_arn:
    description:
      - The server-side encryption key that is used to protect the backups.
    type: str
  creator_request_id:
    description:
      - A unique string that identifies the request and allows failed requests to be retried without the risk of running the operation twice.
      - If used, this parameter must contain 1 to 50 alphanumeric or "-_." characters.
    type: str

extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
  - amazon.aws.tags
a	  
- name: create backup vault
  amazon.aws.backup_vault:
    state: present
    backup_vault_name: default-vault
    encryption_key_arn: arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab
    tags:
      environment: dev
      Name: default
a  
exists:
    description: Whether the Backup Vault exists.
    returned: always
    type: bool
    sample: true
vault:
    description: Backup Vault details.
    returned: always
    type: complex
    sample: hash/dictionary of values
    contains:
        backup_vault_name:
            description: The name of a logical container where backups are stored.
            returned: always
            type: str
            sample: default-name
        backup_vault_arn:
            description: An Amazon Resource Name (ARN) that uniquely identifies a backup vault.
            returned: always
            type: str
            sample: arn:aws:backup:us-east-1:123456789012:vault:aBackupVault
        creation_date:
            description: The date and time a backup vault is created, in Unix format and Coordinated Universal Time (UTC).
            returned: always
            type: str
            sample: "2024-05-21T13:21:10.062000-07:00"
        encryption_key_arn:
            description: The server-side encryption key that is used to protect your backups.
            returned: always
            type: str
            sample: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
        locked:
            description:
                - A Boolean that indicates whether Backup Vault Lock is currently protecting the backup vault.
                - True means that Vault Lock causes delete or update operations on the recovery points stored in the vault to fail.
            returned: always
            type: bool
            sample: false
        number_of_recovery_points:
            description: The number of recovery points that are stored in a backup vault.
            returned: always
            type: int
            sample: 0
        tags:
            description: Tags of the backup vault.
            returned: on create/update
            type: str
            sample: {
                        "TagKey1": "TagValue1",
                        "TagKey2": "TagValue2"
                    }
    )camel_dict_to_snake_dict)get_backup_resource_tags)is_boto3_error_code)AnsibleAWSModule)compare_aws_tags)BotoCoreError)ClientErrorc                     i }|j                         D ci c]  \  }}|	|| }}}	  |j                  di |}|S c c}}w # t        t        f$ r}| j	                  |d       Y d}~|S d}~ww xY w)z
    Creates a Backup Vault

    module : AnsibleAWSModule object
    client : boto3 client connection object
    params : The parameters to create a backup vault
    NzFailed to create Backup Vaultmsg )itemscreate_backup_vaultr   r	   fail_json_aws)moduleclientparamsrespkverrs          k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/backup_vault.pyr   r      s     D%||~?tq!ad?F?G)v))3F3 K @ 	 G 	S&EFFKGs   
>>A A1A,,A1Nc                 r   |y|xs i }t        |||      \  }}|s|sy| j                  ry|r	 |j                  ||       |r	 |j                  ||       yy# t        t        f$ r}| j                  |d       Y d}~?d}~ww xY w# t        t        f$ r}| j                  |d	       Y d}~yd}~ww xY w)
a  
    Creates, updates, removes tags on a Backup Vault resource

    module : AnsibleAWSModule object
    client : boto3 client connection object
    tags : Dict of tags converted from ansible_dict to boto3 list of dicts
    vault_arn : The ARN of the Backup Vault to operate on
    curr_tags : Dict of the current tags on resource, if any
    purge_tags : true/false to determine if current tags will be retained or not
    NF)
purge_tagsT)ResourceArn
TagKeyListz$Failed to remove tags from the vaultr   )r   TagszFailed to add tags to Vault)r   
check_modeuntag_resourcer   r	   r   tag_resource)	r   r   tags	vault_arn	curr_tagsr   tags_to_addtags_to_remover   s	            r   	tag_vaultr&      s     |RI"29dz"ZK~	R!!iN!S 	IIKH  {+ 	R  *P QQ	R {+ 	I  *G HH	Is/   A B
 B*BB
B6B11B6c                 8   d}	 |j                  |      }|r4|j                  d      r!|j                  d      }t        | ||      |d<   |S y# t        d      $ r | j                  d       Y Xt        t        f$ r}| j                  |d       Y d}~~d}~ww xY w)	z
    Describes existing vault in an account

    module : AnsibleAWSModule object
    client : boto3 client connection object
    vault_name : Name of the backup vault
    NBackupVaultNameAccessDeniedExceptionz-Access Denied trying to describe backup vaultzUnable to get vault factsr   BackupVaultArnr!   )describe_backup_vaultr   warnr   r	   r   getr   )r   r   
vault_namer   r   resources         r   get_vault_factsr1      s     DC++J+G 88$%xx 01H3FFHMDL  67 ECD;' CS&ABBCs   A B.B<BBc                     	 |j                  |       y# t        t        f$ r}| j                  |d       Y d}~yd}~ww xY w)z
    Delete a Backup Vault

    module : AnsibleAWSModule object
    client : boto3 client connection object
    vault_name : Backup Vault Name
    r(   z!Failed to delete the Backup Vaultr   N)delete_backup_vaultr   r	   r   )r   r   r/   r   s       r   r3   r3      sG    K"":">;' KS&IJJKs    A<Ac                     t        t        dddg      t        dd      t        dd      t        d	      t        d
dg      t        dd            } dddgfdddgfg}t        | d|      }|j                  d   dv rd}n|j                  d   dv rd}|j                  d   }|j                  d   }t        |j                  d   |j                  d   |j                  d   |j                  d         }|j                  d      }t        dd      }	 t	        |||d         }		d|d<   dk(  r9|d   r4d|d <   d|d<   t               |d!<   |j                  st        |||	d          n|dk(  rq|d   sld|d <   d|d<   |j                  s$|r||d"<   t        |||       t	        |||d         }	|j                  rut               }	|	j                  |       d#|	d$<   ||	d<   nO|dk(  rJ|d   rEt        ||||	d%   |	d   |&      }|r,t               }|s|	d   }|j                  |       d|d <   ||	d<   |	rt        |	dg'      |d(<    |j                  d)i | y # t
        t        f$ r}
|j                  d|
        Y d }
~
Yd }
~
ww xY w)*Npresentabsent)defaultchoicesTstr)requiredtypeF)r;   no_log)r;   dictresource_tags)r;   aliasesbool)r7   r;   )statebackup_vault_nameencryption_key_arncreator_request_idr!   r   rA   rB   enabled)argument_specsupports_check_moderequired_if)r5   rE   )r6   disabledr!   r   rC   rD   )r)   BackupVaultTagsEncryptionKeyArnCreatorRequestIdbackup)changedexistsr)   zUnable to get vault facts rO   rN   backupvaultrJ    rK   r+   )r!   r"   r#   r   )ignore_listvaultr   )r=   r   r   r   r1   r   r	   debugr   r3   r   updater&   r   	exit_json)rF   rH   r   rA   r!   r   	ct_paramsr   resultsrS   r   tags_changedupdated_tagss                r   mainr[      s   9y(.CD59U59U+v'8962M 
)123	)123K
 MtalmF }}W!77	w	#9	9== D|,J&9:f-';<';<	I ]]8$F5/G9	:K0LM
  WX.!	!!%  6G0HI	)	GH$5!	   /3	+,	: $FFI>O4PQE FELL#(*E$% E&M	)	 1 ,-Fm!
 6L$V}%!%GI(E&M 3ExPFws ;' 91#7889s   I J&J  J__main__)NT)DOCUMENTATIONEXAMPLESRETURN0ansible.module_utils.common.dict_transformationsr   :ansible_collections.amazon.aws.plugins.module_utils.backupr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   botocore.exceptionsr   r	   ImportErrorr   r&   r1   r3   r[   __name__r       r   <module>ri      s   &P	4
n V _ \ X X	1/
($N<K^ B zF Y  		s   A AA