
    Vh $                         d dl mZmZmZ eZd dlZd dlmZ dZ		 d dl
mZ d dl
mZ dZd dlmZmZ d d	lmZ d d
lmZ d Zd Zd Z G d de      Zy# e$ r dZ ej                          Z	Y Cw xY w)    )absolute_importdivisionprint_functionN)sleep)
ProxmoxAPI)__version__TF)env_fallbackmissing_required_lib)	to_native)LooseVersionc                      t        t        ddt        dgf      t        dt        dgf      t        ddt        dgf      t        ddt        d	gf
      t        dd      t        dd      t        dd            S )NstrTPROXMOX_HOST)typerequiredfallbackintPROXMOX_PORT)r   r   PROXMOX_USERPROXMOX_PASSWORD)r   no_logr   F)r   r   bool)r   default)api_hostapi_portapi_userapi_passwordapi_token_idapi_token_secretvalidate_certs)dictr	        r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/proxmox/plugins/module_utils/proxmox.pyproxmox_auth_argument_specr%      s    5# ,~.>? 5 ,~.>? 5# ,~.>? u!%$03E2F#G u!& 5%)  $)- r#   c                     | dk(  rdS dS )zBConvert Proxmox representation of a boolean to be ansible-friendly   TFr"   values    r$   proxmox_to_ansible_boolr*   9   s    A:4(5(r#   c                 f    | yt        | t              st        | dt        |             | rdS dS )zBConvert Ansible representation of a boolean to be proxmox-friendlyNz must be of type bool not r'   r   )
isinstancer   
ValueErrorr   r(   s    r$   ansible_to_proxmox_boolr.   >   s5    }eT"UDKPQQ11r#   c                   h    e Zd ZdZdZd Zd Zd Zd Zd Z	ddZ
dd	Zd
 Zd Zd Zd Zd ZddZy)ProxmoxAnsiblezBase class for Proxmox modulesztimeout expiredc                    t         s |j                  t        d      t               t        t        d      k  r"| j                  j                  dt                || _        t        | _        | j                         | _        	 | j                  j                  j                          y # t        $ r2}|j                  d|z  t        j                                Y d }~y d }~ww xY w)N	proxmoxermsg	exceptionz2.0z/Requires proxmoxer 2.0 or newer; found version %s)HAS_PROXMOXER	fail_jsonr
   PROXMOXER_IMP_ERRproxmoxer_versionr   module_connectproxmox_apiversionget	Exception	traceback
format_exc)selfr;   es      r$   __init__zProxmoxAnsible.__init__M   s    !5k!BN_`|E22KK!!$STeSf"gh!2==?	M$$((* 	MY5I5I5KLL	Ms   $B( (	C#1(CC#c                 H   | j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }d|i}|r||d	<   |r||d
<   n
||d<   ||d<   	 t        |fd|i|S # t        $ r<}	| j                   j	                  d|	z  t        j                                Y d }	~	y d }	~	ww xY w)Nr   r   r   r   r   r   r    userportpassword
token_nametoken_value
verify_sslr6   r3   )r;   paramsr   r@   r8   rA   rB   )
rC   r   r   r   r   r   r   r    	auth_argsrD   s
             r$   r<   zProxmoxAnsible._connect\   s   ;;%%j1;;%%j1;;%%j1{{)).9{{)).9;;--.@A++,<=X&	 (If$0Ij!&2Il#'7Im$	RhO>OYOO 	RKK!!dQh):N:N:P!QQ	Rs   C 	D!%2DD!c                     	 | j                   j                  j                         }t        |d         S # t        $ r)}| j
                  j                  d|z         Y d }~y d }~ww xY w)Nr>   z)Unable to retrieve Proxmox VE version: %sr4   )r=   r>   r?   r   r@   r;   r8   )rC   
apiversionrD   s      r$   r>   zProxmoxAnsible.versionu   sc    	W))11557J
9 566 	WKK!!&QTU&U!VV	Ws   14 	A&A!!A&c                     	 | j                   j                  j                         D cg c]  }|d   |k(  s| }}r|d   S d S c c}w # t        $ r)}| j                  j                  d|z         Y d }~;d }~ww xY w)Nnodez&Unable to retrieve Proxmox VE node: %srP   r   )r=   nodesr?   r@   r;   r8   )rC   rS   nrT   rD   s        r$   get_nodezProxmoxAnsible.get_node|   s    	T $ 0 0 6 6 : : <R1&	T@QQRER !uQx*d* S 	TKK!!&NQR&R!SS	Ts,   &A
 AAA
 A
 
	A<A77A<c                     	 | j                   j                  j                  j                         S # t        $ r)}| j
                  j                  d|z         Y d }~y d }~ww xY w)Nz%Unable to retrieve next free vmid: %srP   )r=   clusternextidr?   r@   r;   r8   )rC   rD   s     r$   get_nextvmidzProxmoxAnsible.get_nextvmid   sZ    	S##++226688 	SKK!!&MPQ&Q!RR	Ss   -0 	A"AA"c                    	 | j                   j                  j                  j                  d      D cg c]  }|j                  d      |k(  s|d    }}s'|ry | j
                  j                  d|z         |d   S t        |      d	kD  r!|s| j
                  j                  d
|z         |d   S c c}w # t        $ r,}| j
                  j                  d|d|       Y d }~d }~ww xY w)Nvmr   namevmidz0Unable to retrieve list of VMs filtered by name : rP   zNo VM with name %s foundr'   z5Multiple VMs with name %s found, provide vmid insteadr   )r=   rX   	resourcesr?   r@   r;   r8   len)rC   r^   ignore_missingchoose_first_if_multipler\   vmsrD   s          r$   get_vmidzProxmoxAnsible.get_vmid   s    	l(,(8(8(@(@(J(J(N(NTX(N(Yt"]_]c]cdj]kos]s2f:tCt KK!!&@4&G!H 1v X\":KK!!&]`d&d!e1v u 	lKK!!bfhi&j!kk	ls.   2B9 B4B4B9 4B9 9	C."C))C.c                 p   	 | j                   j                  j                  j                  d      D cg c]  }|d   t	        |      k(  s| }}r|d   S |ry | j                  j                  d|z         y c c}w # t
        $ r,}| j                  j                  d|d|       Y d }~_d }~ww xY w)	Nr\   r]   r_   z0Unable to retrieve list of VMs filtered by vmid r`   rP   r   z)VM with vmid %s does not exist in cluster)r=   rX   ra   r?   r   r@   r;   r8   )rC   r_   rc   r\   re   rD   s         r$   get_vmzProxmoxAnsible.get_vm   s    	l $ 0 0 8 8 B B F FD F Qm"UWX^U_cfgkclUl2mCm q6MKK!!&QTX&X!Y n 	lKK!!bfhi&j!kk	ls.   2B  A;A;B  ;B   	B5	"B00B5c                 x   	 | j                   j                  |      j                  |      j                  j	                         }t        |j	                  d      xs d      }|d   dk(  xr |dk(  xs |j                  d      S # t        $ r,}| j                  j                  d|d|	       Y d }~y d }~ww xY w)
N
exitstatus statusstoppedOKWARN)Unable to retrieve API task ID from node r`   rP   )
r=   rT   tasksrl   r?   r   
startswithr@   r;   r8   )rC   rS   taskidrl   rj   rD   s         r$   api_task_okzProxmoxAnsible.api_task_ok   s    	e%%++D177?FFJJLF"6::l#;#ArBJ(#y0jjD6H6iJLaLabhLij 	eKK!![_ab&c!dd	es   BB 	B9"B44B9c                    	 | j                   j                  |      j                  |      j                  j	                         }|d   dk(  xr |d   dk7  S # t
        $ r,}| j                  j                  d|d|       Y d}~yd}~ww xY w)	zK Explicitly check if the task stops but exits with a failed status
        rl   rm   rj   rn   rp   r`   rP   N)r=   rT   rq   rl   r?   r@   r;   r8   )rC   rS   rs   rl   rD   s        r$   api_task_failedzProxmoxAnsible.api_task_failed   s    	e%%++D177?FFJJLF(#y0QVL5IT5QQ 	eKK!![_ab&c!dd	es   AA 	B"BBc                    i }|r	 | j                   j                  |      j                  |      j                  j	                         }|d   dk(  r|d   dk(  ry	d
|d   fS |dz  }|dk  rd
t        j                  fS t        d       |ryy# t
        $ r,}| j                  j                  d|d|       Y d}~td}~ww xY w)aX  Wait until the task stops or times out.

        :param node_name: Proxmox node name where the task is running.
        :param task_id: ID of the running task.
        :param timeout: Timeout in seconds to wait for the task to complete.
        :return: Task completion status (True/False) and ``exitstatus`` message when status=False.
        rp   r`   rP   Nrl   rm   rj   rn   )TNFr'   r   )r=   rT   rq   rl   r?   r@   r;   r8   r0   TASK_TIMED_OUTr   )rC   	node_nametask_idtimeoutrl   rD   s         r$   api_task_completez ProxmoxAnsible.api_task_complete   s     n))//	:@@IPPTTV h9,,'4/% &"6661a< ."?"???a   n%%_hjk*l%mmns   AB 	C"B<<Cc                     	 | j                   j                  |      j                         S # t        $ r,}| j                  j                  d|d|       Y d}~yd}~ww xY w)zzRetrieve pool information

        :param poolid: str - name of the pool
        :return: dict - pool information
        zUnable to retrieve pool z information: rP   N)r=   poolsr?   r@   r;   r8   )rC   poolidrD   s      r$   get_poolzProxmoxAnsible.get_pool   sY    	b##))&15577 	bKK!!V\^_&`!aa	bs   (+ 	A "AA c                     	 | j                   j                  j                  |      S # t        $ r,}| j                  j                  d|d|       Y d}~yd}~ww xY w)zRetrieve storages information

        :param type: str, optional - type of storages
        :return: list of dicts - array of storages
        r]   z2Unable to retrieve storages information with type r`   rP   N)r=   storager?   r@   r;   r8   )rC   r   rD   s      r$   get_storageszProxmoxAnsible.get_storages   sY    	n##++//T/:: 	nKK!!dhjk&l!mm	ns   %( 	A"AANc                    	 | j                   j                  |      j                  |      j                         j	                  ||      S # t
        $ r5}| j                  j                  d|d|d|d|d|
       Y d }~y d }~ww xY w)N)contentr_   zUnable to list content on z, z for z and r`   rP   )r=   rT   r   r   r?   r@   r;   r8   )rC   rS   r   r   r_   rD   s         r$   get_storage_contentz"ProxmoxAnsible.get_storage_content   s}    	  &&t,!W40	  	KK!!'44 "  	s   AA 	B	+BB	)FF)F)NN)__name__
__module____qualname____doc__rx   rE   r<   r>   rV   rZ   rf   rh   rt   rv   r|   r   r   r   r"   r#   r$   r0   r0   I   sT    (&NMR2W+S Zee4	b	nr#   r0   )
__future__r   r   r   r   __metaclass__rA   timer   r9   r2   r   r   r:   r7   ImportErrorrB   ansible.module_utils.basicr	   r
   +ansible.module_utils.common.text.convertersr   Bansible_collections.community.proxmox.plugins.module_utils.versionr   r%   r*   r.   objectr0   r"   r#   r$   <module>r      s    A @   /$:M J A [:)
kV ko  /M,	,,./s   A A,+A,