From 085b650d124b577e5744e5476a7f6e717cd9e766 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 2 Aug 2021 17:02:46 +0200 Subject: [PATCH] SONAR-15235 api/system/health add local parameter --- .../server/platform/ws/HealthAction.java | 5 ++++- .../platform/ws/HealthActionSupport.java | 10 ++++++++- .../server/platform/ws/HealthActionTest.java | 21 ++++++++++++++++++- .../platform/ws/SafeModeHealthActionTest.java | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthAction.java index 68c739fe8b7..809dcb0e186 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthAction.java @@ -28,6 +28,8 @@ import org.sonar.server.user.SystemPasscode; import org.sonar.server.user.UserSession; import org.sonar.server.ws.WsUtils; +import static org.sonar.server.platform.ws.HealthActionSupport.LOCAL_PARAM; + public class HealthAction implements SystemWsAction { private final WebServer webServer; private final HealthActionSupport support; @@ -52,7 +54,8 @@ public class HealthAction implements SystemWsAction { throw new ForbiddenException("Insufficient privileges"); } - if (webServer.isStandalone()) { + boolean localParam = request.mandatoryParamAsBoolean(LOCAL_PARAM); + if (webServer.isStandalone() || localParam) { WsUtils.writeProtobuf(support.checkNodeHealth(), request, response); } else { WsUtils.writeProtobuf(support.checkClusterHealth(), request, response); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthActionSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthActionSupport.java index f8a2a6a9100..5272c44126d 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthActionSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthActionSupport.java @@ -32,6 +32,7 @@ import org.sonarqube.ws.System; import static org.sonar.api.utils.DateUtils.formatDateTime; public class HealthActionSupport { + static final String LOCAL_PARAM = "local"; private static final Comparator NODE_HEALTH_COMPARATOR = Comparator.comparingInt(s -> s.getDetails().getType().ordinal()) .thenComparing(a -> a.getDetails().getName()) .thenComparing(a -> a.getDetails().getHost()) @@ -43,7 +44,7 @@ public class HealthActionSupport { } void define(WebService.NewController controller, SystemWsAction handler) { - controller.createAction("health") + WebService.NewAction action = controller.createAction("health") .setDescription("Provide health status of SonarQube." + "

Require 'Administer System' permission or authentication with passcode

" + "

" + @@ -56,6 +57,13 @@ public class HealthActionSupport { .setSince("6.6") .setResponseExample(Resources.getResource(this.getClass(), "example-health.json")) .setHandler(handler); + + action.createParam(LOCAL_PARAM) + .setDescription("True: Provide health for the node only, False: cluster-wide health (Only applies to SonarQube Datacenter Edition)") + .setExampleValue(false) + .setDefaultValue(false) + .setSince("9.1") + .setRequired(false); } System.HealthResponse checkNodeHealth() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java index 71bb50131c2..8a4de5eedd0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java @@ -54,6 +54,8 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.api.utils.DateUtils.parseDateTime; @@ -85,7 +87,7 @@ public class HealthActionTest { assertThat(definition.since()).isEqualTo("6.6"); assertThat(definition.isInternal()).isFalse(); assertThat(definition.responseExample()).isNotNull(); - assertThat(definition.params()).isEmpty(); + assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("local"); } @Test @@ -305,6 +307,23 @@ public class HealthActionTest { .containsExactly(expected); } + @Test + public void request_returns_only_node_health_when_cluster_with_local_parameter_true() { + authenticateWithRandomMethod(); + + when(webServer.isStandalone()).thenReturn(false); + Health.Builder builder = newHealthCheckBuilder().setStatus(Health.Status.GREEN); + Health health = builder.build(); + when(healthChecker.checkNode()).thenReturn(health); + + System.HealthResponse response = underTest.newRequest().setParam("local", "true") + .executeProtobuf(System.HealthResponse.class); + + assertThat(response.getHealth().name()).isEqualTo(health.getStatus().name()); + assertThat(response.getNodes().getNodesList()).isEmpty(); + verify(healthChecker, never()).checkCluster(); + } + private NodeHealth randomNodeHealth() { NodeHealth.Builder builder = newNodeHealthBuilder() .setStatus(NodeHealth.Status.values()[random.nextInt(NodeHealth.Status.values().length)]); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java index 1518c8f8a45..02719c45b61 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java @@ -63,7 +63,7 @@ public class SafeModeHealthActionTest { assertThat(definition.since()).isEqualTo("6.6"); assertThat(definition.isInternal()).isFalse(); assertThat(definition.responseExample()).isNotNull(); - assertThat(definition.params()).isEmpty(); + assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("local"); } @Test -- 2.39.5