python ovirtsdk with rhev 4.1

时间:2018-03-22 23:55:10

标签: python python-3.x python-2.7 redhat ovirt

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>

1 个答案:

答案 0 :(得分:1)

您在这里混合了两个API。如您正确编写的那样,oVirt具有API v3和v4的两个版本。默认情况下,Python SDK v3可与v3 API一起使用,因此您应与以前一样将其与oVirt 4.1 / RHV 4.1一起使用。

如果您使用API​​ v3列出主机状态:

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

您将获得:

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

因此,您应该能够以以下方式获取state Python SDK v3:

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

在APIv4中,您将获得:

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

<status>up</status>

但这并不重要,因为您仍在使用使用API​​v3的Python SDK v3。