RangerConfigCalculation extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = m_clusters.getCluster(clusterName); Config sourceConfig = cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE); if (null == sourceConfig) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", MessageFormat.format("Source type {0} not found, skipping", SOURCE_CONFIG_TYPE), ""); } String dbProp = "DB_FLAVOR"; String dbHostProp = "db_host"; String dbNameProp = "db_name"; String dbAuditNameProp = "audit_db_name"; StringBuilder stdout = new StringBuilder(); String db = sourceConfig.getProperties().get(dbProp); if (null == db) { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", MessageFormat.format("Target database from {0}/{1} not found, skipping", SOURCE_CONFIG_TYPE, dbProp), ""); } stdout.append(MessageFormat.format("Database type is {0}\n", db)); db = db.toLowerCase(); if (!"mysql".equals(db) && !"oracle".equals(db)) { stdout.append(MessageFormat.format("Target database {0} is not recognized, skipping", db)); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); } String dbHost = sourceConfig.getProperties().get(dbHostProp); String dbName = sourceConfig.getProperties().get(dbNameProp); String auditDbName = sourceConfig.getProperties().get(dbAuditNameProp); if (null == auditDbName) { auditDbName = dbName; } stdout.append(MessageFormat.format("Database host: {0}\n", dbHost)); stdout.append(MessageFormat.format("Database name: {0}\n", dbName)); stdout.append(MessageFormat.format("Audit database name: {0}\n", auditDbName)); if (null == dbHost) { stdout.append(MessageFormat.format("Hostname must be set using {0}/{1} , skipping", SOURCE_CONFIG_TYPE, dbHostProp)); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); } String driver = null; String url = null; String dialect = null; String auditUrl = null; String userJDBCUrl = null; if ("mysql".equals(db)) { if (null == dbName) { stdout.append(MessageFormat.format("Target database {0} requires {1} to be set, skipping", db, dbName)); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); } driver = "com.mysql.jdbc.Driver"; url = MessageFormat.format("jdbc:mysql: auditUrl = MessageFormat.format("jdbc:mysql: dialect = "org.eclipse.persistence.platform.database.MySQLPlatform"; userJDBCUrl = MessageFormat.format("jdbc:mysql: } else if ("oracle".equals(db)) { driver = "oracle.jdbc.OracleDriver"; url = MessageFormat.format("jdbc:oracle:thin:@ auditUrl = MessageFormat.format("jdbc:oracle:thin:@ dialect = "org.eclipse.persistence.platform.database.OraclePlatform"; userJDBCUrl = MessageFormat.format("jdbc:oracle:thin:@ } stdout.append(MessageFormat.format("Database driver: {0}\n", driver)); stdout.append(MessageFormat.format("Database url: {0}\n", url)); stdout.append(MessageFormat.format("Database audit url: {0}\n", auditUrl)); stdout.append(MessageFormat.format("Database dialect: {0}", dialect)); stdout.append(MessageFormat.format("Database user jdbc url: {0}", userJDBCUrl)); Config config = cluster.getDesiredConfigByType(RANGER_ADMIN_SITE_CONFIG_TYPE); Map<String, String> targetValues = config.getProperties(); targetValues.put("ranger.jpa.jdbc.driver", driver); targetValues.put("ranger.jpa.jdbc.url", url); targetValues.put("ranger.jpa.jdbc.dialect", dialect); targetValues.put("ranger.jpa.audit.jdbc.driver", driver); targetValues.put("ranger.jpa.audit.jdbc.url", auditUrl); targetValues.put("ranger.jpa.audit.jdbc.dialect", dialect); config.setProperties(targetValues); config.save(); config = cluster.getDesiredConfigByType(RANGER_ENV_CONFIG_TYPE); targetValues = config.getProperties(); targetValues.put("ranger_privelege_user_jdbc_url", userJDBCUrl); config.setProperties(targetValues); config.save(); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testAction() throws Exception { Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); RangerConfigCalculation action = new RangerConfigCalculation(); m_clusterField.set(action, m_clusters); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); CommandReport report = action.execute(null); assertNotNull(report); Cluster c = m_clusters.getCluster("c1"); Config config = c.getDesiredConfigByType("ranger-admin-site"); Map<String, String> map = config.getProperties(); assertTrue(map.containsKey("ranger.jpa.jdbc.driver")); assertTrue(map.containsKey("ranger.jpa.jdbc.url")); assertTrue(map.containsKey("ranger.jpa.jdbc.dialect")); assertTrue(map.containsKey("ranger.jpa.audit.jdbc.driver")); assertTrue(map.containsKey("ranger.jpa.audit.jdbc.url")); assertTrue(map.containsKey("ranger.jpa.audit.jdbc.dialect")); assertEquals("com.mysql.jdbc.Driver", map.get("ranger.jpa.jdbc.driver")); assertEquals("jdbc:mysql: assertEquals("org.eclipse.persistence.platform.database.MySQLPlatform", map.get("ranger.jpa.jdbc.dialect")); assertEquals("com.mysql.jdbc.Driver", map.get("ranger.jpa.audit.jdbc.driver")); assertEquals("jdbc:mysql: assertEquals("org.eclipse.persistence.platform.database.MySQLPlatform", map.get("ranger.jpa.audit.jdbc.dialect")); config = c.getDesiredConfigByType("ranger-env"); map = config.getProperties(); assertEquals("jdbc:mysql: config = c.getDesiredConfigByType("admin-properties"); config.getProperties().put("DB_FLAVOR", "oracle"); report = action.execute(null); assertNotNull(report); config = c.getDesiredConfigByType("ranger-admin-site"); map = config.getProperties(); assertEquals("oracle.jdbc.OracleDriver", map.get("ranger.jpa.jdbc.driver")); assertEquals("jdbc:oracle:thin:@ assertEquals("org.eclipse.persistence.platform.database.OraclePlatform", map.get("ranger.jpa.jdbc.dialect")); assertEquals("oracle.jdbc.OracleDriver", map.get("ranger.jpa.audit.jdbc.driver")); assertEquals("jdbc:oracle:thin:@ assertEquals("org.eclipse.persistence.platform.database.OraclePlatform", map.get("ranger.jpa.audit.jdbc.dialect")); config = c.getDesiredConfigByType("ranger-env"); map = config.getProperties(); assertEquals("jdbc:oracle:thin:@ }