
    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)    )annotationsa  
---
module: ssh_key

short_description: Create and manage ssh keys on the Hetzner Cloud.


description:
    - Create, update and manage ssh keys on the Hetzner Cloud.

author:
    - Lukas Kaemmerling (@LKaemmerling)

options:
    id:
        description:
            - The ID of the Hetzner Cloud ssh_key to manage.
            - Only required if no ssh_key I(name) is given
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud ssh_key to manage.
            - Only required if no ssh_key I(id) is given or a ssh_key does not exist.
        type: str
    fingerprint:
        description:
            - The Fingerprint of the Hetzner Cloud ssh_key to manage.
            - Only required if no ssh_key I(id) or I(name) is given.
        type: str
    labels:
        description:
            - User-defined labels (key-value pairs)
        type: dict
    public_key:
        description:
            - The Public Key to add.
            - Required if ssh_key does not exist.
        type: str
    state:
        description:
            - State of the ssh_key.
        default: present
        choices: [ absent, present ]
        type: str
extends_documentation_fragment:
- hetzner.hcloud.hcloud

a  
- name: Create a basic ssh_key
  hetzner.hcloud.ssh_key:
    name: my-ssh_key
    public_key: ssh-rsa AAAjjk76kgf...Xt
    state: present

- name: Create a ssh_key with labels
  hetzner.hcloud.ssh_key:
    name: my-ssh_key
    public_key: ssh-rsa AAAjjk76kgf...Xt
    labels:
      key: value
      mylabel: 123
    state: present

- name: Ensure the ssh_key is absent (remove if needed)
  hetzner.hcloud.ssh_key:
    name: my-ssh_key
    state: absent
a  
hcloud_ssh_key:
    description: The ssh_key instance
    returned: Always
    type: complex
    contains:
        id:
            description: ID of the ssh_key
            type: int
            returned: Always
            sample: 12345
        name:
            description: Name of the ssh_key
            type: str
            returned: Always
            sample: my-ssh-key
        fingerprint:
            description: Fingerprint of the ssh_key
            type: str
            returned: Always
            sample: b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f
        public_key:
            description: Public key of the ssh_key
            type: str
            returned: Always
            sample: "ssh-rsa AAAjjk76kgf...Xt"
        labels:
            description: User-defined labels (key-value pairs)
            type: dict
            returned: Always
            sample:
                key: value
                mylabel: 123
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundSSHKeyc                  `     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 )AnsibleHCloudSSHKeyhcloud_ssh_keyNzBoundSSHKey | Nonec                    t        | j                  j                        | j                  j                  | j                  j                  | j                  j
                  | j                  j                  dS )N)idnamefingerprint
public_keylabels)strr   r   r   r   r   r   selfs    j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/ssh_key.py_prepare_resultz#AnsibleHCloudSSHKey._prepare_result   s\    d)),,-'',,..::--88))00
 	
    c                   	 | j                   j                  j                  d      N| j                  j                  j                  | j                   j                  j                  d            | _        y | j                   j                  j                  d      N| j                  j                  j                  | j                   j                  j                  d            | _        y | j                   j                  j                  d      N| j                  j                  j                  | j                   j                  j                  d            | _        y y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nr   r   r   )moduleparamsgetclientssh_keys	get_by_idr   get_by_fingerprintget_by_namer   fail_json_hcloudr   	exceptions     r   _get_ssh_keyz AnsibleHCloudSSHKey._get_ssh_key   s   		-{{!!%%d+7&*kk&:&:&D&DT[[EWEWE[E[\`Ea&b###''6B&*kk&:&:&M&MdkkN`N`NdNderNs&t###''/;&*kk&:&:&F&Ft{{GYGYG]G]^dGe&f# <  	-!!),,	-s&   A2E 5A2E (A2E 	F %E;;F c                &   | j                   j                  ddg       | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  s'	  | j
                  j                  j                  di | | j                          | j                          y # t        $ r}| j                  |       Y d }~@d }~ww xY w)Nr   r   required_paramsr   )r   r   r    )r   fail_on_missing_paramsr   r   
check_moder   r   creater   r!   _mark_as_changedr$   )r   r   r#   s      r   _create_ssh_keyz#AnsibleHCloudSSHKey._create_ssh_key   s    **FL;Q*RKK&&**62++,,00>kk((,,X6
 {{%%1+$$++5f5 	 # 1%%i001s   %&C, ,	D5DDc                f   | 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                          y )Nr   r   r&   )r   r   )r   )r   r   r   r   r   r)   r*   updater,   r   r$   )r   r   r   s      r   _update_ssh_keyz#AnsibleHCloudSSHKey._update_ssh_key   s    {{!!%%f- 3 3 8 8D @KK..v.F;;))##***5!!###''1$"5"5"<"<"F;;))##**&*9!!#r   c                ~    | j                          | j                  | j                          y | j                          y N)r$   r   r-   r0   r   s    r   present_ssh_keyz#AnsibleHCloudSSHKey.present_ssh_key   s2    &  "  "r   c                D   | j                          | j                  V| j                  j                  s0	 | j                  j
                  j                  | j                         | j                          d | _        y # t        $ r}| j                  |       Y d }~7d }~ww xY wr2   )
r$   r   r   r*   r   r   deleter   r!   r,   r"   s     r   delete_ssh_keyz"AnsibleHCloudSSHKey.delete_ssh_key   s    *;;))5KK((//0C0CD !!#" ' 5)))445s   /A; ;	BBBc                    t        t        dddiddiddiddiddiddgdddt        |          g d	gd
ddgggd      S )Ntypeintr   dictabsentpresent)choicesdefault)r   r   r   r   r   state)r   r   r   r?   r   T)argument_specrequired_one_ofrequired_ifsupports_check_moder(   )r   r:   superbase_module_arguments)cls	__class__s    r   define_modulez!AnsibleHCloudSSHKey.define_module   s}     E?e_"E?#UO' ()4( '/1 ;;!9vh78 $
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r$   r-   r0   r3   r6   classmethodrH   __classcell__)rG   s   @r   r
   r
   z   sE     I)-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   r6   r3   	exit_json
get_result)r   hcloudr?   s      r   mainrT      sr     ..0F (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.ssh_keysr   r
   rT   rI   r(   r   r   <module>r^      sW    #/b,!
F 5 / 8 =^
- ^
B
, zF r   