
    VhU                        d dl mZmZmZ eZdZdZ	 d dlZdZ		 d dlmZ dZd dlm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#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_pool
author: "Maciej Delmanowski (@drybjed)"
short_description: Manage libvirt storage pools
description:
    - Manage I(libvirt) storage pools.
options:
    name:
        aliases: [ "pool" ]
        description:
            - Name of the storage pool being managed. Note that pool must be previously
              defined with xml.
        type: str
    state:
        choices: [ "active", "inactive", "present", "absent", "undefined", "deleted" ]
        description:
            - Specify which state you want a storage pool to be in.
              If 'active', pool will be started.
              If 'present', ensure that pool is present but do not change its
              state; if it is missing, you need to specify xml argument.
              If 'inactive', pool will be stopped.
              If 'undefined' or 'absent', pool will be removed from I(libvirt) configuration.
              If 'deleted', pool contents will be deleted and then pool undefined.
        type: str
    command:
        choices: [ "define", "build", "create", "start", "stop", "destroy",
                   "delete", "undefine", "get_xml", "list_pools", "facts",
                   "info", "status", "refresh" ]
        description:
            - In addition to state management, various non-idempotent commands are available.
              See examples.
        type: str
    autostart:
        type: bool
        description:
            - Specify if a given storage pool should be started automatically on system boot.
    mode:
        choices: [ 'new', 'repair', 'resize', 'no_overwrite', 'overwrite', 'normal', 'zeroed' ]
        description:
            - Pass additional parameters to 'build' or 'delete' commands.
        type: str
extends_documentation_fragment:
    - community.libvirt.virt.options_uri
    - community.libvirt.virt.options_xml
    - community.libvirt.requirements
requirements:
    - "python-lxml"
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

# Gather facts about storage pools
# Facts will be available as 'ansible_libvirt_pools'
- name: Gather facts about storage 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
NTF)etree)AnsibleModule      )createstatusstartstopbuilddeleteundefinedestroyget_xmldefinerefresh)
list_poolsfactsinfoinactiveactive)r   r   noyesbuildingrunningdegradedinaccessible)r   r   r	         r!      )newrepairresizeno_overwrite	overwrite)normalzeroedc                       e Zd Zy)EntryNotFoundN)__name__
__module____qualname__     o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/libvirt/plugins/modules/virt_pool.pyr+   r+      s    r0   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)LibvirtConnectionc                 d    || _         t        j                  |      }|st        d      || _        y )Nzhypervisor connection failure)modulelibvirtopen	Exceptionconn)selfurir5   r9   s       r1   __init__zLibvirtConnection.__init__   s-    ||C ;<<	r0   c                     | j                   j                         }|dk(  r|S |D ]  }|j                         |k(  s|c S  t        d|z        )Nzstorage pool %s not found)r9   listAllStoragePoolsnamer+   )r:   entryidresultsentrys       r1   
find_entryzLibvirtConnection.find_entry   sW     ))//1b=N 	Ezz|w&	 7'ABBr0   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)r5   
check_moderD   r
   isActiver8   	exit_jsonr:   rA   states      r1   r
   zLibvirtConnection.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 rF   )r5   rH   rD   r   rI   rJ   r:   rA   s     r1   r   zLibvirtConnection.destroy   sY    {{%%??7+3355w'002{{,,T,:: 3r0   c                     | j                   j                  s| j                  |      j                         S | j                  |      s| j                   j	                  d      S y rF   )r5   rH   rD   r   rJ   rN   s     r1   r   zLibvirtConnection.undefine   sP    {{%%??7+4466??7+{{,,T,:: ,r0   c                 N    |j                         }t        j                  |d      S Nunknown)rI   ENTRY_STATE_ACTIVE_MAPget)r:   rC   rL   s      r1   get_status2zLibvirtConnection.get_status2  s!     %))%;;r0   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   )r5   rH   rD   rI   rS   rT   r8   rK   s      r1   
get_statuszLibvirtConnection.get_status
  s    {{%%OOG,557E)--eY??I099;-11%CC I-11*iHHIs   4B B$#B$c                 @    | j                  |      j                         S N)rD   
UUIDStringrN   s     r1   get_uuidzLibvirtConnection.get_uuid  s    w'2244r0   c                 B    | j                  |      j                  d      S Nr   )rD   XMLDescrN   s     r1   r   zLibvirtConnection.get_xml  s    w'//22r0   c                 @    | j                  |      j                         S rY   )rD   r   rN   s     r1   get_infozLibvirtConnection.get_info  s    w',,..r0   c                 @    | j                  |      j                         S rY   )rD   numOfVolumesrN   s     r1   get_volume_countz"LibvirtConnection.get_volume_count  s    w'4466r0   c                 @    | j                  |      j                         S rY   )rD   listAllVolumesrN   s     r1   get_volume_namesz"LibvirtConnection.get_volume_names!  s    w'6688r0   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   
fromstringrD   r^   xpathappendrT   r8   
ValueError)r:   rA   xmlresultdevices        r1   get_deviceszLibvirtConnection.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   ri   rD   r^   rj   rT   r8   rl   r:   rA   rm   rn   s       r1   
get_formatzLibvirtConnection.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 specifiedrs   rt   s       r1   get_hostzLibvirtConnection.get_host7  sn    tw7??BC	3YY23A6::6BF   	3122	3rv   c                     t        j                  | j                  |      j                  d            }	 |j	                  d      d   j                  d      }|S # t        $ r t        d      w xY w)Nr   z/pool/source/dirrh   zSource path not specifiedrs   rt   s       r1   get_source_pathz!LibvirtConnection.get_source_path?  sn    tw7??BC	:YY121599&AF   	:899	:rv   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   ri   rD   r^   rj   textr8   rl   rt   s       r1   get_pathzLibvirtConnection.get_pathG  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   rr   )r   ri   rD   r^   rT   r:   rA   rm   s      r1   get_typezLibvirtConnection.get_typeO  s4    tw7??BCwwvr0   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rF   )r5   rH   rD   r   r8   rJ   r:   rA   flagsrL   s       r1   r   zLibvirtConnection.buildS  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rF   )r5   rH   rD   r   r8   rJ   r   s       r1   r   zLibvirtConnection.delete^  s    {{%%??7+22599;0 {{,,T,::   ;{{,,T,::;r   c                 l    | j                  |      j                         }t        j                  |d      S rQ   )rD   	autostartENTRY_STATE_AUTOSTART_MAPrT   rK   s      r1   get_autostartzLibvirtConnection.get_autostarti  s,    (224(,,UI>>r0   c                    | j                   j                  s| j                  |      j                         S 	 | j                  |      j                         S # t        $ r | j                   j                  d      cY S w xY wrF   )r5   rH   rD   r   r8   rJ   rN   s     r1   get_autostart2z LibvirtConnection.get_autostart2m  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rF   )r5   rH   rD   setAutostartr   r8   rJ   bool)r:   rA   valrL   s       r1   set_autostartzLibvirtConnection.set_autostartv  s    {{%%??7+88==;0::< E{c!{{,,T,:: "  ;{{,,T,::;s   B %B*)B*c                 @    | j                  |      j                         S rY   )rD   r   rN   s     r1   r   zLibvirtConnection.refresh  s    w'//11r0   c                 l    | j                  |      j                         }t        j                  |d      S rQ   )rD   isPersistentENTRY_STATE_PERSISTENT_MAPrT   rK   s      r1   get_persistentz LibvirtConnection.get_persistent  s,    (557)--eY??r0   c                     | j                   j                  s| j                  j                  |      S 	 | j	                  |       y # t
        $ r | j                   j                  d      cY S w xY wrF   )r5   rH   r9   storagePoolDefineXMLrD   r8   rJ   r   s      r1   define_from_xmlz!LibvirtConnection.define_from_xml  s\    {{%%9911#66;( ;{{,,T,::;s   A %A-,A-N)r,   r-   r.   r<   rD   r
   r   r   rU   rW   r[   r   r`   rc   rf   rp   ru   rx   rz   r}   r   r   r   r   r   r   r   r   r   r/   r0   r1   r3   r3      s    	C	;;;<	I53/79	5	;	;?;	;2@;r0   r3   c                       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 Zd Zd Zd Zd Zd Zd Zd ZddZy)VirtStoragePoolc                 j    || _         || _        t        | j                  | j                         | _        y rY   )r5   r;   r3   r9   )r:   r;   r5   s      r1   r<   zVirtStoragePool.__init__  s&    %dhh<	r0   c                 8    | j                   j                  |      S rY   )r9   rD   rN   s     r1   get_poolzVirtStoragePool.get_pool      yy##G,,r0   Nc                 
   g }| j                   j                  d      D ]b  }|r?|| j                   j                  |      k(  s$|j                  |j	                                D|j                  |j	                                d |S )Nr>   )r9   rD   rU   rk   r@   )r:   rL   rB   rC   s       r1   r   zVirtStoragePool.list_pools  sj    YY))"- 	-EDII11%88NN5::<0uzz|,	- r0   c                     g }| j                         D ]3  }| j                  j                  |      }|j                  |d|       5 |S )N )r   r9   rW   rk   )r:   rB   rC   state_blurbs       r1   rL   zVirtStoragePool.state  sI    __& 	;E))..u5KNNe[9:	; r0   c                 :    | j                   j                  |d      S )NTr9   r   rN   s     r1   r   zVirtStoragePool.autostart  s    yy&&w55r0   c                 8    | j                   j                  |      S rY   )r9   r   rN   s     r1   r   zVirtStoragePool.get_autostart  s    yy''00r0   c                 :    | j                   j                  ||      S rY   r   rK   s      r1   r   zVirtStoragePool.set_autostart  s    yy&&w66r0   c                 8    | j                   j                  |      S rY   r9   r
   rN   s     r1   r
   zVirtStoragePool.create      yy((r0   c                 8    | j                   j                  |      S rY   r   rN   s     r1   r   zVirtStoragePool.start  r   r0   c                 8    | j                   j                  |      S rY   r9   r   rN   s     r1   r   zVirtStoragePool.stop      yy  ))r0   c                 8    | j                   j                  |      S rY   r   rN   s     r1   r   zVirtStoragePool.destroy  r   r0   c                 8    | j                   j                  |      S rY   )r9   r   rN   s     r1   r   zVirtStoragePool.undefine  s    yy!!'**r0   c                 8    | j                   j                  |      S rY   )r9   rW   rN   s     r1   r   zVirtStoragePool.status  r   r0   c                 8    | j                   j                  |      S rY   )r9   r   rN   s     r1   r   zVirtStoragePool.get_xml  r   r0   c                 :    | j                   j                  ||      S rY   )r9   r   r   s      r1   r   zVirtStoragePool.define  s    yy((#66r0   c                 b    | j                   j                  |t        j                  |d            S r]   )r9   r   ENTRY_BUILD_FLAGS_MAPrT   r:   rA   r   s      r1   r   zVirtStoragePool.build  s%    yyw(=(A(A%(KLLr0   c                 b    | j                   j                  |t        j                  |d            S r]   )r9   r   ENTRY_DELETE_FLAGS_MAPrT   r   s      r1   r   zVirtStoragePool.delete  s'    yy)?)C)CE1)MNNr0   c                 8    | j                   j                  |      S rY   )r9   r   rN   s     r1   r   zVirtStoragePool.refresh  r   r0   c                 &    | j                  d      S )Nr   )
facts_moder   )r:   s    r1   r   zVirtStoragePool.info  s    zzVz,,r0   c                    t               }| j                         D ]  }t               ||<   | j                  j                  |      r[| 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                  |      j                         rw| j                  j                  |      ||   d<   t               ||   d<   | j                  j!                  |      D ]'  }||   d   j#                  |j%                                ) nd||   d<   	 | j                  j'                  |      ||   d<   	 | j                  j+                  |      ||   d<   	 | j                  j-                  |      ||   d<   	 | j                  j/                  |      ||   d<   	 | j                  j1                  |      }|||   d<   | 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# t(        $ r Y &w xY w)Nr   rR   r   r	   r    )r   
size_total	size_usedsize_availabler   
persistentrL   rr   uuidvolume_countvolumesr>   rh   hostsource_pathformatdevicesr   ansible_factsansible_libvirt_poolsr   pools)dictr   r9   rD   r`   ENTRY_STATE_INFO_MAPrT   strr   r   rW   r   r[   rI   rc   listrf   rk   r@   r}   rl   rx   rz   ru   rp   )r:   r   rB   rC   datavolumer   r   s           r1   r   zVirtStoragePool.facts  s:   &__& 7	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&99''.77959YY5O5OPU5VGEN>204GEN9-"&))"<"<U"C Hy188GH 68GEN>2-1YY-?-?-FGEN6*-1YY-?-?-FGEN6*48II4M4Me4TGEN=1/3yy/C/CE/JGEN8,"ii33E:G07GEN9-
 +/))*>*>u*Ew'o7	Fr  %)VE/">EE/"#:;  6!$E'NC " 
 " 
 " 
 "  " sZ   6!K9!L:!L!L&>#L59	LL	LL	L#"L#&	L21L25	MMrY   r   )r,   r-   r.   r<   r   r   rL   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r/   r0   r1   r   r     si    =
-617))**+-*7MO*-Ar0   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 r5|j                  |      dk7  rd|	d<   |j                  |      |	d<   t        |	fS |dv r	 |j                  |       t        |	fS |dv rI|j                         }
||
v r-|j                  |      dk7  rd|	d<   |j                  |      |	d<   t        |	fS |dv rZ|j                         }
||
v r>|j                  |      dk7  r|j                  |       d|	d<   |j                  |      |	d<   t        |	fS |dv rl|j                         }
||
v rP|j                  |      dk7  r|j                  |       |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         ||s| j                  d
       d|	d<   |r3|j)                  |      sd|	d<   |j+                  |d      |	d<   t        |	fS |j)                  |      rd|	d<   |j+                  |d      |	d<   t        |	fS | j                  d!       y # t        $ r; |s| j                  d|z   dz          |j                  ||       d|d}	Y t        |	fS w xY w# t        $ r! |j                  ||       d|d}	Y t        |	fS w xY w)"NrL   r@   commandr;   rm   r   moder   )rL   z&state change requires a specified namemsgFrG   )r   r   Tr   )presentzstorage pool 'z$' not present, but xml not specified)rG   created)r   r   )	undefinedabsent)deletedzunexpected statez%s requires 1 argument: namer   zdefine requires xml argumentr   r   zCommand %s not recognizedz3expected state or command parameter to be specified)paramsrT   r   r   
isinstancer   VIRT_SUCCESS	fail_jsonr   r   r   r+   r   r   r   r   ENTRY_COMMANDSr   getattrhasattrr   r   )r5   rL   r@   r   r;   rm   r   r   vresentriess              r1   corer     s7   MMgt,E==VT*Dmm	40G
--

E4
(C
--

E4
(C!!+t4I==VT*DV$A
CL(lll'#t$C.CS  !IJIJxx~)!%IWWT]E
B S  A k!9

4 < S  1 l"llnGw88D>Z/%)C	N!"4CJ& S  % --llnGw88D>Z/IIdO!%IZZ-E
 S   k!llnGw88D>Z/IIdOt$!%IZZ-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!IJI??4(!%I__T48E
 S  	 t$!%I__T59E
S  
NOi ! 9$$)9D)@Ci)i$js#"&482 S  ;9P % =HHT3'&*t<C#S((=s$   "R ,S :SS S21S2c                     t        t        t        dg      t        g d      t        t              t        d      t               t        d      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)Npool)aliases)r   r   r   r   r   r   )choiceszqemu:///system)defaultr   )rr   )r@   rL   r   r;   rm   r   r   T)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   r8   r   rJ   )r5   r   rn   es       r1   mainr     s    vh'bc.-.'i(
 !F Q 	 	
 N 	 	
 
B%&\
F 
QwBF+"6"  %SV$$%s   $C 	D'DD__main__)'
__future__r   r   r   rr   __metaclass__DOCUMENTATIONEXAMPLESr6   r   ImportErrorlxmlr   r   ansible.module_utils.basicr   VIRT_FAILEDr   VIRT_UNAVAILABLEr   r   HOST_COMMANDSextendrS   r   r   r   r   r   r   keysr8   r+   objectr3   r   r   r   r,   r/   r0   r1   <module>r     s   A @0dIV H G 4  I/   N #   M "            
 	 	  &++- . 	  ',,. /	I 	~; ~;BIf IXsPl"#J zF _  H  Gs"   C2 C? 2C<;C<?D	D	