Name
sn_itom_integ_app.ConnectorTagsUtil
Description
Connector instance tag utils script.
Script
var ConnectorTagsUtil = Class.create();
ConnectorTagsUtil.prototype = {
CONNECTOR_INSTANCE_TAG_SCOPED_CACHE_NAME: "sn_itom_integ_app_instance_tags_cache",
TABLE_INSTANCE_DETAILS: "sn_itom_integ_app_instance_details",
EMPTY_INSTANCE_TAG_VALUE: "<EMPTY>",
initialize: function() {
this.evtMgmtConnectorMediator = new global.EvtMgmtConnectorMediator();
},
/*
* Function to return json string of all the tags that are attached to connector instance by sysId.
* It makes use of scoped cache to store instance sys_id - tag details.
* Since, tags on connector are stored without alert tag prefix, this will append the tag name with alert tag prefix before returning it.
*/
getConnectorTags: function(connectorInstanceId) {
if (gs.nil(connectorInstanceId))
return;
// check if tags are present in cache for given connector sys_id
var tagJsonString = this._getFromCache(connectorInstanceId);
if (!tagJsonString) {
gs.info("No entry found in cache for connector instance id - {0} so, making a glide query.", [connectorInstanceId]);
var gr = this._getInstanceDetailsGrByInstanceReferenceId(connectorInstanceId);
if (gr.next()) {
tagJsonString = gr.getValue('instance_tags');
if (tagJsonString) {
// add alert tag prefix to instance tag name before it gets set to alert
var alertTagPrefix = gs.getProperty('evt_mgmt.alert_tags_prefix', 't_');
var tagJson = JSON.parse(tagJsonString);
var newTagJson = {};
for (var key in tagJson) {
newTagJson[alertTagPrefix + key] = tagJson[key];
}
tagJsonString = JSON.stringify(newTagJson);
} else {
// when tags are not present for instance, set it to EMPTY_INSTANCE_TAG_VALUE to avoid redundent database query.
tagJsonString = this.EMPTY_INSTANCE_TAG_VALUE;
}
// put the entry in cache
this._putInCache(connectorInstanceId, tagJsonString, gr.getValue('sys_id'));
}
}
return tagJsonString == this.EMPTY_INSTANCE_TAG_VALUE ? null : tagJsonString;
},
/*
* Function to attach tags based on connector instance reference id. Here tags is valid json string.
*/
addTagsToInstance: function(instanceReferenceId, tags) {
try {
if (gs.nil(instanceReferenceId) || gs.nil(tags))
throw new Error(gs.getMessage("Invalid input - instance reference id or tags is empty/null."));
var instanceDetailsGr = this._getInstanceDetailsGrByInstanceReferenceId(instanceReferenceId);
if (instanceDetailsGr.next()) {
instanceDetailsGr.setValue('instance_tags', tags);
instanceDetailsGr.update();
return true;
} else {
throw new Error(gs.getMessage("Instance details record not found for instance reference id - {0}.", [instanceReferenceId]));
}
} catch (e) {
gs.error("Failed to add instance tags due to {0}.", [e.toString()]);
}
return false;
},
/*
* Function to return list of tag names present in em_alert_tags table.
* Alert tag prefix will be removed from the tag name before sending the list becouse we are storing tags on
* connector without alert tag prefix.
*/
getAllTags: function() {
var tagList = [];
try {
/*
* This api is designed to return most recently used top 100 tags from em_alert_tags table.
* The number of results is limited by the value of the property evt_mgmt.max_alert_tags_for_event_rule
*/
var alertTags = this.evtMgmtConnectorMediator.getAlertTags();
for (var i in alertTags) {
tagList.push(alertTags[i].substring(alertTags[i].indexOf('_') + 1));
}
} catch (e) {
gs.error("Failed to fetch tags due to " + e.toString());
}
return tagList;
},
/*
* Function to add connector instance tag name to em_alert_tags table if it is not present already
* It append alert tag prefix to tag name and calls api to add it in em_alert_tags table if does not exist alreay.
* Tags on connector instance are stored without alert tag prefix.
*
*/
addNewAlertTagIfDoesntExistInTable: function(connectorTags) {
if (connectorTags) {
var alertTagPrefix = gs.getProperty('evt_mgmt.alert_tags_prefix', 't_');
// Add new tags to em_alert_tags table.
// Tags added on connector instance are without alert tag prefix so, append the prefix before adding them to em_alert_tags table
var tagsJson = JSON.parse(connectorTags);
for (var tagName in tagsJson) {
this.evtMgmtConnectorMediator.addNewAlertTagIfDoesntExistInTable(alertTagPrefix + tagName);
}
}
},
/*
* Function to return tags based on connector instance id from the scoped cache.
*/
_getFromCache: function(connectorInstanceId) {
return sn_scoped_cache.ScopedCacheManager.get(this.CONNECTOR_INSTANCE_TAG_SCOPED_CACHE_NAME, "" + connectorInstanceId);
},
/*
* Function to store tag json string for given connector instance id.
*
* Cache behavior: The cache type is 'Table row and column' which means whenever, the 'instance_tag' column of instance details table record for given sysId changes,
* the corresponding cache entry will be flushed out.
*/
_putInCache: function(connectorInstanceId, tagJsonString, instanceDetailsSysId) {
sn_scoped_cache.ScopedCacheManager.putRow(this.CONNECTOR_INSTANCE_TAG_SCOPED_CACHE_NAME, "" + connectorInstanceId, tagJsonString, instanceDetailsSysId, this.TABLE_INSTANCE_DETAILS);
},
_getInstanceDetailsGrByInstanceReferenceId: function(instanceRefTableId) {
var instanceGr = new GlideRecord(this.TABLE_INSTANCE_DETAILS);
instanceGr.addQuery("instance_reference_id", instanceRefTableId);
instanceGr.query();
return instanceGr;
},
type: 'ConnectorTagsUtil'
};
Sys ID
1b46631543952110425078114bb8f2bd