Name
sn_itom_pattern.CloudEventSensor
Description
Parsing the response from GCP events rest api and updating Events Cloud table accordingly
Script
var CloudEventSensor = Class.create();
CloudEventSensor.prototype = {
GLIDE_CMP_DEBUG: 'glide.cmp.debug',
initialize: function() {
},
/***
* this method will return the latest event timestamp
* @param entriesArr
*/
getLastTimeStamp : function(entriesArr){
var latestTimestamp;
if(entriesArr.length > 0){
latestTimestamp = entriesArr[entriesArr.length-1].timestamp;
}
return latestTimestamp;
},
parseJson : function(jsonString) {
var entriesArr = this.getEntriesList(jsonString);
var accountID = '';
if(entriesArr != null){
for(var key in entriesArr){
var entry = entriesArr[key];
accountID = entry.resource.labels.project_id;
var resourceId = entry.resource.labels.instance_id;
var resourceType = entry.resource.type;
this.populateCloudEvent(entry, accountID, resourceId, resourceType);
}
var latestTimestamp = this.getLastTimeStamp(entriesArr);
if(latestTimestamp){
gs.info("CloudEventSensor: updated account ID "+accountID+" with timestamp "+latestTimestamp);
this.updateSaHashTable(accountID, latestTimestamp);
}else{
var currentTimestamp= jsonString[0].currentProcessingTimestamp;
accountID= jsonString[0].accountId;
gs.info("CloudEventSensor: updated account ID "+ accountID +" with currentTimestamp "+ currentTimestamp);
this.updateSaHashTable(accountID, currentTimestamp);
}
}else{
gs.warn("CloudEventSensor: could not find entries tag");
}
},
updateSaHashTable : function(accountID, timestamp)
{
var tsHandler = new EventsTimestampHandler();
tsHandler.updateSaTableForAccountWithLatestEvent(accountID, timestamp);
},
populateCloudEvent : function(entry, serviceAccountId, resourceId, resourceType) {
var cloudEventGr = new GlideRecord('sn_cmp_cloud_event');
var headerName = 'event-type';
var headers = {};
headers[headerName] = 'GoogleStackDriver';
cloudEventGr.initialize();
cloudEventGr.event_name = 'ConfigurationItemChangeNotification';
cloudEventGr.state = 'ready';
cloudEventGr.source = 'googlestackdriver';
cloudEventGr.resource_id = resourceId;
cloudEventGr.sys_domain = this.getDomain(serviceAccountId);
cloudEventGr.resource_type = this._getResourceType(resourceType);
cloudEventGr.headers = JSON.stringify(headers);
cloudEventGr.query_params = '{}';
cloudEventGr.payload = JSON.stringify(entry);
cloudEventGr.insert();
//}
},
getDomain : function(serviceAccountId) {
var accountGR = new GlideRecord("cmdb_ci_cloud_service_account");
accountGR.addQuery("account_id", serviceAccountId);
accountGR.setLimit(1);
accountGR.query();
if (accountGR.next()) {
var sys_domain = accountGR.getValue("sys_domain");
}
return sys_domain;
},
getEntriesList : function(jsonString) {
return jsonString[0].entries;
},
_getResourceType : function(resourceName)
{
var ciGr = new GlideRecord('sn_capi_resource_type');
ciGr.addQuery('name',resourceName);
ciGr.query();
if(ciGr.next())
return ciGr.sys_id;
},
type: 'CloudEventSensor'
};
Sys ID
6f8bdd671b053f802e1cfccf1d4bcbd3