Name

global.DiscoveryErrorsUtilities

Description

Code to handle processing of discovery errors

Script

var DiscoveryErrorsUtilities;

(function () {
  var FILE_NAME = 'DiscoveryErrorsUtilities',
  	LOG_PREFIX = '[' + FILE_NAME + ']';
  
  var nodeLogger = new sn_automation.AutomationAPI().getNodeLogger();
  var discovery_app_id = 'd5069fe9e70332001a310a6103f6a94b';
  var authenticationFailedErrorByCredentialType = {
  	'ssh password': 'SN-1005',
  	'ssh private key': 'SN-1005',
  	'windows': 'SN-1099',
  	'cim': 'SN-1100'
  };
  var missingCredentialsErrorCode = 'SN-1068';

  DiscoveryErrorsUtilities = {
  	retryPendingErrors: retryPendingErrors,
  	getFirstMatchedItomErrorCodeByCredentialTypes: 
  		getFirstMatchedItomErrorCodeByCredentialTypes
  };

  // Run quick discovery on all errors that are in "pending" state.
  // Place the errors in "in discovery" state before kicking off the discovery
  function retryPendingErrors() {
  	// Find (in batches) all of the errors that are in "pending" state
  	// For each pending error, attempt a quick discovery
  	var error = new GlideRecord('automation_error_msg');
  	error.addQuery('status', '4'); // "Pending discovery" errors.
  	error.addQuery('application', discovery_app_id);
  	error.orderBy('sys_updated_on'); // fifo
  	error.setLimit(gs.getProperty('glide.discovery.errors.retry.batchsize', 100));
  	error.query();
  	while (error.next()) {
  		retrySingleError(error.getValue('sys_id'));
  	}
  }

  function retrySingleError(errorSysId) {
  	var manager = new sn_svcerr.ErrorMgrScript(discovery_app_id);
  	// Retry a single error but in the context of Retry All so that we get the correct UI behavior
  	manager.retrySingleErrorForMultiple(errorSysId);
  }
  
  function getFirstMatchedItomErrorCodeByCredentialTypes(credentialTypes) {
  	if (!Array.isArray(credentialTypes))
  		throw TypeError('Invalid Argument: Expected argument to be an array');

  	if (credentialTypes.length == 0)
  		throw new Error('No credential types were provided');
  	nodeLogger.debug(LOG_PREFIX 
  					+ ' Trying to find the first error code matching to one of the following values: ' 
  					+ JSON.stringify(credentialTypes));
  	
  	var errorCode = missingCredentialsErrorCode;
  	
  	for (var index in credentialTypes) {
  		var credentialType = credentialTypes[index];
  		if (isMappedToErrorCode(credentialType)) {
  			errorCode = authenticationFailedErrorByCredentialType[credentialType];
  			break;
  		}	
  	}
  	return errorCode;
  }
  
  function isMappedToErrorCode(credentialType) {
  	nodeLogger.debug(LOG_PREFIX 
  					+ ' Looking for "' 
  					+ credentialType 
  					+ '" in ' 
  					+ JSON.stringify(
  						Object.keys(
  							authenticationFailedErrorByCredentialType
  						)));
  	var found = authenticationFailedErrorByCredentialType[credentialType] !== undefined;
  	nodeLogger.debug(LOG_PREFIX + '"' + credentialType + '" was ' + (!found ? 'not' : '') + 'found');

  	return found;
  }

})();

Sys ID

e7c6bb74674113002e8aa0119585ef47

Offical Documentation

Official Docs: