Hello ,

I want to  write a script to attach a  '.iso' image to a vm 
so ,can you please help me or tell me some api so that i can continue my work !!!
I have some '.iso ' file in my storage .

script       rhevm_test.py

from ovirtsdk.api import API
from ovirtsdk.xml import params
import time

rhevm_uri = "https://rhevm301.xxx.xx.com:8443/api"
rhevm_username = "admin@rhevm301.xxx.xx.com"
rhevm_password = "iso*help"

api = API(url=rhevm_uri, username=rhevm_username, password=rhevm_password)
print "Connected to RHEVM Successful"

MB = 1024*1024
GB = 1024*MB
VM_NAME = 'test_vm'
DESCRIP = 'testing vm'
CLUSTER_NAME =  'Default'
STORAGE_NAME =  'rhevmVMdata'

domain_name= 'rhevmiso'  
domain_type = 'ISO'


try:
    api.vms.add(params.VM(name=VM_NAME,description=DESCRIP, memory=2*GB,cluster=api.clusters.get(CLUSTER_NAME), template=api.templates.get('Blank')))
    
    print 'VM created'

    api.vms.get(VM_NAME).nics.add(params.NIC(name='nic1', network=params.Network(name='rhevm'), interface='Red Hat VirtIO'))
    print 'NIC added to VM'

    api.vms.get(VM_NAME).disks.add(params.Disk(storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(STORAGE_NAME)]),size=512*MB,status=None,interface='VirtIO',format='Preallocated',sparse=True,bootable=True))

    print 'Disk added to VM'
    print 'Waiting for VM to reach Down status'
    while api.vms.get(VM_NAME).status.state != 'down':
        time.sleep(1)
except Exception as e:
    print 'Failed to create VM with disk and NIC\n%s' % str(e)

time.sleep(10)

try :
    api.vms.get(VM_NAME).cdroms.add(params.CdRom(name='CD-ROM' , file=params.File(name='CentOS-6.2-x86_64-LiveDVD.iso')))
except Exception as e:
     print 'Failed to attach disk :\n%s' % str(e)



try:
    if api.vms.get(VM_NAME).status.state != 'up':
        print 'Starting VM'
        api.vms.get(VM_NAME).start()
        print 'Waiting for VM to reach Up status'
        while api.vms.get(VM_NAME).status.state != 'up':
            time.sleep(1)
    else:
        print 'VM already up'
except Exception as e:
     print 'Failed to Start VM:\n%s' % str(e)

 
and got some following output :

Connected to RHEVM Successful
VM created
NIC added to VM
Disk added to VM
Waiting for VM to reach Down status
Failed to attach disk :

status: 400
reason: Bad Request
detail: CdRom [file.id] required for add
Starting VM
Failed to Start VM:

status: 400
reason: Bad Request
detail: Cannot run VM without at least one bootable disk.



if i remove this line in red color the code will works fine and it will create a vm with blank template w/o any '.iso' attached !!






With Regards,
Romil