Share:|

I see a lot of requests on the communities that end up requiring one to list a bunch of objects and then do something so I thought I'd provide some examples from each workspace that show how to listallthethings.

 

First off we'll start with servers.

 

Listing all the servers in a server group is pretty easy:

blcli_execute Server listServersInGroup "/Workspace/All Available Servers by OS/Linux - Red Hat/All Red Hat"

spits out a list of all my RedHat server names.  That's great, but sometimes I need the server DBKey or server id.  Since this command does almost what I want I'm going to look at what it's calling in the Unreleased blcli commands and documentation we see:

CommandInputReturn value stored name
ServerGroup.groupNameToId$qualifiedGroupName$groupId
Server.findAllHeadersByServerGroupNAMED_OBJECT=groupId false-
SDeviceHeader.getNameno input-
Utility.setTargetObjectno input-
Utility.listPrintno input-

Ok, so that's good - it's not directly calling an API command so I can probably run the same commands in sequence but instead of SDeviceHeader.getName there's hoptfully a .getId and/or .getDBKey in SDeviceHeader.  Upon inspection of the SDeviceHeader namespace I do see a getDeviceId and getDBKey.  Great!  So I can put together a couple sets of commands:

 

# interestingly this works for both smart and static groups
blcli_execute ServerGroup groupNameToId "/Workspace/All Available Servers by OS/Linux"
blcli_storeenv serverGroupId
blcli_execute Server findAllHeadersByServerGroup ${serverGroupId} true
blcli_execute SDeviceHeader getDBKey
blcli_execute Utility setTargetObject
blcli_execute Utility listPrint
blcli_storelocal serverDBKeys

 

or

blcli_execute ServerGroup groupNameToId "/Workspace/All Available Servers by OS/Linux"
blcli_storeenv serverGroupId
blcli_execute Server findAllHeadersByServerGroup ${serverGroupId}
blcli_execute SDeviceHeader getDeviceId
blcli_execute Utility setTargetObject
blcli_execute Utility listPrint
blcli_storelocal serverIDs

 

If I want to get *all* the servers in my environment then I'll look at the Server.listAllServers command and see what it's calling:

CommandInputReturn value stored name
Server.findAllDeviceHeadersno input-
SDeviceHeader.getNameno input-
Utility.setTargetObjectno input-
Utility.listPrintno input-

Same idea, replace the SDeviceHeader.getName with the command you want.

 

Then you have a list (serverDBKeys, serverIDs, etc) you can iterate over:

while read serverDBKey
    do
    echo "DBKey: ${serverDBKey}"
# trim off the trailing empty line of the key list.
done "$(awk 'NF' <<< "${serverDBKeys)")"

 

Note in the above example we are loading the object header, not the full object, which is much faster and uses less memory.  There are commands like Server.findAllBy* and those will load the entire server object for all objects in the group (or all or whatever you call) and that will likely be slower and use more of the blcli's heap so those examples were not provided above.  The one advantage with loading the object is that you can iterate through the list of objects and directly act directly on each object to get or set something.  Sometimes you need to do that because there are not other blcli commands for the action you are trying to do that take a DBKey, name or Id as input.

 

For example you can do something like:

blcli_execute Server findAllByServerGroup ${groupId}
blcli_execute Utility setTargetObject serverList
blcli_execute Utility listLength
blcli_storelocal listLength
for i in {0..$((${listLength}-1))}
   do
   blcli_execute Utility setTargetObject serverList
   blcli_execute Utility listItemSelect ${i}
   blcli_execute Utility setTargetObject
   blcli_execute Utility storeTargetObject server
   blcli_execute Server setDescription "Test Server"
   blcli_execute Server update NAMED_OBJECT=server
done

And iterate through the objects themselves, load them and then act on them.  The above example sets the description of the server object to 'Test Server".  This is just a simple example for this exercise.

 

Hopefully after the above example we've gotten a little more familiar with using the Unreleased blcli commands and documentation, understanding how to construct our own sequence of unreleased commands to do something that released commands don't do and learning a couple ways we can get a list of objects and iterate through them.