
    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  G d
 de      Zd Zedk(  r e        yy)    )annotationsao  
---
module: floating_ip

short_description: Create and manage cloud Floating IPs on the Hetzner Cloud.


description:
    - Create, update and manage cloud Floating IPs on the Hetzner Cloud.

author:
    - Lukas Kaemmerling (@lkaemmerling)
version_added: 0.1.0
options:
    id:
        description:
            - The ID of the Hetzner Cloud Floating IPs to manage.
            - Only required if no Floating IP I(name) is given.
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud Floating IPs to manage.
            - Only required if no Floating IP I(id) is given or a Floating IP does not exist.
        type: str
    description:
        description:
            - The Description of the Hetzner Cloud Floating IPs.
        type: str
    home_location:
        description:
            - Home Location of the Hetzner Cloud Floating IP.
            - Required if no I(server) is given and Floating IP does not exist.
        type: str
    server:
        description:
            - Server Name the Floating IP should be assigned to.
            - Required if no I(home_location) is given and Floating IP does not exist.
        type: str
    type:
        description:
            - Type of the Floating IP.
            - Required if Floating IP does not exist
        choices: [ ipv4, ipv6 ]
        type: str
    force:
        description:
            - Force the assignment or deletion of the Floating IP.
        type: bool
    delete_protection:
        description:
            - Protect the Floating IP for deletion.
        type: bool
    labels:
        description:
            - User-defined labels (key-value pairs).
        type: dict
    state:
        description:
            - State of the Floating IP.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
a  
- name: Create a basic IPv4 Floating IP
  hetzner.hcloud.floating_ip:
    name: my-floating-ip
    home_location: fsn1
    type: ipv4
    state: present
- name: Create a basic IPv6 Floating IP
  hetzner.hcloud.floating_ip:
    name: my-floating-ip
    home_location: fsn1
    type: ipv6
    state: present
- name: Assign a Floating IP to a server
  hetzner.hcloud.floating_ip:
    name: my-floating-ip
    server: 1234
    state: present
- name: Assign a Floating IP to another server
  hetzner.hcloud.floating_ip:
    name: my-floating-ip
    server: 1234
    force: true
    state: present
- name: Floating IP should be absent
  hetzner.hcloud.floating_ip:
    name: my-floating-ip
    state: absent
aS  
hcloud_floating_ip:
    description: The Floating IP instance
    returned: Always
    type: complex
    contains:
        id:
            description: ID of the Floating IP
            type: int
            returned: Always
            sample: 12345
        name:
            description: Name of the Floating IP
            type: str
            returned: Always
            sample: my-floating-ip
        description:
            description: Description of the Floating IP
            type: str
            returned: Always
            sample: my-floating-ip
        ip:
            description: IP Address of the Floating IP
            type: str
            returned: Always
            sample: 116.203.104.109
        type:
            description: Type of the Floating IP
            type: str
            returned: Always
            sample: ipv4
        home_location:
            description: Name of the home location of the Floating IP
            type: str
            returned: Always
            sample: fsn1
        server:
            description: Name of the server the Floating IP is assigned to.
            type: str
            returned: Always
            sample: "my-server"
        delete_protection:
            description: True if Floating IP 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)BoundFloatingIPc                  `     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 )AnsibleHCloudFloatingIPhcloud_floating_ipNzBoundFloatingIP | Nonec           
        t        | j                  j                        | j                  j                  | j                  j                  | j                  j
                  | j                  j                  | j                  j                  j                  | j                  j                  | j                  j                   | j                  j                  j                  nd | j                  j                  d   d	S )Ndelete)	idnamedescriptioniptypehome_locationlabelsserverdelete_protection)strr   r   r   r   r   r   r   r   r   
protectionselfs    n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/floating_ip.py_prepare_resultz'AnsibleHCloudFloatingIP._prepare_result   s    d--001++0022>>)),,++00!44BBGG--44=A=T=T=[=[=gd--4499mq!%!8!8!C!CH!M

 
	
    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floating_ips	get_by_idr   get_by_namer   fail_json_hcloudr   	exceptions     r   _get_floating_ipz(AnsibleHCloudFloatingIP._get_floating_ip   s    	-{{!!%%d+7*.++*B*B*L*LT[[M_M_McMcdhMi*j'*.++*B*B*N*Nt{{OaOaOeOeflOm*n' 	-!!),,	-s   A2C 5AC 	C'C""C'c                   | j                   j                  dg       	 | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  j                  d      L| j                  j
                  j                  | j                   j                  j                  d            |d<   n| j                   j                  j                  d      L| j                  j                  j                  | j                   j                  j                  d            |d<   n| j                   j                  d	       | j                   j                  j                  d
      (| j                   j                  j                  d
      |d
<   | j                   j                  s | j                  j                  j                  di |}|j                  | _        | j                   j                  j                  d      }|,| j                  j                  |      }|j                          | j%                          | j'                          y # t         $ r}| j#                  |       Y d }~@d }~ww xY w)Nr   )required_paramsr   r   )r   r   r   r   r   z7one of the following is required: home_location, server)msgr   r   r    )r   fail_on_missing_paramsr    r!   r"   	locationsr%   servers	fail_json
check_moder#   createfloating_ipr   change_protectionwait_until_finishedr   r&   _mark_as_changedr)   )r   r    respr   actionr(   s         r   _create_floating_ipz+AnsibleHCloudFloatingIP._create_floating_ip   s   **F8*D	-#{{1155mD**..v6**..v6F
 {{!!%%o6B*.++*?*?*K*KDKKL^L^LbLbcrLs*t'##''1=#';;#6#6#B#B4;;CUCUCYCYZbCc#dx %%*c%d{{!!%%h/;#';;#5#5#9#9(#Cx ;;))6t{{//66@@*.*:*:'$(KK$6$6$:$:;N$O!$0!44FFN_F`F..0 	  	-!!),,	-s   IJ 	J>#J99J>c                   	 | j                   j                  j                  d      }|[|| j                  j                  k7  rB| j                   j
                  s| j                  j                  |       | j                          | j                   j                  j                  d      }|[|| j                  j                  k7  rB| j                   j
                  s| j                  j                  |       | j                          | j                   j                  j                  d      }|8| j                  j                  !| j                   j                  j                  d      r|| j                  j                  j                  k7  rg| j                   j
                  s| j                  j                  | j                  j                  j                  |             | j                          nG|| j                  j                  j                  k7  r#| j                   j                  d| j                  j                  j                   d       | j                          n|{| j                  j                  e| j                   j
                  s>| j                  j                  | j                  j                  j                  |             | j                          nX|V| j                  j                  @| 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 }~y d }~ww xY w)Nr   )r   r   )r   r   forcez2Floating IP is already assigned to another server 9. You need to unassign the Floating IP or use force=true.r   r   r-   )r   r    r!   r   r   r3   updater8   r   r   r   assignr"   r1   r%   warnunassignr   r6   r7   r)   r   r&   )r   r   r   r   r   r:   r(   s          r   _update_floating_ipz+AnsibleHCloudFloatingIP._update_floating_ip   s   ,	-[[''++H5F!f0G0G0N0N&N{{--++22&2A%%'++,,00?K&;$:Q:Q:]:]+]{{--++22{2K%%'[[''++H5F!d&=&=&D&D&P;;%%))'2vAXAXA_A_AdAd7d;;11//66t{{7J7J7V7VW]7^_--/t66==BBBKK$$L2299>>? @FF
 ))+#(?(?(F(F(N{{--++224;;3F3F3R3RSY3Z[%%'D$;$;$B$B$N{{--++446%%' $ 2 2 6 67J K ,1BdF]F]FhFhiqFr1r{{--!44FFN_F`F..0%%'!!# 	-!!),,	-s   OO! !	P*P  Pc                ~    | j                          | j                  | j                          y | j                          y )N)r)   r   r;   rC   r   s    r   present_floating_ipz+AnsibleHCloudFloatingIP.present_floating_ip  s2    ""*$$&$$&r   c                6   	 | j                          | j                  | j                  j                  j	                  d      s| j                  j
                  F| j                  j                  sm| j                  j                  j                  | j                         n=| j                  j                  d| j                  j
                  j                   d       | j                          d | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nr=   z,Floating IP is currently assigned to server r>   )r)   r   r   r    r!   r   r3   r"   r#   r   rA   r   r8   r   r&   r'   s     r   delete_floating_ipz*AnsibleHCloudFloatingIP.delete_floating_ip  s    	-!!#&&2;;%%))'2d6M6M6T6T6\;;1100778O8OPKK$$F2299>>? @FF
 %%'&*D# 	-!!),,	-s   C1C4 4	D=DDc                    t        t        dddiddiddiddiddiddidddgiddiddid	d
gd
dd
t        |          ddggddggd      S )Nr   intr   boolchoicesipv4ipv6dictabsentpresent)rK   default)
r   r   r   r   r   r=   r   r   r   stater   r   r   r   T)argument_specrequired_one_ofmutually_exclusivesupports_check_moder.   )r   rN   superbase_module_arguments)cls	__class__s    r   define_modulez%AnsibleHCloudFloatingIP.define_module*  s     E?e_#UO%uov&&&!12'#)6"2 ()4( '/1  #F^,!0( ;< $'
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r)   r;   rC   rE   rG   classmethodr[   __classcell__)rZ   s   @r   r
   r
      sG    $I15.5
- <--^'-$ 
 
r   r
   c                     t         j                         } t        |       }| j                  d   }|dk(  r|j                          n|dk(  r|j	                           | j
                  di |j                          y )NrR   rO   rP   r.   )r
   r[   r    rG   rE   	exit_json
get_result)r   hcloudrR   s      r   mainrg   C  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.floating_ipsr   r
   rg   r\   r.   r   r   <module>rq      sX    #AF<6
p 5 / 8 EW
m W
t
, zF r   