
    Vhe                        d Z dZd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dlmZ ddlmZ 	 ddlmZ ddlmZ  ej,                         d        Zd Zdedeeef   fdZdeddfdZdeddfdZdedeeef   fdZdedefdZd Z d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(e)d"k(  r e(        yy# e$ r Y |w xY w)#ao	  
module: route53_zone
short_description: add or delete Route 53 zones
version_added: 5.0.0
description:
    - Creates and deletes Route 53 private and public zones.
    - This module was originally added to C(community.aws) in release 1.0.0.
options:
    zone:
        description:
            - "The DNS zone record (eg: foo.com.)"
        required: true
        type: str
    state:
        description:
            - Whether or not the zone should exist or not.
        default: present
        choices: [ "present", "absent" ]
        type: str
    vpc_id:
        description:
            - The VPC ID the zone should be a part of (if this is going to be a private zone).
        type: str
    vpc_region:
        description:
            - The VPC Region the zone should be a part of (if this is going to be a private zone).
        type: str
    vpcs:
        version_added: 5.3.0
        description:
            - The VPCs the zone should be a part of (if this is going to be a private zone).
        type: list
        elements: dict
        suboptions:
            id:
                description:
                    - The ID of the VPC.
                type: str
                required: true
            region:
                description:
                    - The region of the VPC.
                type: str
                required: true
    comment:
        description:
            - Comment associated with the zone.
        default: ''
        type: str
    hosted_zone_id:
        description:
            - The unique zone identifier you want to delete or "all" if there are many zones with the same domain name.
            - Required if there are multiple zones identified with the above options.
        type: str
    delegation_set_id:
        description:
            - The reusable delegation set ID to be associated with the zone.
            - Note that you can't associate a reusable delegation set with a private hosted zone.
        type: str
    dnssec:
        description:
            - Enables DNSSEC signing in a specific hosted zone.
        type: bool
        default: false
        version_added: 9.2.0
extends_documentation_fragment:
    - amazon.aws.common.modules
    - amazon.aws.region.modules
    - amazon.aws.tags
    - amazon.aws.boto3
notes:
    - Support for O(tags) and O(purge_tags) was added in release 2.1.0.
author:
    - "Christopher Troup (@minichate)"
a  
- name: create a public zone
  amazon.aws.route53_zone:
    zone: example.com
    comment: this is an example

- name: delete a public zone
  amazon.aws.route53_zone:
    zone: example.com
    state: absent

- name: create a private zone
  amazon.aws.route53_zone:
    zone: devel.example.com
    vpc_id: '{{ myvpc_id }}'
    vpc_region: us-west-2
    comment: developer domain

- name: create a private zone with multiple associated VPCs
  amazon.aws.route53_zone:
    zone: crossdevel.example.com
    vpcs:
      - id: vpc-123456
        region: us-west-2
      - id: vpc-000001
        region: us-west-2
    comment: developer cross-vpc domain

- name: create a public zone associated with a specific reusable delegation set
  amazon.aws.route53_zone:
    zone: example.com
    comment: reusable delegation set example
    delegation_set_id: A1BCDEF2GHIJKL

- name: create a public zone with tags
  amazon.aws.route53_zone:
    zone: example.com
    comment: this is an example
    tags:
      Owner: Ansible Team

- name: modify a public zone, removing all previous tags and adding a new one
  amazon.aws.route53_zone:
    zone: example.com
    comment: this is an example
    tags:
      Support: Ansible Community
    purge_tags: true
a  
comment:
    description: Optional hosted zone comment.
    returned: when hosted zone exists
    type: str
    sample: "Private zone"
name:
    description: Hosted zone name.
    returned: when hosted zone exists
    type: str
    sample: "private.local."
private_zone:
    description: Whether hosted zone is private or public.
    returned: when hosted zone exists
    type: bool
    sample: true
vpc_id:
    description: Id of the first vpc attached to private hosted zone (use vpcs for associating multiple).
    returned: for private hosted zone
    type: str
    sample: "vpc-1d36c84f"
vpc_region:
    description: Region of the first vpc attached to private hosted zone (use vpcs for assocaiting multiple).
    returned: for private hosted zone
    type: str
    sample: "eu-west-1"
vpcs:
    version_added: 5.3.0
    description: The list of VPCs attached to the private hosted zone.
    returned: for private hosted zone
    type: list
    elements: dict
    sample: "[{'id': 'vpc-123456', 'region': 'us-west-2'}]"
    contains:
        id:
            description: ID of the VPC.
            returned: for private hosted zone
            type: str
            sample: "vpc-123456"
        region:
            description: Region of the VPC.
            returned: for private hosted zone
            type: str
            sample: "eu-west-2"
zone_id:
    description: Hosted zone id.
    returned: when hosted zone exists
    type: str
    sample: "Z6JQG9820BEFMW"
delegation_set_id:
    description: Id of the associated reusable delegation set.
    returned: for public hosted zones, if they have been associated with a reusable delegation set
    type: str
    sample: "A1BCDEF2GHIJKL"
dnssec:
    description: Information about DNSSEC for a specific hosted zone.
    returned: when O(state=present) and the hosted zone is public
    version_added: 9.2.0
    type: dict
    contains:
        key_signing_key:
            description: The key-signing key (KSK) that the request creates.
            returned: when O(state=present)
            type: list
            elements: dict
            contains:
                name:
                    description: A string used to identify a key-signing key (KSK).
                    type: str
                kms_arn:
                    description: The Amazon resource name (ARN) used to identify the customer managed key in Key Management Service (KMS).
                    type: str
                flag:
                    description: An integer that specifies how the key is used.
                    type: int
                signing_algorithm_mnemonic:
                    description: A string used to represent the signing algorithm.
                    type: str
                signing_algorithm_type:
                    description: An integer used to represent the signing algorithm.
                    type: int
                digest_algorithm_mnemonic:
                    description: A string used to represent the delegation signer digest algorithm.
                    type: str
                digest_algorithm_type:
                    description: An integer used to represent the delegation signer digest algorithm.
                    type: int
                key_tag:
                    description: An integer used to identify the DNSSEC record for the domain name.
                    type: int
                digest_value:
                    description: A cryptographic digest of a DNSKEY resource record (RR).
                    type: str
                public_key:
                    description: The public key, represented as a Base64 encoding.
                    type: str
                ds_record:
                    description: A string that represents a delegation signer (DS) record.
                    type: str
                dnskey_record:
                    description: A string that represents a DNSKEY record.
                    type: str
                status:
                    description: A string that represents the current key-signing key (KSK) status.
                    type: str
                status_message:
                    description: The status message provided for ACTION_NEEDED or INTERNAL_FAILURE statuses.
                    type: str
                created_date:
                    description: The date when the key-signing key (KSK) was created.
                    type: str
                last_modified_date:
                    description: The last time that the key-signing key (KSK) was changed.
                    type: str
            sample: [{
                "created_date": "2024-12-04T15:15:36.715000+00:00",
                "digest_algorithm_mnemonic": "SHA-256",
                "digest_algorithm_type": 2,
                "digest_value": "xxx",
                "dnskey_record": "xxx",
                "ds_record": "xxx",
                "flag": 257,
                "key_tag": 18948,
                "kms_arn": "arn:aws:kms:us-east-1:xxx:key/xxx",
                "last_modified_date": "2024-12-04T15:15:36.715000+00:00",
                "name": "ansible-test-44230979--ksk",
                "public_key": "xxxx",
                "signing_algorithm_mnemonic": "ECDSAP256SHA256",
                "signing_algorithm_type": 13,
                "status": "INACTIVE"
            }]
        status:
            description: A dictionary representing the status of DNSSEC.
            type: dict
            contains:
                serve_signature:
                    description: A string that represents the current hosted zone signing status.
                    type: str
            sample: {
                "serve_signature": "SIGNING"
            }
tags:
    description: Tags associated with the zone.
    returned: when tags are defined
    type: dict
    N)Any)Dict)camel_dict_to_snake_dict)is_boto3_error_code)AnsibleAWSModule)AWSRetry)get_tags)manage_tags)BotoCoreError)ClientErrorc                  h    t         j                  d      } | j                         j                         S )Nlist_hosted_zones)clientget_paginatorpaginatebuild_full_result)	paginators    k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/route53_zone.py_list_zonesr   +  s+    $$%89I1133    c                     	 t               }g }d   D ]2  }|d   | k7  r|d   d   r|s|d   d   r|r"|j                  |       4 |S # t        t        f$ r!}t        j	                  |d       Y d }~id }~ww xY w)Nz#Could not list current hosted zonesmsgHostedZonesNameConfigPrivateZone)r   r   r   modulefail_json_awsappend)zone_inprivate_zoneresultsezonesr53zones         r   
find_zonesr'   1  s    K- E=) "6?g%Hm,!-0LL!" L ;' KQ$IJJKs   
A
 
A:A55A:hosted_zone_idreturnc                     	 t         j                  |       S # t        t        f$ r$}t        j                  |d|         Y d }~y d }~ww xY w)NHostedZoneIdz/Could not get dnssec details about hosted zone r   )r   
get_dnssecr   r   r   r   r(   r$   s     r   r-   r-   C  sS    h  n ==;' hQ&UVdUe$fggh    AAAzone_idc                     	 t         j                  |        y # t        t        f$ r$}t        j                  |d|         Y d }~y d }~ww xY wNr+   zCould not enable DNSSEC for r   )r   enable_hosted_zone_dnssecr   r   r   r   r0   r$   s     r   r3   r3   J  sN    N((g(>;' NQ&B7)$LMMN    AAAc                     	 t         j                  |        y # t        t        f$ r$}t        j                  |d|         Y d }~y d }~ww xY wr2   )r   disable_hosted_zone_dnssecr   r   r   r   r4   s     r   r7   r7   Q  sN    N))w)?;' NQ&B7)$LMMNr5   c                     	 t         j                  |       S # t        t        f$ r$}t        j                  |d|         Y d }~y d }~ww xY w)NIdz(Could not get details about hosted zone r   )r   get_hosted_zoner   r   r   r   r.   s     r   r;   r;   X  sR    a%%%88;' aQ&N~N^$_``ar/   c                 >   d}t         j                  j                  d      }t        |       }|d   d   }|rD|dk(  rt         j                  st        |        d}|S |dk(  rt         j                  d|  d	       |S |d
k(  rt         j                  st        |        d}|S )NFdnssecStatusServeSignatureNOT_SIGNINGTDELETINGzGDNSSEC signing is in the process of being removed for the hosted zone: z.Could not enable it.SIGNING)r   paramsgetr-   
check_moder3   warnr7   )r0   changedr=   responsednssec_statuss        r   ensure_dnssecrJ   _  s    G]]x(F'"HX&'78M
 M)$$)'2G  N j(KKYZaYb c' ' N I%$$*73G Nr   c           	         t         j                  j                  d      j                         }t         j                  j                  d      }t         j                  j                  d      }t         j                  j                  d      xs |r|r||dgnd }t         j                  j                  d      }t         j                  j                  d      }t         j                  j                  d      }t         j                  j                  d	      }|j	                  d
      s|d
z  }t        |      }	|	|xr |d   d   |xr |d   d   ||||d d}
|	rt        | |
      \  }}nt        | |
      \  }}|j                  d      }|rg|	s+|t        |      z  }t        t        |            |d<   |d   d= ||t        t         t        d|||      z  }t        t         t        d|      |d<   ||fS ||d<   ||fS )Nzonevpc_id
vpc_regionvpcsidregioncommentdelegation_set_idtags
purge_tags.r   rQ   rR   )r"   rM   rN   rO   rS   namerT   r0   r0   r=   response_metadata
hostedzone)r   rC   rD   lowerendswithboolcreate_or_update_privatecreate_or_update_publicrJ   r   r-   r
   r   r	   )matching_zonesr!   rM   rN   rO   rS   rT   rU   rV   r"   recordrG   resultr0   s                 r   createrc     s   mm'--/G]]x(F""<0J==V$sSY^h:*N)OnrDmm	*G))*=>==V$D""<0JC 3:L %(474=0tAwx0.	F 2>6J1.&Ijj#G}W--G  8
78KLF8x !45 {66<$PZ[[G!&&,Hv F? vF?r   c           	         | D ]<  }t        |d         }|d   }|d   }d}t        |t              r(t        |d         dk(  r|d   d   |d   d	   d
   k(  r}d}nzt	        |d   D cg c]  }|d
   	 c}      t	        |D cg c]  }|d   	 c}      k(  r>t	        |d   D cg c]  }|d   	 c}      t	        |D cg c]  }|d   	 c}      k(  rd}|s|d   j                  dd      |d<   d|d   v rB|d   d   |d   k7  r4t        j                  s	 t        j                  |d   |d          d|fc S d|d<   d|fc S  t        j                  s	 t        j                  |d   |d   |d   nddd|d   d	   d   |d   d	   d
   d|d    dt        j                                }d   }
|
d   j                  dd      }||d<   t        |d         dkD  r.|d   dd  D ]#  }	 t        j                  ||d   |d
   d      }% d}||fS c c}w c c}w c c}w c c}w # t        t        f$ r(}	t        j                  |	d|d           Y d }	~	%d }	~	ww xY w# t        t        f$ r!}	t        j                  |	d       Y d }	~	d }	~	ww xY w# t        t        f$ r!}	t        j                  |	d        Y d }	~	d }	~	ww xY w)!Nr:   
HostedZoneVPCsFrO      VPCVPCIdr   rQ   TrR   	VPCRegion/hostedzone/ r0   Commentr   rS   r:   rm   )Could not update comment for hosted zone r   zThere is already a private hosted zone in the same region with the same VPC(s) you chose. Unable to create a new private hosted zone in the same name space.r   rX   rm   r   )rj   ri   -)r   HostedZoneConfigrh   CallerReferenceCould not create hosted zone)r,   rh   z4Could not associate additional VPCs with hosted zone)r;   
isinstancedictlensortedreplacer   rE   r   update_hosted_zone_commentr   r   r   create_hosted_zonetimeassociate_vpc_with_hosted_zone)r`   ra   zrb   zone_detailsvpc_detailsmatchingvpcvr$   hosted_zoner0   rG   s                r   r^   r^     sa    % 4)l+Vnk4(S-@A-E5!'*fVnQ.?.EE F6N;Ss4y;<\gGhWX'
Gh@iint*0.93X9o=A;=>o?   ,T 2 : :>2 NF9L22|H7Mi7X\bcl\m7m((v99\$=OY_`iYj9k V|#e u f}$;%> 	H..F^4:94E4Qvi0WY#'"
 "(!28!<#F^A.t4 $*&>"2!DIIK= A / F \*d#++NB?#yvf~"f~ab) 
h	h#BB%,),X%(Y C F
h GF?q <Gh9= *;7 v,,Q6_`lmq`r_s4t,uuv0 {+ 	H  (F GG	H" &{3 h((0f(gghsa   %H
 H!H
<HH$AI ( J$I3IIJ-J		JK J<<Kc                    d i }}| D ]p  }t        |d         }|d   }|j                  di       }d|d   v r?|d   d   |d   k7  r1t        j                  s	 t        j                  |d   |d          d
}nd} n |t        j                  s~	 t        |d   |d   |d   nddd|d    dt        j                                }|j                  d      |d   |d<   t	        j                  di |}	|	d   }|	j                  di       }d
}t        j                  r|r|d   j                  dd      |d<   |fS |d   j                  dd      |d<   |d   |d<   |j                  dd      j                  dd      |d<   |fS # t        t        f$ r(}t        j                  |d|d    	       Y d }~Cd }~ww xY w# t        t        f$ r!}t        j                  |d	       Y d }~d }~ww xY w)Nr:   re   DelegationSetrm   r   rS   rn   ro   r   TFrX   rl   rp   rq   )r   rr   rs   rT   DelegationSetIdrt   rk   r0   r   z/delegationset/ )r;   rD   r   rE   r   rz   r   r   r   rv   r|   r{   ry   )
r`   ra   r   zone_delegation_set_detailsmatching_zonerL   r$   rG   rC   rb   s
             r   r_   r_     s_   04b-L' }T23L)&*hh&C#X..<3I)3TX^_hXi3i$$r55d9KU[\eUf5g GG   L8>y8I8U6)#4[]',& (.f~&6a		}$E ::12>067J0KF,-22<V<%l3.4jj".M+  ,T 2 : :>2 NF9 F?	 ).66~rJy%f-v&A&E&EdB&O&W&WXikm&n"#F?M &{3 r((2[\him\n[o0p(qqr4 ";/ L$$Q,J$KKLs1   FA=F= F:F55F:=G-G((G-c           	      b   | D ]  }t        |d         }|d   }|d   }t        |t              rN|d   d   |d   d   k(  s>|d   d   |d   d	   k(  sPt        j                  st        |d          d
d|d    fc S t        |D cg c]  }|d   	 c}      t        |D cg c]  }|d   	 c}      k(  st        |D cg c]  }|d   	 c}      t        |D cg c]  }|d	   	 c}      k(  st        j                  st        |d          d
d|d    fc S  yc c}w c c}w c c}w c c}w )Nr:   re   rf   rh   ri   r   rQ   rR   rj   TSuccessfully deleted r   )Fz,The VPCs do not match a private hosted zone.)r;   ru   rv   r   rE   delete_hosted_zonerx   )r`   rO   r~   rb   r   r   r   r   s           r   delete_privater   -  sR    L 4)l+Vnk4(5!'*d1gdm;Q@QU`afUghsUt@t((&qw/4\&5I4JKKK D1Ss4y12fR]=^Qaj=^6__dj*./3X/e=A;=>e? ((&qw/4\&5I4JKKK!L$ A 2=^/=s   D
!D" D'
D,c                     t        |       dkD  rd}d}||fS t        j                  st        | d   d          d}d| d   d    }||fS )Nrg   FzTThere are multiple zones that match. Use hosted_zone_id to specify the correct zone.r   r:   Tr   )rw   r   rE   r   )r`   rG   r   s      r   delete_publicr   C  sh    
>Qd C<	   ~a067%nQ&7&=%>?C<r   c                 R   | dk(  rGg }|D ]5  }|j                  |d          t        j                  r(t        |d          7 d}d| }||fS | |D cg c]  }|d   j	                  dd       c}v r&t        j                  st        |        d}d|  }||fS d}d	|  d
}||fS c c}w )Nallr:   TzSuccessfully deleted zones: rk   rl   zSuccessfully deleted zone: Fz7There is no zone to delete that matches hosted_zone_id rW   )r    r   rE   r   ry   )r(   r`   deletedr~   rG   r   zos          r   delete_hosted_idr   O  s     	,ANN1T7#$$"1T7+	, ,WI6 C< 
>ZRBtH,,^R@Z	Z  ~.+N+;< C< GGWWXYC< [s   B$c                     	 t         j                  |        y # t        d      $ r$}t        j	                  |d|         Y d }~y d }~wt
        t        f$ r$}t        j	                  |d|         Y d }~y d }~ww xY w)Nr9   HostedZoneNotEmptyz!Could not get delete hosted zone r   zCould not delete hosted zone )r   r   r   r   r   r   r   r.   s     r   r   r   c  s~    V!!^!434 ZQ&GGW$XYY;' VQ&CNCS$TUUVs     A<AA<A77A<c                 n   t         j                  j                  d      j                         }t         j                  j                  d      }t         j                  j                  d      }t         j                  j                  d      xs |r|r||dgnd }t         j                  j                  d      }|j	                  d      s|dz  }t        |      }|| D cg c]  }|d   	 c}v r<|rt        ||       \  }}	||	fS |rt        | |      \  }}	||	fS t        |       \  }}	||	fS d	}d
}	||	fS c c}w )NrL   rM   rN   rO   rP   r(   rW   r   FzNo zone to delete.)	r   rC   rD   r[   r\   r]   r   r   r   )
r`   r!   rM   rN   rO   r(   r"   r~   rG   rb   s
             r   deleter   l  s4   mm'--/G]]x(F""<0J==V$sSY^h:*N)OnrD]]&&'78NC 3:Ln51V955.~~NOGV F? "0"F F? #0"?
 F? %F? 6s   D2c                     t        t        d      t        dddg      t        d       t        d       t        dd dt        t        d      t        d      	      
      t        d      t               t               t        ddg      t        dd      t        dd            } ddgddgddgddgddgg}t        | |d      at        j                  j	                  d      j                         }t        j                  j	                  d      j                         }t        j                  j	                  d      }t        j                  j	                  d      }t        j                  j	                  d      }|j                  d      s|dz  }t        |xs |xr |      }t        j                  dt        j                               at        ||      }|dk(  rt        |      \  }	}
n|dk(  rt        |      \  }	}
t        
t               rt        j                  d	|
d|
 y t        j                  	|
       y )NT)requiredpresentabsent)defaultchoices)r   listrv   rP   )typer   elementsoptionsrl   resource_tags)r   aliasesr]   )r   r   F)rL   staterM   rN   rO   rS   r(   rT   rU   rV   r=   rT   rM   rN   rO   )argument_specmutually_exclusivesupports_check_moderL   r   rW   route53)retry_decorator)r`   )rG   rb   r   )rv   r   r   rC   rD   r[   r\   r]   r   r   jittered_backoffr'   rc   r   ru   	exit_json)r   r   r!   r   rM   rN   rO   r"   r%   rG   rb   s              r   mainr     s    4 9y(.CDD!%VZH[dhrvdw@x
 R v&v'89VT2/M" 
h'	l+	f%		 #- F mm'--/GMMg&,,.E]]x(F""<0J==V$DC 37!6J8L]]9h6O6O6Q]RFw-E	 6	(	 6&$BB6B8r   __main__)*DOCUMENTATIONEXAMPLESRETURNr|   typingr   r   0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   ;ansible_collections.amazon.aws.plugins.module_utils.route53r	   r
   botocore.exceptionsr   r   ImportErrorr   r   r'   strr-   r3   r7   r;   r]   rJ   rc   r^   r_   r   r   r   r   r   r   __name__r   r   r   <module>r      sD  JX0dQ
f    U \ X P P S	1/
 4 4
$hs htCH~ hNs Nt NN N NaC aDcN a3 4 D0fCL0fA,	(V689v zF w  		s   B= =CC