
    Vh>3                        d dl mZ 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  G d de      Zd Zedk(  r e        yy)    )annotationsa  
---
module: rdns

short_description: Create and manage reverse DNS entries on the Hetzner Cloud.


description:
    - Create, update and delete reverse DNS entries on the Hetzner Cloud.

author:
    - Lukas Kaemmerling (@lkaemmerling)

options:
    server:
        description:
            - Name or ID of the Hetzner Cloud server you want to add the reverse DNS entry to.
        type: str
    floating_ip:
        description:
            - Name or ID of the Hetzner Cloud Floating IP you want to add the reverse DNS entry to.
        type: str
    primary_ip:
        description:
            - Name or ID of the Hetzner Cloud Primary IP you want to add the reverse DNS entry to.
        type: str
    load_balancer:
        description:
            - Name or ID of the Hetzner Cloud Load Balancer you want to add the reverse DNS entry to.
        type: str
    ip_address:
        description:
            - The IP address that should point to I(dns_ptr).
        type: str
        required: true
    dns_ptr:
        description:
            - The DNS address the I(ip_address) should resolve to.
            - Omit the param to reset the reverse DNS entry to the default value.
        type: str
    state:
        description:
            - State of the reverse DNS entry.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
a  
- name: Create a reverse DNS entry for a server
  hetzner.hcloud.rdns:
    server: my-server
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Create a reverse DNS entry for a Floating IP
  hetzner.hcloud.rdns:
    floating_ip: my-floating-ip
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Create a reverse DNS entry for a Primary IP
  hetzner.hcloud.rdns:
    primary_ip: my-primary-ip
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Create a reverse DNS entry for a Load Balancer
  hetzner.hcloud.rdns:
    load_balancer: my-load-balancer
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Ensure the reverse DNS entry is absent (remove if needed)
  hetzner.hcloud.rdns:
    server: my-server
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: absent
a  
hcloud_rdns:
    description: The reverse DNS entry
    returned: always
    type: complex
    contains:
        server:
            description: Name of the server
            type: str
            returned: always
            sample: my-server
        floating_ip:
            description: Name of the Floating IP
            type: str
            returned: always
            sample: my-floating-ip
        primary_ip:
            description: Name of the Primary IP
            type: str
            returned: always
            sample: my-primary-ip
        load_balancer:
            description: Name of the Load Balancer
            type: str
            returned: always
            sample: my-load-balancer
        ip_address:
            description: The IP address that point to the DNS ptr
            type: str
            returned: always
            sample: 123.123.123.123
        dns_ptr:
            description: The DNS that resolves to the IP
            type: str
            returned: always
            sample: example.com
N)Any)AnsibleModule   )AnsibleHCloud)HCloudException)BoundFloatingIP)BoundLoadBalancer)BoundPrimaryIP)BoundServerc                  t     e Zd ZU dZdZded<   dZded<   d Zd Zd Z	d	 Z
d
 Zd Zd Ze fd       Z xZS )AnsibleHCloudReverseDNShcloud_rdnsNzIBoundServer | BoundFloatingIP | BoundLoadBalancer | BoundPrimaryIP | Nonehcloud_resourcezdict[str, Any] | Nonec                F   d d d | j                   d   | j                   d   d}| j                  j                  j                  d      r| j                  j
                  |d<   |S | j                  j                  j                  d      r| j                  j
                  |d<   |S | j                  j                  j                  d      r| j                  j
                  |d<   |S | j                  j                  j                  d      r| j                  j
                  |d<   |S )N
ip_addressdns_ptr)serverfloating_ipload_balancerr   r   r   r   r   
primary_ip)r   moduleparamsgetr   name)selfresults     g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/rdns.py_prepare_resultz'AnsibleHCloudReverseDNS._prepare_result   s   !**<8''	2
 ;;!!(+#3388F8  [[##M2$($8$8$=$=F=!
 	 [[##O4&*&:&:&?&?F?#  [[##L1#'#7#7#<#<F<     c                T   	 | j                   j                  j                  d      r;| j                  d| j                   j                  j                  d            | _        y | j                   j                  j                  d      r;| j                  d| j                   j                  j                  d            | _        y | j                   j                  j                  d      r;| j                  d| j                   j                  j                  d            | _        y | j                   j                  j                  d      r;| j                  d| j                   j                  j                  d            | _        y y # t
        $ r}| j                  |       Y d }~y d }~ww xY w)	Nr   serversr   floating_ipsr   primary_ipsr   load_balancers)r   r   r   _client_get_by_name_or_idr   r   fail_json_hcloudr   	exceptions     r   _get_resourcez%AnsibleHCloudReverseDNS._get_resource   sM   	-{{!!%%h/'+'E'EKK&&**84($ ##''6'+'E'E"KK&&**=9($ ##''5'+'E'E!KK&&**<8($ ##''8'+'E'E$KK&&**?;($ 9
  	-!!),,	-s-   AF "AF AF "AF 	F'F""F'c                	   | j                   j                  j                  d      }	 t        j                  |      }j                  dk(  r| j                   j                  j                  d      r| j                  j                  j                  j                  |k(  r[| j                  j                  j                  j                  | j                  j                  j                  j                  d| _        y | j                   j                  d       y | j                   j                  j                  d      ro| j                  j                  |k(  r9| j                  j                  | j                  j                  d	   d
   d| _        y | j                   j                  d       y | j                   j                  j                  d      ro| j                  j                  |k(  r9| j                  j                  | j                  j                  d	   d
   d| _        y | j                   j                  d       y | j                   j                  j                  d      r| j                  j                  j                  j                  |k(  r[| j                  j                  j                  j                  | j                  j                  j                  j                  d| _        y | j                   j                  d       y y |j                  dk(  r| j                   j                  j                  d      rI| j                  j                  j                  j                  D ]  }|d   |k(  s|d   |d
   d| _         y | j                   j                  j                  d      r5| j                  j                  D ]  }|d   |k(  s|d   |d
   d| _         y | j                   j                  j                  d      r5| j                  j                  D ]  }|d   |k(  s|d   |d
   d| _         y | j                   j                  j                  d      rI| j                  j                  j                  j                  D ]  }|d   |k(  s|d   |d
   d| _         y y y # t
        $ r# | j                   j                  d|        Y w xY w)Nr   z#The given IP address is not valid: )msg   r   )r   r   z1The selected server does not have this IP addressr   r   r   z6The selected Floating IP does not have this IP addressr   z5The selected Primary IP does not have this IP addressr   z8The selected Load Balancer does not have this IP address   ip)r   r   r   	ipaddressr   
ValueError	fail_jsonversionr   
public_netipv4r/   r   r   ipv6)r   r   ip_address_objipv6_address_dns_ptrs       r   	_get_rdnsz!AnsibleHCloudReverseDNS._get_rdns   s(   [[''++L9
	Z&11*=N !!Q&{{!!%%h/''2277::jH&*&:&:&E&E&J&J&M&M#'#7#7#B#B#G#G#O#O(D$
 KK)).a)b##''6''**j8&*&:&:&=&=#'#7#7#?#?#B9#M(D$
 KK)).f)g##''5''**j8&*&:&:&=&=#'#7#7#?#?#B9#M(D$
 KK)).e)f##''8''2277::jH&*&:&:&E&E&J&J&M&M#'#7#7#B#B#G#G#O#O(D$
 KK)).h)i 9 ##q({{!!%%h/,0,@,@,K,K,P,P,X,X (+D1Z?*>t*D';I'F,( ##''6,0,@,@,H,H (+D1Z?*>t*D';I'F,( ##''5,0,@,@,H,H (+D1Z?*>t*D';I'F,( ##''8,0,@,@,K,K,P,P,X,X (+D1Z?*>t*D';I'F,( 9- )K  	ZKK!!(KJ<&X!Y	Zs   R+ +(SSc                   | j                   j                  dg       | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  s-	  | j
                  j                  di |}|j                          | j                          | j                          | j                          y # t        $ r}| j                  |       Y d }~Pd }~ww xY w)Nr   )required_paramsr   r/   r    )r   fail_on_missing_paramsr   r   
check_moder   change_dns_ptrwait_until_finishedr   r'   _mark_as_changedr*   r9   )r   r   actionr)   s       r   _create_rdnsz$AnsibleHCloudReverseDNS._create_rdns  s    **I;*G++$$((6{{))--i8

 {{%%1<--<<FvF**, 		 # 1%%i001s    ,C 	D&C<<Dc                   | j                   j                  j                  d      }|| j                  d   k7  r| j                   j                  j                  d      |d}| j                   j                  s-	  | j
                  j                  di |}|j                          | j                          | j                          | j                          y y # t        $ r}| j                  |       Y d }~Qd }~ww xY w)Nr   r   r<   r=   )r   r   r   r   r?   r   r@   rA   r   r'   rB   r*   r9   )r   r   r   rC   r)   s        r   _update_rdnsz$AnsibleHCloudReverseDNS._update_rdns  s    ++$$((3d&&y11kk((,,\:"F
 ;;))5@T11@@J6JF..0 !!# NN 2 ' 5)))445s   7,C 	C9C44C9c                    | j                          | j                          | j                  | j                          y | j	                          y )N)r*   r9   r   rD   rF   )r   s    r   present_rdnsz$AnsibleHCloudReverseDNS.present_rdns1  s<    #r    c                Z   | j                          | j                          | j                  Q| j                  j                  s+	 | j
                  j                  | j                  d   d        | j                          d | _        y # t        $ r}| j                  |       Y d }~7d }~ww xY w)Nr   r<   )
r*   r9   r   r   r?   r   r@   r   r'   rB   r(   s     r   delete_rdnsz#AnsibleHCloudReverseDNS.delete_rdns9  s    ';;))5((774;K;KL;Ycg7h !!# ' 5)))445s   *B 	B*B%%B*c                    t        t        dddiddiddiddidddddiddgdddt        |          g d	gg d	gd
      S )NtypestrT)rL   requiredabsentpresent)choicesdefault)r   r   r   r   r   r   state)r   r   r   r   )argument_specrequired_one_ofmutually_exclusivesupports_check_moder=   )r   dictsuperbase_module_arguments)cls	__class__s    r   define_modulez%AnsibleHCloudReverseDNS.define_moduleE  s|     #UO%uo"E?$)t< ()4( '/1 VV XY $!
 	
r    )__name__
__module____qualname__	representr   __annotations__r   r   r*   r9   rD   rF   rH   rJ   classmethodr]   __classcell__)r\   s   @r   r   r      sW    IaeO^e)-K&-&-2FP"$ 
  
 
r    r   c                     t         j                         } t        |       }| j                  d   }|dk(  r|j                          n|dk(  r|j	                           | j
                  di |j                          y )NrS   rO   rP   r=   )r   r]   r   rJ   rH   	exit_json
get_result)r   hcloudrS   s      r   mainri   [  sl    $224F$V,FMM'"E	)	F+v((*+r    __main__)
__future__r   DOCUMENTATIONEXAMPLESRETURNr0   typingr   ansible.module_utils.basicr   module_utils.hcloudr   module_utils.vendor.hcloudr   'module_utils.vendor.hcloud.floating_ipsr	   )module_utils.vendor.hcloud.load_balancersr
   &module_utils.vendor.hcloud.primary_ipsr   "module_utils.vendor.hcloud.serversr   r   ri   r^   r=   r    r   <module>rw      sg    #1f#J$
L   4 / 8 E I C <D
m D
N
, zF r    