
    Vh"I                     r    d dl mZmZmZ eZdZdZdZd dl	m
Z
 g dZ G d de
      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona  
module: one_image
short_description: Manages OpenNebula images
description:
  - Manages OpenNebula images.
requirements:
  - pyone
extends_documentation_fragment:
  - community.general.opennebula
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  id:
    description:
      - A O(id) of the image you would like to manage.
    type: int
  name:
    description:
      - A O(name) of the image you would like to manage.
      - Required if O(create=true).
    type: str
  state:
    description:
      - V(present) - state that is used to manage the image.
      - V(absent) - delete the image.
      - V(cloned) - clone the image.
      - V(renamed) - rename the image to the O(new_name).
    choices: ["present", "absent", "cloned", "renamed"]
    default: present
    type: str
  enabled:
    description:
      - Whether the image should be enabled or disabled.
    type: bool
  new_name:
    description:
      - A name that will be assigned to the existing or new image.
      - In the case of cloning, by default O(new_name) will take the name of the origin image with the prefix 'Copy of'.
    type: str
  persistent:
    description:
      - Whether the image should be persistent or non-persistent.
    type: bool
    version_added: 9.5.0
  create:
    description:
      - Whether the image should be created if not present.
      - This is ignored if O(state=absent).
    type: bool
    version_added: 10.0.0
  template:
    description:
      - Use with O(create=true) to specify image template.
    type: str
    version_added: 10.0.0
  datastore_id:
    description:
      - Use with O(create=true) to specify datastore for image.
    type: int
    version_added: 10.0.0
  wait_timeout:
    description:
      - Seconds to wait until image is ready, deleted or cloned.
    type: int
    default: 60
    version_added: 10.0.0
author:
  - "Milan Ilic (@ilicmilan)"
a  
- name: Fetch the IMAGE by id
  community.general.one_image:
    id: 45
  register: result

- name: Print the IMAGE properties
  ansible.builtin.debug:
    var: result

- name: Rename existing IMAGE
  community.general.one_image:
    id: 34
    state: renamed
    new_name: bar-image

- name: Disable the IMAGE by id
  community.general.one_image:
    id: 37
    enabled: false

- name: Make the IMAGE persistent
  community.general.one_image:
    id: 37
    persistent: true

- name: Enable the IMAGE by name
  community.general.one_image:
    name: bar-image
    enabled: true

- name: Clone the IMAGE by name
  community.general.one_image:
    name: bar-image
    state: cloned
    new_name: bar-image-clone
  register: result

- name: Delete the IMAGE by id
  community.general.one_image:
    id: '{{ result.id }}'
    state: absent

- name: Make sure IMAGE is present
  community.general.one_image:
    name: myyy-image
    state: present
    create: true
    datastore_id: 100
    template: |
      PATH = "/var/tmp/image"
      TYPE = "OS"
      SIZE = 20512
      FORMAT = "qcow2"
      PERSISTENT = "Yes"
      DEV_PREFIX = "vd"

- name: Make sure IMAGE is present with a longer timeout
  community.general.one_image:
    name: big-image
    state: present
    create: true
    datastore_id: 100
    wait_timeout: 900
    template: |-
      PATH = "https://192.0.2.200/repo/tipa_image.raw"
      TYPE = "OS"
      SIZE = 82048
      FORMAT = "raw"
      PERSISTENT = "Yes"
      DEV_PREFIX = "vd"
a  
id:
  description: Image ID.
  type: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: 153
name:
  description: Image name.
  type: str
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: app1
group_id:
  description: Image's group ID.
  type: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: 1
group_name:
  description: Image's group name.
  type: str
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: one-users
owner_id:
  description: Image's owner ID.
  type: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: 143
owner_name:
  description: Image's owner name.
  type: str
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: ansible-test
state:
  description: State of image instance.
  type: str
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: READY
used:
  description: Is image in use.
  type: bool
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: true
running_vms:
  description: Count of running vms that use this image.
  type: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample: 7
permissions:
  description: The image's permissions.
  type: dict
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
  contains:
    owner_u:
      description: The image's owner USAGE permissions.
      type: str
      sample: 1
    owner_m:
      description: The image's owner MANAGE permissions.
      type: str
      sample: 0
    owner_a:
      description: The image's owner ADMIN permissions.
      type: str
      sample: 0
    group_u:
      description: The image's group USAGE permissions.
      type: str
      sample: 0
    group_m:
      description: The image's group MANAGE permissions.
      type: str
      sample: 0
    group_a:
      description: The image's group ADMIN permissions.
      type: str
      sample: 0
    other_u:
      description: The image's other users USAGE permissions.
      type: str
      sample: 0
    other_m:
      description: The image's other users MANAGE permissions.
      type: str
      sample: 0
    other_a:
      description: The image's other users ADMIN permissions.
      type: str
      sample: 0
  sample:
    owner_u: 1
    owner_m: 0
    owner_a: 0
    group_u: 0
    group_m: 0
    group_a: 0
    other_u: 0
    other_m: 0
    other_a: 0
type:
  description: The image's type.
  type: str
  sample: 0
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
disk_type:
  description: The image's format type.
  type: str
  sample: 0
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
persistent:
  description: The image's persistence status (1 means true, 0 means false).
  type: int
  sample: 1
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
source:
  description: The image's source.
  type: str
  sample: /var/lib/one//datastores/100/somerandomstringxd
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
path:
  description: The image's filesystem path.
  type: str
  sample: /var/tmp/hello.qcow2
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
fstype:
  description: The image's filesystem type.
  type: str
  sample: ext4
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
size:
  description: The image's size in MegaBytes.
  type: int
  sample: 10000
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
cloning_ops:
  description: The image's cloning operations per second.
  type: int
  sample: 0
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
cloning_id:
  description: The image's cloning ID.
  type: int
  sample: -1
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
target_snapshot:
  description: The image's target snapshot.
  type: int
  sample: 1
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
datastore_id:
  description: The image's datastore ID.
  type: int
  sample: 100
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
datastore:
  description: The image's datastore name.
  type: int
  sample: image_datastore
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
vms:
  description: The image's list of vm ID's.
  type: list
  elements: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample:
    - 1
    - 2
    - 3
  version_added: 9.5.0
clones:
  description: The image's list of clones ID's.
  type: list
  elements: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample:
    - 1
    - 2
    - 3
  version_added: 9.5.0
app_clones:
  description: The image's list of app_clones ID's.
  type: list
  elements: int
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  sample:
    - 1
    - 2
    - 3
  version_added: 9.5.0
snapshots:
  description: The image's list of snapshots.
  type: list
  returned: when O(state=present), O(state=cloned), or O(state=renamed)
  version_added: 9.5.0
  sample:
    - date: 123123
      parent: 1
      size: 10228
      allow_orphans: 1
      children: 0
      active: 1
      name: SampleName
)OpenNebulaModule)INITREADYUSEDDISABLEDLOCKEDERRORCLONEDELETE	USED_PERSLOCKED_USEDLOCKED_USED_PERSc                   d    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y)ImageModulec                 X   t        t        d      t        d      t        dg dd      t        d      t        d      t        d      t        d      t        d      t        d      t        dd	      

      }dddggddg dgg}ddgg}t        j                  | |d||       y )Nint)typestr)presentabsentclonedrenamedr   )r   choicesdefaultbool<   )r   r   )
idnamestateenablednew_name
persistentcreatetemplatedatastore_idwait_timeoutr"   r   r    r&   T)r'   r(   r!   r!   )supports_check_modemutually_exclusiverequired_if)dictr   __init__)selfargument_specr,   r+   s       o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/one_image.pyr.   zImageModule.__init__|  s    5!E+U_hif%u%(V$u%5)5"5
 i$(tAB

 6N
 	!!$"/6:5G.9		;    c                    |j                   }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }	|j                  d      }
|j                  d      }|j                  d      }|j                  d	      }|j                  d
      }i | _        | j                  ||      }|sY|dk7  rT|r| j	                  ||||      | _        n8||j                  dt        |      z          n||j                  d|z          |dk(  r| j                  ||      | _        nl|
| j                  ||
      | _        || j                  ||      | _        |dk(  r| j                  ||	|      | _        n|dk(  r| j                  ||	      | _        | j                          y )Nr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r   zThere is no image with id=msgzThere is no image with name=r   r   )paramsgetresultget_image_instancecreate_image	fail_jsonr   delete_imagechange_persistenceenable_imageclone_imagerename_imageexit)r/   onemoduler8   r6   r    r!   desired_stater#   r$   r%   r&   r'   r(   r)   images                   r1   runzImageModule.run  s   ZZzz&!

7+**Y'::j)ZZ-
H%::j)zz.1zz.1''D1(2"//hl[  %ACG%K L!  %Cd%J KH$++E<@DK%"55eZH""//w?("..uhM)+"//x@		r2   c                     | j                   j                  j                  dddd      }|j                  D ]  } ||      s|c S  y )N)rB   	imagepoolinfoIMAGE)r/   	predicatepoolrE   s       r1   	get_imagezImageModule.get_image  sG    xx!!&&r2r26ZZ 	E	 r2   c                 ,    | j                  fd      S )Nc                 "    | j                   k(  S N)NAME)rE   
image_names    r1   <lambda>z/ImageModule.get_image_by_name.<locals>.<lambda>  s    UZZ:-E r2   rO   )r/   rT   s    `r1   get_image_by_namezImageModule.get_image_by_name  s    ~~FGGr2   c                 ,    | j                  fd      S )Nc                 "    | j                   k(  S rR   )ID)rE   image_ids    r1   rU   z-ImageModule.get_image_by_id.<locals>.<lambda>  s    UXX-A r2   rV   )r/   r[   s    `r1   get_image_by_idzImageModule.get_image_by_id  s    ~~BCCr2   c                 J    || j                  |      S | j                  |      S rR   )r\   rW   )r/   requested_idrequested_names      r1   r9   zImageModule.get_image_instance  s*    #''55)).99r2   c                    | j                   j                  sc| j                  j                  j	                  d|z   dz   |z   |      }| j                  ||       | j                  |      }| j                  |      }dd<   |S )NzNAME = "z"
Tchanged)rC   
check_moderB   rE   allocatewait_for_readyr\   get_image_info)r/   rT   r'   r(   r)   r[   rE   r8   s           r1   r:   zImageModule.create_image  sy    {{%%xx~~..{Z/G&/PS[/[]ijH,7((2E((/F yr2   c                    dd l }|j                         }|j                         |z
  |k  r| j                  j                  j                  |      }|j                  }|t
        j                  d      fv r,| j                  j                  d|j                  d   z          |t
        j                  d      fv ry|j                  d       |j                         |z
  |k  r| j                  j                  d       y )	Nr   r   zGot an ERROR state: r4   r   T   Wait timeout has expired!)timerB   rE   rK   STATEIMAGE_STATESindexrC   r;   TEMPLATEsleep)r/   r[   r)   ri   
start_timerE   r"   s          r1   rd   zImageModule.wait_for_ready  s    YY[
yy{Z'<7HHNN''1EKKE++G455%%*@5>>RYCZ*Z%[++G455JJqM yy{Z'<7 	"=>r2   c                    dd l }|j                         }|j                         |z
  |k  rs	 | j                  j                  j                  |      }j                  }|t        j                  d      fv ry|j                  d       |j                         |z
  |k  rs| j                  j                  d       y # t        $ r | j                  |      }|sY yY w xY w)Nr   Tr   rg   rh   r4   )ri   rB   rE   rK   	Exceptionr9   rj   rk   rl   rn   rC   r;   )r/   r[   r)   ri   ro   rE   check_imager"   s           r1   wait_for_deletezImageModule.wait_for_delete  s    YY[
yy{Z'<7 ++H5 KKE++H566JJqM yy{Z'<7  	"=>   "55h?" # s   %B; ;CCc                    | j                   j                  j                  |j                        }d}|j                  }|t
        j                  d      t
        j                  d      t
        j                  d      fvrU|r*| j                  j                  dt
        |   z   dz          n)| j                  j                  dt
        |   z   dz          |r|t
        j                  d      k7  s|s|t
        j                  d      k7  rd	}|rF| j                  j                  s0| j                   j                  j                  |j                  |       | j                  |      }||d
<   |S )NFr   r
   r   zCannot enable  image!r4   zCannot disable Tra   )rB   rE   rK   rZ   rj   rk   rl   rC   r;   rb   enablere   r/   rE   rv   ra   r"   r8   s         r1   r>   zImageModule.enable_image  s&   ##EHH-++G4l6H6H6TVbVhVhipVqrr%%*:\%=P*PS\*\%]%%*;l5>Q*QT]*]%^!3!3G!<<););J)G GG4;;11HHNN!!%((F3$$U+#yr2   c                    | j                   j                  j                  |j                        }d}|j                  }|t
        j                  d      t
        j                  d      t
        j                  d      fvrU|r*| j                  j                  dt
        |   z   dz          n)| j                  j                  dt
        |   z   dz          |r|t
        j                  d      k7  s|s|t
        j                  d      k7  rd	}|rF| j                  j                  s0| j                   j                  j                  |j                  |       | j                  |      }||d
<   |S )NFr   r
   r   zCannot enable persistence for ru   r4   zCannot disable persistence for Tra   )rB   rE   rK   rZ   rj   rk   rl   rC   r;   rb   r%   re   rw   s         r1   r=   zImageModule.change_persistence  s(   ##EHH-++G4l6H6H6TVbVhVhipVqrr%%*J\Z_M`*`cl*l%m%%*Kl[`Na*adm*m%n!3!3G!<<););J)G GG4;;11HHNN%%ehh7$$U+#yr2   c                     |d|j                   z   }| j                  |      }|r| j                  |      }d|d<   |S |j                  t        j                  d      k(  r| j                  j                  d       | j                  j                  sg| j                  j                  j                  |j                  |      }| j                  ||       | j                  j                  j                  |      }| j                  |      }d|d<   |S )NzCopy of Fra   r
   zCannot clone DISABLED imager4   T)rS   rW   re   rj   rk   rl   rC   r;   rb   rB   rE   clonerZ   rd   rK   )r/   rE   r$   r)   	tmp_imager8   new_ids          r1   r?   zImageModule.clone_image6  s    !EJJ.H**84	((/F %F9M;;,,,Z88KK!!&C!D{{%%XX^^))%((H=F5HHNN''/E$$U+ yr2   c                    || j                   j                  d       ||j                  k(  r| j                  |      }d|d<   |S | j	                  |      }|r8| j                   j                  d|z   dz   t        |j                        z          | j                   j                  s0| j                  j                  j                  |j                  |       | j                  |      }d|d<   |S )NzA'new_name' option has to be specified when the state is 'renamed'r4   Fra   zName 'z$' is already taken by IMAGE with id=T)rC   r;   rS   re   rW   r   rZ   rb   rB   rE   rename)r/   rE   r$   r8   r{   s        r1   r@   zImageModule.rename_imageM  s    KK!!&i!juzz!((/F %F9M**84	KK!!h&9<b&behiriuiuev&v!w{{%%HHNN!!%((H5$$U+ yr2   c                 `   |sddiS |j                   dkD  r5| j                  j                  dt        |j                         z   dz          | j                  j                  sK| j
                  j                  j                  |j                         | j                  |j                  |       ddiS )Nra   Fr   zCannot delete image. There are z VMs using it.r4   T)
RUNNING_VMSrC   r;   r   rb   rB   rE   deleterZ   rs   )r/   rE   r)   s      r1   r<   zImageModule.delete_imagea  s    u%%q KK!!&G#eN_N_J`&`cs&s!t{{%%HHNN!!%((+  <84  r2   N)r   )__name__
__module____qualname__r.   rF   rO   rW   r\   r9   r:   rd   rs   r>   r=   r?   r@   r<    r2   r1   r   r   {  sK    ;6%NHD:?"?,00.(!r2   r   c                  4    t               j                          y rR   )r   
run_moduler   r2   r1   mainr   o  s    Mr2   __main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNEansible_collections.community.general.plugins.module_utils.opennebular   rk   r   r   r   r   r2   r1   <module>r      se    C BHTGRT
n c Kq!" q!h zF r2   