RangerWebAlertConfigAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = m_clusters.getCluster(clusterName); String ranger_admin_process = "ranger_admin_process"; try { AlertDefinitionEntity rangerAlertDefinitionEntity = alertDefinitionDAO.findByName(cluster.getClusterId(), ranger_admin_process); if (rangerAlertDefinitionEntity != null) { logger.info("Updating the alert definition for ranger_admin_process."); String rangerAlertCurrentDefinitionSource = rangerAlertDefinitionEntity.getSource(); JsonObject rangerAlertsCurrentDefinitionJSON = new JsonParser().parse(rangerAlertCurrentDefinitionSource).getAsJsonObject(); rangerAlertsCurrentDefinitionJSON.remove("uri"); JsonObject rangerUpdatedURLConfig = new JsonObject(); rangerUpdatedURLConfig.addProperty("http", "{{admin-properties/policymgr_external_url}}/login.jsp"); rangerUpdatedURLConfig.addProperty("https", "{{admin-properties/policymgr_external_url}}/login.jsp"); rangerUpdatedURLConfig.addProperty("kerberos_keytab", "{{cluster-env/smokeuser_keytab}}"); rangerUpdatedURLConfig.addProperty("kerberos_principal", "{{cluster-env/smokeuser_principal_name}}"); rangerUpdatedURLConfig.addProperty("https_property", "{{ranger-admin-site/ranger.service.https.attrib.ssl.enabled}}"); rangerUpdatedURLConfig.addProperty("https_property_value", "true"); rangerUpdatedURLConfig.addProperty("connection_timeout", 5.0); rangerAlertsCurrentDefinitionJSON.add("uri", rangerUpdatedURLConfig); rangerAlertDefinitionEntity.setHash(UUID.randomUUID().toString()); rangerAlertDefinitionEntity.setSource(rangerAlertsCurrentDefinitionJSON.toString()); alertDefinitionDAO.merge(rangerAlertDefinitionEntity); Set<String> invalidatedHosts = alertDefinitionHash.invalidateHosts(rangerAlertDefinitionEntity); AlertHashInvalidationEvent alertInvalidationEvent = new AlertHashInvalidationEvent( rangerAlertDefinitionEntity.getClusterId(), invalidatedHosts); eventPublisher.publish(alertInvalidationEvent); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", "Ranger service alert check configuration has been updated successfully.", ""); } else { return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", String.format( "The %s configuration type was not found; unable to update Ranger Alert properties.", ranger_admin_process), ""); } } catch (Exception e) { logger.error("RangerWebAlertConfigAction.execute : There was an error in updating Ranger alerts.", e); } return null; }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testExecute() throws Exception { String CLUSTER_NAME = "cl1"; String RANGER_ADMIN_PROCESS = "ranger_admin_process"; String pathname = "src/test/resources/stacks/HDP/2.5.0/services/RANGER/alerts.json"; Cluster cluster = Mockito.mock(Cluster.class); Mockito.when(m_clusters.getCluster(Mockito.anyString())).thenReturn(cluster); Mockito.when(cluster.getClusterId()).thenReturn(1L); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", CLUSTER_NAME); ExecutionCommand m_executionCommand = new ExecutionCommand(); m_executionCommand.setCommandParams(commandParams); m_executionCommand.setClusterName(CLUSTER_NAME); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(m_executionCommand)).anyTimes(); replay(hrc); Mockito.when(alertDefinitionDAO.findByName(1L, RANGER_ADMIN_PROCESS)).thenReturn(alertDefinitionEntity); try { File alertsFile = new File(pathname); Assert.assertTrue(alertsFile.exists()); StringBuilder rangerAlertsConfigFile = new StringBuilder((int) alertsFile.length()); Scanner scanner = new Scanner(alertsFile); String lineSeparator = System.getProperty("line.separator"); try { while (scanner.hasNextLine()) { rangerAlertsConfigFile.append(scanner.nextLine() + lineSeparator); } Mockito.when(alertDefinitionEntity.getSource()).thenReturn(rangerAlertsConfigFile.toString()); } finally { scanner.close(); } } catch (Exception e) { e.printStackTrace(); } rangerWebAlertConfigAction.alertDefinitionDAO = alertDefinitionDAO; rangerWebAlertConfigAction.m_clusters = m_clusters; rangerWebAlertConfigAction.alertDefinitionHash = alertDefinitionHash; rangerWebAlertConfigAction.eventPublisher = eventPublisher; rangerWebAlertConfigAction.setExecutionCommand(m_executionCommand); rangerWebAlertConfigAction.setHostRoleCommand(hrc); ConcurrentMap<String, Object> requestSharedDataContext = new ConcurrentHashMap<String, Object>(); CommandReport commandReport = null; try { commandReport = rangerWebAlertConfigAction.execute(requestSharedDataContext); } catch (Exception e) { e.printStackTrace(); } Assert.assertNotNull(commandReport); Assert.assertEquals(0, commandReport.getExitCode()); Assert.assertEquals(HostRoleStatus.COMPLETED.toString(), commandReport.getStatus()); Assert.assertEquals("Ranger service alert check configuration has been updated successfully.", commandReport.getStdOut()); }