
    Vh              
          d dl mZmZmZ eZdZdZdZ	 d dl	Z	dZ
d dlZd dlmZ d d	lmZ d d
lmZmZ g dddgg dddgg dg dg dg g 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& Z%d' Z&d( Z'd) Z(d* Z)d+ Z*d, Z+d- Z,d. Z-g d/Z.g d0Z/d1 Z0d2 Z1d3 Z2d4 Z3d5 Z4dEd6Z5d7 Z6d8 Z7d9 Z8d: Z9d; Z:d< Z;d= Z<d> Z=g d?Z>d@ Z?dA Z@dB ZAdC ZBeCdDk(  r eB        yy# e$ r dZ
Y w xY w)F    )absolute_importdivisionprint_functiona  
module: one_vm
short_description: Creates or terminates OpenNebula instances
description:
  - Manages OpenNebula instances.
requirements:
  - pyone
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  api_url:
    description:
      - URL of the OpenNebula RPC server.
      - It is recommended to use HTTPS so that the username/password are not transferred over the network unencrypted.
      - If not set then the value of the E(ONE_URL) environment variable is used.
    type: str
  api_username:
    description:
      - Name of the user to login into the OpenNebula RPC server. If not set then the value of the E(ONE_USERNAME) environment
        variable is used.
    type: str
  api_password:
    description:
      - Password of the user to login into OpenNebula RPC server. If not set then the value of the E(ONE_PASSWORD) environment
        variable is used. if both O(api_username) or O(api_password) are not set, then it will try authenticate with ONE auth
        file. Default path is C(~/.one/one_auth).
      - Set environment variable E(ONE_AUTH) to override this path.
    type: str
  template_name:
    description:
      - Name of VM template to use to create a new instance.
    type: str
  template_id:
    description:
      - ID of a VM template to use to create a new instance.
    type: int
  vm_start_on_hold:
    description:
      - Set to true to put vm on hold while creating.
    default: false
    type: bool
  instance_ids:
    description:
      - 'A list of instance IDs used for states: V(absent), V(running), V(rebooted), V(poweredoff).'
    aliases: ['ids']
    type: list
    elements: int
  state:
    description:
      - V(present) - create instances from a template specified with C(template_id)/C(template_name).
      - V(running) - run instances.
      - V(poweredoff) - power-off instances.
      - V(rebooted) - reboot instances.
      - V(absent) - terminate instances.
    choices: ["present", "absent", "running", "rebooted", "poweredoff"]
    default: present
    type: str
  hard:
    description:
      - Reboot, power-off or terminate instances C(hard).
    default: false
    type: bool
  wait:
    description:
      - Wait for the instance to reach its desired state before returning. Keep in mind if you are waiting for instance to
        be in running state it does not mean that you will be able to SSH on that machine only that boot process have started
        on that instance. See the example using the M(ansible.builtin.wait_for) module for details.
    default: true
    type: bool
  wait_timeout:
    description:
      - How long before wait gives up, in seconds.
    default: 300
    type: int
  attributes:
    description:
      - A dictionary of key/value attributes to add to new instances, or for setting C(state) of instances with these attributes.
      - Keys are case insensitive and OpenNebula automatically converts them to upper case.
      - Be aware V(NAME) is a special attribute which sets the name of the VM when it is deployed.
      - C(#) character(s) can be appended to the C(NAME) and the module will automatically add indexes to the names of VMs.
      - 'For example: V(NAME: foo-###) would create VMs with names V(foo-000), V(foo-001),...'
      - When used with O(count_attributes) and O(exact_count) the module will match the base name without the index part.
    default: {}
    type: dict
  labels:
    description:
      - A list of labels to associate with new instances, or for setting C(state) of instances with these labels.
    default: []
    type: list
    elements: str
  count_attributes:
    description:
      - A dictionary of key/value attributes that can only be used with O(exact_count) to determine how many nodes based on
        a specific attributes criteria should be deployed. This can be expressed in multiple ways and is shown in the EXAMPLES
        section.
    type: dict
  count_labels:
    description:
      - A list of labels that can only be used with O(exact_count) to determine how many nodes based on a specific labels
        criteria should be deployed. This can be expressed in multiple ways and is shown in the EXAMPLES section.
    type: list
    elements: str
  count:
    description:
      - Number of instances to launch.
    default: 1
    type: int
  exact_count:
    description:
      - Indicates how many instances that match O(count_attributes) and O(count_labels) parameters should be deployed. Instances
        are either created or terminated based on this value.
      - B(NOTE:) Instances with the least IDs will be terminated first.
    type: int
  mode:
    description:
      - Set permission mode of the instance in octet format, for example V(0600) to give owner C(use) and C(manage) and nothing
        to group and others.
    type: str
  owner_id:
    description:
      - ID of the user which will be set as the owner of the instance.
    type: int
  group_id:
    description:
      - ID of the group which will be set as the group of the instance.
    type: int
  memory:
    description:
      - The size of the memory for new instances (in MB, GB, ..).
    type: str
  disk_size:
    description:
      - The size of the disk created for new instances (in MB, GB, TB,...).
      - B(NOTE:) If The Template hats Multiple Disks the Order of the Sizes is matched against the order specified in O(template_id)/O(template_name).
    type: list
    elements: str
  cpu:
    description:
      - Percentage of CPU divided by 100 required for the new instance. Half a processor is written 0.5.
    type: float
  vcpu:
    description:
      - Number of CPUs (cores) new VM will have.
    type: int
  networks:
    description:
      - A list of dictionaries with network parameters. See examples for more details.
    default: []
    type: list
    elements: dict
  disk_saveas:
    description:
      - Creates an image from a VM disk.
      - It is a dictionary where you have to specify C(name) of the new image.
      - Optionally you can specify C(disk_id) of the disk you want to save. By default C(disk_id) is 0.
      - B(NOTE:) This operation will only be performed on the first VM (if more than one VM ID is passed) and the VM has to
        be in the C(poweredoff) state.
      - Also this operation will fail if an image with specified C(name) already exists.
    type: dict
  persistent:
    description:
      - Create a private persistent copy of the template plus any image defined in DISK, and instantiate that copy.
    default: false
    type: bool
    version_added: '0.2.0'
  datastore_id:
    description:
      - Name of Datastore to use to create a new instance.
    version_added: '0.2.0'
    type: int
  datastore_name:
    description:
      - Name of Datastore to use to create a new instance.
    version_added: '0.2.0'
    type: str
  updateconf:
    description:
      - When O(instance_ids) is provided, updates running VMs with the C(updateconf) API call.
      - When new VMs are being created, emulates the C(updateconf) API call using direct template merge.
      - Allows for complete modifications of the C(CONTEXT) attribute.
      - "Supported attributes include:"
      - B(BACKUP_CONFIG:) V(BACKUP_VOLATILE), V(FS_FREEZE), V(INCREMENT_MODE), V(KEEP_LAST), V(MODE);
      - B(CONTEXT:) (Any value, except V(ETH*). Variable substitution will be made);
      - B(CPU_MODEL:) V(FEATURES), V(MODEL);
      - B(FEATURES:) V(ACPI), V(APIC), V(GUEST_AGENT), V(HYPERV), V(IOTHREADS), V(LOCALTIME), V(PAE), V(VIRTIO_BLK_QUEUES), V(VIRTIO_SCSI_QUEUES);
      - B(GRAPHICS:) V(COMMAND), V(KEYMAP), V(LISTEN), V(PASSWD), V(PORT), V(TYPE);
      - B(INPUT:) V(BUS), V(TYPE);
      - B(OS:) V(ARCH), V(BOOT), V(BOOTLOADER), V(FIRMWARE), V(INITRD), V(KERNEL), V(KERNEL_CMD), V(MACHINE), V(ROOT), V(SD_DISK_BUS), V(UUID);
      - B(RAW:) V(DATA), V(DATA_VMX), V(TYPE), V(VALIDATE);
      - B(VIDEO:) V(ATS), V(IOMMU), V(RESOLUTION), V(TYPE), V(VRAM).
    type: dict
    version_added: 6.3.0
author:
  - "Milan Ilic (@ilicmilan)"
  - "Jan Meerkamp (@meerkampdvv)"
a  
- name: Create a new instance
  community.general.one_vm:
    template_id: 90
  register: result

- name: Print VM properties
  ansible.builtin.debug:
    msg: result

- name: Deploy a new VM on hold
  community.general.one_vm:
    template_name: 'app1_template'
    vm_start_on_hold: 'True'

- name: Deploy a new VM and set its name to 'foo'
  community.general.one_vm:
    template_name: 'app1_template'
    attributes:
      name: foo

- name: Deploy a new VM and set its group_id and mode
  community.general.one_vm:
    template_id: 90
    group_id: 16
    mode: 660

- name: Deploy a new VM  as persistent
  community.general.one_vm:
    template_id: 90
    persistent: true

- name: Change VM's permissions to 640
  community.general.one_vm:
    instance_ids: 5
    mode: 640

- name: Deploy 2 new instances and set memory, vcpu, disk_size and 3 networks
  community.general.one_vm:
    template_id: 15
    disk_size: 35.2 GB
    memory: 4 GB
    vcpu: 4
    count: 2
    networks:
      - NETWORK_ID: 27
      - NETWORK: "default-network"
        NETWORK_UNAME: "app-user"
        SECURITY_GROUPS: "120,124"
      - NETWORK_ID: 27
        SECURITY_GROUPS: "10"

- name: Deploy a new instance which uses a Template with two Disks
  community.general.one_vm:
    template_id: 42
    disk_size:
      - 35.2 GB
      - 50 GB
    memory: 4 GB
    vcpu: 4
    count: 1
    networks:
      - NETWORK_ID: 27

- name: "Deploy an new instance with attribute 'bar: bar1' and set its name to 'foo'"
  community.general.one_vm:
    template_id: 53
    attributes:
      name: foo
      bar: bar1

- name: "Enforce that 2 instances with attributes 'foo1: app1' and 'foo2: app2' are deployed"
  community.general.one_vm:
    template_id: 53
    attributes:
      foo1: app1
      foo2: app2
    exact_count: 2
    count_attributes:
      foo1: app1
      foo2: app2

- name: Enforce that 4 instances with an attribute 'bar' are deployed
  community.general.one_vm:
    template_id: 53
    attributes:
      name: app
      bar: bar2
    exact_count: 4
    count_attributes:
      bar:

# Deploy 2 new instances with attribute 'foo: bar' and labels 'app1' and 'app2' and names in format 'fooapp-##'
# Names will be: fooapp-00 and fooapp-01
- name: Deploy 2 new instances
  community.general.one_vm:
    template_id: 53
    attributes:
      name: fooapp-##
      foo: bar
    labels:
      - app1
      - app2
    count: 2

# Deploy 2 new instances with attribute 'app: app1' and names in format 'fooapp-###'
# Names will be: fooapp-002 and fooapp-003
- name: Deploy 2 new instances
  community.general.one_vm:
    template_id: 53
    attributes:
      name: fooapp-###
      app: app1
    count: 2

# Reboot all instances with name in format 'fooapp-#'
# Instances 'fooapp-00', 'fooapp-01', 'fooapp-002' and 'fooapp-003' will be rebooted
- name: Reboot all instances with names in a certain format
  community.general.one_vm:
    attributes:
      name: fooapp-#
    state: rebooted

# Enforce that only 1 instance with name in format 'fooapp-#' is deployed
# The task will delete oldest instances, so only the 'fooapp-003' will remain
- name: Enforce that only 1 instance with name in a certain format is deployed
  community.general.one_vm:
    template_id: 53
    exact_count: 1
    count_attributes:
      name: fooapp-#

- name: Deploy an new instance with a network
  community.general.one_vm:
    template_id: 53
    networks:
      - NETWORK_ID: 27
  register: vm

- name: Wait for SSH to come up
  ansible.builtin.wait_for:
    port: 22
    host: '{{ vm.instances[0].networks[0].ip }}'

- name: Terminate VMs by ids
  community.general.one_vm:
    instance_ids:
      - 153
      - 160
    state: absent

- name: Reboot all VMs that have labels 'foo' and 'app1'
  community.general.one_vm:
    labels:
      - foo
      - app1
    state: rebooted

- name: "Fetch all VMs that have name 'foo' and attribute 'app: bar'"
  community.general.one_vm:
    attributes:
      name: foo
      app: bar
  register: results

- name: Deploy 2 new instances with labels 'foo1' and 'foo2'
  community.general.one_vm:
    template_name: app_template
    labels:
      - foo1
      - foo2
    count: 2

- name: Enforce that only 1 instance with label 'foo1' will be running
  community.general.one_vm:
    template_name: app_template
    labels:
      - foo1
    exact_count: 1
    count_labels:
      - foo1

- name: Terminate all instances that have attribute foo
  community.general.one_vm:
    template_id: 53
    exact_count: 0
    count_attributes:
      foo:

- name: "Power-off the VM and save VM's disk with id=0 to the image with name 'foo-image'"
  community.general.one_vm:
    instance_ids: 351
    state: poweredoff
    disk_saveas:
      name: foo-image

- name: "Save VM's disk with id=1 to the image with name 'bar-image'"
  community.general.one_vm:
    instance_ids: 351
    disk_saveas:
      name: bar-image
      disk_id: 1

- name: "Deploy 2 new instances with a custom 'start script'"
  community.general.one_vm:
    template_name: app_template
    count: 2
    updateconf:
      CONTEXT:
        START_SCRIPT: ip r r 169.254.16.86/32 dev eth0

- name: "Add a custom 'start script' to a running VM"
  community.general.one_vm:
    instance_ids: 351
    updateconf:
      CONTEXT:
        START_SCRIPT: ip r r 169.254.16.86/32 dev eth0

- name: "Update SSH public keys inside the VM's context"
  community.general.one_vm:
    instance_ids: 351
    updateconf:
      CONTEXT:
        SSH_PUBLIC_KEY: |-
          ssh-rsa ...
          ssh-ed25519 ...
a  
instances_ids:
  description: A list of instances IDs whose state is changed or which are fetched with O(instance_ids) option.
  type: list
  returned: success
  sample: [1234, 1235]
instances:
  description: A list of instances info whose state is changed or which are fetched with O(instance_ids) option.
  type: complex
  returned: success
  contains:
    vm_id:
      description: Vm ID.
      type: int
      sample: 153
    vm_name:
      description: Vm name.
      type: str
      sample: foo
    template_id:
      description: Vm's template ID.
      type: int
      sample: 153
    group_id:
      description: Vm's group ID.
      type: int
      sample: 1
    group_name:
      description: Vm's group name.
      type: str
      sample: one-users
    owner_id:
      description: Vm's owner ID.
      type: int
      sample: 143
    owner_name:
      description: Vm's owner name.
      type: str
      sample: app-user
    mode:
      description: Vm's mode.
      type: str
      returned: success
      sample: 660
    state:
      description: State of an instance.
      type: str
      sample: ACTIVE
    lcm_state:
      description: Lcm state of an instance that is only relevant when the state is ACTIVE.
      type: str
      sample: RUNNING
    cpu:
      description: Percentage of CPU divided by 100.
      type: float
      sample: 0.2
    vcpu:
      description: Number of CPUs (cores).
      type: int
      sample: 2
    memory:
      description: The size of the memory in MB.
      type: str
      sample: 4096 MB
    disk_size:
      description: The size of the disk in MB.
      type: str
      sample: 20480 MB
    networks:
      description: A list of dictionaries with info about IP, NAME, MAC, SECURITY_GROUPS for each NIC.
      type: list
      sample: [
        {
          "ip": "10.120.5.33",
          "mac": "02:00:0a:78:05:21",
          "name": "default-test-private",
          "security_groups": "0,10"
        },
        {
          "ip": "10.120.5.34",
          "mac": "02:00:0a:78:05:22",
          "name": "default-test-private",
          "security_groups": "0"
        }
      ]
    uptime_h:
      description: Uptime of the instance in hours.
      type: int
      sample: 35
    labels:
      description: A list of string labels that are associated with the instance.
      type: list
      sample: ["foo", "spec-label"]
    attributes:
      description: A dictionary of key/values attributes that are associated with the instance.
      type: dict
      sample: {
        "HYPERVISOR": "kvm",
        "LOGO": "images/logos/centos.png",
        "TE_GALAXY": "bar",
        "USER_INPUTS": null
      }
    updateconf:
      description: A dictionary of key/values attributes that are set with the updateconf API call.
      type: dict
      version_added: 6.3.0
      sample: {
        "OS": { "ARCH": "x86_64" },
        "CONTEXT": {
          "START_SCRIPT": "ip r r 169.254.16.86/32 dev eth0",
          "SSH_PUBLIC_KEY": "ssh-rsa ...\\nssh-ed25519 ..."
        }
      }
tagged_instances:
  description:
    - A list of instances info based on a specific attributes and/or labels that are specified with O(count_attributes) and
      O(count_labels) options.
  type: complex
  returned: success
  contains:
    vm_id:
      description: Vm ID.
      type: int
      sample: 153
    vm_name:
      description: Vm name.
      type: str
      sample: foo
    template_id:
      description: Vm's template ID.
      type: int
      sample: 153
    group_id:
      description: Vm's group ID.
      type: int
      sample: 1
    group_name:
      description: Vm's group name.
      type: str
      sample: one-users
    owner_id:
      description: Vm's user ID.
      type: int
      sample: 143
    owner_name:
      description: Vm's user name.
      type: str
      sample: app-user
    mode:
      description: Vm's mode.
      type: str
      returned: success
      sample: 660
    state:
      description: State of an instance.
      type: str
      sample: ACTIVE
    lcm_state:
      description: Lcm state of an instance that is only relevant when the state is ACTIVE.
      type: str
      sample: RUNNING
    cpu:
      description: Percentage of CPU divided by 100.
      type: float
      sample: 0.2
    vcpu:
      description: Number of CPUs (cores).
      type: int
      sample: 2
    memory:
      description: The size of the memory in MB.
      type: str
      sample: 4096 MB
    disk_size:
      description: The size of the disk in MB.
      type: list
      sample: ["20480 MB", "10240 MB"]
    networks:
      description: A list of dictionaries with info about IP, NAME, MAC, SECURITY_GROUPS for each NIC.
      type: list
      sample: [
        {
          "ip": "10.120.5.33",
          "mac": "02:00:0a:78:05:21",
          "name": "default-test-private",
          "security_groups": "0,10"
        },
        {
          "ip": "10.120.5.34",
          "mac": "02:00:0a:78:05:22",
          "name": "default-test-private",
          "security_groups": "0"
        }
      ]
    uptime_h:
      description: Uptime of the instance in hours.
      type: int
      sample: 35
    labels:
      description: A list of string labels that are associated with the instance.
      type: list
      sample: ["foo", "spec-label"]
    attributes:
      description: A dictionary of key/values attributes that are associated with the instance.
      type: dict
      sample: {"HYPERVISOR": "kvm", "LOGO": "images/logos/centos.png", "TE_GALAXY": "bar", "USER_INPUTS": null}
    updateconf:
      description: A dictionary of key/values attributes that are set with the updateconf API call.
      type: dict
      version_added: 6.3.0
      sample: {"OS": {"ARCH": "x86_64"}, "CONTEXT": {"START_SCRIPT": "ip r r 169.254.16.86/32 dev eth0", "SSH_PUBLIC_KEY": "ssh-rsa ...\\nssh-ed25519 ..."}}
NTF)AnsibleModule)
dict_merge)flattenrender)	ARCHMACHINEKERNELINITRD
BOOTLOADERBOOTSD_DISK_BUSUUIDFIRMWAREMODELFEATURES)	ACPIPAEAPIC	LOCALTIMEHYPERVGUEST_AGENTVIRTIO_BLK_QUEUESVIRTIO_SCSI_QUEUES	IOTHREADSTYPEBUS)r   LISTENPORTPASSWDKEYMAPCOMMAND)ATSIOMMU
RESOLUTIONr   VRAM)DATADATA_VMXr   VALIDATE)	FS_FREEZE	KEEP_LASTBACKUP_VOLATILEMODEINCREMENT_MODE)	OS	CPU_MODELr   INPUTGRAPHICSVIDEORAWCONTEXTBACKUP_CONFIGc                    |j                         D ]m  \  }}|t        vr!| j                  dj                  |             t        |   s9|D ]0  }|t        |   vs| j                  dj                  ||             2 o y)zDChecks if attributes are compatible with one.vm.updateconf API call.z#'{0:}' is not a valid VM attribute.msgz/'{0:}' is not a valid VM subattribute of '{1:}'N)itemsUPDATECONF_ATTRIBUTES	fail_jsonformat)moduleto_checkattrsubattributessubattrs        l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/one_vm.pycheck_updateconfrF     s    '~~/ nm,,!F!M!Md!ST$T*$ 	nG3D99  %V%]%]^egk%l m	nn    c                     i }| j                         D ]J  \  }}|t        vri }|j                         D ]  \  }}t        |   r|t        |   vr|||<   ! |sF|||<   L |S )z4Extracts 'updateconf' attributes from a VM template.)r<   r=   )vm_template
updateconfrB   rC   tmprD   values          rE   parse_updateconfrM     s    J*002 	#m,,+113 	!NGU$T*w>STX>Y/Y CL	! "Jt	# rG   c                     |j                   j                  dddd      }d}d }d}|j                  D ]  } ||      s|dz   }|}|j                  }  |dk(  ry |dkD  r| j	                  d|z          |S )Nr       z$There are more templates with name: r:   )templatepoolinfo
VMTEMPLATENAMEr>   )r@   client	predicatepoolfoundfound_templatetemplate_nametemplates           rE   get_templater^     s    ##BB3DENMOO *XAIE%N$MMM	* z	CmSTrG   c                 $    t        | |fd      S )Nc                 "    | j                   k(  S NrV   )r]   r\   s    rE   <lambda>z&get_template_by_name.<locals>.<lambda>  s    (--=:X rG   r^   )r@   rW   r\   s     `rE   get_template_by_namere     s    (YZZrG   c                 $    t        | |fd      S )Nc                 "    | j                   k(  S ra   ID)r]   template_ids    rE   rc   z$get_template_by_id.<locals>.<lambda>  s    (++:T rG   rd   )r@   rW   rj   s     `rE   get_template_by_idrk     s    (UVVrG   c                 X    |t        | ||      nt        | ||      }|r|j                  S y ra   )rk   re   ri   )r@   rW   requested_idrequested_namer]   s        rE   get_template_idro     sA    COC[!&&,?auv|  E  GU  bVH{{rG   c                     |j                   j                         }d}d }d}|j                  D ]  } ||      s|dz   }|}|j                  }  |dk(  ry |dkD  r| j	                  d|z          |S )Nr   rQ   rR   z%There are more datastores with name: r:   )datastorepoolrT   	DATASTORErV   r>   )r@   rW   rX   rY   rZ   found_datastoredatastore_name	datastores           rE   get_datastorerv     s    $$&DEON^^ ,	YAIE'O&^^N	, z	D~UVrG   c                 $    t        | |fd      S )Nc                 "    | j                   k(  S ra   rb   )ru   rt   s    rE   rc   z'get_datastore_by_name.<locals>.<lambda>  s    INNn<\ rG   rv   )r@   rW   rt   s     `rE   get_datastore_by_namerz     s    )]^^rG   c                 $    t        | |fd      S )Nc                 "    | j                   k(  S ra   rh   )ru   datastore_ids    rE   rc   z%get_datastore_by_id.<locals>.<lambda>  s    ILLL<X rG   ry   )r@   rW   r}   s     `rE   get_datastore_by_idr~     s    )YZZrG   c                 X    |rt        | ||      nt        | ||      }|r|j                  S y ra   )r~   rz   ri   )r@   rW   rm   rn   ru   s        rE   get_datastore_idr   
  s:    EQ#FFLAWlmsu{  ~L  XMI||rG   c                 n    	 | j                   j                  t        |            }|S # t        $ r Y y w xY wra   )vmrT   intBaseException)rW   vm_idr   s      rE   get_vm_by_idr     s8    YY^^CJ' I  s   $( 	44c                     g }|D ]D  }t        ||      }|#|dk7  r| j                  dt        |      z          |j                  |       F |S )NabsentzThere is no VM with id=r:   )r   r>   strappend)r@   rW   stateidsvmsr   r   s          rE   get_vms_by_idsr     s[    
C &%(:%8+!:SZ!GH

2	 JrG   c           
         | j                   j                  |j                        }g }g }d|j                  v rmt	        |j                  d   t
              r,|j                  d   D ]  }|j                  |d   dz           n$|j                  |j                  d   d   dz          d|j                  v rt	        |j                  d   t
              rk|j                  d   D ]X  }|j                  |j                  dd      |j                  dd      |j                  dd      |j                  d	d      d
       Z n|j                  |j                  d   j                  dd      |j                  d   j                  dd      |j                  d   j                  dd      |j                  d   j                  d	d      d
       dd l}|j                         }|j                  |j                        }|j                  |      |j                  |      z
  }	|	dz  }	t        | |      }
d }|j                  t        j                  d      k(  rt         |j"                     }t%        | |j                        \  }}t'        |j                        }i dt)        |j                  d         d|j                  d|j*                  dt        |j                     d|d|j,                  d|j.                  d|d|d|j                  d   dz   d|j                  d   d|j                  d   d|j0                  d|j2                  d t)        |	      d!|d"|
||d#}|S )$NDISKSIZEz MBNICIPrQ   MACNETWORKSECURITY_GROUPS)ipmacnamesecurity_groupsr   i  ACTIVErj   TEMPLATE_IDr   vm_namer   	lcm_state
owner_nameowner_idnetworks	disk_sizememoryMEMORYvcpuVCPUcpuCPU
group_namegroup_iduptime_h
attributesmode)labelsrJ   )r   rT   ri   TEMPLATE
isinstancelistr   gettime	localtimeSTIMEmktimeparse_vm_permissionsSTATE	VM_STATESindex
LCM_STATES	LCM_STATE!get_vm_labels_and_attributes_dictrM   r   rV   UNAMEUIDGNAMEGID)rW   r   networks_infor   disknicr   current_timevm_start_time	vm_uptimepermissions_strvm_lcm_state	vm_labelsvm_attributesrJ   rT   s                   rE   get_vm_infor   &  sh   		BMIbkk&)40F+ 7  f!567 R[[085@Abkk%($/{{5) $$''$+775"-GGIr2'*ww/@"'E	&    kk%(,,T26{{5)--eR8E*..y"=KK&**+<bA"  >>#LNN288,ML)DKK,FFI'I*626O L	xx9??8,,!",,/@OI}!"++.Js2;;}56 	277 	288$	
 	\ 	bhh 	BFF 	M 	Y 	"++h'%/ 	F# 	r{{5! 	bhh 	BFF 	C	N  	m!" 	#$  'D, KrG   c                 T   | j                   j                  |j                        j                  }t	        |j
                        dz  t	        |j                        dz  z   t	        |j                        z   }t	        |j                        dz  t	        |j                        dz  z   t	        |j                        z   }t	        |j                        dz  t	        |j                        dz  z   t	        |j                        z   }t        |      t        |      z   t        |      z   }|S )N      )r   rT   ri   PERMISSIONSr   OWNER_UOWNER_MOWNER_AGROUP_UGROUP_MGROUP_AOTHER_UOTHER_MOTHER_Ar   )rW   r   vm_PERMISSIONSowner_octalgroup_octalother_octalpermissionss          rE   r   r   q  s    YY^^BEE*66Nn,,-1C8N8N4ORS4SSVYZhZpZpVqqKn,,-1C8N8N4ORS4SSVYZhZpZpVqqKn,,-1C8N8N4ORS4SSVYZhZpZpVqqKk"S%55K8HHKrG   c                    d}|D ]  }|j                   j                  |j                        }t        ||      }|xs ||k7  }| j                  rJ||k7  sPt        t        |d            dd  }|D cg c]  }t        |       }	}	 |j                   j                  |j                  |	d   |	d   |	d   |	d   |	d   |	d	   |	d
   |	d   |	d   
        |S c c}w # t        j                  $ r | j                  d       Y w xY w)NF   )baser   r   rR      r            zUPermissions changing is unsuccessful, but instances are present if you deployed them.r:   )r   rT   ri   r   
check_modebinr   chmodpyoneOneAuthorizationExceptionr>   )
r@   rW   r   r   changedr   old_permissionsr   d	mode_bitss
             rE   set_vm_permissionsr   }  s:   G ~YY^^BEE".vr:;_;  _%C!#k":;AB?O)89AQ9I9~		EE9Q<1y|Yq\S\]^S_ajklamoxyzo{  ~G  HI  ~J  LU  VW  LXY~ N : 22 ~  %| }~s   2CA	C%D Dc                    d}|D ]  }|j                   j                  |j                        }||j                  }||j                  }|xs  ||j                  k7  xs ||j                  k7  }| j
                  ru||j                  k7  s||j                  k7  s	 |j                   j                  |j                  ||        |S # t        j                  $ r | j                  d       Y w xY w)NFzSOwnership changing is unsuccessful, but instances are present if you deployed them.r:   )
r   rT   ri   r   r   r   chownr   r   r>   )r@   rW   r   r   r   r   r   s          rE   set_vm_ownershipr     s    G |YY^^BEE"vvHvvHEX/E8rvv3E  h"&&&8H<N|		x:| N 22 |  %z {|s   'C%C-,C-c                 8   d}|s|S |j                   j                  |j                        j                  }|j                   j	                  |j                  t        |      d       |j                   j                  |j                        j                  }||k7  }|S )NFrR   )r   rT   ri   r   rJ   r	   )r@   rW   r   updateconf_dictr   beforeafters          rE   	update_vmr     st    GYY^^BEE"++F
II 7;IINN255!**EoGNrG   c                 <    d}|D ]  }t        | ||g| xs |} |S NF)r   )r@   rW   r   argsr   r   s         rE   
update_vmsr     s5    G BFFB66A'BNrG   c                    g d}|}|}d}|r|dd j                         s|dd dk(  r,||d   z  }|dd  }|r|dd j                         r#|dd dk(  r,t        |      }|j                         }||vr| j                  d|||fz         ddi}t	        |dd        D ]  \  }}d|dz   d	z  z  ||<    t        |||   z        }	|	d
z  }
|
S )N)BKBMBGBTBrQ   r   rR   .zCannot interpret %r %r %dr:   r   
   i   )isdigitfloatstripr>   	enumerater   )r@   size_strSYMBOLSsinitnumsymbolprefixisize_in_bytes
size_in_MBs              rE   get_size_in_MBr    s
   +GAD
C
!A AaFcMqtabE !A AaFcM *CWWYFW8D&#;NNO1XF'!"+& &1!a%2%q	& fVn,-M+.JrG   c
                    |r|j                  dd      }
|j                  j                  |      j                  }t	        t        |j                  dg                   }|rFt	        t        |            }||k7  r-| j                  dt        |      z   dz   t        |      z          t        |xs i |xs i       }t        |dj                  |      t        |d	      t        t        t        |j                  dg             t        |xs d g|z              D cg c]5  \  }}|.|s|n)t        |d
t        t        t        | |                  i      7 c}}d	      d      }t        ||	xs i       }	 |j                  j                  |
|t        |      |      }t#        |      }t%        ||      S c c}}w # t        j                   $ r%}| j                  t        |             Y d }~Qd }~ww xY w)NrV   rQ   r   zThis template has z disks but you defined r:   ,T)extractr   )LABELSr   r   )r   r]   rT   r   lenr   r>   r   r   joinzipr   r  instantiater	   r   OneExceptionr   r   )r@   rW   rj   attributes_dictlabels_listr   network_attrs_listvm_start_on_holdvm_persistentr   r   r]   
disk_count
size_countvm_extra_templater   sizer   er   s                       rE   	create_vmr&    s   !%%fb1##K099HWX\\&"567J+,
#!5J!GJc!cfijtfu!uv"8>r?3HbI"#4((;')48 "VR01	8dVj%89		
 d  D*TCvt <=>4 # 	
 	7  ##4o6KL%++K,3,<,23D,E,9	; 
fe	$Bvr""/	
&  %SV$$%s   :F55(F; ;G3G..G3c                     d}t        |      j                  |      }|| v r$|dz   }t        |      j                  |      }|| v r$|S Nr   rR   )r   zfill)vm_filled_indexes_listnum_sign_cntcountercnt_strs       rE   generate_next_indexr.    sS    G'l  .G
+
+A+g,$$\2 +
+ NrG   c                     | j                   j                  |      j                  }i }g }|j                         D ]'  \  }}|dk7  r|||<   |||j	                  d      }) ||fS )Nr  r  )r   rT   USER_TEMPLATEr<   split)rW   r   vm_USER_TEMPLATE
attrs_dictr  keyrL   s          rE   r   r     sv    yy~~e,::JK&,,. /
U(?#JsO5#4#kk#./ 
""rG   c                 4   | j                   j                  dddd      j                  }g }d}|r|j                  dd      }|dk7  r|d t	        |      |j                  d      z
   }|j                  d      }|D ]|  }|j                  j                  |      s|r8|j                  t	        |      d  j                         r|j                  |       Y|r\|j                  |k(  sl|j                  |       ~ |}dd l}	|	j                  |      }|D ]  }g }
t        | |j                        \  }}|r[t	        |      dkD  rM|j                         D ]:  \  }}||v r|s||   |k7  s|
j                  |        n|
j                  |        n t        t!        |      j#                  t!        |
                  }g }
|r,t	        |      dkD  r|D ]  }||vs|
j                  |        n t        t!        |      j#                  t!        |
                  } |S )NrO   rP   rQ   rV   #r   )vmpoolrT   VMpopr  countendswithrV   
startswithr  r   copyr   ri   r<   r   set
difference)rW   r  r  rY   vm_listr   	base_name	with_hashr   r=  remove_listvm_labels_listvm_attributes_dictr4  vallabels                   rE   get_all_vms_by_attributesrH    s   ==b"b"-00DGD""62.rz5#d)djjo56	MM#&	 	'Bww!!),Y!9!A!A!CNN2&"rww$NN2&	' iioG B-NvWYW\W\-]**s?3a7+113 S,,1#6#=#**2.&&r* s7|..s;/?@A3{+a/$ .&&r* s7|..s;/?@A+B. NrG   c                 f   g }d}|r|j                  dd      }| j                  rdg g fS d }|j                  d      |dk7  r<dkD  r7t        |d|id       }|d t	        |      z
   }t        fd|D              }|dkD  rx|}|"t        |      }|j                  |       ||z  }||d<   t        | |||||||
||
      }|j                  d      }t        ||      }|j                  |       |dz  }|dkD  rx|
r|r-|D ]  }t        | |||	        n|r|D ]  }t        | |||	        d|g fS )	NrQ   rV   Tr6  r   c              3   l   K   | ]+  }|j                   t              d  j                         - y wra   )rV   r  r)  ).0r   rA  r+  s     rE   	<genexpr>z&create_count_of_vms.<locals>.<genexpr>f  s,     %gY[rwws9~'?'E'El'S%gs   14r   rR   )r   r   r:  rH  r  r   r.  r   r&  r   wait_for_holdwait_for_running)r@   rW   rj   r:  r  r  r   r  waitwait_timeoutr  r   r   new_vms_listr   r*  r@  new_vm_name
next_indexnew_vm_dict	new_vm_idnew_vmr   rA  r+  s                          @@rE   create_count_of_vmsrW  Q  s    LG!%%fb1R| "==%L"})+FVW4EtL8S\L89	!%%g_f%g!g
!)!-,-C\RJ"))*5:%K"- +_k9Vh 0-R  OOG,	fi0F#
 !)" " @ffb,?@ " C \BC r!!rG   c                    t        |||      }|t        |      z
  }|dk7  }g }g }|}| j                  r|||fS |dkD  r!t        | |||||||	|||||      \  }}}||z  }n|dk  rzg }|dk  r;|j	                  d      }|j                  |       t        | |||
       |dz  }|dk  r;|r|D ]  }t        | |||        |}t        |      }|D cg c]	  }||vs| }}|||fS c c}w r(  )	rH  r  r   rW  r9  r   terminate_vmwait_for_doner>  )r@   rW   rj   exact_countr  count_attributes_dictr  count_labels_listr   r  hardrO  rP  r  r   r   r@  vm_count_diffr   rQ  instances_listtagged_instances_listtagged_instancesold_vms_listold_vmr   old_vms_sets                              rE   create_exact_count_of_vmsrf    s_    (0EGXYG#g,.Mq GLN#(===q4GPVXcer  uDHSU^`rtx  {GHXZgix5z1!1 	/		a[[^F'6QM	 a " @ffb,?@ &,'.5 O;9N O ON$999 !Ps   	C"C")INITPENDINGHOLDr   STOPPED	SUSPENDEDDONErQ   POWEROFF
UNDEPLOYEDCLONINGCLONING_FAILURE)LCM_INITPROLOGr   RUNNINGMIGRATE	SAVE_STOPSAVE_SUSPENDSAVE_MIGRATEPROLOG_MIGRATEPROLOG_RESUMEEPILOG_STOPEPILOGSHUTDOWNSTATE13STATE14CLEANUP_RESUBMITUNKNOWNHOTPLUGSHUTDOWN_POWEROFFBOOT_UNKNOWNBOOT_POWEROFFBOOT_SUSPENDEDBOOT_STOPPEDCLEANUP_DELETEHOTPLUG_SNAPSHOTHOTPLUG_NICHOTPLUG_SAVEASHOTPLUG_SAVEAS_POWEROFFHOTPULG_SAVEAS_SUSPENDEDSHUTDOWN_UNDEPLOYc           	         dd l }|j                         }|j                         |z
  |k  r	|j                  j                  |j                        }|j                  }|j
                  } |||      r|S |t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d      fvr| j                  dt        |   z   	       |j                  d
       |j                         |z
  |k  r	| j                  d	       y )Nr   rg  rh  ri  r   ro  rm  z"Action is unsuccessful. VM state: r:   rR   zWait timeout has expired!)
r   r   rT   ri   r   r   r   r   r>   sleep)	r@   rW   r   rP  state_predicater   
start_timer   r   s	            rE   wait_for_stater    s   J99;#|
3YY^^BEE"LL	5),I9??62IOOI4NPYP_P_`fPg$??84iooi6PR[RaRablRmo o!E	RWHX!XY

1 99;#|
3 45rG   c                 "    t        | |||d       S )Nc                 f    | t         j                  d      fv xr |t        j                  d      fv S )Nr   rs  )r   r   r   r   r   s     rE   rc   z"wait_for_running.<locals>.<lambda>  s5    &+	0I/J&J&yy]g]m]mnw]x\yOy rG   r  r@   rW   r   rP  s       rE   rN  rN    s    &&"l ={ | |rG   c                 "    t        | |||d       S )Nc                 2    | t         j                  d      fv S )Nrl  r   r   r  s     rE   rc   zwait_for_done.<locals>.<lambda>      V[`i`o`opv`w_xVx rG   r  r  s       rE   rZ  rZ        &&"l<yzzrG   c                 "    t        | |||d       S )Nc                 2    | t         j                  d      fv S )Nri  r  r  s     rE   rc   zwait_for_hold.<locals>.<lambda>  r  rG   r  r  s       rE   rM  rM    r  rG   c                 "    t        | |||d       S )Nc                 2    | t         j                  d      fv S )Nrm  r  r  s     rE   rc   z#wait_for_poweroff.<locals>.<lambda>  s    V[`i`o`opz`{_|V| rG   r  r  s       rE   wait_for_poweroffr    s    &&"l<}~~rG   c                     d}|s|S d}| j                   sP|r(|j                  j                  d|j                         |S |j                  j                  d|j                         |S )NFTzterminate-hard	terminate)r   r   actionri   )r@   rW   r   r^  r   s        rE   rY  rY    s_    GGII-ruu5 N II["%%0NrG   c                 <    d}|D ]  }t        | |||      xs |} |S r   )rY  r@   rW   r   r^  r   r   s         rE   terminate_vmsr    s4    G Dvvr48CGD NrG   c                    |j                   j                  |j                        }d}|j                  }|j                  }|t
        j                  d      t
        j                  d      fvr|t        j                  d      fvrd}|r\| j                  sP|s(|j                   j                  d|j                         |S |j                   j                  d|j                         |S )NFr|  r  rm  Tpoweroffzpoweroff-hard)
r   rT   ri   r   r   r   r   r   r   r  )r@   rW   r   r^  r   r   r   s          rE   poweroff_vmr    s    		BGIHHE))*5z7G7GH[7\]]bgpypp  AK  qL  pM  cMv((IIZ/ N II_bee4NrG   c                 <    d}|D ]  }t        | |||      xs |} |S r   )r  r  s         rE   poweroff_vmsr    s4    G Cffb$7B7C NrG   c                 v   | j                   s|D ]  }|j                  j                  |j                        }|j                  }|j
                  }|t        j                  d      fvsY|t        j                  d      fvsrt        | |||        |D ]  }t        | |||        |D ]  }t        | ||        y)Nr  rm  T)r   r   rT   ri   r   r   r   r   r   r  r  	resume_vm)r@   rW   r   rP  r^  r   r   r   s           rE   
reboot_vmsr    s     	6B&BIHHE!1!12E!F GGEZcZiZijtZuYvLvFFB5	6  	@Bffb,?	@  	*Bffb)	* rG   c                    |j                   j                  |j                        }d}|j                  }|t        j                  d      fv rt        | ||      }|S |j                  }|t        j                  d      k(  r| j                  d       |t        j                  d      fvrd}|r2| j                  s&|j                   j                  d|j                         |S )	NFri  r  zCannot perform action 'resume' because this action is not available for LCM_STATE: 'SHUTDOWN_POWEROFF'. Wait for the VM to shutdown properlyr:   rs  Tresume)r   rT   ri   r   r   r   
release_vmr   r   r>   r   r  )r@   rW   r   r   r   r   s         rE   r  r  *  s    		BGHHE())VVR0IJ$$%899 d 	e)))455v((		255)NrG   c                 :    d}|D ]  }t        | ||      xs |} |S r   )r  )r@   rW   r   r   r   s        rE   
resume_vmsr  @  s0    G ;FFB/:7; NrG   c                 .   |j                   j                  |j                        }d}|j                  }|t        j                  d      k7  r| j                  d       nd}|r2| j                  s&|j                   j                  d|j                         |S )NFri  zgCannot perform action 'release' because this action is not available because VM is not in state 'HOLD'.r:   Trelease)	r   rT   ri   r   r   r   r>   r   r  )r@   rW   r   r   r   s        rE   r  r  I  s|    		BGHHE	'' > 	? v((		BEE*NrG   c                     |j                  d      rNdd l}|j                  d|j                  d            (| j                  d|j                  d      z   dz          y y y )NrV   r   z	^[^#]+#*$zIllegal 'NAME' attribute: 'zZ' .Signs '#' are allowed only at the end of the name and the name cannot contain only '#'.r:   )r   rematchr>   )r@   r   r  s      rE   check_name_attributer  Z  sc    ~~f88L*.."89A!>PVAW!Wy"z { B rG   )r   r   r1   r   r   r   r   r3   r4   r7   
CREATED_BYCPU_COST	DISK_COSTMEMORY_COSTr   VMIDAUTOMATIC_DS_REQUIREMENTSDEPLOY_FOLDERr  c                     |j                         D ]#  }|t        v s| j                  d|z   dz          % t        | |       y )NzRestricted attribute `z$` cannot be used when filtering VMs.r:   )keysTEMPLATE_RESTRICTED_ATTRIBUTESr>   r  )r@   r   r4  s      rE   check_attributesr  g  sL      j00!9C!?Bh!hij ,rG   c                    |j                  d      s| j                  d       |j                  d      }|j                  dd      }| j                  sm|j                  t        j                  d      k7  r| j                  d       	 |j                  j                  |j                  ||dd	       t        | |||       y y # t        j                  $ r%}| j                  t        |             Y d }~Cd }~ww xY w)
Nr   z/Key 'name' is required for 'disk_saveas' optionr:   disk_idr   rm  zG'disksaveas' option can be used only when the VM is in 'POWEROFF' stater1   rP   )r   r>   r   r   r   r   r   
disksaveasri   r   r  r   r  )r@   rW   r   disk_saveasrP  
image_namer  r%  s           rE   disk_save_asr  o  s    ??6"NO(Jooi+G88yz22!jk	)II  T2F 	&&"l; 
 !! 	)Q((	)s   )C C9C44C9c                    | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }|st        j                  j                  d      }|st        j                  j                  d      }|st        j                  j                  d      }|s|st        j                  j                  d      }|>t        j                  j                  t        j                  j                  d      d	d
      }	 t        |d      5 }|j                         j                         }d d d        j                  d      d   }|j                  d      d   }|s| j                  d       ddlm}  |dd      } ||||      S # 1 sw Y   _xY w# t        t        f$ r | j                  d|z         Y `t        $ r | j                  d|z         Y w xY w)Napi_urlapi_usernameapi_passwordONE_URLONE_USERNAMEONE_PASSWORDONE_AUTHHOMEz.oneone_authr:r   rR   z,Could not find or read ONE_AUTH file at '%s'r:   z,Error occurs when read ONE_AUTH file at '%s'z-Opennebula API url (api_url) is not specified)
namedtupleauth)urlusernamepassword)paramsr   osenvironpathr  openreadrstripr1  OSErrorIOErrorr>   	Exceptioncollectionsr  )	r@   r  r  r  authfilefp
authstringr  auth_paramss	            rE   get_connection_infor    s   
--

I
&C}}  0H}}  0HjjnnY'::>>.1::>>.1zz~~j1H77<<

v(>
Sb(C( 4B!#!1!1!3J4%++C03%++C03
 LM&V%DEK3HEE4 4 W% b  &TW_&_ a b  &TW_&_ abs0   F. %F"0F. "F+'F. .$G4G43G4c                    0 i dddddddddddddd	dd
gdddddddddddddddddg dddddddddddddddddddddddddddddddddd dd!ddddddd"ddddddg dd#d$d%dddddi d#ddd#dg ddd$dddd"d&d#idddd&d#id'} t        | g d(g d)g d*g d+d	d,gd	d-gd	d.gd/d-gd/d.gd,d0gd0dgd	dgd	d!gd	dgd	d1gd	d2gd3d1ggd4      }t        s|j                  d56       t        |      }|j                  }|j                  d	      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }	|j                  d      }
|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d!      }|j                  d1      }|j                  d7      }|j                  d8      }|j                  d2      }|j                  d0      }|j                  d,      }|j                  d-      }|j                  d9      }|j                  d.      }|j                  d:      }|j                  d/      }|j                  d3      }|j                  d;      }|j                  r|j                  s|j                  d<       n;t        j                  |j                  |j                  d=z   |j                  z   >      0|r=|j                         D  ci c]  \  }} |j                         |  }}} t        ||       |re|j                         D  ci c]  \  }} |j                         |  }}} |s&d?d l}!|j                  d@       |!j                  |      }t        ||       |rt!        ||       |r|s|j                  dA       |}d }"||rHt#        |0||      }"|"8||j                  dBt%        |      z   6       n|r|j                  dC|z   6       d }#|s|rZt'        |0||      }#|#9|r|j                  dDt%        |      z   6       n)|r'|j                  dE|z   6       ndFt%        |#      z   |dG<   |r|"|j                  dH6       ||s|s|j                  dI6       |s|r||j                  dJ6       |"|dk7  r|j                  dK6       |r!t%        t)        t+        ||                  |dL<   |rt%        |      |dM<   |rt%        |      |dN<   ||dk7  r|j                  dO6       ||d?k  r|j                  dP6       |d?k  r|j                  dQ6       |	(d?d l}$|$j/                  dR|	      |j                  dS6       |"t1        |0|"|||||||||||||      \  }%}&}'|'}(nU|"$|dk(  rt3        |0|"||||||||||      \  }%}&}'|&}(n/|s|s|s|j                  dT6       |s|s|s|s|r|j                  dU6       |r|dVvr|j                  dW6       g }(d})d}%|rt5        |0||      }(nd})t7        0||      }(t9        |(      d?k(  r|dXk7  r|dk7  r|j                  dY6       t9        |(      d?k(  r|dk(  r|)s|j                  dZ6       |)r|dXk(  r|j                  d[6       |dXk(  rt;        |0|(|      }%n;|d\k(  rt=        |0|(||      }%n&|d]k(  rt?        |0|(|      }%n|d^k(  rtA        |0|(      }%|(}&g }'|	tC        |0|(|	      xs |%}%|
|tE        |0|(|
|      xs |%}%|"|tG        |0|(|      xs |%}%|r@|jH                  s4|dk7  r/tJ        tL        tN        tL        d_}*|(D ]  }+|+ |*|   |0|+|        |4t9        |(      d?k(  r|j                  d`6       tQ        |0|(d?   ||       d}%tS        0fda|&D              },tS        db |&D              }-tS        0fdc|'D              }.|%|,|-|.dd}/ |jT                  dei |/ y c c} }w c c} }w )fNr  Fr   )requiredtyper  r  T)r  r  no_loginstance_idsr   r   r   )r  aliasesr  elementsr\   rj   r  bool)defaultr  r   present)r  r   rebooted
poweredoffrunning)r  choicesr  r   r   r   rO  rP  i,  r^  r   r   r  r   )r  r  r  dict)r  r  r  rR   r  )r   rt   r}   r   r:  r[  r   count_attributesr   count_labelsr  
persistentrJ   )rj   r\   r  )rj   r\   r  )r  r  r:  )r  r  r:  r[  r   r   r  r:  r   r   r  )argument_specmutually_exclusivesupports_check_modez#This module requires pyone to work!r:   r}   rt   r  r  rJ   zCredentials missingr  )sessionr   zWhen you pass `count_attributes` without `attributes` option when deploying, `attributes` option will have same values implicitly.zvWhen you pass `count_labels` without `labels` option when deploying, `labels` option will have same values implicitly.z'There is no template with template_id: z There is no template with name: z)There is no datastore with datastore_id: z!There is no datastore with name: zID=SCHED_DS_REQUIREMENTSz7Option `exact_count` needs template_id or template_namezZEither `count_attributes` or `count_labels` has to be specified with option `exact_count`.zbOption `exact_count` has to be specified when either `count_attributes` or `count_labels` is used.z.Only state 'present' is valid for the templater   r   r   z>The `exact_count` option is valid only for the `present` statez#`exact_count` cannot be less than 0z `count` has to be greater than 0z
^[0-7]{3}$zNOption `mode` has to have exactly 3 digits and be in the octet format e.g. 600zDAt least one of `instance_ids`,`attributes`,`labels` must be passed!zgParameters as `memory`, `cpu`, `vcpu`, `disk_size` and `networks` you can only set when deploying a VM!)r  r  r   r  zlThe 'hard' option can be used only for one of these states: 'rebooted', 'poweredoff', 'absent' and 'present'r   zRThere are no instances with specified `instance_ids`, `attributes` and/or `labels`z5There are no instances with specified `instance_ids`.z9Option `instance_ids` is required when state is `absent`.r  r  r  )r   r  r  r  z(There is no VM whose disk will be saved.c              3   <   K   | ]  }|t        |        y wra   r   rK  r   
one_clients     rE   rL  zmain.<locals>.<genexpr>  s     \RR^[R0\   c              3   :   K   | ]  }||j                     y wra   rh   )rK  r   s     rE   rL  zmain.<locals>.<genexpr>  s     J22>Js   c              3   <   K   | ]  }|t        |        y wra   r  r  s     rE   rL  zmain.<locals>.<genexpr>  s     jB[][iK
B7jr  )r   	instancesinstances_idsrb   )+r   	HAS_PYONEr>   r  r  r   r  r  warnr   	OneServerr  r<   upperr  r=  rF   ro   r   r   r   r  r  r  rf  rW  r   rH  r  r  r  r  r  r   r   r   r   rZ  rN  r  r  r   	exit_json)1fieldsr@   r  r  r  requested_template_namerequested_template_idput_vm_on_holdr   r   r   r   rO  rP  r^  r   r   r   r   requested_datastore_idrequested_datastore_namer   r:  r[  r   r  r   r  r  r  rJ   r4  rL   r=  rj   r}   r  r   r`  ra  r   taggedwait_forr   r  r  rb  resultr  s1                                                   @rE   mainr    st
   #u5#UE:# 	UETJ# 	Uw\ab	#
 	eU;# 	E59# 	v># 	 Q
# 	UE2# 	6# 	6#  	D&1!#" 	C7##$ 	E62%#& 	ue4'#( 	E73)#* 	UE2+#, #(UK',e<%*E: "FG.$)59"$f5).? &eD%*FN'"'8v&E#FJ ONLH .> .= .9 -|< -x8 -w7 '0 .68P .9NK;X .
; ,k:/" 04%5F( BCv&D]]F::n-L$jj9"JJ}5ZZ 23NJJwE**V$Kzz*%Hzz*%H::fD::n-L::fDZZ!F
**U
C::fD

;'I#ZZ7%zz*:;zz*%HJJwE**]+KL)Jzz"45ZZ!F::n-L**]+KL)JL)JMMdmm)*__TXXt}}s7JT]]7Z[
;E;K;K;MNZS%ciik5(N
N,AQAWAWAYZ:3CIIK.ZZKK  ]  ^#34J!12,F  M  	N K(,C%fj:OQhi$0  %NQTUjQk%k l(  %GJa%a b L!9'
<RTlm%  %PSVWmSn%n o)  %HKc%c d27#l:K2KJ./{*VW(8LyzLk.A  B  	C5I#5MN"3~ff'E#FG
8
H
5 Y
65I#5]^;?BCz?@88L+.6!qr9RSY[egrt  BLScekmy  |ES[]acgiu  xF  HR  T^:_6!6 $		 Ui%79LVU_alnsMWY_ajltvz  }IM[]gis:u6!6
  
f!ghSDI  "K  LE!PP  "P  Q ULICF+J
FKCs8q=Uh.5I3E!uvs8q=Ui/!XYex'!\]H#FJTBGj  S,MGl""6:sDAGi S9G "$VZkJUgx3"6:sHhOZSZz5VZjALWF%%%9*<#(+'	
  	FB~
BE	F s8q=!KLVZQlK \>\\IJJJMjBWjj y=n~FFv O [s   aa$__main__)F)D
__future__r   r   r   r  __metaclass__DOCUMENTATIONEXAMPLESRETURNr   r  ImportErrorr  ansible.module_utils.basicr   0ansible.module_utils.common.dict_transformationsr   Eansible_collections.community.general.plugins.module_utils.opennebular   r	   r=   rF   rM   r^   re   rk   ro   rv   rz   r~   r   r   r   r   r   r   r   r   r   r  r&  r.  r   rH  rW  rf  r   r   r  rN  rZ  rM  r  rY  r  r  r  r  r  r  r  r  r  r  r  r  r  __name__r  rG   rE   <module>r(     s   C BHVbHS
jI
 
 4 G a
 k:& Fe_I;3\
 	n *[W&_[	HV	(*6(#V# 1h1"h*:Z N	l
6(|
{{"&*,"{"q 
-<"#FL@F zF k   Is   C* *C43C4