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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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:

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

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

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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:

1
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!

Like this post? Share on: TwitterFacebookEmail


Pablo Iranzo Gómez Avatar Pablo Iranzo Gómez opensource enthusiast and Lego fan doing some python simple programs like @redken_bot in telegram, etc
Comments

comments powered by Disqus

Keep Reading


Published

Category

tech

Tags

Stay in Touch