
    Vhk#                         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
 Zd Zd Z G d de      Zy# e$ r dZ ej                          Z	Y =w xY w)    )absolute_importdivisionprint_functionN)sleep)
ProxmoxAPI)__version__TF)env_fallbackmissing_required_lib)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/general/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)   8   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                 d   t         s |j                  t        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	exception%s)HAS_PROXMOXER	fail_jsonr
   PROXMOXER_IMP_ERRmoduleproxmoxer_version_connectproxmox_apiversionget	Exception	traceback
format_exc)selfr9   es      r#   __init__zProxmoxAnsible.__init__L   s    !5k!BN_`!2==?	M$$((* 	MY5I5I5KLL	Ms   $A4 4	B/=(B**B/c                    | 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=| j                  t        d      k  r| j                   j	                  d       ||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passwordz1.1.0z=Using "token_name" and "token_value" require proxmoxer>=1.1.0
token_nametoken_value
verify_sslr5   r2   )	r9   paramsr:   r   r7   r   r?   r@   rA   )
rB   r   r   r   r   r   r   r   	auth_argsrC   s
             r#   r;   zProxmoxAnsible._connectY   sC   ;;%%j1;;%%j1;;%%j1{{)).9{{)).9;;--.@A++,<=X&	 (If$0Ij!%%W(==%%&ef&2Il#'7Im$	RhO>OYOO 	RKK!!dQh):N:N:P!QQ	Rs   D 	E2EE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r3   )r<   r=   r>   r   r?   r9   r7   )rB   
apiversionrC   s      r#   r=   zProxmoxAnsible.versiont   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: %srO   r   )r<   nodesr>   r?   r9   r7   )rB   rR   nrS   rC   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: %srO   )r<   clusternextidr>   r?   r9   r7   )rB   rC   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 : rO   zNo VM with name %s foundr&   z5Multiple VMs with name %s found, provide vmid insteadr   )r<   rW   	resourcesr>   r?   r9   r7   len)rB   r]   ignore_missingchoose_first_if_multipler[   vmsrC   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_   rO   r   z)VM with vmid %s does not exist in cluster)r<   rW   r`   r>   r   r?   r9   r7   )rB   r^   rb   r[   rd   rC   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                    	 | j                   j                  |      j                  |      j                  j	                         }|d   dk(  xr |d   dk(  S # t
        $ r,}| j                  j                  d|d|       Y d }~y d }~ww xY w)Nstatusstopped
exitstatusOK)Unable to retrieve API task ID from node r_   rO   r<   rS   tasksri   r>   r?   r9   r7   rB   rR   taskidri   rC   s        r#   api_task_okzProxmoxAnsible.api_task_ok   s    	e%%++D177?FFJJLF(#y0QVL5IT5QQ 	eKK!![_ab&c!dd	e   AA 	B"BB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
        ri   rj   rk   rl   rm   r_   rO   Nrn   rp   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rs   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.
        rm   r_   rO   Nri   rj   rk   rl   )TNFr&   r   )r<   rS   ro   ri   r>   r?   r9   r7   r/   TASK_TIMED_OUTr   )rB   	node_nametask_idtimeoutri   rC   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: rO   N)r<   poolsr>   r?   r9   r7   )rB   poolidrC   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_   rO   N)r<   storager>   r?   r9   r7   )rB   r   rC   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_   rO   )r<   rS   r   r   r>   r?   r9   r7   )rB   rR   r   r   r^   rC   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__rw   rD   r;   r=   rU   rY   re   rg   rr   ru   r{   r   r   r   r!   r"   r#   r/   r/   H   sT    (&NMR6W+S Zee4	b	nr"   r/   )
__future__r   r   r   r   __metaclass__r@   timer   r8   r1   r   r   r:   r6   ImportErrorrA   ansible.module_utils.basicr	   r
   Bansible_collections.community.general.plugins.module_utils.versionr   r$   r)   r-   objectr/   r!   r"   r#   <module>r      s}    A @   /$:M J [:)
jV jm  /M,	,,./s   A A&%A&