
    Vhs                        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)    )annotationsak  
---
module: subnetwork

short_description: Manage cloud subnetworks on the Hetzner Cloud.


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

author:
    - Lukas Kaemmerling (@lkaemmerling)

options:
    network:
        description:
            - The name or ID of the Hetzner Cloud Networks.
        type: str
        required: true
    ip_range:
        description:
            - IP range of the subnetwork.
        type: str
        required: true
    type:
        description:
            - Type of subnetwork.
        type: str
        choices: [ server, cloud, vswitch ]
        required: true
    network_zone:
        description:
            - Name of network zone.
        type: str
        required: true
    vswitch_id:
        description:
            - ID of the vSwitch you want to couple with your Network.
            - Required if type == vswitch
        type: int
    state:
        description:
            - State of the subnetwork.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
aK  
- name: Create a basic subnetwork
  hetzner.hcloud.subnetwork:
    network: my-network
    ip_range: 10.0.0.0/16
    network_zone: eu-central
    type: cloud
    state: present

- name: Create a basic subnetwork
  hetzner.hcloud.subnetwork:
    network: my-vswitch-network
    ip_range: 10.0.0.0/24
    network_zone: eu-central
    type: vswitch
    vswitch_id: 123
    state: present

- name: Ensure the subnetwork is absent (remove if needed)
  hetzner.hcloud.subnetwork:
    network: my-network
    ip_range: 10.0.0.0/8
    network_zone: eu-central
    type: cloud
    state: absent
a  
hcloud_subnetwork:
    description: One Subnet of a Network
    returned: always
    type: complex
    contains:
        network:
            description: Name of the Network
            type: str
            returned: always
            sample: my-network
        ip_range:
            description: IP range of the Network
            type: str
            returned: always
            sample: 10.0.0.0/8
        type:
            description: Type of subnetwork
            type: str
            returned: always
            sample: server
        network_zone:
            description: Name of network zone
            type: str
            returned: always
            sample: eu-central
        vswitch_id:
            description: ID of the vswitch, null if not type vswitch
            type: int
            returned: always
            sample: 123
        gateway:
            description: Gateway of the subnetwork
            type: str
            returned: always
            sample: 10.0.0.1
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundNetworkNetworkSubnetc                  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 )AnsibleHCloudSubnetworkhcloud_subnetworkNzBoundNetwork | Nonehcloud_networkzNetworkSubnet | Nonec                   | j                   j                  | j                  j                  | j                  j                  | j                  j
                  | j                  j                  | j                  j                  dS )N)networkip_rangetypenetwork_zonegateway
vswitch_id)r   namer   r   r   r   r   r   selfs    m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/subnetwork.py_prepare_resultz'AnsibleHCloudSubnetwork._prepare_result   sf    **//..77**// 22??--55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$AnsibleHCloudSubnetwork._get_network   s`    	-"&"@"@""&&y1#D &*D" 	-!!),,	-s   AA 	A(A##A(c                    | j                   j                  j                  d      }| j                  j                  D ]  }|j
                  |k(  s|| _         y )Nr   )r   r   r    r   subnetsr   r   )r   subnet_ip_range
subnetworks      r   _get_subnetworkz'AnsibleHCloudSubnetwork._get_subnetwork   sM    ++,,00<--55 	4J""o5)3&	4r   c                   | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  j                  d      t        j                  k(  rE| j                   j                  dg       | j                   j                  j                  d      |d<   | j                   j                  s6	 | j                  j                  t        di |      }|j                          | j                          | j                          | j                          y # t        $ r}| j                  |       Y d }~Pd }~ww xY w)	Nr   r   r   )r   r   r   r   )required_params)subnet )r   r   r    r	   TYPE_VSWITCHfail_on_missing_params
check_moder   
add_subnetwait_until_finishedr   r!   _mark_as_changedr#   r(   )r   r   actionr"   s       r   _create_subnetworkz*AnsibleHCloudSubnetwork._create_subnetwork   s4   **..z:KK&&**62 KK..22>B

 ;;!!&)]-G-GGKK..~.N#';;#5#5#9#9,#GF< {{%%1,,77}?Vv?V7W**, 	 # 1%%i001s   5E( (	F1FFc                ~    | j                          | j                          | j                  | j                          y y N)r#   r(   r   r4   r   s    r   present_subnetworkz*AnsibleHCloudSubnetwork.present_subnetwork   s7    !!)##% *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r6   )r#   r(   r   r   r   r/   delete_subnetr1   r   r!   r2   )r   r3   r"   s      r   delete_subnetworkz)AnsibleHCloudSubnetwork.delete_subnetwork   s    !!-$2E2E2Q;;))5!00>>t?U?UVF..0 !!#!% ' 5)))445s   5B 	C&B<<Cc                    t        t        dddddddddg dddddddidd	gd	d
dt        |          d      S )NstrT)r   required)servercloudvswitch)r   r=   choicesr   intabsentpresent)rA   default)r   r   r   r   r   state)argument_specsupports_check_moder,   )r   dictsuperbase_module_arguments)cls	__class__s    r   define_modulez%AnsibleHCloudSubnetwork.define_module   sn     !&D9&+>#B`a"'T:"E? ()4( '/1 !%
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r   r#   r(   r4   r7   r:   classmethodrN   __classcell__)rM   s   @r   r   r      sQ    #I*.N'..2+2
-4*&& 
 
r   r   c                     t         j                         } t        |       }| j                  d   }|dk(  r|j                          n|dk(  r|j	                           | j
                  di |j                          y )NrF   rC   rD   r,   )r   rN   r   r:   r7   	exit_json
get_result)r   hcloudrF   s      r   mainrZ      sl    $224F$V,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   rZ   rO   r,   r   r   <module>rd      sW    #1f6$
L 5 / 8 MX
m X
v
, zF r   