Name

global.CloudDiscovery

Description

The parent of all cloud Discovery This class contains all common methods that are used or might be used in many operations inasmuch as for entire cloud exploration Runtime or for the Wizard Configuration

Script

var CloudDiscovery = Class.create();
CloudDiscovery.prototype = {
  initialize: function() {
  },
  isDebug: false,
  debugCounter: 1,
  statusId: '',
  nextDiscovery: '',
  SPECIAL_PATTERNS: {
      ACCOUNT_VALIDATION: 1,
      ACCOUNTS_DISCOVERY: 2,
      LDCS_DISCOVERY: 3,
      VMS_DISCOVERY: 4
  },
  PROBE_PARAMS: {
      CONFIGURATION: "cloud_configuration",
      PHASE: "cloud_account_phase",
  	SOURCE: "cloud_account_source",
  	ACCOUNT_SYS_ID: "cloud_account_sys_id"
  },	
  _response : {
  	state: "error", //The current state success/error
  	message: "DEFAULT_ERROR_MSG",
  	statusId: this.statusId //discovery_status Id - unique identifier for the resposne 
  },
  
  _isAllowed: function(msg) {
  	var permissions = gs.getUser().hasRole('mid_server') || gs.getUser().hasRole('discovery_admin');
  	if (!permissions ){
  		//gs.error("@@@ Current roles" + gs.getUser().getUserRoles());
  		throw Error("You don't have permissions to use this script -" + msg);
  	}
  		  
  },
  
  _debug: function(msg, obj) {
      if (this.isDebug) {
          var d = new Date();
          var time = d.getTime();
          gs.info('@@@[{0}]'+this.type+', statusId-[{1}], \nmsg-[{2}], \ndata-[{3}]',
  			time +"-"+ this.debugCounter,
  			this.statusId,
  			msg,
  			JSON.stringify(obj, null, 2));
          this.debugCounter++;
      }
  }, 

  _handleError: function(msg, obj) {
  	var d = new Date();
  	var time = d.getTime();
  	gs.error('@@@[{0}]'+this.type+', statusId-[{1}], \nmsg-[{2}], \ndata-[{3}]',
  			time +"-"+ this.debugCounter,
  			this.statusId,
  			msg,
  			JSON.stringify(obj, null, 2));
  	this.debugCounter++;
  	
      this._response.state = 'error';
      this._response.message = msg;
  	this._response.statusId = this.statusId;
  	return this._response;
  },	
  	
  _createDiscoveryStatus: function(desc, source) {
      var newStatus = new GlideRecord("discovery_status");
      newStatus.setValue("description", desc);
      newStatus.setValue("source", source);
      newStatus.setValue("discover", "Cloud Resources");

      var statusId = newStatus.insert();
      return (statusId) ? statusId : null;
  },
  
  _createSuccesResponse: function(msg) {
  	this._response.state = 'success';
      this._response.message = msg;
  	this._response.statusId = this.statusId;
  	return this._response;
  },
  
  _invokePhase: function(configObj, nextPatternId, midAgent, probeParams, probeValues, ppe, source) {
  	this._debug("_invokePhase->conf", { "configObj": configObj,
  									   "nextPatternId": nextPatternId,
  									   "midAgent" : midAgent,
  									   "probeParams": probeParams,
  									   "probeValues": probeValues,
  									   "source": source
  									  });
  	
      if (configObj && nextPatternId) {
  		if (nextPatternId){
  			this._debug("_invokePhase->Start phase invocation", probeParams);
  			source = (source)? source : 'CloudAccountDiscovery';
  						
  			var pLauncher = new SNC.PatternLauncherManager();
  			pLauncher.launchPattern( this.statusId,  'without scheduleId', midAgent, source, nextPatternId, probeParams, probeValues, ppe);
  			this._debug("_invokePhase->End phase invocation", probeParams);				
  		} else {
  			this._handleError("_invokePhase- Failed missing mandatory nextPatternId-", nextPatternId, {});
  		}
      } else {
  		this._handleError("_invokePhase- Failed missing mandatory configObj-", configObj, {});
  	}
      return null;
  },

  /*
  * Get the 'special' cloud LDCs topology discovery patternId
  * @param g_probe - The probe that responsible for the pattern result
  * return LDCs pattern Id
  */
  _getPatternId: function(vendorType, topologyType) {
  	this._debug("_getPatternId->vendorType-"+vendorType , topologyType);
      if ( vendorType && topologyType) {
          var gr = new GlideRecord("sa_cloud_topology_discovery_pattern");
  		gr.addQuery("datacenter_type", vendorType);
  		gr.addQuery("discover_topology_type", topologyType);
  		gr.query();
  		
  		var result = "";
  		if (gr.next()){
  			result = String(gr.pattern);
  			this._debug("_getPatternId->PatternId-" , result);
  			return result;
  		}
  		throw Error("_getPatternId-> Can't find pattern for -"+topologyType);
      }
  	this._handleError("_getPatternId- Failed to retrieve pattern ->vendorType-"+vendorType, topologyType, {});
      return null;
  },

  _buildProbParams: function(configObj, phase, source) {
  	var probeParams = {};
      if (!configObj)
  		throw Error("_buildProbParams must get configObj as input argument" );
      		
      if (!phase)
  		throw Error("_buildProbParams must get phase as input argument" );		
  	
  	//Add Mandatory parameters
  	probeParams[this.PROBE_PARAMS.CONFIGURATION] = JSON.stringify(configObj, null, 2);
  	probeParams[this.PROBE_PARAMS.PHASE] = String(phase);
  	probeParams[this.PROBE_PARAMS.SOURCE] = String(source); //The Breadcrumbs path of the discovery
  	
      return probeParams;
  },
  

  _buildPPE: function(serviceAccountId, ldcId) {
      var ppe = new SNC.PrePatternExecutionData();
      if (serviceAccountId) {
  		var grAccount = this._getAccountGR(serviceAccountId);
  		ppe.addGlideRecordEntry("service_account", grAccount);    //Add GlideRecord as a Table of the SA               
  		ppe.addString('service_account_id', String(serviceAccountId));

  		// Add relation type
  		ppe.addString('hosted_relation_type', 'Hosted on::Hosts');
  		ppe.addString('hosted_relation_id', '5f985e0ec0a8010e00a9714f2a172815');
  	}
  	
  	if (ldcId) {
  		var grLdc = this._getLdcGR(ldcId);
  		ppe.addGlideRecordEntry("ldc", grLdc); //Add GlideRecord of the LDC


  		//Take the host from the real CI table instead of the computerSystem
  		ppe.addString('build_relation_from_host_sys_id','true');

  		var className = grLdc.getValue('sys_class_name');
  		ppe.addGlideRecordEntry(className, grLdc);
      }
      return ppe;
  },
  
  _getAccountGR: function(serviceAccountId) {
      if (serviceAccountId) {
          // Get serviceAccount Record
          var gr = new GlideRecord("cmdb_ci_cloud_service_account");
          gr.get(serviceAccountId);
          if (gr.isValid()) {
              return gr;
          } 
      }
      throw Error("_getAccountGR-> Can't get Service Account-"+ serviceAccountId); 
  },
  
  _getLdcGR: function(ldcId) {
      if (ldcId) {
          // Get LDC Record
          var ldcGr = new GlideRecord("cmdb_ci_logical_datacenter");
          ldcGr.get(ldcId);
          if (ldcGr.isValid()) {
              return ldcGr;
          } 
  		
      }
      throw Error("_getLdcGR-> Can't get LDC-"+ ldcId); 
  },
  
  		/* Get MID automaticlly by its configured capabilities */
  _getAutoMid: function(vendorType, invocationContext) {
  	var capability = "";
  	
  	// get the provider (cloudType) and add it as a capability
  	var glideUtil = new GlideRecordUtil();
  	var capiRec = glideUtil.getGlideRecordByAttribute('sn_capi_provider','datacenter_class', vendorType);			
  	if (!capiRec.next())
  		capability = "Cloud Management";
  	else
  		capability = capiRec.getValue('name');
  			
  	
  	try {
  		var cmdMidSelector = new global.CloudMidSelectionApi();	
  		var mid_id = cmdMidSelector.selectMid(null, capability, null, invocationContext || {});
  		if (gs.nil(mid_id)){
  			this._debug("Can't find MID with " , '');
  			throw Error("Can't find MID with 'Cloud Management' capabillity" );
  		}

  		// Find the mid name
  		var midGr = new GlideRecord('ecc_agent');
  		if (!midGr.get(mid_id)) {
  			this._debug("ecc_agent" , '');
  			throw Error('MID ' + mid_id + ' not found in table ecc_agent');
  		}
  		
  		return String(midGr.name);
  	} catch (err) {
  		this._debug("No active MID was found" , err);
  		throw Error('No active MID was found');
  	}
  },
  
  _createEmptyConfigObj: function(){
  	var configObj = {
  			"service_account": {
  				"sys_id": "",
  				"name": "",
  				"account_id": "",
  				"discovery_credentials": "",
  				"datacenter_url": "",
  				"datacenter_type": "",
  				"is_master_account": ""
  			},
  			"is_master_account": false,
  			"parent_account": null,
  			"all_datacenters": false,
  			"all_accounts_for_master": false,
  			"member_accounts": [],
  			"datacenters": []
  		};
  	return configObj;
  },
  type: 'CloudDiscovery'
};

Sys ID

ed07e248c79023000a1123622b97631a

Offical Documentation

Official Docs: