
    VhE'                        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)    )annotationsa4  
---
module: certificate

short_description: Create and manage certificates on the Hetzner Cloud.


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

author:
    - Lukas Kaemmerling (@lkaemmerling)

options:
    id:
        description:
            - The ID of the Hetzner Cloud certificate to manage.
            - Only required if no certificate I(name) is given
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud certificate to manage.
            - Only required if no certificate I(id) is given or a certificate does not exist.
        type: str
    labels:
        description:
            - User-defined labels (key-value pairs)
        type: dict
    certificate:
        description:
            - Certificate and chain in PEM format, in order so that each record directly certifies the one preceding.
            - Required if certificate does not exist and I(type=uploaded).
        type: str
    private_key:
        description:
            - Certificate key in PEM format.
            - Required if certificate does not exist and I(type=uploaded).
        type: str
    domain_names:
        description:
            - Domains and subdomains that should be contained in the Certificate issued by Let's Encrypt.
            - Required if I(type=managed).
        type: list
        default: [ ]
        elements: str
    type:
        description:
            - Choose between uploading a Certificate in PEM format or requesting a managed Let's Encrypt Certificate.
        default: uploaded
        choices: [ uploaded, managed ]
        type: str
    state:
        description:
            - State of the certificate.
        default: present
        choices: [ absent, present ]
        type: str
extends_documentation_fragment:
- hetzner.hcloud.hcloud

a  
- name: Create a basic certificate
  hetzner.hcloud.certificate:
    name: my-certificate
    certificate: -----BEGIN CERTIFICATE-----...
    private_key: -----BEGIN PRIVATE KEY-----...
    state: present

- name: Create a certificate with labels
  hetzner.hcloud.certificate:
    name: my-certificate
    certificate: -----BEGIN CERTIFICATE-----...
    private_key: -----BEGIN PRIVATE KEY-----...
    labels:
      key: value
      mylabel: 123
    state: present

- name: Create a managed certificate
  hetzner.hcloud.certificate:
    name: my-certificate
    type: managed
    domain_names:
      - example.com
      - www.example.com
    state: present

- name: Ensure the certificate is absent (remove if needed)
  hetzner.hcloud.certificate:
    name: my-certificate
    state: absent
a  
hcloud_certificate:
    description: The certificate instance
    returned: Always
    type: complex
    contains:
        id:
            description: Numeric identifier of the certificate
            returned: always
            type: int
            sample: 1937415
        name:
            description: Name of the certificate
            returned: always
            type: str
            sample: my website cert
        fingerprint:
            description: Fingerprint of the certificate
            returned: always
            type: str
            sample: "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f"
        certificate:
            description: Certificate and chain in PEM format
            returned: always
            type: str
            sample: "-----BEGIN CERTIFICATE-----..."
        domain_names:
            description: List of Domains and Subdomains covered by the Certificate
            returned: always
            type: dict
        not_valid_before:
            description: Point in time when the Certificate becomes valid (in ISO-8601 format)
            returned: always
            type: str
        not_valid_after:
            description: Point in time when the Certificate stops being valid (in ISO-8601 format)
            returned: always
            type: str
        labels:
            description: User-defined labels (key-value pairs)
            returned: always
            type: dict
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundCertificatec                  `     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 )AnsibleHCloudCertificatehcloud_certificateNzBoundCertificate | Nonec           
        t        | j                  j                        | j                  j                  | j                  j                  | j                  j
                  | j                  j                  | j                  j                  j                         | j                  j                  j                         | j                  j                  | j                  j                  d	S )N)	idnametypefingerprintcertificatenot_valid_beforenot_valid_afterdomain_nameslabels)strr   r   r   r   r   r   r   	isoformatr   r   r   selfs    n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/certificate.py_prepare_resultz(AnsibleHCloudCertificate._prepare_result   s    d--001++00++0022>>22>> $ 7 7 H H R R T#66FFPPR 33@@--44

 
	
    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 y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nr   r   )
moduleparamsgetclientcertificates	get_by_idr   get_by_namer   fail_json_hcloudr   	exceptions     r   _get_certificatez)AnsibleHCloudCertificate._get_certificate   s    	-{{!!%%d+7*.++*B*B*L*LT[[M_M_McMcdhMi*j'##''/;*.++*B*B*N*Nt{{OaOaOeOeflOm*n' <  	-!!),,	-s   A2C) 5A2C) )	D2DDc                   | j                   j                  dg       | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  j                  d      dk(  r| j                   j                  ddg       | j                   j                  j                  d      |d<   | j                   j                  j                  d      |d<   | j                   j                  s	  | j
                  j                  j                  di | n| j                   j                  d	g       | j                   j                  j                  d	      |d	<   | j                   j                  sC	  | j
                  j                  j                  di |}|j                  j                  d
       | j                          | j                          y # t        $ r}| j                  |       Y d }~@d }~ww xY w# t        $ r}| j                  |       Y d }~gd }~ww xY w)Nr   required_paramsr   )r   r   r   uploadedr   private_keyr   >   )max_retries )r   fail_on_missing_paramsr   r    
check_moder!   r"   creater   r%   create_managedactionwait_until_finished_mark_as_changedr(   )r   r   r'   resps       r   _create_certificatez,AnsibleHCloudCertificate._create_certificate   s   **F8*D KK&&**62kk((,,X6
 ;;!!&)Z7KK..}?].^$(KK$6$6$:$:=$IF=!$(KK$6$6$:$:=$IF=!;;))53DKK,,33=f= KK..?O.P%)[[%7%7%;%;N%KF>";;))5B4;;33BBLVLD KK333C 	 ' 5)))445 ' 5)))445s1   &G< AH# <	H HH #	I,IIc                   	 | 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 # t        $ r}| j                  |       Y d }~0d }~ww xY w)Nr   r   r*   )r   r   )r   )r   r   r    r   r   r1   r2   updater7   r   r   r%   r(   )r   r   r   r'   s       r   _update_certificatez,AnsibleHCloudCertificate._update_certificate   s   	-;;%%))&1DD$;$;$@$@D$H22D62J{{--++222=%%'[[''++H5F!d&=&=&D&D&N{{--++22&2A%%' 	  	-!!),,	-s   D!D4 4	E=EEc                ~    | j                          | j                  | j                          y | j                          y N)r(   r   r9   r<   r   s    r   present_certificatez,AnsibleHCloudCertificate.present_certificate   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r>   )
r(   r   r   r2   r!   r"   deleter   r%   r7   r&   s     r   delete_certificatez+AnsibleHCloudCertificate.delete_certificate   s    "".;;))5KK,,33D4K4KL !!#"& ' 5)))445s   /A; ;	BBBc                    t        t        dddiddiddgddddg dddidd	d
ddiddgdddt        |          ddggdddgggd	      S )Nr   intr   r,   managed)choicesdefaultlist)r   elementsrG   T)r   no_logdictabsentpresent)r   r   r   r   r   r-   r   stater   r   rN   )argument_specrequired_one_ofrequired_ifsupports_check_moder0   )r   rK   superbase_module_arguments)cls	__class__s    r   define_modulez&AnsibleHCloudCertificate.define_module   s     E?e_ *I6) '-%BO#UO%*d;' ()4( '/1" #F^,!9vh78 $)
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r(   r9   r<   r?   rB   classmethodrW   __classcell__)rV   s   @r   r
   r
      sF    $I26/6
- > $'	' 
 
r   r
   c                    t         j                         } t        |       }| j                  j                  d      }|dk(  r|j	                          n|dk(  r|j                           | j                  di |j                          y )NrN   rL   rM   r0   )r
   rW   r   r    rB   r?   	exit_json
get_result)r   hcloudrN   s      r   mainrc     sr    %335F%f-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.certificatesr   r
   rc   rX   r0   r   r   <module>rm      sX    #<|B*
X 5 / 8 Fv
} v
r
, zF r   