Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonwsaxe/8104b82e1f52e03e53920fef26ef0286 to your computer and use it in GitHub Desktop.
Save brandonwsaxe/8104b82e1f52e03e53920fef26ef0286 to your computer and use it in GitHub Desktop.
vRealize Orchestrator JS to perform blocking compute resource data collection in vRealize Automation
dataCollectComputeResource(computeResource, 'inventory', 60);
/**
*
*@param {vCAC:HostMachine} computeResource
* @param {string} collectionType - The type of data collection. E.g. 'inventory', 'state', 'performance'
* @param {number} interval - number seconds to wait between polling for collection completion
*/
function dataCollectComputeResource(computeResource, collectionType, interval) {
var entities = getEntities(computeResource, collectionType || 'inventory');
doDataCollection((Server.findForType('vCAC:VCACHost', computeResource.getEntity().hostId)), interval, entities);
/**
* Get the IaaS entities for which to trigger data collection
* @param {vCAC:HostMachine} computeResource
* @param {string} collectionType - The type of data collection. E.g. 'inventory', 'state', 'performance'
*/
function getEntities(computeResource, collectionType) {
const FILTER_SPEC_NAME = 'vSphere';
//const collectionType = 'inventory';
//System.log(System.getObjectClassName(computeResource));
var entity = computeResource.getEntity();
host = Server.findForType('vCAC:VCACHost', entity.hostId);
var result = vCACEntityManager.readModelEntitiesBySystemQuery(
entity.hostId, "ManagementModelEntities.svc", "FilterSpecs",
"FilterSpecGroup/FilterSpecGroupName eq '" + collectionType + "' " +
"and FilterSpecName eq '" + FILTER_SPEC_NAME + "'", null, "*", null, null, null);
System.log(result);
System.log(System.getObjectClassName(result));
System.log(result.length);
result.forEach(function (e) {
var record = [],
tuple;
for (var f in e.getProperties()) {
tuple = [f, e.getProperty(f)];
record.push(tuple.join("="));
}
System.log(record);
});
var result = vCACEntityManager.readModelEntitiesBySystemQuery(
entity.hostId, "ManagementModelEntities.svc", "DataCollectionStatuses",
"FilterSpec/FilterSpecID eq " + result[0].keyString +
" and EntityID eq " + entity.keyString +
" and LastCollectedTime ne null and LastCollectedStatus eq true and IsDisabled eq false",
null, "*", null, null, null);
System.log(result);
System.log(System.getObjectClassName(result));
System.log(result.length);
result.forEach(function (e) {
var record = [],
tuple;
for (var f in e.getProperties()) {
tuple = [f, e.getProperty(f)];
record.push(tuple.join("="));
}
System.log(record);
});
dataCollectionEntities = [result[0]];
return dataCollectionEntities;
}
/**
*
* @param {vCAC:VCACHost} host
* @param {number} interval
* @param {Array/vCAC:Entity} dataCollectionEntities
*/
function doDataCollection(host, interval, dataCollectionEntities) {
const modelName = 'ManagementModelEntities.svc';
const entitySetName = 'DataCollectionStatuses';
interval = (interval * 1000) || (30 * 1000);
function getLastCollectedTime(guid) {
var entities = vCACEntityManager.readModelEntitiesByCustomFilter(
host.id, modelName, entitySetName, new Properties({
DataCollectionStatusID: guid
}), null);
if (entities.length != 1)
throw new Error("unexpected result");
return entities[0].getProperty("LastCollectedTime");
}
/* initialize status record */
var statuses = new Array(dataCollectionEntities.length);
for (var e in dataCollectionEntities)
statuses[e] = null;
/* trigger Data Collection */
dataCollectionEntities.forEach(function (entity) {
var entityKey = entity.keyString;
System.log("Updating entity with key: " + entityKey);
var links = null;
var headers = null;
var updateProperties = {
"LastCollectedTime": null
};
vCACEntityManager.updateModelEntityBySerializedKey(host.id, modelName, entitySetName, entityKey, updateProperties, links, headers);
})
/* block until done */
while (~statuses.indexOf(null)) {
System.sleep(interval);
for (var e in dataCollectionEntities)
statuses[e] = getLastCollectedTime(dataCollectionEntities[e].getProperty("DataCollectionStatusID"));
System.log(statuses.toSource());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment