Name

sn_itom_pattern.CapiToPatternPrerequisiteScript

Description

No description available

Script

var CapiToPatternPrerequisiteScript = Class.create();
CapiToPatternPrerequisiteScript.prototype = Object.extendsObject(global.ProbeToPatternMigrationAjax, {
  initialize : function() {
  	this.newLine = "\n";
  	this.dash = "--------------------------------------------------------";
  	this.passTrue = "- Pass: TRUE";
  	this.passFalse = "- Pass: FALSE";
  	this.overallStatus = true;
  	this.awsConversion = false;
  	this.azureConversion = false;
  	this.patternLogs = this.newLine;
  	this.capiLogs = this.newLine;
  	this.RUN_ALL = false;
  	this.scriptIncludeArray =
  		[{name: 'DiscoveryPatternOrchestratorEventHandler', sysId: '4eea4e860f3580106dc4fe39b4767ef3'},
  		{name: 'DiscoveryPatternOrchestratorFlowLauncher', sysId: 'b0b205820ff180106dc4fe39b4767e4b'},
  		{name: 'DiscoveryPatternOrchestratorFlowManager', sysId: 'baf9b6b00f9040106dc4fe39b4767e5e'},
  		{name: 'DiscoveryPatternOrchestratorUtil', sysId: '9c3d828a0f3580106dc4fe39b4767eb1'},
  		{name: 'PatternPayloadUtil', sysId: 'c9f7ed010ff140106dc4fe39b4767ec8'},
  		{name: 'DiscoveryPageManager', sysId: '5b5dbf100f2700106dc4fe39b4767e17'},
  		{name: 'AbstractDiscoveryMigrationHelper', sysId: '650e9815dbee3300ef28f3e51d961972'}];
  	
  	this.awsScriptInclude = {name: 'DiscoveryAwsMigrationHelper', sysId: 'c1865899dbae3300ef28f3e51d961966'};
  	this.azureScriptInclude = {name: 'DiscoveryAzureMigrationHelper', sysId: '7b8f5895dbee3300ef28f3e51d961966'};
  	
  	this.awsPatternArray =
  		[{name: 'Amazon AWS - ACL (LP)', sysId: '7d5eccbfdb72c0109149f2131f961919'},
  		{name: 'Amazon AWS - Application and Network LB (LP)', sysId: '057eb3afdb32c0109149f2131f961987'},
  		{name: 'Amazon AWS - Availability Zone (LP)', sysId: 'fde331c71bfe0010053d971a2d4bcb9e'},
  		{name: 'Amazon AWS - Classic LB (LP)', sysId: 'd0d3b880db8b04109149f2131f96194e'},
  		{name: 'Amazon AWS - Customer Gateway (LP)', sysId: '265d0ac0db4384109149f2131f9619ae'},
  		{name: 'Amazon AWS - Executable Template (LP)', sysId: 'e1da12c3dbf240109149f2131f96190d'},
  		{name: 'Amazon AWS - Hardware Type (LP)', sysId: 'f80c73351bda4010e1b863913d4bcb9d'},
  		{name: 'Amazon AWS - Internet Gateway (LP)', sysId: '3e009ec8db4384109149f2131f9619e9'},
  		{name: 'Amazon AWS - IP Address (LP)', sysId: 'c2a1d9bbdb7ec0109149f2131f96199c'},
  		{name: 'Amazon AWS - Key Pair (LP)', sysId: '4cd48f8bdbb640109149f2131f9619e4'},
  		{name: 'Amazon AWS - LB Pool (LP)', sysId: 'f1382d40dbc744109149f2131f961995'},
  		{name: 'Amazon AWS - LB Service (LP)', sysId: '47223440db8b04109149f2131f96196b'},
  		{name: 'Amazon AWS - NAT Gateway (LP)', sysId: '73f8b94cdb4f44109149f2131f961956'},
  		{name: 'Amazon AWS - Network (LP)', sysId: '63a25bb11bde4010053d971a2d4bcbb8'},
  		{name: 'Amazon AWS - NIC (LP)', sysId: 'ddd36adfdbfe40109149f2131f96197c'},
  		{name: 'Amazon AWS - Owned Template (LP)', sysId: '22a7bff8546f4cc0b80090cee8cff419'},
  		{name: 'Amazon AWS - Public IP Address (LP)', sysId: '916515ffdb7ec0109149f2131f96192a'},
  		{name: 'Amazon AWS - Route Table (LP)', sysId: 'ba30cec4dbcf44109149f2131f9619ed'},
  		{name: 'Amazon AWS - Security Group  (LP)', sysId: '2e33af5bdb3680109149f2131f961940'},
  		{name: 'Amazon AWS - Storage (LP)', sysId: '3e4c0bf91b9e4010053d971a2d4bcbba'},
  		{name: 'Amazon AWS - Sub Account (LP)', sysId: 'fcf42f0bdbc7c0109149f2131f961916'},
  		{name: 'Amazon AWS - Subnet (LP)', sysId: '9fab9bdbdbf280109149f2131f961944'},
  		{name: 'Amazon AWS - Virtual Server (LP)', sysId: '3106f6c7db7640109149f2131f96190b'},
  		{name: 'Amazon AWS - VPN Connections (LP)', sysId: '8a1f16c0db8784109149f2131f96199c'},
  		{name: 'Amazon AWS - VPN Gateway (LP)', sysId: 'a56e39c0db8f44109149f2131f9619b0'},
  		{name: 'Amazon AWS - AutoScaling Groups (LP)', sysId: '3b1bce7c1b845d10b8f0dd7edd4bcbcb'}];
  	
  	this.azurePatternArray =
  		[//{name: 'Azure - Application LB (LP)', sysId: 'd8b159d91b55101010a6dce7cc4bcb98'},
  		{name: 'Azure - Availability Set (LP)', sysId: '4534ad0d1b59101010a6dce7cc4bcbdb'},
  		{name: 'Azure - Classic LB (LP)', sysId: '428ac0151b51101010a6dce7cc4bcb05'},
  		{name: 'Azure - Hardware Type (LP)', sysId: 'c1d05d691b15101010a6dce7cc4bcb9c'},
  		{name: 'Azure - Image (LP)', sysId: 'e6769b3bdb811010b2a1f96275961966'},
  		{name: 'Azure - Network and Subnet (LP)', sysId: '95ff0b63db451010b2a1f96275961916'},
  		{name: 'Azure - NIC (LP)', sysId: '9d41a723db851010b2a1f96275961951'},
  		{name: 'Azure - Private Gateway (LP)', sysId: 'ae8ded491b99101010a6dce7cc4bcb70'},
  		{name: 'Azure - Public IP (LP)', sysId: 'db7dfbe7dbc51010b2a1f962759619e8'},
  		{name: 'Azure - Resource Group (LP)', sysId: 'bba7faabdbc11010b2a1f96275961998'},
  		{name: 'Azure - Route Table (LP)', sysId: 'b68118051b55101010a6dce7cc4bcbe9'},
  		{name: 'Azure - Security Group (LP)', sysId: '01c81dcd1b19101010a6dce7cc4bcbdd'},
  		{name: 'Azure - Storage Account (LP)', sysId: 'c24ddb33dbc11010b2a1f9627596198a'},
  		{name: 'Azure - Storage Container(LP)', sysId: 'fee255e5db5020108aa4c6ab139619c9'},
  		{name: 'Azure - Storage Volume (LP)', sysId: '551a4d5fdbc11010e10fc16c1396197e'},
  		{name: 'Azure - Sub Account (LP)', sysId: '47a8eeb7dbe310106f660bd3e29619d7'},
  		{name: 'Azure - Virtual Machine (LP)', sysId: '62043ae7dbc11010b2a1f9627596197e'},
  		{name: 'Azure - File Share (LP)', sysId: '276178cbdbb9f010082230d6f49619b1'},
  		{name: 'Azure - Express Route Circuit (LP)', sysId: '76da4fb41b4234103c55eced7b4bcb94'},
  		{name: 'Azure - Local Network Gateway (LP)', sysId: 'c32e52e01bc2b0103c55eced7b4bcb58'},
  		{name: 'Azure - NAT Gateway (LP)', sysId: '16bf89931b31f0103c55eced7b4bcb42'},
  		{name: 'Azure - Private DNS Zone (LP)', sysId: 'd23ef8471bf530103c55eced7b4bcbfd'},
  		{name: 'Azure - Virtual Network Gateway Connection (LP)', sysId: '7ce66ea01b46b0103c55eced7b4bcb9b'},
  		{name: 'Azure - VM Scale Set (LP)', sysId: '1f47e88fdb39f010082230d6f49619cc'}];
  },

  runAllPrerequisitesCheck : function() {
  	this.RUN_ALL = true;
  	this.startLogRecord("Capi Migration Prerequisite Check");
  	this.overallResultLog = this.introMessage("Overall Results");
  	this.patternAndCapiCheck();
  	this.scriptIncludeCheck();
  	if (this.overallStatus)
  		this.migrationCheck();
  	this.saveLogs(this.overallResultLog);
  	this.saveLogs(this.patternLogs);
  	this.saveLogs(this.capiLogs);
  	this.saveLogs(this.scriptIncludeLogs);
  	this.endLogs();
  },
  
  patternAndCapiCheck : function() {
  	var patternConvert;
  	var capiConvert;
  	if (this.awsPatternArray) {
  		this.scriptIncludeArray.push(this.awsScriptInclude);
  		patternConvert = this.awsPatternCheck();
  		capiConvert = this.awsCapiCheck();
  		if (patternConvert || capiConvert)
  			this.awsConversion = true;
  	}
  	if (this.azurePatternArray) {
  		this.scriptIncludeArray.push(this.azureScriptInclude);
  		patternConvert = this.azurePatternCheck();
  		capiConvert = this.azureCapiCheck();
  		if (patternConvert || capiConvert)
  			this.azureConversion = true;
  	}
  },
  
  scriptIncludeCheck : function() {
  	var siStatus = true;
  	var passLog;
  	var logMessage = this.introMessage("Script Include Results");
  	var scriptIncludeGr = new GlideRecord('sys_script_include');
  	for (var x = 0; x < this.scriptIncludeArray.length; x++) {
  		var siName = this.scriptIncludeArray[x].name;
  		var startMessage = this.newLine + "Script Include: " + siName + this.newLine;
  		scriptIncludeGr.initialize();
  		scriptIncludeGr.addQuery('name', siName);
  		scriptIncludeGr.addActiveQuery();
  		scriptIncludeGr.query();
  		var count = scriptIncludeGr.getRowCount();
  		if (count == 0) {
  			siStatus = false;
  			this.overallStatus = false;
  			logMessage += startMessage + this.passFalse + this.newLine + "Reason: Couldn't find active Script Include record. Please address before migration.";
  		} else if (count > 1) {
  			siStatus = false;
  			this.overallStatus = false;
  			var siDefaultSysId = this.scriptIncludeArray[x].sysId;
  			var siDefaultGr = new GlideRecord('sys_script_include');
  			if (siDefaultGr.get('sys_id', siDefaultSysId) && siDefaultGr.getValue('active') == true) {
  				logMessage += startMessage + this.passFalse + this.newLine + "Reason: Found multiple active records and default record" + this.newLine + "Please deactivate the following duplicate records before migrating:";
  				while (scriptIncludeGr.next()) {
  					var siGrSysId = scriptIncludeGr.getValue('sys_id');
  					if (!siGrSysId.equals(siDefaultSysId))
  						logMessage += this.newLine + " - sys_id: " + siGrSysId;
  				}
  			} else
  				logMessage += startMessage + this.passFalse + this.newLine + "Reason: Found duplicate active records, but could not find active default record. Please address before migration.";
  		} else
  			logMessage += startMessage + this.passTrue + " (Found one active record)";
  		logMessage += this.newLine;
  	}
  	logMessage += this.dash + this.newLine;

  	passLog = this.getPassLog("Script Include", siStatus);

  	if (this.RUN_ALL) {
  		this.overallResultLog += this.newLine + passLog;
  		this.scriptIncludeLogs = logMessage;
  	} else
  		this.saveLogs(passLog + this.newLine + this.dash + logMessage);
  },

  awsPatternCheck : function() {
  	return this.basePatternCheck(this.awsPatternArray, "AWS Pattern");
  },
  
  azurePatternCheck : function() {
  	return this.basePatternCheck(this.azurePatternArray, "Azure Pattern");
  },
  
  basePatternCheck : function(patternArray, message) {
  	var patternWarning = false;
  	var patternStatus = true;
  	var convert = false;
  	var passLog;
  	var warnMessage = "";
  	var logMessage = "";
  	var introMessage = this.introMessage(message + " Results");
  	var patternGr = new GlideRecord('sa_pattern');
  	for (var x = 0; x < patternArray.length; x++) {
  		var patternSysId = patternArray[x].sysId;
  		var patternName = patternArray[x].name;
  		var startMessage = this.newLine + "Pattern: " + patternName + " (" + patternSysId + ")" + this.newLine;
  		if (patternGr.get('sys_id', patternSysId)) {
  			if (patternGr.getValue('name').equals(patternName))
  				logMessage += startMessage + this.passTrue + " (Found default pattern record)";
  			else
  				logMessage += startMessage + this.passTrue + " (Found pattern record but with different name: " + patternGr.getValue('name') + ")";
  		} else {
  			logMessage += startMessage + "*** Did not find pattern record" + this.newLine;
  			patternWarning = true;
  			continue;
  		}

  		if (patternGr.getValue('active') == true)
  			logMessage += this.newLine + "- Pattern already active";
  		else
  			convert = true;

  		logMessage += this.newLine;
  	}				
  	logMessage += this.dash;
  	passLog = this.getPassLog(message, patternStatus);

  	if (patternWarning)
  		warnMessage = this.newLine + "*** WARNING: Some expected patterns were not found. Please check results below and confirm if this is expected before proceeding with migration ***" + this.newLine;

  	if (this.RUN_ALL) {
  		this.overallResultLog += this.newLine + passLog;
  		this.patternLogs += introMessage + warnMessage + logMessage;
  	} else
  		this.saveLogs(passLog + this.newLine + this.dash + warnMessage + logMessage);
  	return convert; 
  },

  awsCapiCheck : function() {
  	var migrationHelper = new sn_cmp.DiscoveryAwsMigrationHelper();
  	return this.baseCapiCheck(migrationHelper, "AWS CAPI");
  },
  
  azureCapiCheck : function() {
  	var migrationHelper = new sn_cmp.DiscoveryAzureMigrationHelper();
  	return this.baseCapiCheck(migrationHelper, "Azure CAPI");
  },
  
  baseCapiCheck : function(capihelper, message) {
  	var capiWarning = false;
  	var capiStatus = true;
  	var convert = false;
  	var passLog;
  	var warnMessage = "";
  	var logMessage = "";
  	var logMessageStart = this.introMessage(message + " Results");
  	var capiGr = new GlideRecord('sn_cmp_rb_op_impl_step');
  	for (var i = 0; i < capihelper.discoverySteps.length; i++) {
  		var capiSysId = capihelper.discoverySteps[i];
  		if (capiGr.get(capiSysId))
  			logMessage += this.newLine + "CAPI Step: " + capiGr.getValue('name') + " (" + capiSysId + ")" + this.newLine + "- Pass: TRUE (Found default CAPI step)";
  		else {
  			logMessage += this.newLine + "*** CAPI Step with sys_id: " + capiSysId + " was not found" + this.newLine;
  			capiWarning = true;
  			continue;
  		}
  		
  		if (capiGr.getValue('enabled') == false)
  			logMessage += this.newLine + "- CAPI Step already disabled";
  		else
  			convert = true;
  		
  		logMessage += this.newLine;
  	}
  	logMessage += this.dash;
  	passLog = this.getPassLog(message, capiStatus);

  	if (capiWarning)
  		warnMessage = this.newLine + "*** WARNING: Some expected CAPI steps were not found. Please check results below and confirm if this is expected before proceeding with migration ***" + this.newLine;

  	if (this.RUN_ALL) {
  		this.overallResultLog += this.newLine + passLog;
  		this.capiLogs += logMessageStart + warnMessage + logMessage;
  	} else
  		this.saveLogs(passLog + this.newLine + this.dash + warnMessage + logMessage);
  	return convert;
  },
  
  migrationCheck : function() {
  	var logMessage = this.introMessage("Following Migrations can be ran");
  	if (this.awsConversion)
  		logMessage += this.newLine + "* DiscoveryAwsMigrationHelper";
  	if (this.azureConversion)
  		logMessage += this.newLine + "* DiscoveryAzureMigrationHelper";			
  	
  	if (this.RUN_ALL)
  		this.overallResultLog += logMessage;
  	else
  		this.saveLogs(logMessage);
  },
  
  getPassLog : function(message, status) {
  	if (status)
  		return message + " check PASSED";
  	else {
  		this.PASS_ALL = false;
  		var failMessage = message + " check FAILED";
  		if (!this.MIGRATE)
  			failMessage += " - Check " + message + " Results to see which ones did not pass.";
  		return failMessage;
  	}
  },
  
  startLogRecord : function(source) {
  	var logGr = new GlideRecord('probe_to_pattern_log');
  	if (!logGr.get('sys_id', this.logSysId)) {
  		logGr.initialize();
  		this.logSysId = logGr.insert();
  	}
  	logGr.name = source;
  	logGr.status = "In Progress";
  	logGr.update();
  },
  
  setLogRecord : function(logRecord) {
  	this.logSysId = logRecord;
  },

  getLogRecord : function() {
  	return this.logSysId;
  },

  saveLogs : function(message) {
  	var logRecord = new GlideRecord('probe_to_pattern_log');
  	if (logRecord.get('sys_id', this.logSysId)) {
  		logRecord.log += message.trim() + this.newLine;
  		logRecord.update();
  	}
  },

  endLogs : function() {
  	var logRecord = new GlideRecord('probe_to_pattern_log');
  	logRecord.get('sys_id', this.logSysId);
  	if (logRecord.log.contains("check FAILED"))
  		logRecord.status = "Failed";
  	else
  		logRecord.status = "Completed";
  	logRecord.update();
  },
  
  introMessage : function(message) {
  	return this.newLine + this.dash + this.newLine + message + this.newLine + this.dash;
  },

  type: 'CapiToPatternPrerequisiteScript'
});

Sys ID

5cac3f11f18d5810f8777a03e16e7e75

Offical Documentation

Official Docs: