Name
sn_cmp.CMPVMUtils
Description
Utility methods that can be used to derive various Virtual Machine properties, while designing Operations. These methods can be used in expressions like the following to derive VM properties. For example, to get IP address of a VM, the following expression can be used $(Script CMPVMUtils.getReachableIp arg=${parameter.resourceId} ) ) where ${parameter.resourceId} is the sys_id of the VM CI. Please refer to Virtual Server s Register Node operation (Operation input Node Address specifically)to see how these utility methods can be used while designing Resource Operations.
Script
var CMPVMUtils = Class.create();
CMPVMUtils.prototype = {
initialize: function() {
this.VMWARE_PROVISIONED_VM = 1;
this.VMWARE_DISCOVERED_VM = 2;
this.NON_VMWARE_VM = 3;
},
/**
* Get IP Addresses of a virtual machine as a JSON object, given the "sys_id" of the cmdb_ci_vm_instance CI.
* Method returns a JSON with the following format:
* {"private_ip":"10.253.36.155","public_ip":"54.176.57.217"}
*
* vmId - sys_id of cmdb_ci_vm_instance CI
**/
getIPAddresses: function(vmId) {
var ipaddr=this._getIPAddressObject(vmId);
var ipAddress = {};
if(gs.nil(ipaddr))
return "";
var privateIp= ipaddr.getValue('private_ip');
var publicIp= ipaddr.getValue('public_ip');
var ip_address= ipaddr.getValue('ip_address');
ipAddress['private_ip']=privateIp;
ipAddress['public_ip']=publicIp;
// vmware points to a different table and has its custom attribute for storing ip_adderess,need to be relooked.
if(gs.nil(publicIp) && gs.nil(privateIp))
ipAddress['public_ip']=ip_address;
return global.JSON.stringify(ipAddress);
},
getIPAddressesForDay2: function(stackItemId, alias, vmId) {
var vmSysId = this.getInstanceIdForDeprovision(stackItemId,alias,vmId);
var ipAddresses = this.getIPAddresses(vmSysId);
if(gs.nil(ipAddresses)){
ipAddresses = {};
var gr = new GlideRecord("cmdb_ci_vm_instance");
if(gr.get(vmSysId))
ipAddresses['public_ip'] = gr.ip_address+'';
return global.JSON.stringify(ipAddresses);
}
return ipAddresses;
},
getReachableIpForDay2: function(stackItemId,alias, vmId) {
var vmSysId = this.getInstanceIdForDeprovision(stackItemId,alias,vmId);
var ipAddress = this.getReachableIp(vmSysId);
if(gs.nil(ipAddress)){
var gr = new GlideRecord("cmdb_ci_vm_instance");
if(gr.get(vmSysId))
ipAddress = gr.ip_address+'';
}
return ipAddress;
},
getOSTypeForDay2: function(stackItemId,alias, vmId) {
var vmSysId = this.getInstanceIdForDeprovision(stackItemId,alias,vmId);
var osType = this.getOSType(vmSysId);
return osType;
},
/**
* Gets an IP Addresses of a virtual machine based on the following priority given the "sys_id" of
* the cmdb_ci_vm_instance CI. IP Addresses are read by following the endpoint relationship to the NIC
* configuration item (cmdb_ci_nic or cmdb_ci_vmware_nic )
*
* 1. Public IP if it is available
* 2. Private IP if it is available
* 3. IP Address from the 'ip_address' field on cmdb_ci_nic/cmdb_ci_vmware_nic CI record.
*
* vmId - sys_id of cmdb_ci_vm_instance CI
**/
getReachableIp: function(vmId) {
var ipaddr=this._getIPAddressObject(vmId);
if(gs.nil(ipaddr))
return "";
var privateIp= ipaddr.getValue('private_ip');
var publicIp= ipaddr.getValue('public_ip');
var ip_address= ipaddr.getValue('ip_address');
var ipAddress = "";
if(!gs.nil(publicIp))
return publicIp;
if(!gs.nil(privateIp))
return privateIp;
if(!gs.nil(ip_address))
return ip_address;
// send back directly the address,this will work for use cases where we were always querying the VM and finding the ip,now we will query the nic and find if public/private ip has been set, and return that.
return ipAddress;
},
/*
* Get Credential alias tag from Orchestration Credential(SSH/Windows)
* which were generated during vm provision, given the "sys_id" of the cmdb_ci_vm_instance CI.
* vmId - sys_id of cmdb_ci_vm_instance CI
*/
getCredentialAlias: function(nodeSysId){
var nodeMgmtCred = new GlideRecord('sn_cmp_vm_node_mgmt_creds');
nodeMgmtCred.query('node', nodeSysId);
if(nodeMgmtCred.next()){
var orchestrationCred = nodeMgmtCred.getValue('orchestration_credential');
if(!gs.nil(orchestrationCred)){
var credentialTags = nodeMgmtCred.orchestration_credential.tag;
if(!gs.nil(credentialTags)){
var credAlias = credentialTags.split(',');
return this._getSysAliasId(credAlias[0]);
}
}
}
},
/*
* Get the OS image used by the VM by following CI relationships, given the "sys_id" of the cmdb_ci_vm_instance CI.
* vmId - sys_id of cmdb_ci_vm_instance CI
*/
getOSType: function (vmId){
var osType;
var rel_ci = new GlideRecord('cmdb_rel_ci');
rel_ci.addQuery('parent', vmId);
rel_ci.addQuery('child.sys_class_name', 'cmdb_ci_os_template').addOrCondition('child.sys_class_name', 'cmdb_ci_vmware_template');
rel_ci.addJoinQuery('cmdb_rel_type', 'type', 'sys_id').addCondition('name', 'Provisioned From::Provisioned');
rel_ci.query();
if(rel_ci.next())
osType = rel_ci.child.guest_os;
gs.info("Guest os info: "+osType);
return osType;
},
_getSysAliasId: function(aliasSysId){
var aliasId;
var sysAlias = new GlideRecord('sys_alias');
if(sysAlias.get(aliasSysId))
aliasId = sysAlias.id;
gs.info('Sys alias record Id:'+ aliasId);
return aliasId;
},
_getIPAddressObject: function(vmId) {
var nicRecord = this._findNicRecordForUseTypeRelation(vmId);
return this._findRecordFor(this._getTypeOfVM(vmId), nicRecord);
},
_getTypeOfVM: function(vmId){
var cmdbVmwareGR = new GlideRecord('cmdb_ci_vmware_instance');
var isVMwareVM = cmdbVmwareGR.get('sys_id', vmId);
var stackItemGR = new GlideRecord('sn_cmp_stack_item');
var isProvisioned = stackItemGR.get('ci_instance_id', vmId);
if (isVMwareVM && isProvisioned){
return this.VMWARE_PROVISIONED_VM;
} else if (isVMwareVM && !isProvisioned){
return this.VMWARE_DISCOVERED_VM;
} else {
return this.NON_VMWARE_VM;
}
},
_findRecordFor: function(typeOfVM, nicRecord){
// handle flip in the key of the query here for VMware VM
var key = ((typeOfVM == this.NON_VMWARE_VM) ? 'parent' : 'child');
var implementTypeId = this._getSysIdForRelName('Implement End Point To::Implement End Point From');
var cmdbRelCiGR = new GlideRecord('cmdb_rel_ci');
cmdbRelCiGR.addQuery('type', implementTypeId);
cmdbRelCiGR.addQuery(key, nicRecord);
cmdbRelCiGR.setLimit(1);
cmdbRelCiGR.query();
if (cmdbRelCiGR.next()){
var identifierTuple = this._createRelbasedIdentifierTuple(typeOfVM, cmdbRelCiGR);
var nicTableGR = new GlideRecord(identifierTuple.tableName);
var isRecPresent = nicTableGR.get('sys_id', identifierTuple.sysId);
if (isRecPresent)
return nicTableGR;
}
},
_findNicRecordForUseTypeRelation: function(vmId){
var useTypeId = this._getSysIdForRelName('Use End Point To::Use End Point From');
var cmdbRelCiGR = new GlideRecord('cmdb_rel_ci');
cmdbRelCiGR.addQuery('type', useTypeId);
cmdbRelCiGR.addQuery('child.sys_class_name', 'cmdb_ci_endpoint_vnic');
cmdbRelCiGR.addQuery('parent', vmId);
cmdbRelCiGR.setLimit(1);
cmdbRelCiGR.query();
if (cmdbRelCiGR.next()){
return cmdbRelCiGR.getValue('child');
}
},
_createRelbasedIdentifierTuple: function(typeOfVM, cmdbRelCiGR){
var tableName, sysId;
if (typeOfVM == this.VMWARE_DISCOVERED_VM){
tableName = cmdbRelCiGR.parent.getRefRecord().getTableName();
sysId = cmdbRelCiGR.getValue('parent');
} else {
tableName = cmdbRelCiGR.child.getRefRecord().getTableName();
sysId = cmdbRelCiGR.getValue('child');
}
return {'tableName': tableName, 'sysId' : sysId};
},
_getSysIdForRelName: function(relName){
var recSysId = "";
var cmdbRelTypeGR = new GlideRecord("cmdb_rel_type");
var isRecFound = cmdbRelTypeGR.get('name', relName);
if(isRecFound)
recSysId = cmdbRelTypeGR.getUniqueValue();
return recSysId;
},
/*
* nodeId is vm_inst_id if node is provisioned from cmp.In case of provisioned vm
* vm_inst_id is mapped to uuid of VMware vm and vm_instance_id is empty.
* nodeId is vm_instance_id if it is discovered.In case of discovery vm_instance_id
* is mapped to instanceUuid of VMware vm and vm_inst_id is empty.
*
* */
getVMwareInstanceId : function (vm_sys_id) {
var gr = new GlideRecord("cmdb_ci_vmware_instance");
if (gr.get(vm_sys_id)) {
var nodeId = gs.nil(gr.getValue("vm_inst_id")) ? gr.getValue("vm_instance_uuid") : gr.getValue("vm_inst_id");
return nodeId;
}
},
/*
* TODO: Will be used while changing mappings for Virtual Machine Store Extension Interface for Terraform.
*/
getNodeNameById : function (vmId) {
var gr = new GlideRecord("cmdb_ci_vm_instance");
if(gr.get(vmId))
return gr.name + '';
},
/*
* Method returns the Node credentials of the vm based on the vmId or credential Id.
* Querying the className table based on the node Id and fetching the node credentials.
* To run Day2 operations on BrownField VMs, credential Id has to be passed which will used as node credentials.
* Inputs:
vmId - sys_id of the VM for which node credential has to be fetched.
className - node management table (sn_cmp_vm_node_mgmt_creds).
credentialId - credentials passed for BrownField VMs.
* Output: CAPI resolver expression with node credentials to be used for authentication.
* */
getCredentials : function (stackItemId, alias, vmId, className, credentialId) {
if(!gs.nil(credentialId))
return "$(capiResolver.NodeCredentialResolver#nodeCredentialId=" + credentialId + ")";
var vmSysId = this.getInstanceIdForDeprovision(stackItemId,alias, vmId);
var nodeMgmtCred = new GlideRecord(className);
nodeMgmtCred.query('node', vmSysId);
if(nodeMgmtCred.next()){
var nodeCredId = nodeMgmtCred.getValue('cred_id');
if(!gs.nil(nodeCredId))
return "$(capiResolver.NodeCredentialResolver#nodeCredentialId=" + nodeCredId + ")";
else
gs.warn("Unable to resolve expression for vmId: " + vmSysId + "Check " + className + " for vm record." );
}
},
getSubnetObjectId : function (stackExpId, alias, subnetId) {
var subnetSysId = this.getInstanceIdForDeprovision(stackExpId, alias, subnetId);
var gr = new GlideRecord("cmdb_ci_cloud_subnet");
if(gr.get(subnetSysId))
return gr.getValue("object_id");
},
getNetworkObjectId : function (stackExpId, alias, networkId) {
var netId = this.getInstanceIdForDeprovision(stackExpId, alias, networkId);
var gr = new GlideRecord("cmdb_ci_network");
if(gr.get(netId))
return gr.getValue("object_id");
},
getStorageObjectId : function (storageId) {
var gr = new GlideRecord("cmdb_ci_storage_volume");
if(gr.get(storageId))
return gr.getValue("object_id");
},
getLoadBalancerName : function (stackExpId, alias, lbId) {
var loadBalancerId = this.getInstanceIdForDeprovision(stackExpId, alias, lbId);
var gr = new GlideRecord("cmdb_ci_cloud_load_balancer");
if(gr.get(loadBalancerId))
return gr.getValue("object_id");
},
/*
* Method returns the Network containing the provided subnet.
* Querying the cmdb_rel_ci table based on the containment rule between network and subnet.
* Input: subnetId - sys_id of the subnet for which parent network has to be fetched.
* Output: networkId - object_id of the network containing the subnet.
* */
getNetworkIdBySubnetId : function (stackExpId, alias, subnetId) {
var subnetSysId = this.getInstanceIdForDeprovision(stackExpId, alias, subnetId);
var networkId = '';
var encodedQuery = "child="+subnetSysId+"^parent.sys_class_name=cmdb_ci_network^type.name=Contains::Contained by";
var relGr = new GlideRecord("cmdb_rel_ci");
var networkGr = new GlideRecord("cmdb_ci_network");
relGr.addEncodedQuery(encodedQuery);
relGr.query();
if(relGr.next() && networkGr.get(relGr.getValue("parent")))
networkId = networkGr.getValue("object_id");
return networkId;
},
getInstanceIdForDeprovision : function (stackExpId, alias, resourceId) {
var stackGr = new GlideRecord("sn_cmp_stack");
if(stackGr.get(stackExpId)){
var stackItemGr = new GlideRecord("sn_cmp_stack_item");
stackItemGr.addQuery("alias", alias.replace("[","").replace("]","").replaceAll("$!$", ")"));
stackItemGr.addQuery("stack",stackExpId);
stackItemGr.addQuery('ci_instance_id.sys_class_name', 'cmdb_ci_vm_instance');
stackItemGr.query();
/* DEF0323739: Multiple stack items can exist with same alias esp in terraform eg VM and Subnet
Performing this lookup only if there is one match as it guarantees the VM */
if(stackItemGr.getRowCount() == 1){
stackItemGr.next();
return stackItemGr.ci_instance_id+'';
}
}
return resourceId;
},
type: 'CMPVMUtils'
};
Sys ID
ebe8bdbd0b1003003246ad4363673a81