
    VhO#                        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  G d
 de      Zd Zedk(  r e        yy)    )annotationsa  
---
module: network

short_description: Create and manage cloud Networks on the Hetzner Cloud.


description:
    - Create, update and manage cloud Networks on the Hetzner Cloud.
    - You need at least hcloud-python 1.3.0.

author:
    - Lukas Kaemmerling (@lkaemmerling)

options:
    id:
        description:
            - The ID of the Hetzner Cloud Networks to manage.
            - Only required if no Network I(name) is given.
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud Network to manage.
            - Only required if no Network I(id) is given or a Network does not exist.
        type: str
    ip_range:
        description:
            - IP range of the Network.
            - Required if Network does not exist.
        type: str
    expose_routes_to_vswitch:
        description:
            - Indicates if the routes from this network should be exposed to the vSwitch connection.
            - The exposing only takes effect if a vSwitch connection is active.
        type: bool
    labels:
        description:
            - User-defined labels (key-value pairs).
        type: dict
    delete_protection:
        description:
            - Protect the Network for deletion.
        type: bool
    state:
        description:
            - State of the Network.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
z
- name: Create a basic network
  hetzner.hcloud.network:
    name: my-network
    ip_range: 10.0.0.0/8
    state: present

- name: Ensure the Network is absent (remove if needed)
  hetzner.hcloud.network:
    name: my-network
    state: absent
a  
hcloud_network:
    description: The Network
    returned: always
    type: complex
    contains:
        id:
            description: ID of the Network
            type: int
            returned: always
            sample: 12345
        name:
            description: Name of the Network
            type: str
            returned: always
            sample: my-volume
        ip_range:
            description: IP range of the Network
            type: str
            returned: always
            sample: 10.0.0.0/8
        expose_routes_to_vswitch:
            description: Indicates if the routes from this network should be exposed to the vSwitch connection.
            type: bool
            returned: always
            sample: false
        delete_protection:
            description: True if Network is protected for deletion
            type: bool
            returned: always
            sample: false
            version_added: "0.1.0"
        labels:
            description: User-defined labels (key-value pairs)
            type: dict
            returned: always
            sample:
                key: value
                mylabel: 123
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundNetworkc                  `     e Zd ZU dZdZded<   d Zd Zd Zd Z	d Z
d	 Ze fd
       Z xZS )AnsibleHCloudNetworkhcloud_networkNzBoundNetwork | Nonec                   t        | j                  j                        | j                  j                  | j                  j                  | j                  j
                  | j                  j                  d   | j                  j                  dS )Ndelete)idnameip_rangeexpose_routes_to_vswitchdelete_protectionlabels)strr   r   r   r   r   
protectionr   selfs    j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/network.py_prepare_resultz$AnsibleHCloudNetwork._prepare_result   sp    d)),,-'',,++44(,(;(;(T(T!%!4!4!?!?!I))00
 	
    c                   	 | j                   j                  j                  d      N| j                  j                  j                  | j                   j                  j                  d            | _        y | j                  j                  j                  | j                   j                  j                  d            | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nr   r   )
moduleparamsgetclientnetworks	get_by_idr   get_by_namer   fail_json_hcloudr   	exceptions     r   _get_networkz!AnsibleHCloudNetwork._get_network   s    	-{{!!%%d+7&*kk&:&:&D&DT[[EWEWE[E[\`Ea&b#&*kk&:&:&F&Ft{{GYGYG]G]^dGe&f# 	-!!),,	-s   A2C 5AC 	C'C""C'c                D   | j                   j                  ddg       | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  j                  d      }|||d<   	 | j                   j                  s | j
                  j                  j                  d	i | | j                   j                  j                  d      }|<| j                          | j                  j                  |      }|j                          | j                          | j                          y # t        $ r}| j                  |       Y d }~@d }~ww xY w)
Nr   r   required_paramsr   )r   r   r   r   r   r    )r   fail_on_missing_paramsr   r   
check_moder   r    creater&   r   change_protectionwait_until_finishedr   r#   _mark_as_changed)r   r   r   r   actionr%   s         r   _create_networkz$AnsibleHCloudNetwork._create_network   sZ   **FJ;O*PKK&&**62**..z:kk((,,X6
 $(;;#5#5#9#9:T#U #/1IF-.
	-;;))+$$++5f5$(KK$6$6$:$:;N$O!$0%%'!00BBJ[B\F..0 	  	-!!),,	-s   ;BE; ;	FFFc                   	 | j                   j                  j                  d      }|x| j                  j                  |k7  r_| j                   j                  dg       | j                   j                  s| j                  j                  |       | j                          | j                   j                  j                  d      }|[|| j                  j                  k7  rB| j                   j                  s| j                  j                  |       | j                          | j                   j                  j                  d      }|k|| j                  j                  k7  rR| j                   j                  s,| j                  j                  |      }|j                          | j                          | j                   j                  j                  d	      }|[|| j                  j                  k7  rB| j                   j                  s| j                  j                  |
       | j                          | j                   j                  j                  d      }|n|| j                  j                  d   k7  rR| j                   j                  s,| j                  j                  |      }|j                          | j                          | j%                          y # t         $ r}| j#                  |       Y d }~0d }~ww xY w)Nr   r   r(   )r   r   )r   r   )r   r   )r   r   r   r*   )r   r   r   r   r   r,   r-   updater1   r   r   change_ip_ranger0   r   r   r/   r   r#   r&   )r   r   r   r   r2   r   r   r%   s           r   _update_networkz$AnsibleHCloudNetwork._update_network   sh   %	-;;%%))&1DD$7$7$<$<$D22D62J{{--''..D.9%%'[[''++H5F!f0C0C0J0J&J{{--''..f.=%%'{{))--j9H#D4G4G4P4P(P{{--!00@@(@SF..0%%''+{{'9'9'='=>X'Y$(4,0C0C0\0\\{{--''..H`.a%%' $ 2 2 6 67J K ,1BdFYFYFdFdemFn1n{{--!00BBJ[B\F..0%%' 	  	-!!),,	-s   K
K 	L&K<<Lc                ~    | j                          | j                  | j                          y | j                          y N)r&   r   r3   r7   r   s    r   present_networkz$AnsibleHCloudNetwork.present_network   s2    &  "  "r   c                R   	 | j                          | j                  U| j                  j                  s/| j                  j
                  j                  | j                         | j                          d | _        y # t        $ r"}| j                  |       Y d }~d | _        y d }~ww xY wr9   )
r&   r   r   r-   r   r    r   r1   r   r#   r$   s     r   delete_networkz#AnsibleHCloudNetwork.delete_network   s    	-"".{{--KK((//0C0CD%%' #  	-!!),,"	-s   A1A; ;	B&B!!B&c                    t        t        dddiddiddiddiddiddiddgddd	t        |          d
dggd      S )Ntypeintr   booldictabsentpresent)choicesdefault)r   r   r   r   r   r   stater   r   T)argument_specrequired_one_ofsupports_check_moder+   )r   rA   superbase_module_arguments)cls	__class__s    r   define_modulez"AnsibleHCloudNetwork.define_module   s{     E?e_ %*0&)9'#)6"2 ()4( '/1 #F^, $
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r&   r3   r7   r:   r<   classmethodrN   __classcell__)rM   s   @r   r
   r
   |   sF     I*.N'.
-4'R#	# 
 
r   r
   c                     t         j                         } t        |       }| j                  d   }|dk(  r|j                          n|dk(  r|j	                           | j
                  di |j                          y )NrF   rB   rC   r+   )r
   rN   r   r<   r:   	exit_json
get_result)r   hcloudrF   s      r   mainrZ      sl    !//1F!&)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
   rZ   rO   r+   r   r   <module>rd      sW    #4l'
R 5 / 8 >
= 
D
, zF r   