The most pressing need for this class are machine queries. For example, there is often a need to select machines based on a specific cluster. Also, we may need to order these results and combine the various filters on a per-query basis.
Here is what the method definition looks like:
#ECP - The Query.get_machines() method.
@classmethod
def get_machines(cls, *args, **kw):
"""Retrieve a list of machines.
@status: Experimental"""
filter=False
join=[]
if kw.has_key('order_by'):
order_by=kw['order_by']
else:
order_by='machine_name'
if kw.has_key('machine_uuid'):
machine_uuid=kw['machine_uuid']
else:
machine_uuid=False
if kw.has_key('cluster_uuid'):
cluster_uuid=kw['cluster_uuid']
else:
cluster_uuid=False
if kw.has_key('namefilter'):
namefilter=kw['namefilter']
else:
namefilter=False
if kw.has_key('children'):
children=kw['children']
else:
children=False
if cluster_uuid:
try:
cluster_obj=UUIDSearch.cluster(cluster_uuid)
except E2ClusterNotFound, e:
e.store_traceback()
return []
filter=[]
machine_part=Machine.q.id==ClusterMachine.q.machine
join_part=ClusterMachine.q.clusters==cluster_obj.id
filter.append(machine_part)
filter.append(join_part)
if namefilter:
name_part=LIKE(Machine.q.machine_name, "%s%%"%(namefilter))
filter.append(name_part)
elif machine_uuid:
try:
machine_obj=UUIDSearch.machine(machine_uuid)
except E2MachineNotFound, e:
e.store_traceback()
return []
if children:
filter=[]
machine_part=Machine.q.parent==Hypervisor.q.id
join_part=Hypervisor.q.machine==machine_obj
filter.append(machine_part)
filter.append(join_part)
else:
return [machine_obj]
elif namefilter:
filter=LIKE(Machine.q.machine_name, "%s%%"%(namefilter))
result=PermSelect.machine(perm='r',\
orderby=order_by,\
filter=filter,\
join=join)
return result
If a cluster_uuid was specified, we retrieve the Cluster instance. We then construct the table-joining query component that joins the machine and cluster tables. If there is a name_filter specified, we construct a query component based on it.
If a machine_uuid was specified, we retrieve the machine instance. Then, we check if the children parameter was set to true. If so, we build the joining query components to select the children of the specified machine. Otherwise, we return the single machine instance.
Finally, we use the PermSelect class to execute the final query, which integrates permission checking in the selection. Another benefit of this method is that it will significantly cut down on the number of queries executed eventually.
No comments :
Post a Comment