
    Vh4*                        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)    )annotationsar  
---
module: primary_ip

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


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

author:
    - Lukas Kaemmerling (@lkaemmerling)
version_added: 1.8.0
options:
    id:
        description:
            - The ID of the Hetzner Cloud Primary IPs to manage.
            - Only required if no Primary IP I(name) is given.
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud Primary IPs to manage.
            - Only required if no Primary IP I(id) is given or a Primary IP does not exist.
        type: str
    datacenter:
        description:
            - Home Location of the Hetzner Cloud Primary IP.
            - Required if no I(server) is given and Primary IP does not exist.
        type: str
    server:
        description:
            - Name or ID of the Hetzner Cloud Server the Primary IP should be assigned to.
            - The Primary IP cannot be assigned to a running server.
            - Required if no O(datacenter) is given and the Primary IP does not exist.
        type: str
    type:
        description:
            - Type of the Primary IP.
            - Required if Primary IP does not exist
        choices: [ ipv4, ipv6 ]
        type: str
    auto_delete:
        description:
            - Delete the Primary IP when the resource it is assigned to is deleted.
        type: bool
        default: false
    delete_protection:
        description:
            - Protect the Primary IP for deletion.
        type: bool
    labels:
        description:
            - User-defined labels (key-value pairs).
        type: dict
    state:
        description:
            - State of the Primary IP.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
a  
- name: Create a IPv4 Primary IP
  hetzner.hcloud.primary_ip:
    name: my-primary-ip
    datacenter: fsn1-dc14
    type: ipv4
    state: present

- name: Create a IPv6 Primary IP
  hetzner.hcloud.primary_ip:
    name: my-primary-ip
    datacenter: fsn1-dc14
    type: ipv6
    state: present

- name: Delete a Primary IP
  hetzner.hcloud.primary_ip:
    name: my-primary-ip
    state: absent

- name: Ensure the server is stopped
  hetzner.hcloud.server:
    name: my-server
    state: stopped
- name: Create a Primary IP attached to a Server
  hetzner.hcloud.primary_ip:
    name: my-primary-ip
    server: my-server
    type: ipv4
    state: present
- name: Ensure the server is started
  hetzner.hcloud.server:
    name: my-server
    state: started
a  
hcloud_primary_ip:
    description: The Primary IP instance
    returned: Always
    type: complex
    contains:
        id:
            description: ID of the Primary IP
            type: int
            returned: Always
            sample: 12345
        name:
            description: Name of the Primary IP
            type: str
            returned: Always
            sample: my-primary-ip
        ip:
            description: IP Address of the Primary IP
            type: str
            returned: Always
            sample: 116.203.104.109
        type:
            description: Type of the Primary IP
            type: str
            returned: Always
            sample: ipv4
        datacenter:
            description: Name of the datacenter of the Primary IP
            type: str
            returned: Always
            sample: fsn1-dc14
        delete_protection:
            description: True if Primary IP is protected for deletion
            type: bool
            returned: always
            sample: false
        labels:
            description: User-defined labels (key-value pairs)
            type: dict
            returned: Always
            sample:
                key: value
                mylabel: 123
        assignee_id:
            description: ID of the resource the Primary IP is assigned to, null if it is not assigned.
            type: int
            returned: always
            sample: 1937415
        assignee_type:
            description: Resource type the Primary IP can be assigned to.
            type: str
            returned: always
            sample: server
        auto_delete:
            description: Delete the Primary IP when the resource it is assigned to is deleted.
            type: bool
            returned: always
            sample: false
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundPrimaryIPc                  `     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 )AnsibleHCloudPrimaryIPhcloud_primary_ipNzBoundPrimaryIP | Nonec                   t        | j                  j                        | j                  j                  | j                  j                  | j                  j
                  | j                  j                  j                  | j                  j                  | j                  j                  d   | j                  j                  t        | j                  j                        nd | j                  j                  | j                  j                  d
S )Ndelete)
idnameiptype
datacenterlabelsdelete_protectionassignee_idassignee_typeauto_delete)strr   r   r   r   r   r   r   
protectionr   r   r   selfs    m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/primary_ip.py_prepare_resultz&AnsibleHCloudPrimaryIP._prepare_result   s    d,,//0**//((++**//00;;@@,,33!%!7!7!B!B8!L;?;Q;Q;];];iD**667os!33AA11==
 	
    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primary_ips	get_by_idr   get_by_namer   fail_json_hcloudr   	exceptions     r   _get_primary_ipz&AnsibleHCloudPrimaryIP._get_primary_ip   s    	-{{!!%%d+7)-)@)@)J)J4;;K]K]KaKabfKg)h&)-)@)@)L)LT[[M_M_McMcdjMk)l& 	-!!),,	-s   A2C 5AC 	C'C""C'c                x   | j                  ddgddg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<   ng| j                  j                  j                  d      B| j                  d| j                  j                  j                  d            j                  |d	<   | j                  j                  j                  d
      (| j                  j                  j                  d
      |d
<   | j                  j                  s | j                  j                  j                  di |}|j                  |j                  j                          |j                  | _        | j                  j                  j                  d      }|,| j                  j!                  |      }|j                          | j'                          | j)                          y # t"        $ r}| j%                  |       Y d }~@d }~ww xY w)Nr   r   serverr   )requiredrequired_one_ofr   )r   r   r   serversr   r   r   r    )fail_on_invalid_paramsr    r!   r"   r#   datacentersr&   _client_get_by_name_or_idr   
check_moder$   createactionwait_until_finished
primary_ipr   change_protectionr   r'   _mark_as_changedr*   )r   r!   respr   r7   r)   s         r   _create_primary_ipz)AnsibleHCloudPrimaryIP._create_primary_ip   s0   ##f%&56 	$ 	
	-**..v6**..v6#{{1155mDF {{!!%%l3?'+{{'>'>'J'J4;;K]K]KaKabnKo'p|$##''1=(,(F(FyRVR]R]RdRdRhRhiqRr(s(v(v}%{{!!%%h/;#';;#5#5#9#9(#Cx ;;))5t{{..55??;;*KK335)-&$(KK$6$6$:$:;N$O!$0!33EEM^E_F..0 	  	-!!),,	-s   IJ 	J9J44J9c                >   	 i }| j                   j                  j                  d      }||| j                  j                  k7  r||d<   | j                   j                  j                  d      }||| j                  j
                  k7  r||d<   |rB| j                   j                  s | j                  j                  di | | 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   r0   r1   )r    r!   r"   r   r   r   r5   updater;   r   r:   r8   r*   r   r'   )r   changesr   r   r   r7   r)   s          r   _update_primary_ipz)AnsibleHCloudPrimaryIP._update_primary_ip   sc   	-G++,,00?K&;$:P:P:\:\+\)4&[[''++H5F!f0F0F0M0M&M$*!{{--1D**11<G<%%' $ 2 2 6 67J K ,1BdF\F\FgFghpFq1q{{--!33EEM^E_F..0%%'  " 	-!!),,	-s   E5E8 8	FFFc                ~    | j                          | j                  | j                          y | j                          y N)r*   r   r=   rA   r   s    r   present_primary_ipz)AnsibleHCloudPrimaryIP.present_primary_ip  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rC   )
r*   r   r    r5   r#   r$   r   r;   r   r'   r(   s     r   delete_primary_ipz(AnsibleHCloudPrimaryIP.delete_primary_ip  s    	-  "%%1{{--KK++2243I3IJ%%'%)D" 	-!!),,	-s   A8A; ;	BBBc                    t        t        dddiddiddiddidddddd	gidd
iddiddgddd	t        |          ddggd      S )Nr   intr   boolF)r   defaultchoicesipv4ipv6dictabsentpresent)rK   rJ   )	r   r   r   r,   r   r   r   r   stater   r   T)argument_specr.   supports_check_moder1   )r   rN   superbase_module_arguments)cls	__class__s    r   define_modulez$AnsibleHCloudPrimaryIP.define_module  s     E?e_"E?%+>&&!12'#)6"2 ()4( '/1 #F^, $#
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r*   r=   rA   rD   rF   classmethodrX   __classcell__)rW   s   @r   r
   r
      sG    #I/3,3
 - D-8&	- 
 
r   r
   c                     t         j                         } t        |       }| j                  d   }|dk(  r|j                          n|dk(  r|j	                           | j
                  di |j                          y )NrQ   rO   rP   r1   )r
   rX   r!   rF   rD   	exit_json
get_result)r    hcloudrQ   s      r   mainrd   6  sl    #113F#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.primary_ipsr   r
   rd   rY   r1   r   r   <module>rn      sX    #?B"H:
x 5 / 8 CB
] B
J
, zF r   