Search code examples
pythonpython-3.xpython-2.7redhatovirt

python ovirtsdk with rhev 4.1


I'm trying to use the RHEV 4.1 API to get the host status of my hypervisor using python. RHEV 3.6 is working with my script. But if i use the following.

host = api.hosts.get(host_name)
host_state = host.status      //RHEV 3.6 this was host.state.status
print(host_state)

it gives the following

<ovirtsdk.xml.params.Status object at 0x3ce0f10>
dev-dbe101t                <ovirtsdk.xml.params.Status object at    0x3ce0f10>
dev-be101t-data1          None                   master
dev-be101t-data2          None

I can get the hostname and data-center name correctly. even this Status object is not iterable. According to the rhvm 3.6 and rhev 4.1 api difference.

In version 4 of the API this Status type has been removed and replaced by enum types. (my above code is working for rhev 3.6 and it gives the correct status of the host)

How can i retrieve the host status ? i found that the host.status type is a class and then i printed out all related class methods using dir(theobject) and found following useful methods. state,get_state, but it is giving none. but my api status is up

according to the api guide.

status is type of HostStatus ENUM

HOSTSTATUS ENUM contains a NAME called "up"

not sure how to get the host.status from the api. API returns this

<host>
<status>up</status>
</host>


Solution

  • You are mixing here two API's. As you correctly wrote oVirt has two version of the API v3 and v4. Python SDK v3 by default works with v3 API, so you should use it with oVirt 4.1/RHV 4.1, same as before.

    If you list using API v3 the hosts status:

    GET https://fqdn/ovirt-engine/api/v3/hosts

    You will get:

    <status><state>up</state></status>

    So you should be able to fetch the state Python SDK v3 as:

    host = api.hosts.get(host_name) host_state = host.status.state print(host_state)

    In APIv4 you will get:

    GET https://ondra.local:8443/ovirt-engine/api/v4/hosts

    <status>up</status>

    But it's not relevant as you still use Python SDK v3, which uses APIv3.