<?xml version="1.0" encoding="UTF-8"?><record_update table="sys_script_include">
    <sys_script_include action="INSERT_OR_UPDATE">
        <access>package_private</access>
        <active>true</active>
        <api_name>x_turbo_turbonomic.TurbonomicEntityMatcher</api_name>
        <caller_access/>
        <client_callable>false</client_callable>
        <description/>
        <name>TurbonomicEntityMatcher</name>
        <script><![CDATA[var TurbonomicEntityMatcher = (function () {

    var type = 'TurbonomicEntityMatcher';

    var VIRTUAL_MACHINE = 'virtual_machine';
    var HOST = 'physical_machine';
    var STORAGE = 'storage';

    /**
     * Creates an object for matching TurbnomicEntity to the cmdb.
     *
     * @param turbonomicGlide allow unit test to mock out glide
     *        records.
     */
    function TurbonomicEntityMatcher(turbonomicGlide) {
        if (!turbonomicGlide) {
            this._turbonomicGlide = new TurbonomicGlide();
        } else {
            this._turbonomicGlide = turbonomicGlide;
        }
    }

    /**
     * Find the ServiceNow configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching ServiceNow configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findConfigurationItem = function(turbonomicEntity) {
        gs.debug('TurbonomicEntityMatcher.findConfigurationItem() - ' +
                'Trying to find a matching CI for the Turbonomic entity: ' +
                'uuid = ' + turbonomicEntity.getUuid() + ', ' +
                'name = ' + turbonomicEntity.getEntityName() + ', ' +
                'type = ' + turbonomicEntity.getEntityType() + ', ' +
                'target name = ' + turbonomicEntity.getTargetName() + ', ' +
                'target type = ' + turbonomicEntity.getTargetType());

        var result = null;
        var tableAndColumnArray = [];
        var targetType = turbonomicEntity.getTargetType();
        var entityType = turbonomicEntity.getEntityType();

        gs.debug("TurbonomicEntityMatcher.findConfigurationItem() - Traget Type = " + targetType + ', Entity Type = ' + entityType);
        tableAndColumnArray = this.getCITableNameAndColumn(targetType, entityType);

        if (tableAndColumnArray.length == 2) {
            gs.debug('TurbonomicEntityMatcher.findConfigurationItem() - Found valid CMDB CI table name and column for: target type = '
                + targetType + ', entityType = ' + entityType + ' ; table name = ' + tableAndColumnArray[0] +
                ', table column = ' + tableAndColumnArray[1]);
            result = this.findConfigurationItemRecord(turbonomicEntity.getUuid(), tableAndColumnArray[0], tableAndColumnArray[1]);
        }

        if (result != null) {
            gs.debug('TurbonomicEntityMatcher.findConfigurationItem() - Found matching CI in ServiceNow for Turbonomic entity: name = '
                + turbonomicEntity.getEntityName() + ', type = ' + entityType + ', target type = ' + targetType +
                ', entity ID = ' + turbonomicEntity.getUuid());
            return result;
        }

        // Legacy CI matching code for virtual machines below
        if (targetType.startsWith('vcenter')) {
            result = this.findVCenterConfigurationItem(turbonomicEntity);
            if (result == null ) {
                entityType = turbonomicEntity.getEntityType().toLowerCase();
                if (entityType == VIRTUAL_MACHINE) {
                    return this.findNutanixVirtualMachine(turbonomicEntity.getUuid());
                }
            } else {
                return result;
            }
        } else if (targetType.startsWith('hyper-v') || targetType.startsWith('vmm')) {
            return this.findHyperVConfigurationItem(turbonomicEntity);
        } else if (targetType.startsWith('aws')) {
            return this.findAwsConfigurationItem(turbonomicEntity);
        } else if (targetType.startsWith('azure')) {
            return this.findAzureConfigurationItem(turbonomicEntity);
        } else if (targetType.startsWith('gcp project')) {
            return this.findGcpConfigurationItem(turbonomicEntity);
        } else if (targetType.startsWith('nutanix')) {
            return this.findNutanixConfigurationItem(turbonomicEntity);
        }
        else {
            gs.info('TurbonomicEntityMatcher.findConfigurationItem() - ' +
                    'Unsupported target type: ' + targetType);
        }

        gs.debug('TurbonomicEntityMatcher.findConfigurationItem() - ' +
                'Cannot find a matching CI for the Turbonomic entity with ' +
                'name = ' + turbonomicEntity.getEntityName() + ', ' +
                'uuid = ' + turbonomicEntity.getUuid());
        return null;
    };

    /**
     * Find the matching vCenter configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching vCenter configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findVCenterConfigurationItem = function(turbonomicEntity) {
        var entityType = turbonomicEntity.getEntityType().toLowerCase();

        switch (entityType) {
            case VIRTUAL_MACHINE :
                return this.findVCenterVirtualMachine(turbonomicEntity);
            case HOST :
                gs.debug('TurbonomicEntityMatcher.findVCenterConfigurationItem() - ' +
                        'vCenter Host CI matching is not implemented yet');
                break;
            case STORAGE :
                gs.debug('TurbonomicEntityMatcher.findVCenterConfigurationItem() - ' +
                        'vCenter Storage CI matching is not implemented yet');
                break;
            default :
                gs.debug('TurbonomicEntityMatcher.findVCenterConfigurationItem() - ' +
                        'Unsupported vCenter entity type: ' + entityType);
        }

        return null;
    };

    /**
     * Find the matching Hyper-V configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching Hyper-V configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findHyperVConfigurationItem = function(turbonomicEntity) {
        var entityType = turbonomicEntity.getEntityType().toLowerCase();

        switch (entityType) {
            case VIRTUAL_MACHINE :
                return this.findHyperVVirtualMachine(turbonomicEntity);
            default :
                gs.debug('TurbonomicEntityMatcher.findHyperVConfigurationItem() - ' +
                        'Unsupported Hyper-V entity type: ' + entityType);
        }

        return null;
    };

    /**
     * Find the matching AWS configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching AWS configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findAwsConfigurationItem = function(turbonomicEntity) {
        var entityType = turbonomicEntity.getEntityType().toLowerCase();

        switch (entityType) {
            case VIRTUAL_MACHINE :
                return this.findAwsVirtualMachine(turbonomicEntity.getUuid());
            default :
                gs.debug('TurbonomicEntityMatcher.findAwsConfigurationItem() - ' +
                        'Unsupported AWS entity type: ' + entityType);
        }

        return null;
    };

    /**
     * Find the matching Azure configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching Azure configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findAzureConfigurationItem = function(turbonomicEntity) {
        var entityType = turbonomicEntity.getEntityType().toLowerCase();

        switch (entityType) {
            case VIRTUAL_MACHINE :
                return this.findAzureVirtualMachine(turbonomicEntity.getUuid());
            default :
                gs.debug('TurbonomicEntityMatcher.findAzureConfigurationItem() - ' +
                        'Unsupported Azure entity type: ' + entityType);
        }

        return null;
    };

    /**
     * Find the matching Google Cloud Provider (GCP) configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching GCP configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findGcpConfigurationItem = function(turbonomicEntity) {
        var entityType = turbonomicEntity.getEntityType().toLowerCase();

        switch (entityType) {
            case VIRTUAL_MACHINE :
                return this.findGcpVirtualMachine(turbonomicEntity.getUuid());
            default :
                gs.debug('TurbonomicEntityMatcher.findGcpConfigurationItem() - ' +
                        'Unsupported GCP entity type: ' + entityType);
        }

        return null;
    };

    /**
     * Find the matching Nutanix configuration item for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching Nutanix configuration item for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findNutanixConfigurationItem = function(turbonomicEntity) {
        var entityType = turbonomicEntity.getEntityType().toLowerCase();

        switch (entityType) {
            case VIRTUAL_MACHINE :
                return this.findNutanixVirtualMachine(turbonomicEntity.getUuid());
            default :
                gs.debug('TurbonomicEntityMatcher.findNutanixConfigurationItem() - ' +
                        'Unsupported Nutanix entity type: ' + entityType);
        }

        return null;
    };

    /**
     * Find the matching vCenter virtual machine in ServiceNow, for the input Turbonomic entity.
     * We expect the uuid of the input TurbonomicEntity to be equal to the virtual machine MOR Id.
     * Also, we expect the targetName of the input TurbonomicEntity to be set to the IP address of
     * the vCenter target.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching vCenter virtual machine for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findVCenterVirtualMachine = function(turbonomicEntity) {
        var dao = new x_turbo_turbonomic.TurbonomicActionDAO();

        var virtualMachineTableName = dao.getSettingValue('vcenter_vm_table_name');
        if (virtualMachineTableName == '') {
            gs.warn('TurbonomicEntityMatcher.findVCenterVirtualMachine() - Invalid vCenter VM Table Name setting value');
            virtualMachineTableName = 'cmdb_ci_vmware_instance';
		} else {
			gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - Found valid vCenter VM Table Name setting: ' + virtualMachineTableName);
		}

        var virtualMachineTableColumn = dao.getSettingValue('vcenter_vm_table_column');
        if (virtualMachineTableColumn == '') {
            gs.warn('TurbonomicEntityMatcher.findVCenterVirtualMachine() - Invalid vCenter VM Table Column setting value');
            virtualMachineTableColumn = 'object_id';
		} else {
			gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - Found valid vCenter VM Table Column setting: ' + virtualMachineTableColumn);
		}

        // Find the vCenter virtual machine record for the input MOR Id
        var virtualMachineRecord = new GlideRecordSecure(virtualMachineTableName);
        var entityId = turbonomicEntity.getUuid();
        virtualMachineRecord.addQuery(virtualMachineTableColumn, entityId);
        virtualMachineRecord.query();

        if (virtualMachineRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                    'Found valid vCenter VM in ServiceNow with Id: ' + entityId + '; VM name = ' + virtualMachineRecord.name);

            var vCenterRecord = virtualMachineRecord.vcenter_ref;
            if (vCenterRecord) {
                var vCenterIP = vCenterRecord.ip_address;
                gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine(): ServiceNow vCenter IP = ' + vCenterIP);

                if (vCenterIP && vCenterIP.equals(turbonomicEntity.getTargetName())) {
                    gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                            'Found matching vCenter virtual machine in ServiceNow with id: ' + entityId);
                    return virtualMachineRecord;
                } else {
                    gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                            'The vCenter IP in ServiceNow does not match the Turbonomic target name: ' +
                            'vCenter IP in ServiceNow = ' + vCenterIP + ', ' +
                            'Turbonomic target name = ' + turbonomicEntity.getTargetName() + '; VM name = ' + virtualMachineRecord.name);
                    return virtualMachineRecord;
                }
            } else {
                gs.debug('Cannot find the matching vCenter server record for virtual machine: ' + virtualMachineRecord.name);
                return virtualMachineRecord;
            }
        }

        // Perform a heuristic search and match based on the entity and target name values
        var entityName = turbonomicEntity.getEntityName();
        gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - Performing heuristic CI search for the vCenter VM: ' + entityName);

        virtualMachineRecord = new GlideRecordSecure(virtualMachineTableName);
        virtualMachineRecord.addQuery('name', entityName);
        virtualMachineRecord.query();

        while (virtualMachineRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                    'Found candidate vCenter VM in ServiceNow named: ' + entityName);

            var vCenterCandidateRecord = virtualMachineRecord.vcenter_ref;
            if (vCenterCandidateRecord) {
                var ipAddress = vCenterCandidateRecord.ip_address;
                gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine(): vCenter IP in ServiceNow is: ' + ipAddress);

                if (ipAddress && ipAddress.equals(turbonomicEntity.getTargetName())) {
                    gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                            'Found matching vCenter virtual machine in ServiceNow with name = ' + entityName);
                    return virtualMachineRecord;
                } else {
                    gs.warn('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                            'The vCenter IP in ServiceNow does not match the Turbonomic target name: ' +
                            'vCenter IP in ServiceNow = ' + ipAddress + ', ' +
                            'Turbonomic target name = ' + turbonomicEntity.getTargetName() + '; VM name = ' + virtualMachineRecord.name);
                }
            }
        }

        gs.debug('TurbonomicEntityMatcher.findVCenterVirtualMachine() - ' +
                'Cannot find vCenter VM in ServiceNow for Turbonomic entity with Id: ' + entityId);
        return null;
    };

    /**
     * Find the matching Hyper-V virtual machine in ServiceNow, for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching Hyper-V virtual machine for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findHyperVVirtualMachine = function(turbonomicEntity) {
        var dao = new x_turbo_turbonomic.TurbonomicActionDAO();

        var virtualMachineTableName = dao.getSettingValue('hyper_v_vm_table_name');
        if (virtualMachineTableName == '') {
            gs.warn('TurbonomicEntityMatcher.findHyperVVirtualMachine() - Invalid Hyper-V VM Table Name setting value');
            virtualMachineTableName = 'cmdb_ci_hyper_v_instance';
		} else {
			gs.debug('TurbonomicEntityMatcher.findHiperVVirtualMachine() - Found valid Hyper-V VM Table Name setting: ' + virtualMachineTableName);
		}

        var virtualMachineTableColumn = dao.getSettingValue('hyper_v_vm_table_column');
        if (virtualMachineTableColumn == '') {
            gs.warn('TurbonomicEntityMatcher.findHyperVVirtualMachine() - Invalid Hyper-V VM Table Column setting value');
            virtualMachineTableColumn = 'object_id';
		} else {
			gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - Found valid Hyper-V VM Table Column setting: ' + virtualMachineTableColumn);
		}

        // Find the Hyper-V virtual machine record for the input entity ID
        var virtualMachineRecord = new GlideRecordSecure(virtualMachineTableName);
        var entityId = turbonomicEntity.getUuid();
        virtualMachineRecord.addQuery(virtualMachineTableColumn, entityId);
        virtualMachineRecord.query();

        if (virtualMachineRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - ' +
                    'Found valid Hyper-V VM in ServiceNow with Id: ' + entityId + '; VM name = ' + virtualMachineRecord.name);

            var hypervServerRecord = virtualMachineRecord.server;
            if (hypervServerRecord) {
                var hypervServerIP = hypervServerRecord.ip_address;
                gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine(): Hyper-V Server IP address in ServiceNow = ' + hypervServerIP);

                if (hypervServerIP && hypervServerIP.equals(turbonomicEntity.getTargetName())) {
                    gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - ' +
                            'Found matching Hyper-V virtual machine in ServiceNow with id: ' + entityId);
                    return virtualMachineRecord;
                } else {
                    gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - ' +
                            'The Hyper-V server IP in ServiceNow does not match the Turbonomic target name: ' +
                            'Hyper-V server IP in ServiceNow = ' + hypervServerIP + ', ' +
                            'Turbonomic target name = ' + turbonomicEntity.getTargetName() + '; VM name = ' + virtualMachineRecord.name);
                    return virtualMachineRecord;
                }
            } else {
                gs.debug('Cannot find the matching Hyper-V server record for virtual machine: ' + virtualMachineRecord.name);
                return virtualMachineRecord;
            }
        }

        // Perform a heuristic search and match based on the entity and target name values
        var entityName = turbonomicEntity.getEntityName();
        gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - Performing heuristic CI search for the Hyper-V VM: ' + entityName);

        virtualMachineRecord = new GlideRecordSecure(virtualMachineTableName);
        virtualMachineRecord.addQuery('name', entityName);
        virtualMachineRecord.query();

        while (virtualMachineRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.findHYperVVirtualMachine() - ' +
                    'Found candidate Hyper-V VM in ServiceNow named: ' + entityName);

            var serverCandidateRecord = virtualMachineRecord.server;
            if (serverCandidateRecord) {
                var ipAddress = serverCandidateRecord.ip_address;
                gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine(): Hyper-V Server IP in ServiceNow is: ' + ipAddress);

                if (ipAddress && ipAddress.equals(turbonomicEntity.getTargetName())) {
                    gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - ' +
                            'Found matching Hyper-V virtual machine in ServiceNow with name = ' + entityName);
                    return virtualMachineRecord;
                } else {
                    gs.warn('TurbonomicEntityMatcher.findHyperVVirtualMachine() - ' +
                            'The Hyper-V server IP in ServiceNow does not match the Turbonomic target name: ' +
                            'Hyper-V server IP in ServiceNow = ' + ipAddress + ', ' +
                            'Turbonomic target name = ' + turbonomicEntity.getTargetName() + '; VM name = ' + virtualMachineRecord.name);
                }
            }
        }

        gs.debug('TurbonomicEntityMatcher.findHyperVVirtualMachine() - ' +
                'Cannot find Hyper-V VM in ServiceNow for Turbonomic entity with Id: ' + entityId);
        return null;
    };

    /**
     * Find the matching AWS virtual machine in ServiceNow, for the input Turbonomic entity.
     * We expect the input id to be provided in the following AWS format: i-016782f3e89014c3a
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching AWS virtual machine for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findAwsVirtualMachine = function(id) {
        var dao = new x_turbo_turbonomic.TurbonomicActionDAO();

        var virtualMachineTableName = dao.getSettingValue('aws_vm_table_name');
        if (virtualMachineTableName == '') {
            gs.warn('TurbonomicEntityMatcher.findAwsVirtualMachine() - Invalid AWS VM Table Name setting value');
            virtualMachineTableName = 'cmdb_ci_vm_instance';
		} else {
			gs.debug('TurbonomicEntityMatcher.findAwsVirtualMachine() - Found valid AWS VM Table Name setting: ' + virtualMachineTableName);
		}

        var virtualMachineTableColumn = dao.getSettingValue('aws_vm_table_column');
        if (virtualMachineTableColumn == '') {
            gs.warn('TurbonomicEntityMatcher.findAwsVirtualMachine() - Invalid AWS VM Table Column setting value');
            virtualMachineTableColumn = 'object_id';
		} else {
			gs.debug('TurbonomicEntityMatcher.findAwsVirtualMachine() - Found valid AWS VM Table Column setting: ' + virtualMachineTableColumn);
		}

        var result = this.findVirtualMachineInstance(id, virtualMachineTableColumn, virtualMachineTableName);
        if (result != null) {
            gs.debug('TurbonomicEntityMatcher.findAwsVirtualMachine() - ' +
                    'Found valid AWS virtual machine in ServiceNow with id: ' + id);
        }

        return result;
    };

    /**
     * Find the matching Azure virtual machine in ServiceNow, for the input Turbonomic entity.
     * We expect the input id to be provided in the following Azure format:
     *    /subscriptions/758ad253-cbf5-4b18-8863-3eed0825bf07/resourceGroups/turbonomic
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching Azure virtual machine for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findAzureVirtualMachine = function(id) {
        var dao = new x_turbo_turbonomic.TurbonomicActionDAO();

        var virtualMachineTableName = dao.getSettingValue('azure_vm_table_name');
        if (virtualMachineTableName == '') {
            gs.warn('TurbonomicEntityMatcher.findAzureVirtualMachine() - Invalid Azure VM Table Name setting value');
            virtualMachineTableName = 'cmdb_ci_vm_instance';
		} else {
			gs.debug('TurbonomicEntityMatcher.findAzureVirtualMachine() - Found valid Azure VM Table Name setting: ' + virtualMachineTableName);
		}

        var virtualMachineTableColumn = dao.getSettingValue('azure_vm_table_column');
        if (virtualMachineTableColumn == '') {
            gs.warn('TurbonomicEntityMatcher.findAzureVirtualMachine() - Invalid Azure VM Table Column setting value');
            virtualMachineTableColumn = 'vm_inst_id';
		} else {
			gs.debug('TurbonomicEntityMatcher.findAzureVirtualMachine() - Found valid Azure VM Table Column setting: ' + virtualMachineTableColumn);
		}

        var result = this.findVirtualMachineInstance(id, virtualMachineTableColumn, virtualMachineTableName);
        if (result != null) {
            gs.debug('TurbonomicEntityMatcher.findAzureVirtualMachine() - ' +
                    'Found valid Azure virtual machine in ServiceNow with Id: ' + id);
        }

        return result;
    };

    /**
     * Find the matching GCP virtual machine in ServiceNow, for the input Turbonomic entity.
     * We expect the input id to be provided in the following GCP format: gcp::europe-west2-a::VM::3594888957108496123
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching GCP virtual machine for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findGcpVirtualMachine = function(id) {
        var dao = new x_turbo_turbonomic.TurbonomicActionDAO();

        var virtualMachineTableName = dao.getSettingValue('gcp_vm_table_name');
        if (virtualMachineTableName == '') {
            gs.warn('TurbonomicEntityMatcher.findGcpVirtualMachine() - Invalid GCP VM Table Name setting value');
            virtualMachineTableName = 'cmdb_ci_vm_instance';
        } else {
            gs.debug('TurbonomicEntityMatcher.findGcpVirtualMachine() - Found valid GCP VM Table Name setting: ' + virtualMachineTableName);
        }

        var virtualMachineTableColumn = dao.getSettingValue('gcp_vm_table_column');
        if (virtualMachineTableColumn == '') {
            gs.warn('TurbonomicEntityMatcher.findGcpVirtualMachine() - Invalid GCP VM Table Column setting value');
            virtualMachineTableColumn = 'object_id';
        } else {
            gs.debug('TurbonomicEntityMatcher.findGcpVirtualMachine() - Found valid GCP VM Table Column setting: ' + virtualMachineTableColumn);
        }

        var result = this.findVirtualMachineInstance(id, virtualMachineTableColumn, virtualMachineTableName);
        if (result != null) {
            gs.debug('TurbonomicEntityMatcher.findGcpVirtualMachine() - ' +
                    'Found valid GCP virtual machine in ServiceNow with id: ' + id);
        }

        return result;
    };

    /**
     * Find the matching Nutanix virtual machine in ServiceNow, for the input Turbonomic entity.
     *
     * @param turbonomicEntity The input TurbonomicEntity instance.
     *
     * @return The matching Nutanix virtual machine for the input Turbonomic entity if found, null otherwise.
     */
    TurbonomicEntityMatcher.prototype.findNutanixVirtualMachine = function(id) {
        var dao = new x_turbo_turbonomic.TurbonomicActionDAO();

        var virtualMachineTableName = dao.getSettingValue('nutanix_vm_table_name');
        if (virtualMachineTableName == '') {
            gs.warn('TurbonomicEntityMatcher.findNutanixVirtualMachine() - Invalid Nutanix VM Table Name setting value');
            virtualMachineTableName = 'cmdb_ci_vm_instance';
        } else {
            gs.debug('TurbonomicEntityMatcher.findNutanixVirtualMachine() - Found valid Nutanix VM Table Name setting: ' + virtualMachineTableName);
        }

        var virtualMachineTableColumn = dao.getSettingValue('nutanix_vm_table_column');
        if (virtualMachineTableColumn == '') {
            gs.warn('TurbonomicEntityMatcher.findNutanixVirtualMachine() - Invalid Nutanix VM Table Column setting value');
            virtualMachineTableColumn = 'object_id';
        } else {
            gs.debug('TurbonomicEntityMatcher.findNutanixVirtualMachine() - Found valid Nutanix VM Table Column setting: ' + virtualMachineTableColumn);
        }

        var result = this.findVirtualMachineInstance(id, virtualMachineTableColumn, virtualMachineTableName);
        if (result != null) {
            gs.debug('TurbonomicEntityMatcher.findNutanixVirtualMachine() - ' +
                    'Found valid Nutanix virtual machine in ServiceNow with id: ' + id);
        }

        return result;
    };

    /**
     * Find a virtual machine instance in the input table and column, based on the input id.
     *
     * @param id The unique virtual machine object id.
     * @param tableColumn The input table column name.
     * @param tableName The input table name used to search for the object id.
     *
     * @return The virtual machine instance for the input object id, or null if no instance found.
     */
    TurbonomicEntityMatcher.prototype.findVirtualMachineInstance = function(id, tableColumn, tableName) {
        var vmObjectRecord = this._turbonomicGlide.accessTable(tableName);
        vmObjectRecord.addQuery(tableColumn, id);
        vmObjectRecord.query();

        if (vmObjectRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.findVirtualMachineInstance() - ' +
                    'Found matching virtual machine instance in ServiceNow for Turbonomic entity with Id: ' + id +
                    '; table name = ' + tableName + ', table column = ' + tableColumn);

            return vmObjectRecord;
        }

        gs.debug('TurbonomicEntityMatcher.findVirtualMachineInstance() - ' +
                'Cannot find virtual machine instance in ServiceNow for Turbonomic entity with Id: ' + id +
                '; table name = ' + tableName + ', table column = ' + tableColumn);
        return null;
    };

    /**
     * Find a CMDB CI record in the input table and column, using the input Turbonomic entity id.
     *
     * @param id The unique Turbonomic entity id.
     * @param tableName The name of the CMDB CI table where we search for the entity.
     * @param tableColumn The CMDB CI table column name.
     *
     * @return The CMDB CI record for the input Turbonomic entity id, or null if no such record is found.
     */
    TurbonomicEntityMatcher.prototype.findConfigurationItemRecord = function(id, tableName, tableColumn) {
        var cmdbCIRecord = this._turbonomicGlide.accessTable(tableName);
        cmdbCIRecord.addQuery(tableColumn, id);
        cmdbCIRecord.setLimit(1);
        cmdbCIRecord.query();

        if (cmdbCIRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.findConfigurationItemRecord() - ' +
                    'Found matching CMDB CI record in ServiceNow for Turbonomic entity with Id: ' + id +
                    '; table name = ' + tableName + ', table column = ' + tableColumn);

            return cmdbCIRecord;
        }

        gs.debug('TurbonomicEntityMatcher.findConfigurationItemRecord() - ' +
                'Cannot find CMDB CI record in ServiceNow for Turbonomic entity with Id: ' + id +
                '; table name = ' + tableName + ', table column = ' + tableColumn);
        return null;
    };

    /**
     * Query Turbonomic CI Matchings table and get the array of [ci_table_name, ci_table_column], based on the input target and entity type .
     *
     * @param targetType The input Turbonomic target type.
     * @param entityType The input Turbonomic entity type.
     *
     * @return The array of [ci_table_name, ci_table_column] for the input target and entity type or an empty array, if no record found.
     */
    TurbonomicEntityMatcher.prototype.getCITableNameAndColumn = function(targetType, entityType) {
        var result = [];
        var targetTypeId = '';
        var entityTypeId = '';

        var targetTypeRecord = this._turbonomicGlide.accessTable('x_turbo_turbonomic_turbonomic_target_type');
        targetTypeRecord.addQuery('name', targetType);
        targetTypeRecord.setLimit(1);
        targetTypeRecord.query();

        if (targetTypeRecord.next()) {
            targetTypeId = targetTypeRecord.sys_id;
        }

        var entityTypeRecord = this._turbonomicGlide.accessTable('x_turbo_turbonomic_turbonomic_entity_type');
        entityTypeRecord.addQuery('internal_name', entityType);
        entityTypeRecord.setLimit(1);
        entityTypeRecord.query();

        if (entityTypeRecord.next()) {
            entityTypeId = entityTypeRecord.sys_id;
        }

        var tableRecord = this._turbonomicGlide.accessTable('x_turbo_turbonomic_turbonomic_ci_matchings');
        tableRecord.addQuery('target_type', targetTypeId);
        tableRecord.addQuery('entity_type', entityTypeId);
        tableRecord.setLimit(1);
        tableRecord.query();

        if (tableRecord.next()) {
            gs.debug('TurbonomicEntityMatcher.getCITableNameAndColumn() -  CI Matchings entry: name = ' + tableRecord.name +
                    '; target type = ' + targetType + ', entity type = ' + entityType +
                    ', CI table name = ' + tableRecord.ci_table_name + ', CI table column = ' + tableRecord.ci_table_column);

            result.push(tableRecord.ci_table_name);
            result.push(tableRecord.ci_table_column);
        }

        if (result.length == 0) {
            gs.debug('TurbonomicEntityMatcher.getCITableNameAndColumn() -  CI Matchings entry is not defined for: ' +
                'target type = ' + targetType + ' and entity type = ' + entityType);            
        }

        return result;
    };

    return TurbonomicEntityMatcher;
}());
]]></script>
        <sys_class_name>sys_script_include</sys_class_name>
        <sys_created_by>alexpetrean</sys_created_by>
        <sys_created_on>2019-03-15 21:32:56</sys_created_on>
        <sys_id>1b591490db9cff005e691fe96896198f</sys_id>
        <sys_mod_count>119</sys_mod_count>
        <sys_name>TurbonomicEntityMatcher</sys_name>
        <sys_package display_value="Turbonomic Actions" source="x_turbo_turbonomic">be4eeab2db007f005e691fe968961902</sys_package>
        <sys_policy>read</sys_policy>
        <sys_scope display_value="Turbonomic Actions">be4eeab2db007f005e691fe968961902</sys_scope>
        <sys_update_name>sys_script_include_1b591490db9cff005e691fe96896198f</sys_update_name>
        <sys_updated_by>Alex.Petrean@ibm.com</sys_updated_by>
        <sys_updated_on>2025-03-06 00:33:16</sys_updated_on>
    </sys_script_include>
</record_update>
