
    Vh                        d dl mZmZmZ eZdZdZdZd dl	Z		 d dl
mZ d dlmZ dZd d
lmZ dZd ZdZg dZg dZg Zd	ad	a G d de      Z G d de      Zd Zd Zd Zd Z d Z!d Z"e#dk(  r e"        yy# e$ r d	ZY Yw xY w)    )absolute_importdivisionprint_functiona  
module: rhevm
short_description: RHEV/oVirt automation
description:
  - This module only supports oVirt/RHEV version 3.
  - A newer module M(ovirt.ovirt.ovirt_vm) supports oVirt/RHV version 4.
  - Allows you to create/remove/update or powermanage virtual machines on a RHEV/oVirt platform.
requirements:
  - ovirtsdk
author:
  - Timothy Vandenbrande (@TimothyVandenbrande)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  user:
    description:
      - The user to authenticate with.
    type: str
    default: admin@internal
  password:
    description:
      - The password for user authentication.
    type: str
    required: true
  server:
    description:
      - The name/IP of your RHEV-m/oVirt instance.
    type: str
    default: 127.0.0.1
  port:
    description:
      - The port on which the API is reachable.
    type: int
    default: 443
  insecure_api:
    description:
      - A boolean switch to make a secure or insecure connection to the server.
    type: bool
    default: false
  name:
    description:
      - The name of the VM.
    type: str
  cluster:
    description:
      - The RHEV/oVirt cluster in which you want you VM to start.
    type: str
    default: ''
  datacenter:
    description:
      - The RHEV/oVirt datacenter in which you want you VM to start.
    type: str
    default: Default
  state:
    description:
      - This serves to create/remove/update or powermanage your VM.
    type: str
    choices: [absent, cd, down, info, ping, present, restarted, up]
    default: present
  image:
    description:
      - The template to use for the VM.
    type: str
  type:
    description:
      - To define if the VM is a server or desktop.
    type: str
    choices: [desktop, host, server]
    default: server
  vmhost:
    description:
      - The host you wish your VM to run on.
    type: str
  vmcpu:
    description:
      - The number of CPUs you want in your VM.
    type: int
    default: 2
  cpu_share:
    description:
      - This parameter is used to configure the CPU share.
    type: int
    default: 0
  vmmem:
    description:
      - The amount of memory you want your VM to use (in GB).
    type: int
    default: 1
  osver:
    description:
      - The operating system option in RHEV/oVirt.
    type: str
    default: rhel_6x64
  mempol:
    description:
      - The minimum amount of memory you wish to reserve for this system.
    type: int
    default: 1
  vm_ha:
    description:
      - To make your VM High Available.
    type: bool
    default: true
  disks:
    description:
      - This option uses complex arguments and is a list of disks with the options V(name), V(size), and V(domain).
    type: list
    elements: str
  ifaces:
    description:
      - This option uses complex arguments and is a list of interfaces with the options V(name) and V(vlan).
    type: list
    elements: str
    aliases: [interfaces, nics]
  boot_order:
    description:
      - This option uses complex arguments and is a list of items that specify the bootorder.
    type: list
    elements: str
    default: [hd, network]
  del_prot:
    description:
      - This option sets the delete protection checkbox.
    type: bool
    default: true
  cd_drive:
    description:
      - The CD you wish to have mounted on the VM when O(state=cd).
    type: str
  timeout:
    description:
      - The timeout you wish to define for power actions.
      - When O(state=up).
      - When O(state=down).
      - When O(state=restarted).
    type: int
a  
vm:
  description: Returns all of the VMs variables and execution.
  returned: always
  type: dict
  sample:
    {
      "boot_order": [
        "hd",
        "network"
      ],
      "changed": true,
      "changes": [
        "Delete Protection"
      ],
      "cluster": "C1",
      "cpu_share": "0",
      "created": false,
      "datacenter": "Default",
      "del_prot": true,
      "disks": [
        {
          "domain": "ssd-san",
          "name": "OS",
          "size": 40
        }
      ],
      "eth0": "00:00:5E:00:53:00",
      "eth1": "00:00:5E:00:53:01",
      "eth2": "00:00:5E:00:53:02",
      "exists": true,
      "failed": false,
      "ifaces": [
        {
          "name": "eth0",
          "vlan": "Management"
        },
        {
          "name": "eth1",
          "vlan": "Internal"
        },
        {
          "name": "eth2",
          "vlan": "External"
        }
      ],
      "image": false,
      "mempol": "0",
      "msg": [
        "VM exists",
        "cpu_share was already set to 0",
        "VM high availability was already set to True",
        "The boot order has already been set",
        "VM delete protection has been set to True",
        "Disk web2_Disk0_OS already exists",
        "The VM starting host was already set to host416"
      ],
      "name": "web2",
      "type": "server",
      "uuid": "4ba5a1be-e60b-4368-9533-920f156c817b",
      "vm_ha": true,
      "vmcpu": "4",
      "vmhost": "host416",
      "vmmem": "16"
    }
a	  
- name: Basic get info from VM
  community.general.rhevm:
    server: rhevm01
    user: '{{ rhev.admin.name }}'
    password: '{{ rhev.admin.pass }}'
    name: demo
    state: info

- name: Basic create example from image
  community.general.rhevm:
    server: rhevm01
    user: '{{ rhev.admin.name }}'
    password: '{{ rhev.admin.pass }}'
    name: demo
    cluster: centos
    image: centos7_x64
    state: present

- name: Power management
  community.general.rhevm:
    server: rhevm01
    user: '{{ rhev.admin.name }}'
    password: '{{ rhev.admin.pass }}'
    cluster: RH
    name: uptime_server
    image: centos7_x64
    state: down

- name: Multi disk, multi nic create example
  community.general.rhevm:
    server: rhevm01
    user: '{{ rhev.admin.name }}'
    password: '{{ rhev.admin.pass }}'
    cluster: RH
    name: server007
    type: server
    vmcpu: 4
    vmmem: 2
    ifaces:
      - name: eth0
        vlan: vlan2202
      - name: eth1
        vlan: vlan36
      - name: eth2
        vlan: vlan38
      - name: eth3
        vlan: vlan2202
    disks:
      - name: root
        size: 10
        domain: ssd-san
      - name: swap
        size: 10
        domain: 15kiscsi-san
      - name: opt
        size: 10
        domain: 15kiscsi-san
      - name: var
        size: 10
        domain: 10kiscsi-san
      - name: home
        size: 10
        domain: sata-san
    boot_order:
      - network
      - hd
    state: present

- name: Add a CD to the disk cd_drive
  community.general.rhevm:
    user: '{{ rhev.admin.name }}'
    password: '{{ rhev.admin.pass }}'
    name: server007
    cd_drive: rhev-tools-setup.iso
    state: cd

- name: New host deployment + host network configuration
  community.general.rhevm:
    password: '{{ rhevm.admin.pass }}'
    name: ovirt_node007
    type: host
    cluster: rhevm01
    ifaces:
      - name: em1
      - name: em2
      - name: p3p1
        ip: 172.31.224.200
        netmask: 255.255.254.0
      - name: p3p2
        ip: 172.31.225.200
        netmask: 255.255.254.0
      - name: bond0
        bond:
          - em1
          - em2
        network: rhevm
        ip: 172.31.222.200
        netmask: 255.255.255.0
        management: true
      - name: bond0.36
        network: vlan36
        ip: 10.2.36.200
        netmask: 255.255.254.0
        gateway: 10.2.36.254
      - name: bond0.2202
        network: vlan2202
      - name: bond0.38
        network: vlan38
    state: present
N)API)paramsTF)AnsibleModule      desktophostserver)absentcddowninfopingpresentrestartupc                       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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')(RHEVConnzConnection to RHEV-Mc                    || _         |j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }d|d|}	 t        |||t	        |            }|j                          || _        y # t        $ r t        d	      w xY w)
Nuserpasswordr   portinsecure_apizhttps://:)urlusernamer   insecurezFailed to connect to RHEV-M.)moduler   getr   strtestconn	Exception)	selfr"   r   r   r   r   r   r   apis	            k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/rhevm.py__init__zRHEVConn.__init__h  s    }}  (==$$Z0""8,}}  (}}((8!'.	<#x#lJ[\CHHJDI 	<:;;	<s   /C Cc                 8    | j                   j                          y N)r&   
disconnectr(   s    r*   __del__zRHEVConn.__del__z  s    		    c                    	 t        j                  || j                  j                  j	                  |      | j                  j
                  j	                  |      t        j                  d            }| j                  j                  j                  |       t        d       t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)NnameT)clone)r4   clustertemplatedisksVM is createdFailed to create VMF)r   VMr&   clustersr#   	templatesDisksvmsaddsetMsg
setChangedr'   r$   	setFailed)r(   r4   r6   r7   vmparamses         r*   createVMimagezRHEVConn.createVMimage}  s    	yy		**..G.<,,00h0?ll.	H IIMMh'?#L 	()3q6NK		s   B/B2 2	C.;)C))C.c                    	 t        j                  || j                  j                  j	                  |      t        j
                  |      | j                  j                  j	                  d      |      }| j                  j                  j                  |       t        d       t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)	Nr3   )type_Blank)r4   r6   osr7   rH   r9   Tr:   F)r   r;   r&   r<   r#   OperatingSystemr=   r?   r@   rA   rB   r'   r$   rC   )r(   r4   r6   rJ   
actiontyperD   rE   s          r*   createVMzRHEVConn.createVM  s    	yy		**..G.<))3,,00g0> H IIMMh'?#L 	()3q6NK		s   B0B3 3	C/<)C**C/c	                    | j                  |      }	t        j                  |dt        |      z  d||||t        j                  | j                  |      g            }
	 |	j                  j                  |
       |	j                          t        d|z          t                	 |	j                  j                  |	      }d
}|j                  j                   dk7  r|	j                  j                  |	      }|dk(  r8t        d|dt        |j                  j                                t               |d
z  }t#        j$                  d       |j                  j                   dk7  rt        d|z   dz          y# t        $ r9}t                t        d|z   dz          t        t        |             Y d }~yd }~ww xY w# t        $ r9}t                t        d|z   dz          t        t        |             Y d }~yd }~ww xY w)N   @T)storage_domain)r4   sizewipe_after_deletesparse	interfaceformatbootablestorage_domainszSuccessfully added disk Error attaching z;disk, please recheck and remove any leftover configuration.Fr3   r	   okd   zError, disk , state r
   z
The disk  
 is ready.Error getting the state of .)get_VMr   DiskintStorageDomains
get_domainr8   r@   updaterA   rB   r'   rC   r$   r#   statusstatetimesleep)r(   vmnamedisknamedisksize
diskdomaindiskinterface
diskformatdiskallocationtypediskbootr;   newdiskrE   currentdiskattempts                 r*   
createDiskzRHEVConn.createDisk  s   [[ ++#c(m3"%#"11 $
 ;<
		HHLL!IIK-89L	((,,H,5KG$$**d2 hhlll9c>3{GYGYG_G_C`ab#+%qLGJJqM $$**d2 <(*\9: /  	K%03ppq3q6N		$  	K08;cAB3q6N		s8    AE: $CF? (F? :	F</F77F<?	H/G<<Hc                 &   | j                  |      }| j                  |j                  j                        }| j	                  |j
                  j                        }t        j                  ||j                  j                  |      |      }	 |j                  j                  |       |j                          t        d|z          t                	 |j                  j                  |      }
d}|
j$                  durx|j                  j                  |      }
|d	k(  r.t        d
|dt#        |
j$                               t               |dz  }t'        j(                  d       |
j$                  durxt        d|z   dz          y# t        $ r9}	t!                t        d|z   dz          t        t#        |	             Y d }	~	yd }	~	ww xY w# t        $ r9}	t!                t        d|z   dz          t        t#        |	             Y d }	~	yd }	~	ww xY w)Nr3   )r4   networkrT   zSuccessfully added iface rX   z= iface, please recheck and remove any leftover configuration.Fr	   TrZ   zError, iface r[   r
   zThe iface  r\   r]   r^   )r_   get_cluster_byidr6   idget_DC_byiddata_centerr   NICnetworksr#   nicsr@   rd   rA   rB   r'   rC   r$   activerg   rh   )r(   ri   nicnamevlanrT   r;   CLUSTERDCnewnicrE   
currentnicrs   s               r*   	createNICzRHEVConn.createNIC  s   [[ ''

6g11445KKOOO.
		GGKKIIK.89L	'2JG##4/WW[[g[6
c>'3zGXGXCYZ[#+%qLGJJqM ##4/ =7*\9: /  	K%/2qqr3q6N		$  	K07:S@A3q6N		s8   AF	 B#G 7G 		G/GG	H/HHc                 N    | j                   j                  j                  |      S Nr3   r&   datacentersr#   )r(   dc_names     r*   get_DCzRHEVConn.get_DC  s     yy$$((g(66r1   c                 N    | j                   j                  j                  |      S Nrx   r   )r(   dc_ids     r*   ry   zRHEVConn.get_DC_byid  s     yy$$((E(22r1   c                 N    | j                   j                  j                  |      S r   )r&   r?   r#   )r(   vm_names     r*   r_   zRHEVConn.get_VM  s    yy}}  g ..r1   c                 N    | j                   j                  j                  |      S r   r&   r<   r#   )r(   
cluster_ids     r*   rw   zRHEVConn.get_cluster_byid       yy!!%%%44r1   c                 N    | j                   j                  j                  |      S r   r   )r(   cluster_names     r*   get_clusterzRHEVConn.get_cluster  s     yy!!%%<%88r1   c                 N    | j                   j                  j                  |      S r   r&   storagedomainsr#   )r(   dom_ids     r*   get_domain_byidzRHEVConn.get_domain_byid  s     yy''++v+66r1   c                 N    | j                   j                  j                  |      S r   r   )r(   domain_names     r*   rc   zRHEVConn.get_domain
  s     yy''+++==r1   c                 L    | j                   j                  j                  |      S r-   )r&   r8   r#   )r(   disks     r*   get_diskzRHEVConn.get_disk  s    yy""4((r1   c                 V    | j                  |      j                  j                  |      S r-   )r   r|   r#   )r(   r   network_names      r*   get_networkzRHEVConn.get_network  s"    {{7#,,00>>r1   c                 N    | j                   j                  j                  |      S r   )r&   r|   r#   )r(   
network_ids     r*   get_network_byidzRHEVConn.get_network_byid  r   r1   c                 V    | j                  |      j                  j                  |      S r-   )r_   r}   r#   )r(   r   nic_names      r*   get_NICzRHEVConn.get_NIC  s"    {{7#((,,X66r1   c                 N    | j                   j                  j                  |      S r   r&   hostsr#   )r(   	host_names     r*   get_HostzRHEVConn.get_Host  s    yy""	"22r1   c                 N    | j                   j                  j                  |      S r   r   )r(   host_ids     r*   get_Host_byidzRHEVConn.get_Host_byid  s    yy""g"..r1   c                 4   | j                  |      }t        t        |      dz  dz  dz        |_        	 |j                          t	        d       t                y# t        $ r3}t	        d       t	        t        |             t                Y d }~yd }~ww xY w)N   zThe Memory has been updated.TzFailed to update memory.F)	r_   ra   memoryrd   rA   rB   r'   r$   rC   )r(   r4   r   r;   rE   s        r*   
set_MemoryzRHEVConn.set_Memory  sz    [[Fd*T1D89			IIK12L 	-.3q6NK		s   %A 	B$)BBc                 6   | j                  |      }t        |      dz  dz  dz  |j                  _        	 |j	                          t        d       t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)Nr   z#The memory policy has been updated.TzFailed to update memory policy.F)
r_   ra   memory_policy
guaranteedrd   rA   rB   r'   r$   rC   )r(   r4   r   r;   rE   s        r*   set_Memory_PolicyzRHEVConn.set_Memory_Policy-  s}    [[&)-&84&?$&F&M#		IIK89L 	453q6NK		s   %A 	B%)BBc                 8   | j                  |      }t        |      |j                  j                  _        	 |j                          t        d       t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)Nz$The number of CPUs has been updated.Tz$Failed to update the number of CPUs.F)r_   ra   cputopologycoresrd   rA   rB   r'   r$   rC   )r(   r4   r   r;   rE   s        r*   set_CPUzRHEVConn.set_CPU;  so    [[ #C		IIK9:L 	9:3q6NK		s   %A 	B&)BBc                    | j                  |      }t        |      |_        	 |j                          t	        d       t                y# t        $ r3}t	        d       t	        t        |             t                Y d }~yd }~ww xY w)NzThe CPU share has been updated.TzFailed to update the CPU share.F)	r_   ra   
cpu_sharesrd   rA   rB   r'   r$   rC   )r(   r4   	cpu_sharer;   rE   s        r*   set_CPU_sharezRHEVConn.set_CPU_shareI  sf    [[I		IIK45L 	453q6NK		s   %A	 		B)B  Bc                    | j                  |      }t        d|z          |j                         |k7  r(	 |j                  |       t        d       t	                nt        d       t        |j                        dt        |      z  k  r*	 dt        |      z  |_	        t        d       t	                nEt        |j                        dt        |      z  kD  rt        d	       t                yt        d
       t        |j                        t        |      k7  r	 ||_
        t        d       t	                yt        d       y# t
        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w# t
        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w# t
        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)NzChecking disk z$Updated the boot option on the disk.z*Failed to set the boot option on the disk.Fz&The boot option of the disk is correctrO   zUpdated the size of the disk.z&Failed to update the size of the disk.z Shrinking disks is not supportedzThe size of the disk is correctz"Updated the interface of the disk.z+Failed to update the interface of the disk.z$The interface of the disk is correctT)r   rA   get_bootableset_bootablerB   r'   r$   rC   ra   rQ   rT   )r(   rj   rk   rm   rp   DISKrE   s          r*   set_DiskzRHEVConn.set_DiskW  s   }}X&(*+(*!!(+=> ;<tyy>/#h-?@/#h-?	67 ^1CMAB56K45t~~#m"44!.;<  9:G  CDs1v	  ?@s1v	   DEs1v	sG   &E (F G 	F )E;;F 	F?)F::F?	G>)G99G>c                 f   | j                  ||      }| j                  |      }| j                  |j                  j                        }| j                  |j                  j                        }	| j                  t        |	j                        |      }
t                |j                  |k7  r||_	        t        d|z          t                t        |j                  j                        t        |
j                        k7  r)|j                  |
       t        d|z          t                |j                  |k7  r||_        t        d|z          t                	 |j!                          t        d       y# t"        $ r3}t        d       t        t        |             t%                Y d }~yd }~ww xY w)NzUpdating iface name to zUpdating iface network to zUpdating iface interface to z$iface has successfully been updated.zFailed to update the iface.FT)r   r_   rw   r6   rx   ry   rz   r   r$   r4   	checkFailrA   rB   rv   set_networkrT   rd   r'   rC   )r(   ri   r   newnamer   rT   r{   r;   r   r   NETWORKrE   s               r*   set_NICzRHEVConn.set_NIC  sE   ll67+[[ ''

6g11445""3rww<688wCH,w67Ls{{~~#gjj/1OOG$/$67L==I%%CM1I=>L	JJL9:   	013q6NK		s   E4 4	F0=)F++F0c                     | j                  |      }||_        	 |j                          t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)Nz#Failed to update delete protection.FT)r_   delete_protectedrd   rB   r'   rA   r$   rC   )r(   ri   del_protr;   rE   s        r*   set_DeleteProtectionzRHEVConn.set_DeleteProtection  s^    [[ &	IIKL   	893q6NK		s   5 	A1)A,,A1c                 X   | j                  |      }g }|D ]'  }|j                  t        j                  |             ) ||j                  _        	 |j                          t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)N)devz Failed to update the boot order.FT)r_   appendr   BootrJ   bootrd   rB   r'   rA   r$   rC   )r(   ri   
boot_orderr;   	bootorderdevicerE   s          r*   set_BootOrderzRHEVConn.set_BootOrder  s    [[ 	  	6FV[[V45	6
	IIKL   	563q6NK		s   A- -	B)6)B$$B)c                 B   | j                  |      }| j                  |      }|t        d       t               }g }d}	 |D ]-  }		 t        d|	d   z          d|	v r|	d   }d|	vrd|	v rd|	d<   nd	|	d<   d|	vrd|	d<   d
|	vrd|	d
<   d|	vrd|	d<   d|	v rd|	v rg }
|	d   D ]  }|
j	                  ||           	 t        j                  t        j                  |
      t        j                  t        j                  dd      t        j                  dd      g            }	 t        j                  t        j                  |	d         |	d   |	d   t        j                  |	d   |	d
   |	d         d|      }|j	                  |       t        d|	d   z          nt        j                  t        j                  |	d         |	d   |	d   t        j                  |	d   |	d
   |	d               }|j	                  |       t        d|	d   z          nPt        j                  |	d   t        j                         |	d   t        j                  |	d   |	d
   |	d                }||	d   <   0 	 |dk(  rt        d#       t                y	 t        j                   |||t        j"                  d$%      &      }| j$                  j&                  j)                  |      rt+                | j                  |      }|j,                  j.                  }|d'k7  rg|d(k7  rb| j                  |      }|j,                  j.                  }t1        j2                  d)       |d*k(  rt        d+       t                y|d'k7  r|d(k7  rbt        d,       t1        j2                  d-       | j                  |      }|j,                  j.                  }t        d.t        |      z          |j5                          |d/k7  rB| j                  |      }|j,                  j.                  }t1        j2                  d)       |d/k7  rBt        d0       	 |j6                  j9                  t        j:                  ddt        j<                  |      1             t        d2       	 |j?                          t        d4       |jA                          d(k7  r]| j                  |      }|j,                  j.                  }t1        j2                  d)       |d*k(  rt        d9       t                y|d(k7  r]t        d,       yt        d:       y# t        $ r:}t        d|	d   z          t                t        t        |             Y d }~ yd }~ww xY w# t        $ r=}t        d|	d   z   dz          t                t        t        |             Y d }~ yd }~ww xY w# t        $ r:}t        d!|	d   z          t                t        t        |             Y d }~ yd }~ww xY w# t        $ r3}t        d"       t        t        |             t                Y d }~yd }~ww xY w# t        $ r3}t        d3       t                t        t        |             Y d }~yd }~ww xY w# t        $ r3}t        d5       t                t        t        |             Y d }~yd }~ww xY w# t        $ rL}d6t        |      v rt        d7       n)t        d8       t                t        t        |             Y d }~yd }~ww xY w);NzHost does not exist. zcreating host interface r4   
managementipboot_protocolstaticnonenetmaskgatewayrv   bond)host_nicmiimon100)r4   valuemode4)option)slavesoptionszFailed to create the bond for  Fr3   )addressr   r   T)rv   r4   r   r   override_configurationbondingzApplying network zFailed to setz as network interface)rv   r4   r   r   )r4   rv   r   r   zFailed to set zFailed to set networksz No management network is defined	publickey)authentication_method)r4   r   r6   sshnon_operationalr   r	   non_responsivezFailed to add host to RHEVMzstatus host: up   z%State before setting to maintenance: maintenancezstatus host: maintenance)forcecheck_connectivity	host_nicsznics are setzFailed to apply networkconfigzNetwork config is savedzFailed to save networkconfigzThe Host name is already in usezHost already existszFailed to add hostzFailed to apply networkconfig.zHost exists.)!r   r   rA   dictr   r   BondingSlavesOptionsOptionr'   rC   r$   HostNICNetworkIPHostSSHr&   r   r@   rB   re   rf   rg   rh   
deactivater}   setupnetworksActionHostNicscommitnetconfigactivate)r(   r   r6   ifacesHOSTr   	ifacelistnetworklistmanageipifacer   slavetmpifacerE   
tmpnetworkrf   s                   r*   set_HostzRHEVConn.set_Host  sq   }}Y'""7+<)*IKHY# S%ER%9E&MIJ'50',T{H*%7#u}9Ao 69?o 6u,*,E$K$E1/1E),$E1/1E),$-%')-26] !BE$(KK	%0@$A!B!1/5~~/5}}d/K06068SX0Y066QT0U4.1*0&H!11706E)DT0U-26]6;O6L+19949$K49)4D49)4D,*
 @D08
2:J %0$6$6z$B$*+>v+N$O .4^^,2NNi@P,Q).v272H'-yy05d05i0@05i0@(&	.'
 !, 2 2: > &':U6]'J K'-~~%*6](.(8.3O.D#)99,1$K,1),<,1),<$"	(#H 4<	%-0]S%t 2~9:8{{	8WZ`ZdZd  |G  [H  I99??&&t,L==3D KK--E $55%4-#}}Y7 $ 1 1

1 $44"#@A%K#( !$55%4- ,-JJqM==3D KK--EBSZOPOO%=0#}}Y7 $ 1 1

1  =0 56%		//"&/4&,oo{&K1 
 ~.%,,.89 MMO4-}}Y/))

1,,;<K  4- $%  >"M (1 !1$*+LuU[}+\$]$-K$*3q6N+0	!1& (1 !1$*?U6]+JMd+d$e$-K$*3q6N+0	!18 % %/%-?@!s1v$	%
  /0s1v	\ % %>?!s1v$	% % %=>!s1v$	%
  4A>01/0K3q6Ns   X A2W:A+T7&A9U=CW0X C$[	 5
[	  B4[	 5[	 AY Z
 7	U: /U5/W3X 5U::W=	W2V>8W<X >WW	X	/X>X X		X 	Y)YY	Z)Z=[	 Z[	 
	[)[<[	 [[	 		\A\\c                 B    | j                  ||      j                         S r-   )r   delete)r(   ri   r   s      r*   del_NICzRHEVConn.del_NICr  s    ||FG,3355r1   c                     | j                  |      }	 |j                          y# t        $ r3}t        d       t        t	        |             t                Y d }~yd }~ww xY w)NzFailed to remove VM.FT)r_   r  r'   rA   r$   rC   r(   ri   r;   rE   s       r*   	remove_VMzRHEVConn.remove_VMu  sQ    [[ 	IIK   	)*3q6NK		s   $ 	A )AA c                     | j                  |      }	 |j                          | j                  |d|      S # t        $ r3}t        d       t        t	        |             t                Y d }~yd }~ww xY w)NzFailed to start VM.Fr   )r_   startr'   rA   r$   rC   wait_VMr(   ri   timeoutr;   rE   s        r*   start_VMzRHEVConn.start_VM  sa    [[ 	HHJ ||FD'22  	()3q6NK		   6 	A2)A--A2c                     | j                  |      }|j                  j                  |k7  rd| j                  |      }t        j                  d       |dur |dz  }|dk  rt        d       t                y|j                  j                  |k7  rdy)N
   Fr   zTimeout expiredT)r_   re   rf   rg   rh   rA   rC   r(   ri   rf   r!  r;   s        r*   r  zRHEVConn.wait_VM  sy    [[ iioo&V$BJJrNe#2a<,-K  iioo& r1   c                     | j                  |      }	 |j                          | j                  |d|      S # t        $ r3}t        d       t        t	        |             t                Y d }~yd }~ww xY w)NzFailed to stop VM.Fr   )r_   stopr'   rA   r$   rC   r  r   s        r*   stop_VMzRHEVConn.stop_VM  sa    [[ 	GGI ||FFG44  	'(3q6NK		r#  c                 "   | j                  |      }	 t        |j                  j                        dk(  rGt	        j
                  |      }|j                  j                  |       t        d       t                y|j                  j                  d      }|j                  |       |j                  d       t        d       t                	 y# t        $ r3}t        d       t        t        |             t                Y d }~y	d }~ww xY w)
Nr   )filezAttached the image.$00000000-0000-0000-0000-000000000000r   T)currentzFailed to attach image.F)r_   r$   re   rf   r   CdRomcdromsr@   rA   rB   r#   set_filerd   r'   rC   )r(   ri   cd_driver;   cdromrE   s         r*   set_CDzRHEVConn.set_CD  s    [[ 	299??#v-(3		e$,-  		)OPx(T*,-   	,-3q6NK		s   A(C <AC 	D)D		Dc                 :   | j                  |      }| j                  |      }	 ||j                  _        |j	                          t        d|z          t                y# t        $ r3}t        d       t        t        |             t                Y d }~yd }~ww xY w)NzSet startup host to Failed to set startup host.FT)
r_   r   placement_policyr   rd   rA   rB   r'   r$   rC   r(   ri   vmhostr;   r  rE   s         r*   set_VM_HostzRHEVConn.set_VM_Host  s    [[ }}V$		'+B$IIK)F23L   	013q6NK		s   9A 	B')BBc                    | j                  |      }| j                  |j                  j                        }t	        |j
                        |k7  rT	 |j                  t        j                  t        j                  |                   t                t        d|z          yy# t        $ r3}t        d       t        t	        |             t                Y d }~yd }~ww xY w)Nr3   )r   )actionzVM migrated to r5  FT)r_   r   r   rx   r$   r4   migrater   r	  r  rB   rA   r'   rC   r7  s         r*   
migrate_VMzRHEVConn.migrate_VM  s    [[ !!"''**-tyy>V#

!==#[[!'   (612   45s1v	s   AB$ $	C -)CC c                 $   | j                  |      }	 |j                  j                  d      j                          t	        d       t                y# t        $ r3}t	        d       t	        t        |             t                Y d }~yd }~ww xY w)Nr,  r   zRemoved the image.zFailed to remove the image.FT)	r_   r/  r#   r  rA   rB   r'   r$   rC   r  s       r*   	remove_CDzRHEVConn.remove_CD  sr    [[ 	IIMMCMDKKM'(L   	013q6NK		s   ?A 	B)B

BN))__name__
__module____qualname____doc__r+   r0   rF   rM   rt   r   r   ry   r_   rw   r   r   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r"  r  r)  r3  r9  r=  r?   r1   r*   r   r   e  s    <$$&,\&P73/597>)?573/+Z:"qf6		3	5*,r1   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y)RHEVc                     || _         y r-   )r"   )r(   r"   s     r*   r+   zRHEV.__init__  s	    r1   c                 N    t        | j                        | _        | j                  S r-   )r   r"   r&   r/   s    r*   
__get_connzRHEV.__get_conn  s    T[[)	yyr1   c                 $    | j                          y)NOK)_RHEV__get_connr/   s    r*   r%   z	RHEV.test  s    r1   c                    | j                          | j                  j                  |      }|rt               }|j                  |d<   |j
                  |d<   |j                  j                  |d<   |j                  j                  j                  |d<   |j                  j                  j                  |d<   |j                  |d<   t        |j                        dz  dz  dz  |d<   t        |j                  j                         dz  dz  dz  |d	<   |j#                         j$                  |d
<   |j&                  |d<   	 t)        | j                  j+                  t)        |j,                  j                              j
                        |d<   g |d<   |j0                  j3                         D ])  }|d   j5                  t)        |j6                               + g |d<   |j8                  j;                         D ]  }t               }|j
                  |d<   t        |j<                        dz  dz  dz  |d<   t)        | j                  j?                  |jA                         jC                         d   j                        j
                        |d<   |jD                  |d<   |d   j5                  |        g |d<   |jF                  j;                         D ]  }t               }t)        |j
                        |d<   t)        | j                  jI                  |jK                         j                        j
                        |d<   |jD                  |d<   |jL                  jN                  |d<   |d   j5                  |       |jL                  jN                  |t)        |j
                        <    | j                  jQ                  |jR                  j                        }	|	r|	j
                  |d<   |S d}|S # t.        $ r	 d |d<   Y tw xY w)Nuuidr4   re   	cpu_corescpu_socketsr   r   r   mem_polrJ   r   r   r   r8   rQ   r   domainrT   r  r   macr6   F)*rL  r&   r_   r   rx   r4   re   rf   r   r   r   socketsr   ra   r   r   r   get_osrH   r   r$   r   r   r'   rJ   get_bootr   r   r8   listrQ   r   get_storage_domainsget_storage_domainrT   r}   r   r   rS  r   rw   r6   )
r(   r4   r;   vminfoboot_devr   r   r{   r  r   s
             r*   getVMz
RHEV.getVM  sG   YYd#VFUUF6NWWF6N!yyF8"$&&//"7"7F;$&FFOO$;$;F=!#%==F<  #BII$ 6$ >$ FF8!$R%5%5%@%@!AT!IT!QUY!YF999;,,F4L!#!4!4F:&!$TYY%<%<S_%M%R%R!Sv $&F< EENN, ?|$++C,=>? F7O -v#yyV #DII$ 6$ >$ FV!$dii&?&?@X@X@Z@m@m@opq@r@u@u&v%|%|!}X$(NN[!w&&t,-  "F8ww||~ 8 #CHHf #DII$>$>s?P?S?S$T$Y$Y Zf%(]]k""wwex ''.(+s388}%8 ii00?G$+LLy!  F7  &!%v&s   >AO/ /P Pc                 ^    | j                          | j                  j                  ||||      S r-   )rL  r&   rF   )r(   r4   r6   r7   r8   s        r*   rF   zRHEV.createVMimage%  s'    yy&&tWhFFr1   c                 ^    | j                          | j                  j                  ||||      S r-   )rL  r&   rM   )r(   r4   r6   rJ   rL   s        r*   rM   zRHEV.createVM)  s'    yy!!$Z@@r1   c                 Z    | j                          | j                  j                  ||      S r-   )rL  r&   r   )r(   r4   r   s      r*   	setMemoryzRHEV.setMemory-  s#    yy##D&11r1   c                 Z    | j                          | j                  j                  ||      S r-   )rL  r&   r   )r(   r4   r   s      r*   setMemoryPolicyzRHEV.setMemoryPolicy1  s#    yy**4??r1   c                 Z    | j                          | j                  j                  ||      S r-   )rL  r&   r   )r(   r4   r   s      r*   setCPUzRHEV.setCPU5  s#    yy  s++r1   c                 Z    | j                          | j                  j                  ||      S r-   )rL  r&   r   )r(   r4   r   s      r*   setCPUSharezRHEV.setCPUShare9  s#    yy&&tY77r1   c                    | j                          d}d}|D ]  }d|v s|d   du sd} |D ]7  }|dz   t        |      z   dz   |j                  dd      j                  d	d      z   }|j                  d
d      }|j                  dd       }|t	        d       t                 y|j                  dd      }	|j                  dd      }
|j                  dd      }|j                  dd      }|du r|dk(  rd}| j                  j                  |      }|#| j                  j                  |||||	|
||       n| j                  j                  |||	|       t                |dz  }: y)Nr   FrV   T_Disk_r4   r   /rQ   r	   rR  z `domain` is a required disk key.rT   virtiorU   rawthin)rL  r$   r#   replacerA   rC   r&   r   rt   r   r   )r(   r4   r8   counter
bootselectr   rj   rk   rl   rm   rn   ro   rp   r   s                 r*   setDiskszRHEV.setDisks=  s   
 	&DT!
#t+!%J	&
  	Dg~G4s:TXXfb=Q=Y=YZ]_b=ccHxx*H(D1J!9: HH[(;M(E2J!%&%!8xx
E2HU"w!|99%%h/D|		$$T8Xz=Zdfx  {C  D		""8X}hOKqLG/	2 r1   c                    | j                          | j                  j                  |      }d}t        |      }|j                  j                         D ]t  }||k  r6||   }|j                  dd       }|t        d       t                nt        |      t        |j                        k7  rnt        d       |j                  j                         D ](  }| j                  j                  ||j                         * | j                  ||       t                 y|j                  dd       }	|	t        d       t                t                |j                  dd	      }
| j                  j                  |t        |j                        ||	|
       n&| j                  j                  ||j                         |d
z  }t                w ||k  r||   }|j                  dd       }|t        d       t                |j                  dd       }	|	t        d       t                t        du ry|j                  dd	      }
| j                  j!                  |||	|
       |d
z  }t                ||k  ry)Nr   r4   z`name` is a required iface key.z5ifaces are in the wrong order, rebuilding everything.Tr   z`vlan` is a required iface key.rT   rk  r	   F)rL  r&   r_   lenr}   rW  r#   rA   rC   r$   r4   r  setNetworksr   r   failedr   )r(   ri   r  r;   ro  lengthr{   r  r4   r   rT   s              r*   rt  zRHEV.setNetworksa  s	   YYf%V77<<> 	Cwyy.<<=KY#chh-/RS!ww||~ <		))&#((;<$$VV4Kyy.<<=K!IIk8<			!!&#chh-tYO		!!&#((3qLGK1	4 7OE99VT*D|8999VT*D|89~		+x8IIIdI>qLGK! " r1   c                    | j                          | j                  j                  |      }t        |j                        t        |      k7  r2| j                  j                  ||       t                t        d       yt        d       y)Nz%`delete protection` has been updated.z0`delete protection` already has the right value.T)rL  r&   r_   boolr   r   r   rA   )r(   ri   r   r;   s       r*   setDeleteProtectionzRHEV.setDeleteProtection  sl    YYf%##$X6II**68<K:;  EFr1   c                 T   | j                          | j                  j                  |      }g }|j                  j	                         D ]&  }|j                  t        |j                               ( ||k7  r(| j                  j                  ||       t        d       yt        d       y)NzThe boot order has been setz#The boot order has already been setT)
rL  r&   r_   rJ   rV  r   r$   r   r   rA   )r(   ri   r   r;   r   r[  s         r*   setBootOrderzRHEV.setBootOrder  s    YYf%	( 	0HS./	0 "II##FJ701  89r1   c                 ~    | j                          | j                  |dd       | j                  j                  |      S )Nr   i,  )rL  setPowerr&   r  )r(   ri   s     r*   removeVMzRHEV.removeVM  s1    ffc*yy""6**r1   c                 P   | j                          | j                  j                  |      }|t        d       t	                y||j
                  j                  k(  rt        d|z          y
|dk(  r2t        d       | j                  j                  ||       t                nl|dk(  r2t        d       | j                  j                  ||       t                n5|dk(  r0| j                  |d|       t                | j                  |d|       t                t        d	|z          y
)NzVM does not exist.FzVM state was already r   zVM is going to startr   zVM is going to stop	restartedzthe vm state is set to T)rL  r&   r_   rA   rC   re   rf   r"  rB   r)  r}  r   r&  s        r*   r}  zRHEV.setPower  s    YYf%:'(KBIIOO#*U23   }-.		""673&,-		!!&'2+%ffg6fdG4K,u45r1   c                     | j                          |r| j                  j                  ||      S | j                  j                  |      S r-   )rL  r&   r3  r?  )r(   ri   r1  s      r*   setCDz
RHEV.setCD  s;    99##FH5599&&v..r1   c                 Z    | j                          | j                  j                  ||      S r-   )rL  r&   r9  )r(   ri   r8  s      r*   	setVMHostzRHEV.setVMHost  s#    yy$$VV44r1   c                 \    | j                          | j                  j                  |||      S r-   )rL  r&   r  )r(   hostnamer6   r  s       r*   setHostzRHEV.setHost  s%    yy!!(GV<<r1   N)r@  rA  rB  r+   rL  r%   r\  rF   rM   r`  rb  rd  rf  rq  rt  ry  r{  r~  r}  r  r  r  rD  r1   r*   rF  rF    sg    ,\GA2@,8"H2h	+
6/5=r1   rF  c                  F    t         rt        j                  t               y y)NmsgT)ru  r"   	fail_jsonr  rD  r1   r*   r   r     s    S!r1   c                      da y NT)ru  rD  r1   r*   rC   rC     s    Fr1   c                      da y r  )changedrD  r1   r*   rB   rB     s    Gr1   c                 .    t         j                  |        y r-   )r  r   )messages    r*   rA   rA     s    JJwr1   c                    t        |       }| j                  j                  d      }|dk(  r|j                          t        ddifS |dk(  rY| j                  j                  d      }|st        d       t        t        fS |j                  |      }t        t        t        |dfS |dk(  rd	}| j                  j                  d      }|st        d       t        t        fS | j                  j                  d
      }|dk(  s|dk(  r|j                  |      }|rt        d       n| j                  j                  d      }|t        d       t                | j                  j                  d      }|rZ| j                  j                  d      }	|	t        d       t                t                |j                  ||||	      d	u rbt        |fS | j                  j                  d      }
|
t        d       t                t                |j                  |||
|      d	u rt        |fS d}|j                  |      }| j                  j                  d      }|"| j                  j                  d      }|dk(  r|}d}t        |d         |k(  rt        d       d	}d}t        |d         |k(  rt        d       d	}||kD  rt        d       t        t        fS |ri|rg|t        |d         kD  r+|j                  |d   |       |j!                  |d   |       nZ|j!                  |d   |       |j                  |d   |       n/|r|j                  |d   |       n|r|j!                  |d   |       t                | j                  j                  d      }t        |d         |k(  rt        d       n#|j#                  |d   |      d	u rt        t        fS | j                  j                  d       }|@t        |d!         |k(  rt        d"       n#|j%                  |d   |      d	u rt        t        fS | j                  j                  d      }	|	#|j'                  |d   |	      d	u rt        t        fS | j                  j                  d#d       }|#|j)                  |d   |      d	u rt        t        fS | j                  j                  d$      }|j+                  |d   |      d	u rt        t        fS | j                  j                  d%      }|j-                  |d   |      d	u rt        t        fS | j                  j                  d&      }|r#|j/                  |d   |      d	u rt        t        fS |j                  |      }||d'<   t        t        t        |dfS |d(k(  r| j                  j                  d      }|t        d       t                | j                  j                  d#      }|t        d)       t                |j1                  |||      d	u rt        t        fS t        t        t        d*fS y |d+k(  r| j                  j                  d      }|st        d       t        t        fS | j                  j                  d
      }|dk(  s|dk(  r|j                  |      }|r|t        d       | j                  j                  d$      }|j+                  |d   |      d	u rt        t        fS |j3                  |d         d	u rt        t        fS t        d,       d-|d<   nt        d.       t        t        t        |dfS y |d/k(  s
|d0k(  s|d1k(  r| j                  j                  d      }|st        d       t        t        fS | j                  j                  d2      }|j5                  |||      d	u rt        t        fS |j                  |      }t        t        t        |dfS |d3k(  ri| j                  j                  d      }| j                  j                  d4      }|j7                  ||      d	u rt        t        fS t        t        t        d*fS y )5Nrf   r   pongr   r4   z`name` is a required argument.)r  r  vmr   Ftyper   r   z	VM existsr6   zcluster is a required argument.imager8   zdisks is a required argument.osverzosver is a required argument.Tvmmemmempolr   rQ  zMemory is correctr   z5memory_policy cannot have a higher value than memory.vmcpurO  zNumber of CPUs is correctr   r   zCPU share is correct.r  r   r   r8  createdr   zifaces is a required argument.)r  r  r   zVM has been removed.DELETEDzVM was already removed.r   r   r  r!  r   r1  )rF  r   r#   r%   RHEV_SUCCESSrA   RHEV_FAILEDr  r\  r  rC   r   rF   rM   ra   r`  rb  rd  rf  rq  rt  ry  r{  r  r  r~  r}  r  )r"   rrf   r4   rZ  r  rL   r6   r7   r8   rJ   r   r   mem_pol_nokmem_nokr   r   r  r   r   r8  r!  r1  s                          r*   corer    s   VAMMg&E	ff---	&}}  (34##FKKK	)	}}  (34##]]&&v.
!Z9%<WWT]F{# !--++I6?<=K!==,,W5"MM--g6E}>?!KtWhF%O*F22**73Bz>?!Kzz$Z@EI*F22 WWT]F]]&&w/F! & 1 1( ; A%$*M"vi()]:./"'Kvh'(F2./#G 6)RS&++{$s6(+;'<<F6NF;))&.-H))&.-HF6NF;KKv7 %%fVnmD --##G,C6+&'3.2388F6NC0E9&++ ))+6I$vl+,	923}}VF^Y?5H*C// MM%%g.E ::fVne4=&++ ]]&&x6F!==8EA&++ }}((4H$$VF^X>%G"C''  **<8J~~fVnj9UB"C'' ]]&&x0F;;vf~v6%?&++WWT]F 'F9WS!OOOmm''	2G89]]&&x0F~78yyw/58"C''WS!AAA   
(	}}  (34##]]&&v.
!Z9%<WWT]F{# "==,,Z8((BeK&++ ::fVn-6&++-."+w01WS!OOO# &=& 
$%6/Uk-A}}  (34##--##I.::dE7+u4##FKKK	$}}  (==$$Z0774"e+##=== 
r1   c                     t        t        d:i dt        ddg d      dt        dd      d	t        dd
d
      dt        dd      dt        dd      dt        dd      dt        d      dt        d      dt        dd      dt        ddg d      dt        dd      dt        d      dt        dd      d t        dd!      d"t        d#d$      d%t        dd&      d't        d#dd(d)g*      d+t        d      d,t        dd!      d-t        dd
      d.t        dd/      d0t        d#dd1d2g3      d4t        dd
      d5t        d      6      at        st        j	                  d78       t
        } 	 t        t              \  } }| d/k7  rt        j	                  | 9       t        j                  d:i  y # t        $ r)}t        j	                  t        |      8       Y d }~_d }~ww xY w);Nrf   r$   r   )r   r   r   r   r   r   r  r   )r  defaultchoicesr   zadmin@internal)r  r  r   T)r  requiredno_logr   z	127.0.0.1r   ra   i  r   rx  Fr4   )r  r  
datacenterDefaultr  r   r6   r   r8  r  r
   r  r	   r8   rW  )r  elementsr  	rhel_6x64r  
interfacesr}   )r  r  aliasesr!  r  vm_har   r   r   hdrv   )r  r  r  r   r1  )argument_specz@The 'ovirtsdk' module is not importable. Check the requirements.r  )rcr  rD  )
r   r   r"   HAS_SDKr  r  r  r'   r$   	exit_json)r  resultrE   s      r*   mainr    s@    
E9  ?E  F
5*:;
 utDA
 UK8	

 5#.
 659
 5!
 E"
 	:
 5(<YZ
 eR0
 U#
 E1-
 E1-
 FU3
  E;7!
" VelF=ST#
$ e$%
& UA.'
( FD1)
* q1+
, %$	ARS-
. vt4/
0 u%1
F: _`	B%&\
F 
QwBF+
v  %SV$$%s   G 	HH  H__main__)$
__future__r   r   r   r  __metaclass__DOCUMENTATIONRETURNEXAMPLESrg   ovirtsdk.apir   ovirtsdk.xmlr   r  ImportErroransible.module_utils.basicr   r  r  RHEV_UNAVAILABLERHEV_TYPE_OPTS
STATE_OPTSr  r  ru  objectr   rF  r   rC   rB   rA   r  r  r@  rD  r1   r*   <module>r     s    A @M^A
Fn`  #G 5  .Q

	C
v C
Lp=6 p=f

}>@+\ zF ]$  Gs   A6 6B ?B 