
    Vh                         d dl mZmZmZ eZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ ej$                  Zej&                  Z G d de      Z G d	 d
e      Z G d de      Zd Zd Zy)    )absolute_importdivisionprint_functionN)generic_urlparse)urlparse)http_client)to_textc                       e Zd Zd Zd Zy)UnixHTTPConnectionc                 >    t         j                  | d       || _        y )N	localhost)HTTPConnection__init__path)selfr   s     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/module_utils/lxd.pyr   zUnixHTTPConnection.__init__   s    k2	    c                     t        j                   t         j                  t         j                        }|j                  | j                         || _        y N)socketAF_UNIXSOCK_STREAMconnectr   sock)r   r   s     r   r   zUnixHTTPConnection.connect   s3    }}V^^V-?-?@TYY	r   N)__name__
__module____qualname__r   r    r   r   r   r      s    r   r   c                       e Zd Zd Zy)LXDClientExceptionc                      || _         || _        y r   )msgkwargs)r   r"   r#   s      r   r   zLXDClientException.__init__&   s    r   N)r   r   r   r   r   r   r   r    r    %   s    r   r    c                   @    e Zd ZddZd	dZd Zd
dZd Zed        Z	y)	LXDClientNc                    || _         || _        g | _        |j                  d      r|| _        || _        t        t        | j                               }t        j                  t        j                  j                        }|r|j                  |       ||_        |j                  ||       t        |j!                  d      |      | _        y|j                  d      r|t%        d      d }	t'        |	      | _        yt)        d      )	a  LXD Client.

        :param url: The URL of the LXD server. (e.g. unix:/var/lib/lxd/unix.socket or https://127.0.0.1)
        :type url: ``str``
        :param key_file: The path of the client certificate key file.
        :type key_file: ``str``
        :param cert_file: The path of the client certificate file.
        :type cert_file: ``str``
        :param debug: The debug flag. The request and response are stored in logs when debug is true.
        :type debug: ``bool``
        :param server_cert_file: The path of the server certificate file.
        :type server_cert_file: ``str``
        :param server_check_hostname: Whether to check the server's hostname as part of TLS verification.
        :type debug: ``bool``
        zhttps:)cafile)keyfilenetloc)contextzunix:Nz"URL scheme must be unix: or https:)urldebuglogs
startswith	cert_filekey_filer   r   sslcreate_default_contextPurposeSERVER_AUTHload_verify_locationscheck_hostnameload_cert_chainHTTPSConnectionget
connectionlenr   r    )
r   r+   r0   r/   r,   server_cert_fileserver_check_hostnamepartsctxunix_socket_paths
             r   r   zLXDClient.__init__,   s      
	>>(#&DN$DM$Xdhh%78E,,S[[-D-DEC))1A)B!6C	8<-eii.A3ODO^^G$"3w<=101ABDO$%IJJr   c                    | j                  |||||      }|d   dk(  rmdj                  |d         }| j                  d|      }|r)|d   d   d	k(  r| j                  d|      }|d   d   d	k(  r|d   d   d
k7  r| j                  |       |S )N)	body_jsonok_error_codestimeouttypeasyncz{0}/wait	operationGETmetadatastatusRunningSuccess)_send_requestformat_raise_err_from_json)r   methodr+   rB   rC   rD   wait_for_container	resp_jsons           r   dozLXDClient.doP   s    &&vsiXfpw&x	V'##Ik$:;C**5#6I!
+H5B $ 2 25# >I  
+H5B$X.);)))4r   c                 4    d|d}| j                  dd|      S )Nclient)rE   passwordPOSTz/1.0/certificates)rB   )rM   )r   trust_passwordrB   s      r   authenticatezLXDClient.authenticate\   s%    %>B	!!&*=!SSr   c                    	 t        j                  |      }| j                  j                  |||       | j                  j	                         }|j                         }t        |d      }t        j                  |      }	| j                  j                  d||||dd|	id       |	j                  dd       }
|
d	k(  r&|	|	d
   |v r|	S |	d	   dk(  r|	S | j                  |	       |	S # t        j                  $ r}t        d|      d }~ww xY w)N)bodysurrogate_or_strict)errorszsent request)rP   r+   jsonrD   r^   )rE   requestresponserE   error
error_codez"Certificate already in trust storez cannot connect to the LXD server)err)r^   dumpsr:   r_   getresponsereadr	   loadsr-   appendr9   rO   r   ra   r    )r   rP   r+   rB   rC   rD   r[   resp	resp_datarR   	resp_typees               r   rM   zLXDClient._send_request`   s   	P::i(DOO##FCd#;??..0D		I	2GHI

9-III&&,S)X_`#Y/ 
 "fd3IG#!-)L2I^2[$$W%)MM$$)))4|| 	P$%GQOO	Ps$   CC& 		C& C& &D9DDc                 r    i }| j                   r| j                  |d<   t        | j                  |      fi |)Nr-   )r,   r-   r    _get_err_from_resp_json)r   rR   
err_paramss      r   rO   zLXDClient._raise_err_from_jsonx   s7    
::!%Jv !=!=i!HWJWWr   c                 ~    d }| j                  dd       }||j                  dd       }|| j                  dd       }|S )NrI   rc   ra   )r9   )rR   rc   rI   s      r   rn   z!LXDClient._get_err_from_resp_json~   sG    ==T2,,ud+C;--.C
r   )NNFNT)NNNN)NNN)
r   r   r   r   rS   rY   rM   rO   staticmethodrn   r   r   r   r%   r%   +   s4    "KH
TP0X  r   r%   c                  @    t         j                  j                  d      S )Nz~/.config/lxc/client.keyosr   
expanduserr   r   r   default_key_filerv          77899r   c                  @    t         j                  j                  d      S )Nz~/.config/lxc/client.crtrs   r   r   r   default_cert_filery      rw   r   )
__future__r   r   r   rE   __metaclass__rt   r   r1   r^   ansible.module_utils.urlsr   +ansible.module_utils.six.moves.urllib.parser   ansible.module_utils.six.movesr   +ansible.module_utils.common.text.convertersr	   r   r8   r   	Exceptionr    objectr%   rv   ry   r   r   r   <module>r      sv    C B 
  
  6 @ 6 ? ++--  [ [|::r   