
    VhI                         d Z dZdZddlZ	 ddl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 Zedk(  r e        yy# e$ r Y ?w xY w)a  
module: backup_selection
short_description: Create, delete and modify AWS Backup selection
version_added: 6.0.0
description:
  - Manages AWS Backup selections.
  - For more information see the AWS documentation for backup selections
    U(https://docs.aws.amazon.com/aws-backup/latest/devguide/assigning-resources.html).
options:
  backup_plan_name:
    description:
      - Uniquely identifies the backup plan to be associated with the selection of resources.
    required: true
    type: str
    aliases:
      - plan_name
  backup_selection_name:
    description:
      - The display name of a resource selection document. Must contain 1 to 50 alphanumeric or '-_.' characters.
    required: true
    type: str
    aliases:
      - selection_name
  iam_role_arn:
    description:
      - The ARN of the IAM role that Backup uses to authenticate when backing up the target resource.
    type: str
  resources:
    description:
      - A list of Amazon Resource Names (ARNs) to assign to a backup plan. The maximum number of ARNs is 500 without wildcards,
        or 30 ARNs with wildcards. If you need to assign many resources to a backup plan, consider a different resource selection
        strategy, such as assigning all resources of a resource type or refining your resource selection using tags.
    type: list
    elements: str
  list_of_tags:
    description:
      - A list of conditions that you define to assign resources to your backup plans using tags.
      - Condition operators are case sensitive.
      - When you specify more than one condition in O(list_of_tags), you assign all resources that match AT LEAST ONE condition (using OR logic).
    type: list
    elements: dict
    suboptions:
        condition_type:
            description:
            - An operation applied to a key-value pair used to assign resources to your backup plan.
            - Condition only supports V(STRINGEQUALS).
            type: str
        condition_key:
            description:
            - The key in a key-value pair.
            type: str
        condition_value:
            description:
            - The value in a key-value pair.
            type: str
  not_resources:
    description:
      - A list of Amazon Resource Names (ARNs) to exclude from a backup plan. The maximum number of ARNs is 500 without wildcards,
        or 30 ARNs with wildcards. If you need to exclude many resources from a backup plan, consider a different resource
        selection strategy, such as assigning only one or a few resource types or refining your resource selection using tags.
    type: list
    elements: str
  conditions:
    description:
      - A list of conditions (expressed as a dict) that you define to assign resources to your backup plans using tags.
      - When you specify more than one condition in I(conditions), you only assign the resources that match ALL conditions (using AND logic).
      - O(conditions) supports V(string_equals), V(string_like), V(string_not_equals), and V(string_not_like). O(list_of_tags) only supports V(string_equals).
    type: dict
    suboptions:
      string_equals:
        description:
          - Filters the values of your tagged resources for only those resources that you tagged with the same value.
        type: list
        default: []
        elements: dict
        suboptions:
          condition_key:
            description:
              - The key in a key-value pair.
              - O(conditions.string_equals.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name'
            type: str
          condition_value:
            description: The value in a key-value pair.
            type: str
      string_like:
        description:
          - Filters the values of your tagged resources for matching tag values with the use of a wildcard character (*) anywhere in the string.
            For example, "prod*" or "*rod*" matches the tag value "production".
        type: list
        default: []
        elements: dict
        suboptions:
          condition_key:
            description:
              - The key in a key-value pair.
              - O(conditions.string_like.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name'
            type: str
          condition_value:
            description: The value in a key-value pair.
            type: str
      string_not_equals:
        description:
          - Filters the values of your tagged resources for only those resources that you tagged that do not have the same value.
        type: list
        default: []
        elements: dict
        suboptions:
          condition_key:
            description:
              - The key in a key-value pair.
              - O(conditions.string_not_equals.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name'
            type: str
          condition_value:
            description: The value in a key-value pair.
            type: str
      string_not_like:
        description:
          - Filters the values of your tagged resources for non-matching tag values with the use of a wildcard character (*) anywhere in the string.
        type: list
        default: []
        elements: dict
        suboptions:
          condition_key:
            description:
              - The key in a key-value pair.
              - O(conditions.string_not_like.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name'
            type: str
          condition_value:
            description: The value in a key-value pair.
            type: str
  state:
    description:
      - Create, delete a backup selection.
    default: present
    choices: ['present', 'absent']
    type: str
author:
  - Kristof Imre Szabo (@krisek)
  - Alina Buzachis (@alinabuzachis)
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
aW  
- name: Create backup selection
  amazon.aws.backup_selection:
    selection_name: elastic
    backup_plan_name: test-backup-plan
    iam_role_arn: arn:aws:iam::111122223333:role/system-backup
    resources:
      - arn:aws:elasticfilesystem:*:*:file-system/*

- name: Create backup selection, assign resources using tags
  amazon.aws.backup_selection:
    selection_name: elastic
    backup_plan_name: test-backup-plan
    iam_role_arn: arn:aws:iam::111122223333:role/system-backup
    list_of_tags:
      - condition_type: STRINGEQUALS
        condition_key: Owner
        condition_value: Dev01
a  
backup_selection:
  description: Backup selection details.
  returned: always
  type: complex
  contains:
    backup_plan_id:
      description: ID of the backup plan.
      returned: always
      type: str
      sample: "1111f877-1ecf-4d79-9718-a861cd09df3b"
    creation_date:
      description: Creation date of the backup plan.
      returned: on create/update
      type: str
      sample: '2023-01-24T10:08:03.193000+01:00'
    iam_role_arn:
      description: The ARN of the IAM role that Backup uses to authenticate when backing up the target resource.
      returned: always
      type: str
      sample: "arn:aws:iam::111122223333:role/system-backup"
    selection_id:
      description: Backup selection id.
      returned: always
      type: str
      sample: "1111c217-5d71-4a55-8728-5fc4e63d437b"
    selection_name:
      description: The display name of a resource selection document.
      returned: always
      type: str
      sample: elastic
    conditions:
      description: A list of conditions that defines how resources are assigned to backup plans using tags.
      returned: always
      type: dict
      sample: {
                "string_equals": [],
                "string_like": [],
                "string_not_equals": [],
                "string_not_like": []
            }
      contains:
        string_equals:
          description: Filters the values of tagged resources for only those resources that are tagged with the same value.
          returned: always
          type: list
          sample: []
        string_like:
          description: Filters the values of tagged resources for matching tag values with the use of a wildcard character (*) anywhere in the string.
          returned: always
          type: list
          sample: []
        string_not_equals:
          description: Filters the values of tagged resources for only those resources that are not tagged with the same value.
          returned: always
          type: list
          sample: []
        string_not_like:
          description: Filters the values of tagged resources for non-matching tag values with the use of a wildcard character (*) anywhere in the string.
          returned: always
          type: list
          sample: []
    list_of_tags:
      description: Conditions defined to assign resources to the backup plans using tags.
      returned: always
      type: list
      elements: dict
      sample: [
                {
                    "condition_key": "Environment",
                    "condition_type": "STRINGEQUALS",
                    "condition_value": "Dev"
                }
            ]
      contains:
        condition_key:
          description: The key in a key-value pair.
          returned: always
          type: str
          sample: "Environment"
        condition_type:
          description: An operation applied to a key-value pair used to assign resources to backup plan.
          returned: always
          type: str
          sample: "STRINGEQUALS"
        condition_value:
          description: The value in a key-value pair.
          returned: always
          type: str
          sample: "Dev"
    not_resources:
      description: List of Amazon Resource Names (ARNs) that are excluded from the backup plan.
      returned: always
      type: list
      sample: []
    resources:
      description: List of Amazon Resource Names (ARNs) that are assigned to the backup plan.
      returned: always
      type: list
      sample: []
    N)camel_dict_to_snake_dict)snake_dict_to_camel_dict)get_plan_details)get_selection_details)AnsibleAWSModule)AWSRetryc                 ,   d}| d   j                  dd       |k7  rd}g d}|D ]n  }t        j                  | d   j                  |g       d      }t        j                  |j                  |g       d      }||k7  sZ|dk7  rd}b|dk(  r|d	k(  rmd}p |S )
NFr   
IamRoleArnT)	Resources
ListOfTagsNotResources
Conditions)	sort_keysr   zR{"StringEquals": [], "StringLike": [], "StringNotEquals": [], "StringNotLike": []}z[])getjsondumps)current_selectionbackup_selection_dataiam_role_arnupdate_neededfields_to_check
field_namefield_value_from_awsnew_field_values           o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/backup_selection.pycheck_for_updater   %  s    Md3|COO% %
#zz*;A*>*B*B:r*R^bc**%:%>%>z2%NZ^_22\) $$gh#t+ $%     c                  
   t        t        dddg      t        dddg      t        d      t        dd      t        d	t        t        dg d	t        t        dd
      t        d                  t        dg d	t        t        dd
      t        d                  t        dg d	t        t        dd
      t        d                  t        dg d	t        t        dd
      t        d                              t        dd      t        dd	t        t        d      t        dd
      t        d                  t        dddg            } ddg dfddddgfg}t        | |d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }	|j                  j                  d      }
	 |j	                  d t        j                         !      }d
d
i d$}t        |||      }||d%<   |dk(  rd
}||d&}|r||d'<   |rt        |d(      |d)<   |	r|	|d*<   |
rt        |
d(      |d+<   |rdd|d,<   t        |||      }|rI|j                  rd|d-<    |j                   d:i |d.d/i 	 |j#                  d|d0   d1   |d0   d2   3       n|sd|d,<   |r|rSd|d-<   d|d,<   t%        |||      }|j                  r |j                   d:i |d.d/i 	 |j'                  ||d0   d5   6       t        ||||      }t)        | |d8<   nO|dk(  rJ|rHd|d-<   |j                  r |j                   d:i |d.d9i 	 |j#                  d|d0   d1   |d0   d2   3        |j                   d:i | y # t        j                  j                  t        j                  j                  f$ r}|j                  |d"#       Y d }~d }~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |d4#       Y d }~{d }~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |d7#       Y d }~|d }~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |d4#       Y d }~kd }~ww xY w);NstrTselection_name)typerequiredaliases	plan_name)r!   list)r!   elementsdictF)r!   no_log)condition_keycondition_value)r!   defaultr&   options)string_equalsstring_likestring_not_equalsstring_not_like)r!   r,   )condition_typer)   r*   )r!   r&   r,   presentabsent)r+   choices)backup_selection_namebackup_plan_namer   	resources
conditionsnot_resourceslist_of_tagsstater;   )r5   r6   r   r5   r6   )argument_specrequired_ifsupports_check_moder   r7   r:   r9   r8   backup)retry_decoratorzFailed to connect to AWS)msg)changedexistsbackup_selectionr   )SelectionNamer
   r   )capitalize_firstr   r   r   rC   rB   rA   z1Would have created selection if not in check moder   SelectionIdBackupPlanId)	aws_retryrG   rH   zFailed to delete selectionbackup_plan_id)BackupSelectionrH   zFailed to create selectionrD   z8Would have deleted backup selection if not in check mode )r'   r   paramsr   clientr   jittered_backoffbotocore
exceptionsClientErrorBotoCoreErrorfail_json_awsr   r   r   
check_mode	exit_jsondelete_backup_selectionr   create_backup_selectionr   )r<   r=   moduler;   r5   r6   r   r7   r:   r9   r8   rN   eresultsr   r   r   plannew_selections                      r   mainr^   :  s   "GWFXY54+Ou%FU3"# &*e&D(,%(8	 !# &*e&D(,%(8	 #'# &*e&D(,%(8	# !%# &*e&D(,%(8	!9%(
R 7#/"e< $% 0
 9y(.CDq9Mv 
)Z[	(46HIJK M{hlmFMMg&E"MM--.>?}}(();<==$$^4L!!+.I==$$^4LMM%%o6M""<0J@x9R9R9TU  5bIG-ff>NPef#4G 	2GWc d1:!+.2J<jn2o!,/4A!.12J:hl2m!,/ $GH,->@UWcdM$$)-GI&$F$$hwh4ghN22"&$5a$8$G%6q%9.%I 3  #$(! M!%GI $GH#FF4DED     d7d0cdJ..$9QP`Ha /  .ff>NPef&>&N"#	(	!%GI     k7k0jkJ.." 1! 4] C!21!5n!E /  FwO ++X-@-@-N-NO @Q$>??@F !++779L9L9Z9Z[ N((0L(MMN  ''33X5H5H5V5VW J$$Q,H$IIJ" ''33X5H5H5V5VW J$$Q,H$IIJs`   %P	  Q! 4R9  T 	7Q QQ!7R6R11R697T0T		T7U&U!!U&__main__)DOCUMENTATIONEXAMPLESRETURNr   rP   ImportError0ansible.module_utils.common.dict_transformationsr   r   :ansible_collections.amazon.aws.plugins.module_utils.backupr   r   8ansible_collections.amazon.aws.plugins.module_utils.corer   7ansible_collections.amazon.aws.plugins.module_utils.ec2r   r   r^   __name__rL   r   r   <module>ri      su   Od*d
L 	 V U W \ U L*R j zF m  		s   A AA