Questo contenuto non è disponibile nella lingua selezionata.
4.2. Example: Accessing the API Entry Point using Python
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
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
urlof the Console with which to connect. - The
usernameof the user to authenticate. - The
passwordof the user to authenticate. - The
ca_filethat is the path to a certificate. The certificate is expected to be a copy of the Console's Certificate Authority. It can be obtained fromhttps://[HOST]/ca.crt.
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
Connected to Red Hat Storage Console successfully!
Example 4.2. Listing the Cluster Collection using Python
API class provides a cluster collection named default cluster. This collection contains all the clusters in the environment.
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
Default cluster exists, the example outputs:
Default (99408929-82cf-4dc7-a532-9d998063fa95)
Example 4.3. Listing the Networks Collection using Python
API class provides access to a networks collection named Management Networks. This collection contains all the networks in the environment.
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
Management Network
00000000-0000-0000-0000-000000000009
ovirtmgmt
Example 4.4. Listing the Host Collection using Python
API class provides access to a host collection. This collection contains all the hosts in the storage cluster.
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
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
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
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
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