Name

sn_cmdb_int_util.CmdbIntegrationHardwareNameUtil

Description

Utility class to cleanse hardware name (name, domain, fqdn, dns) related fields

Script

var CmdbIntegrationHardwareNameUtil = Class.create();
CmdbIntegrationHardwareNameUtil.prototype = {
  initialize: function() {
  	if (sn_cmdb_int_util.props) {
  		this.hostCaseRule = sn_cmdb_int_util.props.hostCaseRule; 
  		this.nameIncludeDomain = sn_cmdb_int_util.props.nameIncludeDomain; 
  		this.glideRegex = sn_cmdb_int_util.props.glideRegex; 
  	} else {
  		this.hostCaseRule = gs.getProperty('glide.discovery.hostname.case', 'No change');
  		this.nameIncludeDomain = gs.getProperty('glide.discovery.hostname.include_domain', 'false');
  		this.glideRegex = gs.getProperty('glide.discovery.fqdn.regex', "^([^.]+)\\.((?:[^.]+\\.)+[^.]+)$");
  		var props = {}; 
  		props.hostCaseRule = this.hostCaseRule; 
  		props.nameIncludeDomain = this.nameIncludeDomain; 
  		props.glideRegex = this.glideRegex; 
  		sn_cmdb_int_util.props = props; 
  	}
  	
  	this.domainSuffixRegValue = gs.getProperty('sn_cmdb_int_util.domain_suffix_regex', "([.]ac|[.]ac[.]uk|[.]ad|[.]ae|[.]aero|[.]af|[.]ag|[.]ai|[.]al|[.]am|[.]an|[.]ao|[.]aq|[.]ar|[.]arpa|[.]as|[.]asia|[.]at|[.]au|[.]aw|[.]ax|[.]az|[.]ba|[.]bb|[.]bd|[.]be|[.]bf|[.]bg|[.]bh|[.]bi|[.]biz|[.]bj|[.]bm|[.]bn|[.]bo|[.]br|[.]bs|[.]bt|[.]bv|[.]bw|[.]by|[.]bz|[.]ca|[.]cat|[.]cc|[.]cd|[.]cf|[.]cg|[.]ch|[.]ci|[.]ck|[.]cl|[.]cm|[.]cn|[.]co|[.]co[.]uk|[.]com|[.]coop|[.]cr|[.]cs|[.]cu|[.]cv|[.]cw|[.]cx|[.]cy|[.]cz|[.]dd|[.]de|[.]dj|[.]dk|[.]dm|[.]do|[.]dz|[.]ec|[.]edu|[.]ee|[.]eg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.]so|[.]sr|[.]ss|[.]st|[.]store|[.]su|[.]sv|[.]sy|[.]sz|[.]tc|[.]td|[.]tel|[.]tf|[.]tg|[.]th|[.]tj|[.]tk|[.]tl|[.]tm|[.]tn|[.]to|[.]tp|[.]tr|[.]travel|[.]tt|[.]tv|[.]tw|[.]tz|[.]ua|[.]ug|[.]uk|[.]um|[.]us|[.]uy|[.]uz|[.]va|[.]vc|[.]ve|[.]vg|[.]vi|[.]vn|[.]vu|[.]web|[.]wf|[.]ws|[.]xxx|[.]ye|[.]yt|[.]yu|[.]za|[.]zm|[.]zr|[.]zw)$");
  	this.domainSuffixReg = new RegExp(this.domainSuffixRegValue,"i");
  	
  	this.singlePeriodSplit = /_(.+)/;
  	
  	this.invalidValuesList = gs.getProperty('sn_cmdb_int_util.invalid_value_list', "*,N/A,NA,NULL,UNDEFINED,UNKNOWN");
  	this.invalidValues = this.invalidValuesList.split(',');
  	
  	this.nameCandidates = [];
  	this.dnsDomainCandidates = [];
  	
  	this.dnsOut = "";
  	this.fqdnOut = "";
  	this.dnsDomainOut = "";
  	this.nameOut = "";
  	
  	
  },
  
  _initializeOutput: function() {
  	this.nameCandidates = [];
  	this.dnsDomainCandidates = [];
  	this.dnsOut = "";
  	this.fqdnOut = "";
  	this.dnsDomainOut = "";
  	this.nameOut = "";
  },
  
  processNameDomainFqdnDnsSet: function(nameIn, domainIn, fqdnIn, dnsIn ) {
  	
  	this._initializeOutput();
  	this._checkDns(dnsIn);
  	
  	this._checkFqdn(fqdnIn);
  	if (!this.fqdnOut)
  		this._checkFqdn(this.dnsOut);
  	
  	this._checkDnsDomain(domainIn, nameIn);
  	this._checkDnsDomainCandidates(this.dnsDomainCandidates, nameIn);
  	
  	this._checkName(nameIn);
  	this._checkNameCandidates(this.nameCandidates);
  	
  	this._pullNameAndDomainFromFqdn();
  	
  	this._generateFqdnFromNameAndDomain();
  	
  	this._applyNameIncludeDomainFlag();
  	
  	return this;
  },
  
  _pullNameAndDomainFromFqdn: function() {
  	if (this.nameOut && this.dnsDomainOut)
  		return;
  	
  	if (this.fqdnOut) {
  		var fqdnRegex = new RegExp(this.glideRegex);
  		var groups = fqdnRegex.exec(this.fqdnOut);
  		
  		if (!gs.nil(groups) && groups.length == 3) {
  			if (!this.nameOut)
  				this.nameOut = this._applyCase(groups[1]);
  			
  			if (!this.dnsDomainOut)
  				this.dnsDomainOut = this._applyCase(groups[2]);
  		}	
  	}
  },
  
  _applyNameIncludeDomainFlag: function() {
  	if (this.nameOut && this.dnsDomainOut && ("true" == this.nameIncludeDomain)) {
  		this.nameOut = this.nameOut + "." + this.dnsDomainOut;
  	} 
  },
  
  _generateFqdnFromNameAndDomain: function() {
  	if (this.nameOut && this.dnsDomainOut && !this.fqdnOut) {
  		this.fqdnOut = this.nameOut + "." + this.dnsDomainOut;
  	} 
  },
  
  _checkNameCandidates: function(nameCandidates) {
  	if (this.nameOut)
  		return;
  	
  	for (var i = 0; i < nameCandidates.length; i++) {
  		this._checkName(nameCandidates[i]);
  		
  		if (this.nameOut)
  			return;
  	}
  },
  
  _checkName: function(nameIn) {
  	if (!nameIn)
  		return;
  	
  	nameIn = nameIn.trim();
  	
  	if (this.invalidValues.indexOf(nameIn.toUpperCase()) > -1)
  		return;

  	var nameInSplit = nameIn.split(".");
  	
  	var suffixCheck = this.domainSuffixReg.test(nameIn);
  	
  	if (suffixCheck && nameInSplit.length == 1) {
  		return;
  	}
  	
  	// looks like a dnsDomain
  	if (suffixCheck && nameInSplit.length == 2) {
  		this.nameOut = this._applyCase(nameIn);
  		return;
  	}
  	
  	if (suffixCheck && nameInSplit.length > 2) {
  		var fqdnRegex = new RegExp(this.glideRegex);
  		var groups = fqdnRegex.exec(nameIn);
  		
  		if (groups.length == 3) {
  			this.nameOut = this._applyCase(groups[1]);
  			
  			if (!this.dnsDomainOut)
  				this.dnsDomainOut = this._applyCase(groups[2]);
  		}	
  		
  		return;
  	}
  	
  	this.nameOut = this._applyCase(nameIn);

  },
  
  _checkDnsDomainCandidates: function(dnsDomainCandidates, nameIn) {
  	if (this.dnsDomainOut)
  		return;
  	
  	for (var i = 0; i < dnsDomainCandidates.length; i++) {
  		this._checkDnsDomain(dnsDomainCandidates[i], nameIn);
  		
  		if (this.dnsDomainOut)
  			return;
  	}
  	
  },
  
  _checkDnsDomain: function(domainIn, nameIn) {
  	if (!domainIn)
  		return;
  	
  	domainIn = domainIn.trim();
  	
  	if (this.invalidValues.indexOf(domainIn.toUpperCase()) > -1)
  		return;
  	
  	// we are throwing away dns records with spaces because its unclear how to parse
  	if (domainIn.indexOf(" ") > -1)
  		return;

  	var domainInSplit = domainIn.split(".");
  	
  	// if we have no periods this is probably actually a name
  	if (domainInSplit.length == 1) {
  		this.nameCandidates.push(domainIn);
  		return;
  	}
  	
  	var suffixCheck = this.domainSuffixReg.test(domainIn);
  	
  	var startWithName = false;
  	
  	if (nameIn)
  		startWithName = domainIn.indexOf(nameIn) == 0;
  	
  	if (suffixCheck && !startWithName) {
  		this.dnsDomainOut = this._applyCase(domainIn);
  		return;
  	}
  	
  	// looks more like an fqdn
  	if (suffixCheck && startWithName && !this.fqdnOut) {
  		this.fqdnOut = this._applyCase(domainIn);
  		
  		var domainSplit = this.domainIn.split(singlePeriodSplit);
  		
  		this.nameCandidates.push(domainSplit[0]);
  		this.dnsDomainOut = this._applyCase(domainSplit[1]);
  		
  		return;
  	}

  },
  
  _checkFqdn: function(fqdnIn) {
  	if (!fqdnIn)
  		return;
  	
  	fqdnIn = fqdnIn.trim();
  	
  	if (this.invalidValues.indexOf(fqdnIn.toUpperCase()) > -1)
  		return;
  	
  	// we are throwing away dns records with spaces because its unclear how to parse
  	if (fqdnIn.indexOf(" ") > -1)
  		return;

  	var fqdnSplit = fqdnIn.split(".");
  	
  	// if we have no periods this is probably actually a name
  	if (fqdnSplit.length == 1) {
  		this.nameCandidates.push(fqdnIn);
  		return false;
  	}
  	
  	var suffixCheck = this.domainSuffixReg.test(fqdnIn);
  	
  	// looks more like a domain
  	if (fqdnIn.length == 2 && suffixCheck)
  		this.dnsDomainCandidates.push(fqdnIn);
  	else if (suffixCheck)
  		this.fqdnOut = this._applyCase(fqdnIn);
  	
  	// we can't find an fqdn then we will try to use the dnsOut
  	if (!this.fqdnOut && this.dnsOut)
  		this.fqdnOut = this.dnsOut;
  },
  
  _checkDns: function(dnsIn) {
  	if (!dnsIn)
  		return;
  	
  	dnsIn = dnsIn.trim();
  	
  	if (this.invalidValues.indexOf(dnsIn.toUpperCase()) > -1)
  		return;
  	
  	// we are throwing away dns records with spaces because its unclear how to parse
  	if (dnsIn.indexOf(" ") > -1)
  		return;

  	var dnsSplit = dnsIn.split(".");
  	
  	// if we have no periods this is probably actually a name
  	if (dnsSplit.length == 1) {
  		this.nameCandidates.push(dnsIn);
  		return;
  	}
  	
  	var suffixCheck = this.domainSuffixReg.test(dnsIn);
  	
  	// looks more like a domain
  	if (dnsSplit.length == 2 && suffixCheck)
  		this.dnsDomainCandidates.push(dnsIn);
  	else if (suffixCheck)
  		this.dnsOut = this._applyCase(dnsIn);
  	
  	// if the suffix pass fails we really don't have a value worth keeping i think
  },
  
  _applyCase: function(stringValue) {
  	if (!stringValue)
  		return null;
  	
  	if (this.hostCaseRule == 'Lower case')
          return stringValue.toLowerCase();
      else if (this.hostCaseRule == 'Upper case') 
          return stringValue.toUpperCase();
  	
  	return stringValue;
  },
  
  type: 'CmdbIntegrationHardwareNameUtil'
};

Sys ID

c989228d732100102b6265a751ab9e86

Offical Documentation

Official Docs: