Oracle VM (OVM) REST Api
############################################################################################
Update 20180331:
I tried a couple things from the comment section and can add the following:
@chaitanya: ovcmclient worked for me also with create and clone
@Dave: that still gave me the same errors and I am using documentation as its here https://docs.oracle.com/cd/E50245_01/E50253/html/vmprg-rest-example-vm-create-python.html
@Jeff: try chaitanya's suggestion python-ovmclient it worked for me
############################################################################################
Previous article here:
http://blog.ls-al.com/oracle-ovm-rest-api-example/
I am adding a slightly updated script here even though I could not get POST (Create new VM) and PUT (Clone VM) to work yet.
Using a module for most functions.
import json from time import sleep def get_id_from_name(s,baseUri,obj,name): #sp_id=get_id_from_name(s,baseUri,'ServerPool','ovs-home') r=s.get(baseUri+'/'+obj) for i in r.json(): if i['name'] == name: id = i['id']['value'] return id def wait_for_job(joburi,s): while True: time.sleep(1) r=s.get(joburi) job=r.json() if job['summaryDone']: print '{name}: {runState}'.format(name=job['name'], runState=job['jobRunState']) if job['jobRunState'].upper() == 'FAILURE': raise Exception('Job failed: {error}'.format(error=job['error'])) elif job['jobRunState'].upper() == 'SUCCESS': if 'resultId' in job: return job['resultId'] break else: break def check_manager_state(baseUri,s): #https://hostname:port/ovm/core/wsapi/rest/Manager while True: r=s.get(baseUri+'/Manager') manager=r.json() if manager[0]['managerRunState'].upper() == 'RUNNING': break time.sleep(1) return; def serverList(s,baseUri): print "\nServer List:" print "##############" r=s.get(baseUri+'/Server') for i in r.json(): # do something with the content print '{:20} {:20}'.format(i['serverRunState'],i['name']) def vmList(s,baseUri): print "\nVM List:" print "########" r=s.get(baseUri+'/Vm') for i in r.json(): # print '{:20} {:20}'.format(i['vmRunState'],i['name']) print '{:20} {:35} {:30}'.format(i['vmRunState'],i['name'],i['id']['value']), for d in i['vmDiskMappingIds']: print d['value'], print # print '{name} '.format(name=i['name']) # print i def showVm(s,baseUri,name): print "\nVM Show:" print "##########" r=s.get(baseUri+'/Vm') for i in r.json(): if i['name'] == name: print '{:20} {:55}'.format(i['vmRunState'],i['name']), for d in i['vmDiskMappingIds']: if 'CDROM' not in d['name']: print d['value'], disk=s.get(baseUri+'/VmDiskMapping/'+d['value']) d = disk.json() if "obiee" in d['virtualDiskId']['name']: dName = d['virtualDiskId']['name'].replace('obiee_template',i['name']) dName = dName.split('img')[0]+'img' print 'value: {} and name {} should be renamed -> {}'.format(d['virtualDiskId']['value'],d['virtualDiskId']['name'],dName), print def cmdVm(s,baseUri,cmd,name): print "\nVM " + cmd print "##########" vm_id=get_id_from_name(s,baseUri,'Vm',name) print vm_id r=s.put(baseUri+'/Vm/'+vm_id+'/'+cmd) job=r.json() # print job # wait for the job to complete vm_id=wait_for_job(job['id']['uri'],s) def updateVm(s,baseUri,name): print "\nVM Update Vm Disk Names:" print "##########################" r=s.get(baseUri+'/Vm') for i in r.json(): if i['name'] == name: #print i print '{:20} {:20} {:55}'.format(i['id']['value'],i['vmRunState'],i['name']) for disk in i['vmDiskMappingIds']: if 'CDROM' not in disk['name']: #print disk['value'], value=s.get(baseUri+'/VmDiskMapping/'+disk['value']) d = value.json() oldName = d['virtualDiskId']['name'] newName = d['virtualDiskId']['name'].replace('obiee_template',i['name']) newName = newName.split('img')[0]+'img' d['virtualDiskId']['name']=newName d['id']['name']=newName d['name']=newName #print 'value: {:20} name: {:55} new name {}'.format(d['virtualDiskId']['value'],d['virtualDiskId']['name'],dName), print 'value: {:20} name: {:55} new name {}'.format(disk['value'],oldName,newName) #print d uri='{base}/VmDiskMapping/{id}'.format(base=baseUri,id=d['id']['value']) #print uri r1=s.put(uri,data=json.dumps(d)) job=r1.json() #print job # wait for the job to complete wait_for_job(job['id']['uri'],s) i['vmDiskMappingIds'][0]['name']='new.img' #print i uri='{base}/Vm/{id}'.format(base=baseUri,id=i['id']['value']) #print uri r=s.put(uri,data=json.dumps(i)) job=r.json() #print job # wait for the job to complete wait_for_job(job['id']['uri'],s) def updateVmMemory(s,baseUri,name,memory): print "\nVM Update Vm Memory for " + name print "########################################" vm_id=get_id_from_name(s,baseUri,'Vm',name) uri='{base}/Vm/{id}'.format(base=baseUri,id=vm_id) r=s.get(uri) d=r.json() #print d d['memory']='512' r=s.put(uri,data=json.dumps(d)) job=r.json() #print job # wait for the job to complete wait_for_job(job['id']['uri'],s) def updateVirtualDisk(s,baseUri,id,newDiskName): print "\nVM Update Vm Disk Mapping for " + id print "########################################" uri='{base}/VirtualDisk/{id}'.format(base=baseUri,id=id) r=s.get(uri) disk=r.json() #print disk #oldName = disk['virtualDiskId']['name'] #newName = disk['virtualDiskId']['name'].replace('obiee_template',d['name']) #newName = newName.split('img')[0]+'img' disk['name']=newDiskName #disk['name']='newname_system.img' r=s.put(uri,data=json.dumps(disk)) job=r.json() # wait for the job to complete wait_for_job(job['id']['uri'],s) def updateVmDiskNames(s,baseUri,name): print "\nVM Update Vm Disk Names for " + name print "########################################" vm_id=get_id_from_name(s,baseUri,'Vm',name) uri='{base}/Vm/{id}'.format(base=baseUri,id=vm_id) r=s.get(uri) vm=r.json() dNum=0 for disk in vm['vmDiskMappingIds']: if 'CDROM' not in disk['name']: dNum = dNum +1 newDiskName=name + "_disk" + str(dNum) #if "system" in disk['name']: # newDiskName=name + "_system.img" #if "data1" in disk['name']: # newDiskName=name + "_data1.img" ## update VmDiskMapping as shown in Repository dMapping=s.get(baseUri+'/VmDiskMapping/'+disk['value']) dm=dMapping.json() updateVirtualDisk(s,baseUri,dm['virtualDiskId']['value'],newDiskName)
Main program.
import requests import json from ovm_lib import * #import logging s=requests.Session() s.auth=('admin','mypassword') s.verify=False #disables SSL certificate verification s.headers.update({'Accept': 'application/json', 'Content-Type': 'application/json'}) ovmmServer="192.168.1.223:7002" print "Running against OVM Manager Server: " + ovmmServer baseUri='https://'+ovmmServer+'/ovm/core/wsapi/rest' #logging.basicConfig(level=logging.DEBUG) ## Create VM still failing ## GENERAL_JSON_PARSING_ERROR', u'message': u'GEN_000031:An error occurred parsing the JSON request' def createVm(s,baseUri,repository,serverpool,vmName): #file:///home/rrosso/OvmSDK_3.4.2.1384/doc/api/webservices_r/resource_VmRs.html#path__Vm.html #repo_id=get_id_from_name(s,baseUri,'Repository','ovs1') #sp_id=get_id_from_name(s,baseUri,'ServerPool','ovs-home') repo_id=get_id_from_name(s,baseUri,'Repository',repository) sp_id=get_id_from_name(s,baseUri,'ServerPool',serverpool) #print 'repo_id {:20} ServerPool Id {:55}'.format(repo_id,sp_id) #OVM> create Vm name=MyVM repository=MyRepository domainType=XEN_HVM \ #server=MyServer startPolicy=USE_POOL_POLICY on ServerPool name=MyServerPool data={ "name": vmName, "description": "A virtual machine created using the REST API", "vmDomainType": "XEN_PVM", "repositoryId": repo_id, "serverPoolId": sp_id } # data={'serverPoolId':'0004fb00000200006aa35973e4d0e5af','repositoryId':'0004fb00000300000c6c2c52c5708b65'} print data uri='{base}/Vm'.format(base=baseUri) print uri #print json.dumps(data) r=s.post(uri,data=json.dumps(data)) job=r.json() print job # wait for the job to complete vm_id=wait_for_job(job['id']['uri'],s) ## CloneVM failing ## The value for the argument "serverPoolId" was found to be null def cloneVm(s,baseUri,templateVm,vmName): repo_id=get_id_from_name(s,baseUri,'Repository','ovs1') sp_id=get_id_from_name(s,baseUri,'ServerPool','ovs-home') template_id=get_id_from_name(s,baseUri,'Vm',templateVm) print 'clone {} into repo_id {:20} ServerPool Id {:55}'.format(template_id,repo_id,sp_id) data={ "serverPoolId": sp_id, "repositoryId": repo_id, "createTemplate": False } uri='{base}/Vm/{vmId}/clone'.format(base=baseUri,vmId=template_id) r=s.put(uri,data=json.dumps(data)) job=r.json() print job # wait for the job to complete vm_id=wait_for_job(job['id']['uri'],s) print "new vm id:" + vm_id ## change vm name here? if __name__ == "__main__": print print #check_manager_state(baseUri,s) #createVm(s,baseUri,'VM3') createVm(s,baseUri,'ovs2','ovs-home','ovs2-VM3') #cloneVm(s,baseUri,'Ubuntu.0','VM4') serverList(s,baseUri) vmList(s,baseUri) #updateVmMemory(s,baseUri,'VM2','512') #updateVmDiskNames(s,baseUri,'VM2') #showVm(s,baseUri,'VM2') #cmdVm(s,baseUri,'start','VM2') #cmdVm(s,baseUri,'stop','VM2')