Name
global.CloudPatternInvocation
Description
This script include handle a cloud discovery by using a pattern. This file encapsulate all the logic of garnering the required data for the pattern invocation and in the end responsible for invoke itself. The main use for this script is for the Event Based Discovery
Script
var CloudPatternInvocation = Class.create();
var deleteHandler = new global.CloudDeleteStrategyUtil();
CloudPatternInvocation.prototype = {
isDebug: false,
debugCounter: 1,
eventId: '',
_debug: function(msg, obj) {
if (this.isDebug) {
gs.info('@@@CloudPatternInvocation[{0}][{1}]->{2} data->{3}', this.debugCounter, this.eventId, msg, JSON.stringify(obj, null, 2));
this.debugCounter++;
}
},
initialize: function() {},
_printEventError: function(errorMsg, configObj, response) {
var msg = '@@@CloudPatternInvocation->' +errorMsg + " event configObj- " + JSON.stringify(configObj, null, 2);
response.state = 'error';
response.error_message = msg;
gs.error(msg);
},
_validateConfiguratione: function(configObj, response) {
//Validate mandatory inputs on the configObj
var errMessage = 'Error: invokeQuickDiscovery -> ';
if (!configObj || typeof configObj !== 'object') {
errMessage += 'expect to get configuration object as an argument:';
this._printEventError(errMessage, configObj, response);
return false;
} else {
var mandatoryParams = ["changeType", "classType", "accountId", "ldc", "inputObjectId", "cloudId", "patternId", "eventId", "eventType", "eventData"];
for (var i = 0; i < mandatoryParams.length; i++) {
if (!configObj[mandatoryParams[i]]) {
errMessage += 'expect to get mandatory parameter within the object argument: ' + mandatoryParams[i];
this._printEventError(errMessage, configObj, response);
return false;
}
}
}
return true;
},
/* This method expect to get an object of configuration with some mandatory data e.g.
* @Param configObj = {
* changeType: String,
* classType: String,
* accountId: String,
* ldc: String,
* inputObjectId: String,
* cloudId: String,
* patternId, String
* eventData: String
* }
* @Param response- holds the object for the response (e.g. response.state, response.error_message, response.discovery_status...)
* return true if pattern was launched and 'Discovery Status Id' exists or false in any case of error.getContentType
* Detailed of the invocation is returned through the response obj
*/
eventBasedDiscovery: function(configObj, response) {
this._debug("Start eventBasedDiscovery", configObj);
if (!this._validateConfiguratione(configObj, response)) {
return false;
}
this.eventId = configObj.eventId;
//Retrieving the actuall sysIds from the 'strings'
var serviceAccountSysId = this.getServiceAccountSysId(configObj.accountId, response);
if (!serviceAccountSysId){
return false;
}
var ldcsMap = this.getServiceAccountLDCs(serviceAccountSysId);
if (Object.keys(ldcsMap).length < 1){
return false;
}
var currentLdcSysId = this.getLdcSysId(ldcsMap, configObj.ldc, response);
if (!currentLdcSysId && configObj.eventType === "AWS"){
return false;
}
//Enrich the config with necessary data for the discovery invocation
configObj.serviceAccountSysId = serviceAccountSysId;
configObj.ldcSysId = currentLdcSysId;
configObj.ldcsMap = ldcsMap;
if (configObj.changeType.toUpperCase().trim() === "DELETE") {
return deleteHandler.handleDelete(configObj, response);
} else {
//Invoke the Discovery on the AWS resource we got in the event with its correlated pattern
var statusId = this.invokeQuickDiscovery(configObj, response);
response.discovery_status = statusId;
gs.info("'@@@CloudPatternInvocation->eventBaseDiscovery() -> pattern invocation ended with statusId - " + statusId);
return (statusId)? true : false;
}
},
invokeQuickDiscovery: function(configObj, response) {
this._debug("Start invokeQuickDiscovery", configObj);
if (configObj.serviceAccountSysId && configObj.ldcSysId) {
var discoveryStatusId = this.createDiscoveryStatus(configObj);
configObj.discoveryStatusId = discoveryStatusId;
if (discoveryStatusId) {
this._debug("Start pattern invocation", {});
var cloudApplicationDiscovery = new global.CloudApplicationDiscovery();
var result = cloudApplicationDiscovery.discoverSpecificResourceByPattern(configObj);
this._debug("End pattern invocation", result);
if (result){
return discoveryStatusId;
}else {
this._printEventError(gs.getMessage('invokeQuickDiscovery did not succeed to invoke the pattern:'), configObj, response);
return null;
}
} else {
this._printEventError(gs.getMessage('invokeQuickDiscovery did not succeed to create a discovery status:'), configObj, response);
return null;
}
} else {
this._printEventError(gs.getMessage('invokeQuickDiscovery expect to get all sysIds out of the string names from the mandatory parameters: '), configObj, response);
return null;
}
return null; //We should not reach to this point!
},
getServiceAccountSysId: function(serviceAccountIdStr, response) {
this._debug("Start getServiceAccountSysId", serviceAccountIdStr);
var serviceAccountGr = new GlideRecord('cmdb_ci_cloud_service_account');
if (!serviceAccountGr.get('account_id', serviceAccountIdStr)) {
this._printEventError("invokeQuickDiscovery-> getServiceAccountSysId() - can not find account -", serviceAccountIdStr, response);
return null;
}
var sysId = serviceAccountGr.getUniqueValue();
this._debug("End getServiceAccountSysId", sysId);
return (sysId) ? sysId : null;
},
getServiceAccountLDCs: function(serviceAccountSysId, response) {
this._debug("Start getServiceAccountLDCs", serviceAccountSysId);
//The LDC is always dependent on the Account and hosted on it(so the Account is the child and all the parrent should be the LDC)
var relCiGr = new GlideRecord("cmdb_rel_ci");
relCiGr.addQuery("child", serviceAccountSysId);
relCiGr.addQuery('type', '5f985e0ec0a8010e00a9714f2a172815'); // Hosted-on relation
relCiGr.query();
//Garner all ldcs Ids
var listOfLDCSydIds = [];
while (relCiGr.next()) {
listOfLDCSydIds.push(relCiGr.getValue("parent"));
}
this._debug("listOfLDCSydIds", listOfLDCSydIds);
if (listOfLDCSydIds.length < 1) {
this._printEventError("AzureAlertHandlerV2-> getServiceAccountRegions() - No LDCs were found under the account", serviceAccountSysId, response);
return null;
}
var dataCenterGr = new GlideRecord('cmdb_ci_logical_datacenter');
dataCenterGr.addQuery("sys_id", "IN", listOfLDCSydIds.toString());
dataCenterGr.query();
//Build a map of ldcs and their sys_ids
var ldcsMap = {};
while (dataCenterGr.next()) {
var region = dataCenterGr.getValue("region");
var ldcSysId = dataCenterGr.getUniqueValue();
ldcsMap[region] = ldcSysId;
}
this._debug("End getServiceAccountLDCs", ldcsMap);
return ldcsMap;
},
getLdcSysId: function(ldcsMap, ldcStr, response) {
this._debug("Start getLdcSysId ldc-"+ldcStr, ldcsMap);
if (!ldcsMap[ldcStr]) {
this._printEventError("invokeQuickDiscovery-> getServiceAccountSysId() - can not find LDC -", ldcStr, response);
return null;
} else {
this._debug("End getLdcSysId", ldcsMap[ldcStr]);
return ldcsMap[ldcStr];
}
},
createDiscoveryStatus: function(configObj) {
var newStatus = new GlideRecord("discovery_status");
newStatus.setValue("description", configObj.eventType+"-"+configObj.eventId);
newStatus.setValue("source", "cloud_quick_discovery_upon_event");
newStatus.setValue("discover", "Cloud");
var statusId = newStatus.insert();
return (statusId) ? statusId : null;
},
type: 'CloudPatternInvocation'
};
Sys ID
9e0ac0a87753330099808d116810617c