
    Vh1                        d dl mZ d dlZd dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZ ddlmZmZmZ dd	lmZmZmZmZ dd
lmZ ddlmZ  G d de      Z G d d      Zy)    )annotationsN)AnyNoReturn)AnsibleModuleenv_fallback)	to_native)check_missing_parameterscheck_required_one_of   )ClientExceptionclient_check_required_libclient_get_by_name_or_id)APIExceptionClientHCloudExceptionexponential_backoff_function)ActionException)versionc                      e Zd ZU ded<   y)r   dictparamsN)__name__
__module____qualname____annotations__     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/module_utils/hcloud.pyr   r      s    Lr   r   c                      e Zd ZU ded<   ded<   ddZ	 	 d	 	 	 	 	 	 	 ddZddZdd	Zdd
Zddd	 	 	 	 	 ddZ	e
d        ZddZddZy)AnsibleHCloudstr	representr   modulec                0   | j                   s"t        d| j                  j                         || _        dd| j                   d i| _        	 t                | j                          y # t        $ r%}|j                  t        |             Y d }~:d }~ww xY w)Nz&represent property is not defined for changedFmsg)r"   NotImplementedError	__class__r   r#   resultr   r   	fail_jsonr   _build_client)selfr#   	exceptions      r   __init__zAnsibleHCloud.__init__'   s    ~~%(Nt~~OfOfNg&hii %>	7%' 	  	79!566	7s   
A' '	B0BBNc                   t        j                         }i }|||d<   t        |t              r.|j                  |d<   |j
                  |d<   |j                  |d<   nLt        |t              r<|j                  j                  D ci c]  }|t        |j                  |       c}|d<   t        |      }|| d| }n|} | j                  j                  d|||d| y c c}w )	Nr   messagecodedetailsactionz: )r'   r.   failurer   )	traceback
format_exc
isinstancer   r1   r2   r3   r   r4   	__slots__getattrr   r#   r+   )	r-   r.   r'   r   kwargslast_tracebackr5   kexception_messages	            r   fail_json_hcloudzAnsibleHCloud.fail_json_hcloud5   s     #--/ &GHi.!*!2!2GI'nnGFO!*!2!2GI	?3JSJZJZJdJd eQGI,<,<a$@!@ eGH%i0?&'r#/C#C[#[TZ[ !fs   C c                    t        | j                  j                  d   | j                  j                  d   dt        t	        ddd      d	      | _        y )
N	api_tokenapi_endpointzansible-moduleg      ?   g      @)base
multipliercap   )tokenrB   application_nameapplication_versionpoll_intervalpoll_max_retries)r   r#   r   r   r   clientr-   s    r   r,   zAnsibleHCloud._build_clientS   sH    ++$$[1++N;- '6CASVW
r   c                    	 t        | j                  ||      S # t        $ r/}| j                  j	                  t        |             Y d}~yd}~ww xY w)z
        Get a resource by name, and if not found by its ID.

        :param resource: Name of the resource client that implements both `get_by_name` and `get_by_id` methods
        :param param: Name or ID of the resource to query
        r&   N)r   rM   r   r#   r+   r   )r-   resourceparamr.   s       r   _client_get_by_name_or_idz'AnsibleHCloud._client_get_by_name_or_id^   sG    	<+DKK5II 	<KK!!i	&:!;;	<s    	A%AAc                "    d| j                   d<   y )NTr%   )r*   rN   s    r   _mark_as_changedzAnsibleHCloud._mark_as_changedj   s    !%Ir   )requiredrequired_one_ofc               ^   	 |r t        | j                  j                  |       |rG| j                  j                  j                         D ci c]  \  }}|	|| }}}t	        ||       yyc c}}w # t
        $ r/}| j                  j                  t        |             Y d}~yd}~ww xY w)a   
        Run additional validation that cannot be done in the argument spec validation.

        :param required_params: Check that terms exists in the module params.
        :param required_one_of: Check each list of terms to ensure at least one exists in the module parameters.
        Nr&   )r	   r#   r   itemsr
   	TypeErrorr+   r   )r-   rU   rV   r=   vparams_without_noneses          r   fail_on_invalid_paramsz$AnsibleHCloud.fail_on_invalid_paramsm   s    		4(););XF9=9K9K9Q9Q9S'eAWXWd1'e$'e%o7KL 'e  	4KK!!il!33	4s0   AA4 
A.A.A4 .A4 4	B,=%B''B,c                >    ddt         dgfdddt         dgfddgdd	S )
Nr!   THCLOUD_TOKEN)typerU   fallbackno_logHCLOUD_ENDPOINTzhttps://api.hetzner.cloud/v1endpoint)r`   ra   defaultaliases)rA   rB   )r   )clss    r   base_module_argumentsz#AnsibleHCloud.base_module_arguments   sE      )N+;<	 ),=+>?9&<	
 	
r   c                    i S )z#Prepare the result for every moduler   rN   s    r   _prepare_resultzAnsibleHCloud._prepare_result   s    	r   c                    t        | | j                        '| j                         | j                  | j                  <   | j                  S )N)r:   r"   rj   r*   rN   s    r   
get_resultzAnsibleHCloud.get_result   s8    4(4*.*>*>*@DKK'{{r   )r#   r   )NN)r.   r   r'   z
str | Noner   r   returnr   )rm   None)rP   r!   rQ   z	str | int)rU   zlist[str] | NonerV   zlist[list[str]] | Nonerm   rn   )rm   zdict[str, Any])r   r   r   r   r/   r?   r,   rR   rT   r]   classmethodrh   rj   rl   r   r   r   r    r    "   s    N" 	\"\ \ 	\ 
\<	

<& &*26	4 #4 0	4
 
4. 
 
 r   r    )
__future__r   r6   typingr   r   ansible.module_utils.basicr   AnsibleModuleBaser   +ansible.module_utils.common.text.convertersr   &ansible.module_utils.common.validationr	   r
   rM   r   r   r   vendor.hcloudr   r   r   r   vendor.hcloud.actionsr   r   r    r   r   r   <module>rx      sO    #    W A
 Y X  3 % z zr   