
    Vh2                        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: load_balancer

short_description: Create and manage cloud Load Balancers on the Hetzner Cloud.

description:
    - Create, update and manage cloud Load Balancers on the Hetzner Cloud.

author:
    - Lukas Kaemmerling (@LKaemmerling)
version_added: 0.1.0
options:
    id:
        description:
            - The ID of the Hetzner Cloud Load Balancer to manage.
            - Only required if no Load Balancer I(name) is given
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud Load Balancer to manage.
            - Only required if no Load Balancer I(id) is given or a Load Balancer does not exist.
        type: str
    load_balancer_type:
        description:
            - The Load Balancer Type of the Hetzner Cloud Load Balancer to manage.
            - Required if Load Balancer does not exist.
        type: str
    algorithm:
        description:
            - Algorithm of the Load Balancer.
        type: str
        default: round_robin
        choices: [round_robin, least_connections]
    location:
        description:
            - Location of Load Balancer.
            - Required if no I(network_zone) is given and Load Balancer does not exist.
        type: str
    network_zone:
        description:
            - Network Zone of Load Balancer.
            - Required of no I(location) is given and Load Balancer does not exist.
        type: str
    labels:
        description:
            - User-defined labels (key-value pairs).
        type: dict
    disable_public_interface:
        description:
            - Disables the public interface.
        type: bool
        default: False
    delete_protection:
        description:
            - Protect the Load Balancer for deletion.
        type: bool
    state:
        description:
            - State of the Load Balancer.
        default: present
        choices: [ absent, present ]
        type: str
extends_documentation_fragment:
- hetzner.hcloud.hcloud
aK  
- name: Create a basic Load Balancer
  hetzner.hcloud.load_balancer:
    name: my-Load Balancer
    load_balancer_type: lb11
    algorithm: round_robin
    location: fsn1
    state: present

- name: Ensure the Load Balancer is absent (remove if needed)
  hetzner.hcloud.load_balancer:
    name: my-Load Balancer
    state: absent
a-  
hcloud_load_balancer:
    description: The Load Balancer instance
    returned: Always
    type: complex
    contains:
        id:
            description: Numeric identifier of the Load Balancer
            returned: always
            type: int
            sample: 1937415
        name:
            description: Name of the Load Balancer
            returned: always
            type: str
            sample: my-Load-Balancer
        load_balancer_type:
            description: Name of the Load Balancer type of the Load Balancer
            returned: always
            type: str
            sample: cx22
        algorithm:
            description: Algorithm of the Load Balancer.
            returned: always
            type: str
            choices: [round_robin, least_connections]
            sample: round_robin
        ipv4_address:
            description: Public IPv4 address of the Load Balancer
            returned: always
            type: str
            sample: 116.203.104.109
        ipv6_address:
            description: Public IPv6 address of the Load Balancer
            returned: always
            type: str
            sample: 2a01:4f8:1c1c:c140::1
        location:
            description: Name of the location of the Load Balancer
            returned: always
            type: str
            sample: fsn1
        labels:
            description: User-defined labels (key-value pairs)
            returned: always
            type: dict
        delete_protection:
            description: True if Load Balancer is protected for deletion
            type: bool
            returned: always
            sample: false
        disable_public_interface:
            description: True if Load Balancer public interface is disabled
            type: bool
            returned: always
            sample: false
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundLoadBalancerLoadBalancerAlgorithmc                  `     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 )AnsibleHCloudLoadBalancerhcloud_load_balancerNzBoundLoadBalancer | Nonec                   t        | j                  j                        | j                  j                  | j                  j                  j
                  j                  | j                  j                  j                  j                  t        | j                  j                        r#| j                  j                  d   j                  nd | j                  j                  j                  | j                  j                  j                  | j                  j                  j                  | j                  j                  | j                  j                  d   | j                  j                  j                    dS )Nr   delete)idnameipv4_addressipv6_addressprivate_ipv4_addressload_balancer_type	algorithmlocationlabelsdelete_protectiondisable_public_interface)strr   r   r   
public_netipv4ipipv6lenprivate_netr   r   typer   r   
protectionenabledselfs    p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/load_balancer.py_prepare_resultz)AnsibleHCloudLoadBalancer._prepare_result   s   d//223--22 55@@EEHH 55@@EEHH?B4C\C\ChCh?i))55a8;;os"&";";"N"N"S"S22<<AA11::??//66!%!:!:!E!Eh!O,0,E,E,P,P,X,X(X
 	
    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load_balancers	get_by_idr   get_by_namer   fail_json_hcloudr%   	exceptions     r&   _get_load_balancerz,AnsibleHCloudLoadBalancer._get_load_balancer   s    	-{{!!%%d+7,0KK,F,F,P,PQUQ\Q\QcQcQgQghlQm,n),0KK,F,F,R,RSWS^S^SeSeSiSijpSq,r) 	-!!),,	-s   A2C 5AC 	C'C""C'c                   | j                   j                  ddg       	 | j                   j                  j                  d      t	        | j                   j                  j                  dd            | j
                  j                  j                  | j                   j                  j                  d            | j                   j                  j                  d      d}| j                   j                  j                  d	      C| j                   j                  j                  d
      | j                   j                  d       n| j                   j                  j                  d	      q| j                   j                  j                  d
      L| j
                  j                  j                  | j                   j                  j                  d	            |d	<   nr| j                   j                  j                  d	      M| j                   j                  j                  d
      (| j                   j                  j                  d
      |d
<   | j                   j                  s | j
                  j                  j                  di |}|j                  j                          | 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   round_robinr!   r   )r   r   r   r   r   network_zonez8one of the following is required: location, network_zonemsgr   r    )r*   fail_on_missing_paramsr+   r,   r	   r-   load_balancer_typesr0   	fail_json	locations
check_moder.   createactionwait_until_finishedr4   r   change_protectionr   r1   _mark_as_changed)r%   r+   respr   rD   r3   s         r&   _create_load_balancerz/AnsibleHCloudLoadBalancer._create_load_balancer   s   **FDX;Y*Z	-**..v628J8J8N8N{\i8jk&*kk&E&E&Q&QKK&&**+?@' ++,,00:F {{!!%%j19dkk>P>P>T>TUc>d>l%%*d%e##''
3?DKKDVDVDZDZ[iDjDr%)[[%:%:%F%Ft{{GYGYG]G]^hGi%jz"##''
3;@R@R@V@VWe@f@r)-););)?)?)O~&;;))8t{{1188B6B//1$(KK$6$6$:$:;N$O!$0++-!66HHPaHbF..0 	!  	-!!),,	-s   K*L+ +	M4M

Mc                4   	 | 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                          | j                   j                  j                  d      }||| j                  j                  j                   k7  r| j                   j
                  sY|du r+| j                  j                         }|j                          n*| j                  j                         }|j                          | j                          | j                   j                  j                  d      }|| j                  j                   j"                  |k7  r| j$                  j&                  j)                  |      }|s| j                   j+                  d	
       | j                   j
                  s,| j                  j-                  |      }|j                          | j                          | j                   j                  j                  d      }|i| j                  j.                  j0                  |k7  rF| j                  j3                  t5        |            }|j                          | j                          | j                          y # t6        $ r}| j9                  |       Y d }~y d }~ww xY w)Nr   )r   r   r   r<   r   Tr   zunknown load balancer typer:   )r   r   r8   )r   )r*   r+   r,   r   r   rB   updaterG   r"   rF   rE   r4   r   r#   r   enable_public_interfacer   r   r-   r?   r0   r@   change_typer   r!   change_algorithmr	   r   r1   )	r%   r   r   rD   r   r   new_load_balancer_typer   r3   s	            r&   _update_load_balancerz/AnsibleHCloudLoadBalancer._update_load_balancer   s   4	-[[''++H5F!f0I0I0P0P&P{{----44F4C%%' $ 2 2 6 67J K ,1BdF_F_FjFjksFt1t{{--!66HHPaHbF..0%%'##%'+{{'9'9'='=>X'Y$'38P--88@@@9 {{--/47!%!:!:!S!S!U224!%!:!:!R!R!T224%%'!%!3!3!7!78L!M".--@@EEI[[)-)H)H)T)TUg)h&-KK)).J)K{{--!66BB+A C F ..0%%'**..{;I$)B)B)L)L)Q)QU^)^22CCNcirNsCt**,%%'##% 	-!!),,	-s   M0M3 3	N<NNc                ~    | j                          | j                  | j                          y | j                          y N)r4   r   rI   rP   r$   s    r&   present_load_balancerz/AnsibleHCloudLoadBalancer.present_load_balancer  s2    !$$,&&(&&(r(   c                D   	 | j                          | j                  U| j                  j                  s/| j                  j
                  j                  | j                         | j                          d | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY wrR   )
r4   r   r*   rB   r-   r.   r   rG   r   r1   r2   s     r&   delete_load_balancerz.AnsibleHCloudLoadBalancer.delete_load_balancer  s    	-##%((4{{--KK..55d6O6OP%%'(,D% 	-!!),,	-s   A8A; ;	BBBc                    t        t        dddiddiddiddgddddiddiddiddidd	d
ddgddd
t        |          ddggddggd      S )Nr!   intr   r7   least_connections)choicesdefaultdictboolF)r!   rZ   absentpresent)
r   r   r   r   r   r9   r   r   r   stater   r   r   r9   T)argument_specrequired_one_ofmutually_exclusivesupports_check_moder=   )r   r[   superbase_module_arguments)cls	__class__s    r&   define_modulez'AnsibleHCloudLoadBalancer.define_module(  s     E?e_$*E?'46I&JWde %$e_'#)6"228U)K ()4( '/1  #F^,!+^ <= $'
 	
r(   )__name__
__module____qualname__	representr   __annotations__r'   r4   rI   rP   rS   rU   classmethodrh   __classcell__)rg   s   @r&   r   r      sH    &I5929
"-"B5-n)	- 
 
r(   r   c                    t         j                         } t        |       }| j                  j                  d      }|dk(  r|j	                          n|dk(  r|j                           | j                  di |j                          y )Nr_   r]   r^   r=   )r   rh   r+   r,   rU   rS   	exit_json
get_result)r*   hcloudr_   s      r&   mainrt   A  sr    &446F&v.FMMg&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.load_balancersr   r	   r   rt   ri   r=   r(   r&   <module>r~      sZ    #AF8
t 5 / 8_
 _
D
, zF r(   