
    Vh                        d dl mZ 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mZ  G d
 de      Zd Zedk(  r e        yy)    )annotationsa.  
---
module: route

short_description: Create and delete cloud routes on the Hetzner Cloud.


description:
    - Create, update and delete cloud routes on the Hetzner Cloud.

author:
    - Lukas Kaemmerling (@lkaemmerling)

options:
    network:
        description:
            - Name or ID of the Hetzner Cloud Network.
        type: str
        required: true
    destination:
        description:
            - Destination network or host of this route.
        type: str
        required: true
    gateway:
        description:
            - Gateway for the route.
        type: str
        required: true
    state:
        description:
            - State of the route.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
a1  
- name: Create a basic route
  hetzner.hcloud.route:
    network: my-network
    destination: 10.100.1.0/24
    gateway: 10.0.1.1
    state: present

- name: Ensure the route is absent
  hetzner.hcloud.route:
    network: my-network
    destination: 10.100.1.0/24
    gateway: 10.0.1.1
    state: absent
a5  
hcloud_route:
    description: One Route of a Network
    returned: always
    type: complex
    contains:
        network:
            description: Name of the Network
            type: str
            returned: always
            sample: my-network
        destination:
            description: Destination network or host of this route
            type: str
            returned: always
            sample: 10.0.0.0/8
        gateway:
            description: Gateway of the route
            type: str
            returned: always
            sample: 10.0.0.1
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundNetworkNetworkRoutec                  n     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e fd       Z xZS )AnsibleHCloudRoutehcloud_routeNzBoundNetwork | Nonehcloud_networkzNetworkRoute | Nonec                    | j                   j                  | j                  j                  | j                  j                  dS )N)networkdestinationgateway)r   namer   r   r   selfs    h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/route.py_prepare_resultz"AnsibleHCloudRoute._prepare_resulte   s9    **//,,88((00
 	
    c                    	 | j                  d| j                  j                  j                  d            | _        d | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nnetworksr   )_client_get_by_name_or_idmoduleparamsgetr   r   r   fail_json_hcloud)r   	exceptions     r   _get_networkzAnsibleHCloudRoute._get_networkl   s`    	-"&"@"@""&&y1#D !%D 	-!!),,	-s   AA 	A(A##A(c                   | j                   j                  j                  d      }| j                   j                  j                  d      }| j                  j                  D ])  }|j
                  |k(  s|j                  |k(  s#|| _        + y )Nr   r   )r   r   r   r   routesr   r   r   )r   r   r   routes       r   
_get_routezAnsibleHCloudRoute._get_routev   sr    kk((,,];++$$((3((// 	*E  K/EMMW4L$)!	*r   c                   t        | j                  j                  j                  d      | j                  j                  j                  d            }| j                  j                  s-	 | j
                  j                  |      }|j                          | j                          | j                          | j                          y # t        $ r}| j                  |       Y d }~Pd }~ww xY w)Nr   r   )r   r   )r#   )r	   r   r   r   
check_moder   	add_routewait_until_finishedr   r   _mark_as_changedr    r$   )r   r#   actionr   s       r   _create_routez AnsibleHCloudRoute._create_route}   s    **..}=t{{GYGYG]G]^gGh
 {{%%1,,66U6C**, 	 # 1%%i001s   +,C 	C,C''C,c                ~    | j                          | j                          | j                  | j                          y y N)r    r$   r   r+   r   s    r   present_routez AnsibleHCloudRoute.present_route   s5    $  %r   c                   | j                          | j                          | j                  h| j                  \| j                  j
                  s6	 | j                  j                  | j                        }|j                          | j                          d | _        y # t        $ r}| j                  |       Y d }~7d }~ww xY wr-   )r    r$   r   r   r   r&   delete_router(   r   r   r)   )r   r*   r   s      r   r0   zAnsibleHCloudRoute.delete_route   s    (T-@-@-L;;))5!00==d>O>OPF..0 !!#  ' 5)))445s   5B 	C&B<<Cc                n    t        t        d	dddddddddddgdddt        |          d      S )
NstrT)typerequiredabsentpresent)choicesdefault)r   r   r   state)argument_specsupports_check_mode )r   dictsuperbase_module_arguments)cls	__class__s    r   define_modulez AnsibleHCloudRoute.define_module   s\     	!&D9!&D9%*= ()4(		 '/1	 !%
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r   r    r$   r+   r.   r0   classmethodrB   __classcell__)rA   s   @r   r   r   _   sP    I*.N'.(,L%,
-* !! 
 
r   r   c                     t         j                         } t        |       }| j                  d   }|dk(  r|j                          n|dk(  r|j	                           | j
                  di |j                          y )Nr9   r5   r6   r<   )r   rB   r   r0   r.   	exit_json
get_result)r   hcloudr9   s      r   mainrN      sl    --/F'FMM'"E	)	F+v((*+r   __main__N)
__future__r   DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.hcloudr   module_utils.vendor.hcloudr   #module_utils.vendor.hcloud.networksr   r	   r   rN   rC   r<   r   r   <module>rX      sV    #&P 
. 5 / 8 LO
 O
d
, zF r   