16.4. Retrieving Resources from a Collection
Resources are retrieved from a collection using the
get
and list
methods.
-
get
- Retrieves a single resource from the collection. The item to retrieve is determined based on the name provided as an argument. The
get
method takes these arguments:name
- The name of the resource to retrieve from the collection.id
- The globally unique identifier (GUID) of the resource to retrieve from the collection.
-
list
- Retrieves any number of resources from the collection. The items to retrieve are determined based on the criteria provided. The
list
method takes these arguments:**kwargs
- A dictionary of additional arguments allowing keyword-based filtering.query
- A query written in the same format as that used for searches executed using the Red Hat Storage Console.max
- The maximum number of resources to retrieve.case_sensitive
- Whether or not search terms are to be treated as case sensitive (True
orFalse
, the default isTrue
).
Example 16.1. Retrieving a List of Resources in a Collection Matching a Keyword Based Filter
The example below lists all the volumes that have the volume type set to distribute.
from ovirtsdk.api import API from ovirtsdk.xml import params try: api = API (url="https://HOST", username="USER@internal", password="PASS", ca_file="ca.crt") clusterName="CLUSTERNAME" volume_list= api.clusters.get(clusterName).glustervolumes.list(**{"volume_type": "distribute"}) for volume in volume_list: print "VolumeName:", volume.get_name() api.disconnect() except Exception as ex: print "Unexpected error: %s" % ex
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug
16.4.1. Retrieving a Specific Resource from a Collection
In this example, a specific resource is retrieved from a collection using the
get
method.
To retrieve the Default cluster from the clusters collection using the
name
parameter of the get
method:
cl = api.clusters.get("Default")
This syntax is equivalent to:
cl = api.clusters.get(name="Default")
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug
16.4.2. Retrieving a List of Resources from a Collection
In these examples, a list of resources is retrieved from a collection using the
list
method.
In the following example, a list of all resources is retrieved from the clusters collection. The query parameter of the list method allows the use of engine-based queries. In this way the SDK supports the use of queries in the same format as those executed in the Administration and User Portals. The query parameter is also the mechanism for providing pagination arguments while iterating through the collection.
Example 16.2. Listing All Clusters
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") 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
Executing this code on a Python engine will show a list of clusters in the Red Hat Storage Console:
c1 (42c68146-0036-4374-a6e0-bf8d5874d890) cl1 (d62cb3a2-1bea-4267-af3a-72aadac8ee21) cl2 (49bc2dc6-b5fb-4da2-83ba-fad736757c14) cl3 (0c4b3cb9-1557-4448-9fda-a9209830cec9) Default (99408929-82cf-4dc7-a532-9d998063fa95) tcl (cbc131f6-09f1-4f0e-8d8e-89a7c3288984)
In this example, the
get_name()
method provides the name of the cluster and the get_gluster_service()
returns true
or false
based on glusterFS support in the cluster:
Example 16.3. Listing All Hosts
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") h_list = api.hosts.list() for h in h_list: print "%s (%s)" % (h.get_name(), h.get_id()) api.disconnect() except Exception as ex: print "Unexpected error: %s" % ex
In this example,
api.hosts.list()
provides a dictionary list that contains the host name and the host ID.
Executing this code on a Python engine will show a list of hosts in the Red Hat Storage Console:
h1 (51b29199-3a7f-49e4-b0d5-92afdeea1f15) h2 (4e8c3642-e654-456c-9dff-a240c8a43b1f) host1 (14e3823d-d557-4635-bd9f-0c6157ab7f07)
Important
The
list
method of a collection is restricted to returning only as many elements as allowed by the SearchResultsLimit
configuration key in the Red Hat Storage Console. To ensure that all records in a the list
are returned, it is recommended to paginate through the results as illustrated in this example. Alternatively, set the max
parameter of the list
method to the maximum number of records that you wish to retrieve.
Example 16.4. Listing All Red Hat Storage Volumes
from ovirtsdk.api import API from ovirtsdk.xml import params try: api = API (url="https://HOST", username="USER", 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
Executing this code on a Python engine will show a list of Red Hat Storage Volumes in the Red Hat Storage Console:
VolumeName: vol1
Example 16.5. Listing All Users
from ovirtsdk.api import API from ovirtsdk.xml import params try: api = API (url="https://HOST", username="USER", password="PASS", ca_file="ca.crt") for user in api.users.list(): print "User name: %s (User ID: %s)" % (user.get_name(), user.get_id()) api.disconnect() except Exception as ex: print "Unexpected error: %s" % ex
Including this code snippet in the program will return a list of Red Hat Storage Console users:
User name: admin (User ID: fdfc627c-d875-11e0-90f0-83df133b58cc)
Example 16.6. Listing All Roles
The
getUsers()
function lists all the roles in the Red Hat Storage Console.
def listRoles(): """ Return list of user roles """ roles = [] for role in api.roles.list(): roles.append(role) return roles
Including this code snippet in the program will return a list of roles defined in the Red Hat Storage Console:
for i in api.roles.list(): print i.name
SuperUser ClusterAdmin HostAdmin NetworkAdmin GlusterAdmin ExternalEventsCreator ExternalTasksCreator
Example 16.7. Listing All Network Details
from ovirtsdk.api import API from ovirtsdk.xml import params try: API = API(url="https://HOST", username="USER", password="PASS", ca_file="ca.crt") hostName = "HOSTNAME" host = API.hosts.get(hostName) for nic in host.nics.list(): print "NIC Name:", nic.get_name() print "IPAddress:", nic.get_ip().get_address() print "MAC Address:", nic.get_mac().get_address() print "netStatus:", nic.get_status().get_state() API.disconnect() except Exception as ex: print "Unexpected error: %s" % ex
The
nic.get_status().get_state()
returns the network status as either up
or down
.
Executing this code on a Python engine will show the network details:
NIC Name: eth0 IPAddress: 10.70.37.194 MAC Address: 00:1a:4a:46:24:9e netStatus: up
You can also set values for a few configurations like IP, netmask, gateway bonding, enabling or disabling the bridge using the following methods:
nic.get_ip().set_address()
nic.get_ip()set_netmask()
nic.get_ip().set_gateway()
nic.set_bridged()
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug