]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15235 api/system/health add local parameter
authorPierre <pierre.guillot@sonarsource.com>
Mon, 2 Aug 2021 15:02:46 +0000 (17:02 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 3 Aug 2021 20:07:45 +0000 (20:07 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/HealthActionSupport.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java

index 68c739fe8b79dfbb13da9ae24f6f8362123910ee..809dcb0e1862acf585c40abe7ef83ace167d0ba3 100644 (file)
@@ -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);
index f8a2a6a9100ecac3f41a4f10528915a1d10542fb..5272c44126d6619f6d6023a8865cc24195548072 100644 (file)
@@ -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<NodeHealth> NODE_HEALTH_COMPARATOR = Comparator.<NodeHealth>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." +
         "<p>Require 'Administer System' permission or authentication with passcode</p>" +
         "<p> " +
@@ -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() {
index 71bb50131c265c6c07c2f3a9e6694e040f7f5e46..8a4de5eedd070bed6db0ba2b63176e5faece38d4 100644 (file)
@@ -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)]);
index 1518c8f8a45c5062d9d1bdb7b1cb8a5b105fdee8..02719c45b61f8c267e3a4377e53afb7c33e4f066 100644 (file)
@@ -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