def DiscoveryMain(Framework): addResult = Framework.getTriggerCIData("addResult") updateResult = Framework.getTriggerCIData("updateResult") deleteResult = Framework.getTriggerCIData("deleteResult") addRefResult = Framework.getTriggerCIData("referencedAddResult") updateRefResult = Framework.getTriggerCIData("referencedUpdateResult") deleteRefResult = Framework.getTriggerCIData("referencedDeleteResult") logger.info("addResult: ") logger.info(addResult) logger.info("updateResult: ") logger.info(updateResult) logger.info("deleteResult: ") logger.info(deleteResult) saxBuilder = SAXBuilder() addXml = saxBuilder.build(StringReader(addResult)) updateXml = saxBuilder.build(StringReader(updateResult)) deleteXml = saxBuilder.build(StringReader(deleteResult)) addRefXml = saxBuilder.build(StringReader(addRefResult)) updateRefXml = saxBuilder.build(StringReader(updateRefResult)) deleteRefXml = saxBuilder.build(StringReader(deleteRefResult)) objectMappings = HashMap() linkMappings = HashMap() # The update status is used to report status of CIs and Links. updateStatus = ReplicationActionDataFactory.createUpdateStatus() credentialsId = str(Framework.getTriggerCIData("credentialsId")) serverName = Framework.getTriggerCIData("ip_address") port = Integer.valueOf(Framework.getTriggerCIData("port")) customerId = Framework.getTriggerCIData("customerId") isTestConnection = Framework.getTriggerCIData("testConnection") protocol = ProtocolDictionaryManager.getProtocolById(credentialsId) userName = protocol.getProtocolAttribute("protocol_username") password = protocol.getProtocolAttribute("protocol_password") dbType = Framework.getTriggerCIData("dbtype") conn = None if dbType == "Oracle": # The SID is under Schema Name / SID in the integration point parameters. sid = Framework.getTriggerCIData("schemaName") conn = createOracleConnection(serverName, port, sid, userName, password) elif dbType == "SQLServer": schemaName = Framework.getTriggerCIData("schemaName") conn = createSQLServerConnection(serverName, port, schemaName, userName, password) client = SQLClient(conn) allChildren = addXml.getRootElement().getChild("data") doAction(client, allChildren, objectMappings, linkMappings, addRefXml, ADD, updateStatus) allChildren = updateXml.getRootElement().getChild("data") doAction(client, allChildren, objectMappings, linkMappings, updateRefXml, UPDATE, updateStatus) allChildren = deleteXml.getRootElement().getChild("data") doAction(client, allChildren, objectMappings, linkMappings, deleteRefXml, DELETE, updateStatus) client.closeConnection() result = DataPushResultsFactory.createDataPushResults(objectMappings, linkMappings, updateStatus) return result
def DiscoveryMain(Framework): addResult = Framework.getTriggerCIData('addResult') updateResult = Framework.getTriggerCIData('updateResult') deleteResult = Framework.getTriggerCIData('deleteResult') addRefResult = Framework.getTriggerCIData('referencedAddResult') updateRefResult = Framework.getTriggerCIData('referencedUpdateResult') deleteRefResult = Framework.getTriggerCIData('referencedDeleteResult') logger.info('addResult: ') logger.info(addResult) logger.info('updateResult: ') logger.info(updateResult) logger.info('deleteResult: ') logger.info(deleteResult) saxBuilder = SAXBuilder() addXml = saxBuilder.build(StringReader(addResult)) updateXml = saxBuilder.build(StringReader(updateResult)) deleteXml = saxBuilder.build(StringReader(deleteResult)) addRefXml = saxBuilder.build(StringReader(addRefResult)) updateRefXml = saxBuilder.build(StringReader(updateRefResult)) deleteRefXml = saxBuilder.build(StringReader(deleteRefResult)) objectMappings = HashMap() linkMappings = HashMap() #The update status is used to report status of CIs and Links. updateStatus = ReplicationActionDataFactory.createUpdateStatus(); credentialsId = str(Framework.getTriggerCIData('credentialsId')) serverName = Framework.getTriggerCIData('ip_address') port = Integer.valueOf(Framework.getTriggerCIData('port')) customerId = Framework.getTriggerCIData('customerId') isTestConnection = Framework.getTriggerCIData('testConnection') protocol = ProtocolDictionaryManager.getProtocolById(credentialsId) userName = protocol.getProtocolAttribute('protocol_username') password = protocol.getProtocolAttribute('protocol_password') dbType = Framework.getTriggerCIData('dbtype') conn = None if(dbType == 'Oracle'): #The SID is under Schema Name / SID in the integration point parameters. sid = Framework.getTriggerCIData('schemaName') conn = createOracleConnection(serverName, port, sid, userName, password) elif(dbType == 'SQLServer'): schemaName = Framework.getTriggerCIData('schemaName') conn = createSQLServerConnection(serverName, port, schemaName,userName, password) client = SQLClient(conn) allChildren = addXml.getRootElement().getChild('data') doAction(client, allChildren, objectMappings, linkMappings, addRefXml, ADD, updateStatus) allChildren = updateXml.getRootElement().getChild('data') doAction(client, allChildren, objectMappings, linkMappings, updateRefXml, UPDATE, updateStatus) allChildren = deleteXml.getRootElement().getChild('data') doAction(client, allChildren, objectMappings, linkMappings, deleteRefXml, DELETE, updateStatus) client.closeConnection() result = DataPushResultsFactory.createDataPushResults(objectMappings, linkMappings, updateStatus); return result
def DiscoveryMain(Framework): # Prepare the maps to store the mappings of IDs objectMappings = HashMap() linkMappings = HashMap() try: ucmdbUpdateResult = None mamIdToSysIdMap = HashMap() #Stores mapping between UCMDB mamId to ServiceNow sys_id logger.debug('========================================================') logger.debug('Starting Push to Service-Now...') credentialsId = str(Framework.getDestinationAttribute('credentialsId')) credential = ProtocolDictionaryManager.getProtocolById(credentialsId) username = credential.getProtocolAttribute('protocol_username') password = credential.getProtocolAttribute('protocol_password') host = Framework.getDestinationAttribute('ServiceNowDomain') or 'service-now.com' protocol = Framework.getDestinationAttribute('protocol') or 'https' if protocol == 'http': port = Framework.getDestinationAttribute('port') or '80' else: port = Framework.getDestinationAttribute('port') or '443' instance = Framework.getDestinationAttribute('ServiceNowInstance') or 'demo' proxyServer = Framework.getDestinationAttribute('ProxyServer') or None proxyPort = Framework.getDestinationAttribute('ProxyPort') or None importSetsInUse = Framework.getDestinationAttribute('ImportSetsInUse') or 'false' insertMultiple = Framework.getDestinationAttribute('InsertMultiple') or 'false' insertMultipleBulkSize = Framework.getDestinationAttribute('InsertMultipleBulkSize') or '50' retryCount = Framework.getDestinationAttribute('RetryCount') or '3' retryDelaySeconds = Framework.getDestinationAttribute('RetryDelaySeconds') or '5' global IS_INSERT_MULTIPLE, INSERT_MULTIPLE_BULK_SIZE, RETRY_COUNT, RETRY_DELAY_SECONDS, FAIL_BULK failBulk = Framework.getDestinationAttribute('FailBulk') or 'true' FAIL_BULK = failBulk == 'true' IS_INSERT_MULTIPLE = insertMultiple == 'true' INSERT_MULTIPLE_BULK_SIZE = int (insertMultipleBulkSize) RETRY_COUNT = int (retryCount) RETRY_DELAY_SECONDS = int (retryDelaySeconds) logger.debug('Parameters: IS_INSERT_MULTIPLE:%s, INSERT_MULTIPLE_BULK_SIZE:%s, RETRY_COUNT:%s, RETRY_DELAY_SECONDS:%s' % (INSERT_MULTIPLE_BULK_SIZE, INSERT_MULTIPLE_BULK_SIZE, RETRY_COUNT, RETRY_DELAY_SECONDS)) debugPrint(1, '[DiscoveryMain] Service-Now URL: <%s://%s.%s:%s>, using proxy <%s:%s>' % (protocol, instance, host, port, proxyServer, proxyPort)) ## Are Service Now Web Service Import Sets in use? importSetUse = 0 if importSetsInUse and importSetsInUse.lower().strip() in ['yes', 'y', '1', 'true']: importSetUse = 1 #Connection parameter to ServiceNow SNConnPropMap = HashMap() SNConnPropMap.put('host', host) SNConnPropMap.put('port', port) SNConnPropMap.put('instance', instance) SNConnPropMap.put('protocol', protocol) SNConnPropMap.put('username', username) SNConnPropMap.put('password', password) SNConnPropMap.put('proxyServer', proxyServer) SNConnPropMap.put('proxyPort', proxyPort) # get add/update/delete result objects from the Framework addResult = Framework.getTriggerCIData('addResult') updateResult = Framework.getTriggerCIData('updateResult') deleteResult = Framework.getTriggerCIData('deleteResult') debugPrint(3, '****************************************************************') debugPrint(3, '************************* addResult ****************************') debugPrint(3, addResult) debugPrint(3, '****************************************************************') debugPrint(3, '************************* updateResult *************************') debugPrint(3, updateResult) debugPrint(3, '****************************************************************') debugPrint(3, '************************* deleteResult *************************') debugPrint(3, deleteResult) debugPrint(3, '****************************************************************') saxBuilder = SAXBuilder() addXml = saxBuilder.build(StringReader(addResult)) updateXml = saxBuilder.build(StringReader(updateResult)) deleteXml = saxBuilder.build(StringReader(deleteResult)) proceedToNext = 1 resultCountMap = HashMap() resultCountMap.put('add_ci', 0) resultCountMap.put('update_ci', 0) resultCountMap.put('delete_ci', 0) resultCountMap.put('add_rel', 0) resultCountMap.put('update_rel', 0) resultCountMap.put('delete_rel', 0) if addXml: debugPrint(1, '[DiscoveryMain] ========== Process items to add ==========') allObjectChildren = addXml.getRootElement().getChild('data').getChild('objects').getChildren('Object') proceedToNext = processCIs(allObjectChildren, SNConnPropMap, objectMappings, resultCountMap, mamIdToSysIdMap, importSetUse) if proceedToNext: allLinkChildren = addXml.getRootElement().getChild('data').getChild('links').getChildren('link') processRelations(allLinkChildren, SNConnPropMap, linkMappings, resultCountMap, mamIdToSysIdMap, importSetUse) else: Framework.reportError('[DiscoveryMain] Error adding CIs...please check probe logs!') return ucmdbUpdateResult else: logger.info("[DiscoveryMain] No data to add") if proceedToNext: if updateXml: debugPrint(1, '[DiscoveryMain] ========== Process updated items ==========') allObjectChildren = updateXml.getRootElement().getChild('data').getChild('objects').getChildren('Object') processCIs(allObjectChildren, SNConnPropMap, objectMappings, resultCountMap, mamIdToSysIdMap, importSetUse) allLinkChildren = updateXml.getRootElement().getChild('data').getChild('links').getChildren('link') processRelations(allLinkChildren, SNConnPropMap, linkMappings, resultCountMap, mamIdToSysIdMap, importSetUse) else: logger.info("[DiscoveryMain] No data to update") if deleteXml: debugPrint(1, '[DiscoveryMain] ========== Process deleted items ==========') allObjectChildren = deleteXml.getRootElement().getChild('data').getChild('objects').getChildren('Object') processCIs(allObjectChildren, SNConnPropMap, objectMappings, resultCountMap, mamIdToSysIdMap, importSetUse) allLinkChildren = deleteXml.getRootElement().getChild('data').getChild('links').getChildren('link') processRelations(allLinkChildren, SNConnPropMap, linkMappings, resultCountMap, mamIdToSysIdMap, importSetUse) else: logger.info("[DiscoveryMain] No data to delete") debugPrint(1, '[DiscoveryMain] --------------------------------------------------------') logger.info('[DiscoveryMain] CIs added <%s>, updated <%s>, deleted <%s>' % (resultCountMap.get('add_ci'), resultCountMap.get('update_ci'), resultCountMap.get('delete_ci'))) logger.info('[DiscoveryMain] Relationships added <%s>, updated <%s>, deleted <%s>' % (resultCountMap.get('add_rel'), resultCountMap.get('update_rel'), resultCountMap.get('delete_rel'))) debugPrint(1, '[DiscoveryMain] ========================================================') debugPrint(5, '[DiscoveryMain] MAPPING: CIs: ', objectMappings, ', links: ', linkMappings) logger.debug('Finished Push to Service-Now!') logger.debug('========================================================') except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[DiscoveryMain] Exception: <%s>' % excInfo) logger.reportError('[DiscoveryMain] Exception: <%s>' % excInfo) debugPrint(5, '[DiscoveryMain] MAPPING after exception: CIs: ', objectMappings, ', links: ', linkMappings) return DataPushResultsFactory.createDataPushResults(objectMappings, linkMappings)