2. Example: Scripts to Manage Resources of a Specific Type

Section 1, “Example: Scripts to Manage Inventory (All Resource Types)” shows a general resource discovery and import script. In some environments, it may be more useful to discover and import only specific types of resources. For example, and administrator may be constantly deploying new applications to an app server, or maybe spinning up and destroying application servers dynamically to respond to load. In that case, the administrator only wants to find the specific resources which he knows are frequently created.
For this example, the workflow has some pretty basic steps:
  1. Search for new, uncommitted resources of a specific type.
  2. Get the resource IDs.
  3. Import those new resources.
  4. Do something with the newly-imported resources.
Steps 1 through 3 are basically the same as the autoimport.js example in Section 1.1, “Automatically Import New Resources: autoimport.js”, with one significant change. The search has an extra filter for the resource type.
Uncommitted (new) resources can be identified through a ResourceCriteria search by adding a search parameter based on the inventory status and the resource type (in this example, JBoss EAP 6 domain deployments, for new web applications). The new findUncommittedJbasApps() function runs that search.
function findUncommittedJbasApps() {
    var criteria = ResourceCriteria();
    criteria.addFilterInventoryStatus(InventoryStatus.NEW);
    criteria.addFilterResourceTypeName('DomainDeployment');
     
    return ResourceManager.findResourcesByCriteria(criteria);
}
As with the autoimport.js example, a getIds function retrieves an array of the resource IDs.
function getIds(resources) {
	var ids = [];

	if (resources.size() > 0) {
		println("Found resources to import: ");
		for (i = 0; i < resources.size(); i++) {
			resource = resources.get(i);
			ids[i] =  resource.id;
			println("  " + resource.name);
		}
	} else {
		println("No resources found awaiting import...");
	}

    return ids;
}
Those two functions accomplish the first two steps: search for uncommitted resources of a specific type and then retrieve those IDs.
Step 3 then runs the DiscoveryBoss method to import the discovered resources.
The first half of the script, then mimics the original autoimport.js, with the slight adjustment to the new function to search for JBoss EAP 6 domain deployments.
rhq.login('rhqadmin', 'rhqadmin');
println("Running autoImport.js");
 
var resources = findUncommittedJbasApps();
var resourceIds = getIds(resources);
DiscoveryBoss.importResources(resourceIds);
The last step simply does something with the new resource.
For a new domain deployment, then it is probably most useful to assign the new domain deployment to server group, so the new application is deployed. For example, this assigns all new applications automatically to a server group used for the staging environment, using the promote operation:
var config = new Configuration();
config.put(new PropertySimple("server-group", "Staging-Server-Group") );

OperationManager.scheduleResourceOperation(
          resources.get(0).id,
          "promote",
	  0, // 0 means that the delay was skipped
          1,
	  0, // this skips the repeat count
          10000000,
	  config, 
          "promote new app to server group"
	  );
Alternatively, if this script is used to add a new service or a new server, then it may be more appropriate to add the new resource to a group. This example uses an explicit group ID (15001) for an existing compatible group. The assumption is that the group has already been created, before this script is run, and there is no need to create a new group for the resources.
ResourceGroupManager.addResourcesToGroup(15001, [resourceIds]);
Then, have the script log out from the CLI and close.
rhq.logout();