Red Hat Training

A Red Hat training course is available for Red Hat Gluster Storage

4.2. Example: Accessing the API Entry Point using Python

The ovirtsdk Python library provides the API class, which acts as the entry point for the API.

Example 4.1. Accessing the API entry point using Python

To connect the example creates an instance of the API class. If connection is established successfully, a message is printed. Lastly, the disconnect() method of the API class is called to close the connection.
The parameters provided to the constructor for the API class in this example are:
  • The url of the Console with which to connect.
  • The username of the user to authenticate.
  • The password of the user to authenticate.
  • The ca_file that is the path to a certificate. The certificate is expected to be a copy of the Console's Certificate Authority. It can be obtained from https://[HOST]/ca.crt.
The constructor for the API class supports other parameters. Only mandatory parameters are specified in this example.
from ovirtsdk.api import API
from ovirtsdk.xml import params

try:
    api = API (url="https://HOST",
               username="USER",
               password="PASS",
               ca_file="ca.crt")

    print "Connected to %s successfully!" % api.get_product_info().name

    api.disconnect()

except ConnectionError, err:
    print "Connection failed: %s" % err
If the connection attempt was successful, the example outputs the text:
Connected to Red Hat Storage Console successfully!

Example 4.2. Listing the Cluster Collection using Python

The API class provides a cluster collection named default cluster. This collection contains all the clusters in the environment.
This Python example lists the clusters in the default cluster collection.
from ovirtsdk.api import API
from ovirtsdk.xml import params

try:
    api = API (url="https://HOST",
               username="USER",
               password="PASS",
               ca_file="ca.crt")

    c_list = api.clusters.list()

    for c in c_list:
        print "%s (%s)" % (c.get_name(), c.get_id())

    api.disconnect()

except Exception as ex:
    print "Unexpected error: %s" % ex
In an environment where only the Default cluster exists, the example outputs:
Default (99408929-82cf-4dc7-a532-9d998063fa95)

Example 4.3. Listing the Networks Collection using Python

The API class provides access to a networks collection named Management Networks. This collection contains all the networks in the environment.
This Python example lists the networks in the networks collection. It also outputs some basic information about each network in the collection.
from ovirtsdk.api import API
from ovirtsdk.xml import params

try:
    api = API (url="https://HOST",
          username="USER@domain",
          password="PASS",
          ca_file="ca.crt")

    n_list = api.networks.list()

    for n in n_list:
        print n.get_description()
        print n.get_id()
        print n.get_name()

    api.disconnect()

except Exception as ex:
    print "Unexpected error: %s" % ex
In an environment where only the default management network exists, this example outputs the description of the network, the network ID and the name of the network:
Management Network
00000000-0000-0000-0000-000000000009
ovirtmgmt

Example 4.4. Listing the Host Collection using Python

The API class provides access to a host collection. This collection contains all the hosts in the storage cluster.
This Python example lists the hosts in the host collection.
from ovirtsdk.api import API
from ovirtsdk.xml import params

try:
    api = API (url="https://HOST",
          username="USER@domain",
          password="PASS",
          ca_file="ca.crt")

    s_list = api.hosts.list()

    for s in s_list:
        print "host name: %s (host ID: %s)" % (s.get_name(), s.get_id())

    api.disconnect()

except Exception as ex:
    print "Unexpected error: %s" % ex
The code snippet displays the list of hosts in the environment along with the associated host IDs.
host name: 10.70.37.49 (host ID: 5be18d62-e7b0-4407-8ff6-68290a92338f)
host name: 10.70.37.50 (host ID: 3b202041-6e14-43df-a844-92a141bed1ed)

Example 4.5. Listing the Volume Collection using Python

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

try:
    api = API (url="https://HOST",
          username="USER@domain",
          password="PASS",
          ca_file="ca.crt")

    clusterName= "CLUSTERNAME"
    for volume in api.clusters.get(
        clusterName).glustervolumes.list():
        print "volumeName:", volume.get_name(),

    api.disconnect()

except Exception as ex:
    print "Unexpected error: %s" % ex

Example 4.6. Listing the Brick Collection from a Volume using Python

The function getGlusterVolumeBrickDetails()lists the bricks of the volume that is assigned to the cluster.
def getGlusterVolumeBrickDetails(clusterName, volumeName):
    bricks = []
    for brick in API.clusters.get(
        clusterName).glustervolumes.get(
            volumeName).get_bricks().list():
        bricks.append({"Brick": brick.get_name(),
                       "Status": brick.get_status().state})
    return bricks

Example 4.7. Listing hooks in a Red Hat Storage Console

You can view the volume lifecycle extensions in a Red Hat Storage Console using Gluster hooks. The function getHookList(clusterName) lists the Gluster hooks created in the Console.
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
    api = API (url="https://HOST",
          username="USER@domain",
          password="PASS",
          ca_file="ca.crt")

    clusterName="Default"
    hooks=[]
    for hook in api.clusters.get(clusterName).glusterhooks.list():
        print "hookName:", hook.name
        print "glusterCommand:", hook.get_gluster_command()
        print "Stage:", hook.get_stage()
        print "Hook ID:", hook.get_id()

    api.disconnect()

except Exception as ex:
    print "Unexpected error: %s" % ex
The code snippet displays the list of hook scripts in the console along with the associated hook IDs,
hookName: add-brick-PRE-28Quota-enable-root-xattr-heal.sh
glusterCommand: add-brick
Stage: PRE
Hook ID: b368f9dc-02a5-4e86-b96e-ea5393e73dc7
hookName: gsync-create-POST-56glusterd-geo-rep-create-post.sh
glusterCommand: gsync-create
Stage: POST
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug