Pablo Iranzo Gómez's blog

oct 24, 2012

RHEV/OVIRT API with Python

RHEV/oVirt api allows faster and simple development of scripts / utilities ranging from gathering of information to VM/host, etc manipulation.

For example, a simple script for connecting to API and list VM's could be:

import sys
import getopt
import optparse
import os
import time

from ovirtsdk.api import API
from ovirtsdk.xml import params
from random import choice

baseurl = "https://localhost:8443"
api = API(url=baseurl, username="admin@internal",password="redhat",insecure=True)

for vm in api.vms.list():
    print vm.name

The .list() method works pretty well, but beware, it limits collections to 100 elements for performance reasons, so in those cases, we'll need to check how many results do we have, and paginate by passing an extra argument to our ".list()" invocation, for example:

for vm in api.vms.list(query="page 1")

Furthermore, we can check the number of results by using:

len(api.vms.list(query="page 1"))

And playing together, we could set a list that returns all results by running:

vms = []
page = 0
length = 100
while (length > 0):
    page = page + 1
    query = "%s page %s" % (oquery, page)
    tanda = api.vms.list(query=query)
    length = len(tanda)
    for vm in tanda:
        vms.append(vm)

We can also make funny things like migrate VM's to another host by just running:

vm.migrate()

It's expected for RHEV 3.1 to have a developer guide (now in Beta) at https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Virtualization/3.1-Beta/html-single/Developer_Guide/index.html

Check it for more examples of use and put the Virtualization to work for you!

posted at 13:38  ·   ·  python  rhev  ovirt