16.4. Retrieving Resources from a Collection
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
getmethod 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
listmethod 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 Gluster Storage Console.max- The maximum number of resources to retrieve.case_sensitive- Whether or not search terms are to be treated as case sensitive (TrueorFalse, the default isTrue).
Example 16.1. Retrieving a List of Resources in a Collection Matching a Keyword Based Filter
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
16.4.1. Retrieving a Specific Resource from a Collection Copy linkLink copied to clipboard!
get method.
name parameter of the get method:
cl = api.clusters.get("Default")
cl = api.clusters.get(name="Default")
16.4.2. Retrieving a List of Resources from a Collection Copy linkLink copied to clipboard!
list method.
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
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)
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
api.hosts.list() provides a dictionary list that contains the host name and the host ID.
h1 (51b29199-3a7f-49e4-b0d5-92afdeea1f15)
h2 (4e8c3642-e654-456c-9dff-a240c8a43b1f)
host1 (14e3823d-d557-4635-bd9f-0c6157ab7f07)
Important
list method of a collection is restricted to returning only as many elements as allowed by the SearchResultsLimit configuration key in the Red Hat Gluster 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 Gluster 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
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
User name: admin (User ID: fdfc627c-d875-11e0-90f0-83df133b58cc)
Example 16.6. Listing All Roles
getUsers() function lists all the roles in the Red Hat Gluster Storage Console.
def listRoles():
""" Return list of user roles """
roles = []
for role in api.roles.list():
roles.append(role)
return roles
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
nic.get_status().get_state() returns the network status as either up or down.
NIC Name: eth0
IPAddress: 10.70.37.194
MAC Address: 00:1a:4a:46:24:9e
netStatus: up
nic.get_ip().set_address()nic.get_ip()set_netmask()nic.get_ip().set_gateway()nic.set_bridged()