Author Topic: Python: Getting groups list  (Read 4910 times)

chrisg

  • Jr. Member
  • **
  • Posts: 2
Python: Getting groups list
« on: March 30, 2010, 06:50:57 AM »
Hey all,

I'm currently working with qube via the python API, and i have to say it's a pleasant experience. Nice work, qube people!

A (possibly) dumb question, but is there a way to retrieve a list of worker groups from the system other than looping through hostInfo()? Or would that be the way to go?

many thanks,
chrisg

jburk

  • Administrator
  • *****
  • Posts: 493
Re: Python: Getting groups list
« Reply #1 on: March 30, 2010, 02:06:04 PM »
qb.hostinfo() is what you want, and is not a terribly expensive operation even when you have several hundred hosts.  (Unlike qb.jobinfo(subjobs=True,agenda=True), which can be very expensive when you have 1,000's of jobs in the system.  Calling qb.jobinfo() with no filters is to be avoided if at all possible)

That being said, list comprehensions can make things a bit easier:

groups = [x['groups'] for x in qb.hostinfo() if len(x['groups'])]

which will get you something like:

['testGrpA,testGrpB', 'sample2']

Since hosts can belong to more than 1 group, the group membership for any 1 host can be a comma-separated string, like 'testGrpA,testGrpB' in the example above, so you still have to iterate over the list of groups, splitting then and adding them into a set or some other way of culling duplicates.
« Last Edit: March 30, 2010, 02:11:23 PM by jburk »

chrisg

  • Jr. Member
  • **
  • Posts: 2
Re: Python: Getting groups list
« Reply #2 on: March 30, 2010, 10:12:29 PM »
Brilliant. Thanks for the info.  :)

cheers,
chrisg