
    VhU                     .   d dl mZmZmZ eZdZdZd dlm	Z	 	 d dl
Z
dZ	 d dlmZ dZd	Zd Zd
Zg Zg dZg dZej-                  e       ej-                  e       dddZdddZdddZddddddZd d	d
dddZd d	dZg Zej-                  ej=                                ej-                  ej=                                 G d de      Z  G d de!      Z" G d d e!      Z# G d! d"e!      Z$d# Z%d$ Z&e'd%k(  r e&        yy# e$ r dZY w xY w# e$ r dZY w xY w)&    )absolute_importdivisionprint_functiona  
---
module: virt_volume
version_added: '1.4.0'
author:
    - Leonardo Galli (@galli-leo)
    - Niclas Kretschmer (@NK308)
short_description: Manage libvirt volumes inside a storage pool
description:
    - Manage I(libvirt) volumes inside a storage pool.
options:
    name:
        aliases: [ "volume" ]
        description:
            - Name of the volume being managed. Note that the volume must be previously
              defined with xml.
        type: str
    pool:
        required: true
        description:
            - Name of the storage pool, where the volume is located.
        type: str
    state:
        choices: [ "present", "absent", "deleted" ]
        description:
            - Specify which state you want a volume to be in.
            - If C(present), ensure that the volume is present but do not change its
              state; if it's missing, you need to specify xml argument.
            - If C(absent), volume will be removed from I(libvirt) configuration (logically only!).
            - If C(deleted), volume will be wiped clean and then removed.
        type: str
    command:
        choices: [ "create", "create_from", "delete", "download", "info", "list_volumes", "get_xml",
                   "resize", "upload", "wipe", "facts"]
        description:
            - In addition to state management, various non-idempotent commands are available.
              See examples.
        type: str
    mode:
        choices: [ 'new', 'repair', 'resize', 'no_overwrite', 'overwrite', 'normal', 'zeroed' ]
        description:
            - Pass additional parameters to 'wipe' command.
        type: str
extends_documentation_fragment:
    - community.libvirt.virt.options_uri
    - community.libvirt.virt.options_xml
    - community.libvirt.requirements
a  
- name: Define a new storage pool
  community.libvirt.virt_pool:
    command: define
    name: vms
    xml: '{{ lookup("template", "pool/dir.xml.j2") }}'

- name: Build a storage pool if it does not exist
  community.libvirt.virt_pool:
    command: build
    name: vms

- name: Start a storage pool
  community.libvirt.virt_pool:
    command: create
    name: vms

- name: List available pools
  community.libvirt.virt_pool:
    command: list_pools

- name: Get XML data of a specified pool
  community.libvirt.virt_pool:
    command: get_xml
    name: vms

- name: Stop a storage pool
  community.libvirt.virt_pool:
    command: destroy
    name: vms

- name: Delete a storage pool (destroys contents)
  community.libvirt.virt_pool:
    command: delete
    name: vms

- name: Undefine a storage pool
  community.libvirt.virt_pool:
    command: undefine
    name: vms

- name: Gather facts about storage pools. Facts will be available as 'ansible_libvirt_pools'
  community.libvirt.virt_pool:
    command: facts

- name: Gather information about pools managed by 'libvirt' remotely using uri
  community.libvirt.virt_pool:
    command: info
    uri: '{{ item }}'
  with_items: '{{ libvirt_uris }}'
  register: storage_pools

- name: Ensure that a pool is active (needs to be defined and built first)
  community.libvirt.virt_pool:
    state: active
    name: vms

- name: Ensure that a pool is inactive
  community.libvirt.virt_pool:
    state: inactive
    name: vms

- name: Ensure that a given pool will be started at boot
  community.libvirt.virt_pool:
    autostart: true
    name: vms

- name: Disable autostart for a given pool
  community.libvirt.virt_pool:
    autostart: false
    name: vms
)AnsibleModuleNTF)etree      )createcreate_fromdeletedownloadget_xmlresizeuploadwipe)list_volumesfactsinfoinactiveactive)r   r   noyesbuildingrunningdegradedinaccessible)r   r   r	         r      )newrepairr   no_overwrite	overwrite)normalzeroedc                       e Zd Zy)EntryNotFoundN)__name__
__module____qualname__     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/libvirt/plugins/modules/virt_volume.pyr'   r'      s    r,   r'   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)PoolConnectionc                 d    || _         t        j                  |      }|st        d      || _        y Nzhypervisor connection failure)modulelibvirtopen	Exceptionconn)selfurir2   r6   s       r-   __init__zPoolConnection.__init__   s-    ||C ;<<	r,   c                     | j                   j                         }|dk(  r|S |D ]  }|j                         |k(  s|c S  t        d|z        )Nzstorage pool %s not found)r6   listAllStoragePoolsnamer'   r7   entryidresultsentrys       r-   
find_entryzPoolConnection.find_entry   sW     ))//1b=N 	Ezz|w&	 7'ABBr,   c                 @   | j                   j                  s| j                  |      j                         S 	 | j                  |      j	                         }|s| j                   j                  d      S y # t
        $ r | j                   j                  d      cY S w xY wNT)changed)r2   
check_moderB   r
   isActiver5   	exit_jsonr7   r?   states      r-   r
   zPoolConnection.create   s    {{%%??7+2244;099; {{,,T,::   ;{{,,T,::;s   A5 5%BBc                     | j                   j                  s| j                  |      j                         S | j                  |      j	                         r| j                   j                  d      S y rD   )r2   rF   rB   destroyrG   rH   r7   r?   s     r-   rL   zPoolConnection.destroy   sY    {{%%??7+3355w'002{{,,T,:: 3r,   c                     | j                   j                  s| j                  |      j                         S | j                  |      s| j                   j	                  d      S y rD   )r2   rF   rB   undefinerH   rM   s     r-   rO   zPoolConnection.undefine   sP    {{%%??7+4466??7+{{,,T,:: ,r,   c                 N    |j                         }t        j                  |d      S NunknownrG   ENTRY_STATE_ACTIVE_MAPgetr7   rA   rJ   s      r-   get_status2zPoolConnection.get_status2  !     %))%;;r,   c                 N   | j                   j                  s5| j                  |      j                         }t        j                  |d      S 	 | j                  |      j                         }t        j                  |d      S # t        $ r t        j                  dd      cY S w xY wNrR   r   r2   rF   rB   rG   rT   rU   r5   rI   s      r-   
get_statuszPoolConnection.get_status      {{%%OOG,557E)--eY??I099;-11%CC I-11*iHHI   4B B$#B$c                 @    | j                  |      j                         S NrB   
UUIDStringrM   s     r-   get_uuidzPoolConnection.get_uuid      w'2244r,   c                 B    | j                  |      j                  d      S Nr   rB   XMLDescrM   s     r-   r   zPoolConnection.get_xml      w'//22r,   c                 @    | j                  |      j                         S r`   rB   r   rM   s     r-   get_infozPoolConnection.get_info      w',,..r,   c                 @    | j                  |      j                         S r`   )rB   numOfVolumesrM   s     r-   get_volume_countzPoolConnection.get_volume_count  s    w'4466r,   c                 @    | j                  |      j                         S r`   )rB   listAllVolumesrM   s     r-   get_volume_nameszPoolConnection.get_volume_names  s    w'6688r,   c                 0   t        j                  | j                  |      j                  d            }|j	                  d      r8g }|j	                  d      D ]"  }|j                  |j                  d             $ 	 S # t        $ r t        d      w xY w)Nr   z/pool/source/devicepathzNo devices specified)	r   
fromstringrB   rh   xpathappendrU   r5   
ValueError)r7   r?   xmlresultdevices        r-   get_deviceszPoolConnection.get_devices   s    tw7??BC99*+F))$9: 2fjj012	5M 	5344	5s   >B   Bc                     t        j                  | j                  |      j                  d            }	 |j	                  d      d   j                  d      }|S # t        $ r t        d      w xY w)Nr   z/pool/source/formattypezFormat not specifiedr   rv   rB   rh   rw   rU   r5   ry   r7   r?   rz   r{   s       r-   
get_formatzPoolConnection.get_format+  sn    tw7??BC	5YY45a8<<VDF   	5344	5   #A A/c                     t        j                  | j                  |      j                  d            }	 |j	                  d      d   j                  d      }|S # t        $ r t        d      w xY w)Nr   z/pool/source/hostr=   zHost not specifiedr   r   s       r-   get_hostzPoolConnection.get_host3  sn    tw7??BC	3YY23A6::6BF   	3122	3r   c                     t        j                  | j                  |      j                  d            }	 |j	                  d      d   j                  d      }|S # t        $ r t        d      w xY w)Nr   z/pool/source/dirru   zSource path not specifiedr   r   s       r-   get_source_pathzPoolConnection.get_source_path;  sn    tw7??BC	:YY121599&AF   	:899	:r   c                     t        j                  | j                  |      j                  d            }	 |j	                  d      d   j
                  }|S # t        $ r t        d      w xY w)Nr   z/pool/target/pathzTarget path not specified)r   rv   rB   rh   rw   textr5   ry   r   s       r-   get_pathzPoolConnection.get_pathC  si    tw7??BC	:YY23A6;;F   	:899	:s   A A*c                     t        j                  | j                  |      j                  d            }|j	                  d      S )Nr   r   )r   rv   rB   rh   rU   r7   r?   rz   s      r-   get_typezPoolConnection.get_typeK  s4    tw7??BCwwvr,   c                 &   | j                   j                  s | j                  |      j                  |      S 	 | j                  |      }|s| j                   j                  d      S y # t        $ r | j                   j                  d      cY S w xY wrD   )r2   rF   rB   buildr5   rH   r7   r?   flagsrJ   s       r-   r   zPoolConnection.buildO  s    {{%%??7+11%88;0 {{,,T,::   ;{{,,T,::;   A( (%BBc                 &   | j                   j                  s | j                  |      j                  |      S 	 | j                  |      }|r| j                   j                  d      S y # t        $ r | j                   j                  d      cY S w xY wrD   )r2   rF   rB   r   r5   rH   r   s       r-   r   zPoolConnection.deleteZ  s    {{%%??7+22599;0 {{,,T,::   ;{{,,T,::;r   c                 l    | j                  |      j                         }t        j                  |d      S rQ   )rB   	autostartENTRY_STATE_AUTOSTART_MAPrU   rI   s      r-   get_autostartzPoolConnection.get_autostarte  s,    (224(,,UI>>r,   c                    | j                   j                  s| j                  |      j                         S 	 | j                  |      j                         S # t        $ r | j                   j                  d      cY S w xY wrD   )r2   rF   rB   r   r5   rH   rM   s     r-   get_autostart2zPoolConnection.get_autostart2i  sk    {{%%??7+5577;w/99;; ;{{,,T,::;s   A %A>=A>c                 Z   | j                   j                  s | j                  |      j                  |      S 	 | j                  |      j	                         }t        |      |k7  r| j                   j                  d      S y # t
        $ r | j                   j                  d      cY S w xY wrD   )r2   rF   rB   setAutostartr   r5   rH   bool)r7   r?   valrJ   s       r-   set_autostartzPoolConnection.set_autostartr  s    {{%%??7+88==;0::< E{c!{{,,T,:: "  ;{{,,T,::;s   B %B*)B*c                 @    | j                  |      j                         S r`   )rB   refreshrM   s     r-   r   zPoolConnection.refresh}  s    w'//11r,   c                 l    | j                  |      j                         }t        j                  |d      S rQ   )rB   isPersistentENTRY_STATE_PERSISTENT_MAPrU   rI   s      r-   get_persistentzPoolConnection.get_persistent  s,    (557)--eY??r,   c                     | j                   j                  s| j                  j                  |      S 	 | j	                  |       y # t
        $ r | j                   j                  d      cY S w xY wrD   )r2   rF   r6   storagePoolDefineXMLrB   r5   rH   r   s      r-   define_from_xmlzPoolConnection.define_from_xml  s\    {{%%9911#66;( ;{{,,T,::;s   A %A-,A-N)r(   r)   r*   r9   rB   r
   rL   rO   rW   r\   rc   r   rl   rp   rs   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r,   r-   r/   r/      s    	C	;;;<	I53/79	5	;	;?;	;2@;r,   r/   c                   N    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)LibvirtConnectionc                     || _         t        j                  |      }|st        d      || _        || _        t        ||      | _        | j                  j                  |      | _	        y r1   )
r2   r3   r4   r5   r6   poolidr/   poolConnrB   pool)r7   r8   r2   r   r6   s        r-   r9   zLibvirtConnection.__init__  sX    ||C ;<<	&sF3MM,,V4	r,   c                     g }| j                   j                         D ]0  }|dk(  r|j                  |       |j                         |k(  s.|c S  |dk(  r|S t	        d|z        )Nr;   zvolume %s not found)r   rr   rx   r=   r'   r>   s       r-   rB   zLibvirtConnection.find_entry  sj     YY--/ 	E"}u%(		 b=N1G;<<r,   c                 8    | j                   j                  |      S r`   )r   	createXMLr   s      r-   r
   zLibvirtConnection.create  s    yy""3''r,   c                     | j                  |      }	 | j                  j                  ||      S # t        $ r | j                  j                  d      cY S w xY wrD   )rB   r   createXMLFromr5   r2   rH   )r7   r?   rz   to_clonefrom_vols        r-   r   zLibvirtConnection.create_from  sS    ??8,	799**399 	7;;(((66	7s   / %AAc                     | j                   j                  s| j                  |      j                         S | j                  |      r| j                   j	                  d      S y rD   )r2   rF   rB   r   rH   rM   s     r-   r   zLibvirtConnection.delete  sP    {{%%??7+2244w'{{,,T,:: (r,   c                     | j                   j                  s| j                  |      j                         S | j                  |      s| j                   j	                  d      S y rD   )r2   rF   rB   r   rH   )r7   r?   modes      r-   r   zLibvirtConnection.wipe  sP    {{%%??7+0022??7+{{,,T,:: ,r,   c                 N    |j                         }t        j                  |d      S rQ   rS   rV   s      r-   rW   zLibvirtConnection.get_status2  rX   r,   c                 N   | j                   j                  s5| j                  |      j                         }t        j                  |d      S 	 | j                  |      j                         }t        j                  |d      S # t        $ r t        j                  dd      cY S w xY wrZ   r[   rI   s      r-   r\   zLibvirtConnection.get_status  r]   r^   c                 @    | j                  |      j                         S r`   ra   rM   s     r-   rc   zLibvirtConnection.get_uuid  rd   r,   c                 B    | j                  |      j                  d      S rf   rg   rM   s     r-   r   zLibvirtConnection.get_xml  ri   r,   c                 @    | j                  |      j                         S r`   rk   rM   s     r-   rl   zLibvirtConnection.get_info  rm   r,   N)r(   r)   r*   r9   rB   r
   r   r   r   rW   r\   rc   r   rl   r+   r,   r-   r   r     s:    5= (7;;<	I53/r,   r   c                   X    e Zd Zd Zd ZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZddZy)
VirtVolumec                     || _         || _        || _        t        | j                  | j                   | j                        | _        y r`   )r2   r8   r   r   r6   )r7   r8   r2   r   s       r-   r9   zVirtVolume.__init__  s3    %dhhT[[I	r,   c                 8    | j                   j                  |      S r`   )r6   rB   rM   s     r-   
get_volumezVirtVolume.get_volume      yy##G,,r,   Nc                     g }| j                   j                  d      D ]!  }|j                  |j                                # |S )Nr;   )r6   rB   rx   r=   )r7   rJ   r@   rA   s       r-   r   zVirtVolume.list_volumes  s<    YY))"- 	)ENN5::<(	)r,   c                     g }| j                         D ]3  }| j                  j                  |      }|j                  |d|       5 |S )N )
list_poolsr6   r\   rx   )r7   r@   rA   state_blurbs       r-   rJ   zVirtVolume.state  sI    __& 	;E))..u5KNNe[9:	; r,   c                 :    | j                   j                  ||      S r`   )r6   r
   r   s      r-   r
   zVirtVolume.create  s    yy--r,   c                 <    | j                   j                  |||      S r`   )r6   r   )r7   r?   rz   r   s       r-   r   zVirtVolume.create_from  s    yy$$Wc8<<r,   c                 8    | j                   j                  |      S r`   )r6   r   rM   s     r-   r   zVirtVolume.delete  s    yy((r,   c                 8    | j                   j                  |      S r`   )r6   r\   rM   s     r-   statuszVirtVolume.status   r   r,   c                 8    | j                   j                  |      S r`   )r6   r   rM   s     r-   r   zVirtVolume.get_xml  s    yy  ))r,   c                 b    | j                   j                  |t        j                  |d            S rf   )r6   r   ENTRY_DELETE_FLAGS_MAPrU   )r7   r?   r   s      r-   r   zVirtVolume.wipe  s%    yy~~g'='A'A%'KLLr,   c                 &    | j                  d      S )Nr   )
facts_moder   )r7   s    r-   r   zVirtVolume.info	  s    zzVz,,r,   c                 N   t               }| j                         D ]  }t               ||<   | j                  j                  |      rL| j                  j	                  |      }t
        j                  |d   d      t        |d         t        |d         t        |d         d||<   | j                  j                  |      ||   d<   | j                  j                  |      ||   d<   | j                  j                  |      ||   d	<   | j                  j                  |      ||   d
<   | j                  j                  |      ||   d<   | j                  j                  |      ||   d<   | j                  j                  |      j                         ri| j                  j                  |      ||   d<   t!               ||   d<   | j                  j#                  |      D ]  }||   d   j%                  |        nd||   d<   	 | j                  j'                  |      ||   d<   	 | j                  j+                  |      ||   d<   	 | j                  j-                  |      ||   d<   	 | j                  j/                  |      }|||   d<   y| j                  j                  |      ||   d	<    t               }|dk(  rt               |d<   ||d   d<   |S |dk(  r||d<   |S # t(        $ r Y w xY w# t(        $ r Y w xY w# t(        $ r Y w xY w# t(        $ r Y w xY w)Nr   rR   r   r	   r   )r   
size_total	size_usedsize_availabler   
persistentrJ   ru   r   uuidvolume_countvolumesr;   hostsource_pathformatdevicesr   ansible_factsansible_libvirt_poolsr   pools)dictr   r6   rB   rl   ENTRY_STATE_INFO_MAPrU   strr   r   r\   r   r   rc   rG   rp   listrs   rx   r   ry   r   r   r}   )r7   r   r@   rA   datavolumer   r   s           r-   r   zVirtVolume.facts  s   &__& 3	FE!VGENyy##E*yy))%0 366tAw	J"%d1g,!$T!W&)$q'l	" /3ii.E.Ee.L{+/3yy/G/G/N|,*.))*>*>u*Ew')-););E)Bv&)-););E)Bv&)-););E)Bv&99''.77959YY5O5OPU5VGEN>204GEN9-"&))"<"<U"C Ay188@A 68GEN>2-1YY-?-?-FGEN6*48II4M4Me4TGEN=1/3yy/C/CE/JGEN8,"ii33E:G07GEN9-
 +/))*>*>u*Ew'g3	Fj  %)VE/">EE/"#:;  6!$E'N9 " 
 " 
 "  " sH   	!K*+!K9!L/#L*	K65K69	LL	LL	L$#L$r`   r   )r(   r)   r*   r9   r   r   rJ   r
   r   r   r   r   r   r   r   r+   r,   r-   r   r     s@    J-.=)-*M-=r,   r   c                 *   | j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }| j                   j                  dd       }t        || |      }i }	|r3|dk(  r.|j                  |	      }	t	        |	t
              s||	i}	t        |	fS |r|s| j                  d
       d|	d<   |dv rS||j                         vr9|r|j                  ||       d|d}	t        |	fS | j                  d|z   dz          t        |	fS |dv r5|j                         }
||
v rd|	d<   |j                  |      |	d<   t        |	fS |dv rG|j                         }
||
v r+|j                  ||       d|	d<   |j                  |      |	d<   t        |	fS | j                  d       t        |	fS |r8|t        v r|s| j                  d|z         |dk(  r.|s| j                  d       	 |j                  |       t        |	fS |dk(  r0|j                  ||      }	t	        |	t
              sdd||	i}	t        |	fS |dk(  r0|j                  ||      }	t	        |	t
              sdd||	i}	t        |	fS  t!        ||      |      }	t	        |	t
              s||	i}	t        |	fS t#        ||      r- t!        ||             }	t	        |	t
              s||	i}	t        |	fS | j                  d|z         | j                  d       y # t        $ r! |j                  ||       d|d}	Y t        |	fS w xY w)NrJ   r=   r   commandr8   rz   r   r   )rJ   z&state change requires a specified namemsgFrE   )presentT)rE   createdzvolume 'z$' not present, but xml not specified)absentr   )deletedzunexpected statez%s requires 1 argument: namedefinezdefine requires xml argumentr   r   zCommand %s not recognizedz3expected state or command parameter to be specified)paramsrU   r   r   
isinstancer   VIRT_SUCCESS	fail_jsonr
   r   r   ENTRY_COMMANDSget_poolr'   r   r   getattrhasattr)r2   rJ   r=   r   r   r8   rz   r   vresentriess              r-   corer   L  s   MMgt,E==VT*D==VT*Dmm	40G
--

E4
(C
--

E4
(C==VT*D3%A
CN*nn5n)#t$C.CS  !IJIK1>>++HHT3'&*t<C" S   $$d):=c)c$d S   j nn&Gw!%IXXd^E
 S   k!nn&GwtT"!%IXXd^E
 S   !34S  n$  %Cg%M N("$$)G$H=JJt$ $S((G#ggdD)!#t,$dGS9C#S((H$hhtT*!#t,$dGS9C#S((%'!W%d+Cc4(n$$Q %'!W%'Cc4(n$$ !<w!FG
NO9 % =HHT3'&*t<C#S((=s   M( ( NNc                     t        t        t        dg      t        d      t        g d      t        t              t        d      t               t        t              	      d
      } t        s| j                  d       t        s| j                  d       t        }	 t        |       \  }}|dk7  r| j                  |       y  | j                  di  y # t        $ r%}| j                  t        |             Y d }~Ud }~ww xY w)Nr   )aliasesT)required)r   r   r   )choiceszqemu:///system)default)r=   r   rJ   r   r8   rz   r   )argument_specsupports_check_modez?The `libvirt` module is not importable. Check the requirements.r   z<The `lxml` module is not importable. Check the requirements.r   )rcr   r+   )r   r   ALL_COMMANDS	ALL_MODESHAS_VIRTr   HAS_XMLr   r   r5   r   rH   )r2   r  r{   es       r-   mainr
    s    xj)t$?@.-.i(
 !F Q 	 	
 N 	 	
 
B%&\
F 
QwBF+"6"  %SV$$%s   $C 	D'DD__main__)(
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   r3   r  ImportErrorlxmlr   r  VIRT_FAILEDr   VIRT_UNAVAILABLEr  r   HOST_COMMANDSextendrT   r   r   r   ENTRY_BUILD_FLAGS_MAPr   r  keysr5   r'   objectr/   r   r   r   r
  r(   r+   r,   r-   <module>r     s   A @/bGR 5 H G  1   N #   M "            
 	 	  &++- . 	  ',,. /	I 	~;V ~;BN/ N/bj jZTPn"#J zF A  H  Gs"   C= D
 =DD
DD