Red Hat Training

A Red Hat training course is available for Red Hat Virtualization

Chapter 3. Using the Software Development Kit

3.1. Connecting to the API using Python

To connect to the REST API using Python you must create an instance of the API class from the ovirtsdk.api module. To be able to do this it is necessary to first import the class at the start of the script:
from ovirtsdk.api import API
The constructor of the API class takes a number of arguments. Supported arguments are:
url
Specifies the URL of the Manager to connect to, including the /api path. This parameter is mandatory.
username
Specifies the user name to connect using, in User Principal Name (UPN) format. This parameter is mandatory.
password
Specifies the password for the user name provided by the username parameter. This parameter is mandatory.
kerberos
Uses a valid Kerberos ticket to authenticate the connection. Valid values are True and False. This parameter is optional.
key_file
Specifies a PEM formatted key file containing the private key associated with the certificate specified by cert_file. This parameter is optional.
cert_file
Specifies a PEM formatted client certificate to be used for establishing the identity of the client on the server. This parameter is optional.
ca_file
Specifies the certificate file of the certificate authority for the server. This parameter is mandatory unless the insecure parameter is set to True.
port
Specifies the port to connect using, where it has not been provided as component of the url parameter. This parameter is optional.
timeout
Specifies the amount of time in seconds that is allowed to pass before a request is to be considered as having timed out. This parameter is optional.
persistent_auth
Specifies whether persistent authentication is enabled for this connection. Valid values are True and False. This parameter is optional and defaults to False.
insecure
Allows a connection via SSL without certificate authority. Valid values are True and False. If the insecure parameter is set to False - which is the default - then the ca_file must be supplied to secure the connection.
This option should be used with caution, as it may allow man-in-the-middle (MITM) attackers to spoof the identity of the server.
filter
Specifies whether or not user permission based filter is on or off. Valid values are True and False. If the filter parameter is set to False - which is the default - then the authentication credentials provided must be those of an administrative user. If the filter parameter is set to True then any user can be used and the Manager will filter the actions available to the user based on their permissions.
debug
Specifies whether debug mode is enabled for this connection. Valid values are True and False. This parameter is optional.
You can communicate with multiple Red Hat Virtualization Managers by creating and manipulating separate instances of the ovirtsdk.API Python class.
This example script creates an instance of the API class, checks that the connection is working using the test() method, and disconnects using the disconnect() method.
from ovirtsdk.api import API    
    
api_instance = API ( url="https://rhevm31.demo.redhat.com", 
                     username="admin@internal", 
                     password="Password", 
                     ca_file="/etc/pki/ovirt-engine/ca.pem")

print "Connected successfully!"

api_instance.disconnect()
For a full list of methods supported by the API class refer to the pydoc output for the ovirtsdk.api module.
$ pydoc ovirtsdk.api