此内容没有您所选择的语言版本。

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
Copy to Clipboard Toggle word wrap
If the connection attempt was successful, the example outputs the text:
Connected to Red Hat Storage Console successfully!
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap
In an environment where only the Default cluster exists, the example outputs:
Default (99408929-82cf-4dc7-a532-9d998063fa95)
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap
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
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap
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)
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap
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
Copy to Clipboard Toggle word wrap
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat