diff --git a/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java b/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java new file mode 100644 index 0000000000..343bc35b1c --- /dev/null +++ b/discovery-service/src/main/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicator.java @@ -0,0 +1,37 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package org.zowe.apiml.discovery.health; + +import lombok.RequiredArgsConstructor; +import org.springframework.boot.actuate.health.AbstractHealthIndicator; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.stereotype.Component; +import org.zowe.apiml.product.constants.CoreService; + +/** + * Discovery service health information (/application/health) + */ +@Component +@RequiredArgsConstructor +public class DiscoveryServiceHealthIndicator extends AbstractHealthIndicator { + + private final DiscoveryClient discoveryClient; + + @Override + protected void doHealthCheck(Health.Builder builder) { + String gatewayServiceId = CoreService.GATEWAY.getServiceId(); + boolean gatewayDown = this.discoveryClient.getInstances(gatewayServiceId).isEmpty(); + builder + .status(gatewayDown ? new Status("PARTIAL", "Authenticated endpoints not available.") : Status.UP) + .withDetail(gatewayServiceId, gatewayDown ? Status.DOWN : Status.UP); + } +} diff --git a/discovery-service/src/main/resources/application.yml b/discovery-service/src/main/resources/application.yml index 4705355165..f1948d9a7b 100644 --- a/discovery-service/src/main/resources/application.yml +++ b/discovery-service/src/main/resources/application.yml @@ -87,6 +87,8 @@ management: endpoint: shutdown: enabled: true + health: + show-details: always hystrix.command.default.execution.timeout.enabled: false --- diff --git a/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java b/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java new file mode 100644 index 0000000000..fc790b38a5 --- /dev/null +++ b/discovery-service/src/test/java/org/zowe/apiml/discovery/health/DiscoveryServiceHealthIndicatorTest.java @@ -0,0 +1,53 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package org.zowe.apiml.discovery.health; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.zowe.apiml.product.constants.CoreService; + +import java.util.Collections; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DiscoveryServiceHealthIndicatorTest { + + private final DiscoveryClient discoveryClient = mock(DiscoveryClient.class); + private final DiscoveryServiceHealthIndicator discoverServiceHealthIndicator = new DiscoveryServiceHealthIndicator(discoveryClient); + private final Health.Builder builder = new Health.Builder(); + + @Test + void testStatusIsUpWhenGatewayIsAvailable() { + when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn( + Collections.singletonList( + new DefaultServiceInstance(null, CoreService.GATEWAY.getServiceId(), "host", 10010, true))); + + discoverServiceHealthIndicator.doHealthCheck(builder); + + Assertions.assertEquals(Status.UP, builder.build().getStatus()); + Assertions.assertEquals(Status.UP, builder.build().getDetails().get("gateway")); + } + + @Test + void testStatusIsPartialWhenGatewayIsNotAvailable() { + when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(Collections.emptyList()); + + discoverServiceHealthIndicator.doHealthCheck(builder); + + Assertions.assertEquals(new Status("PARTIAL"), builder.build().getStatus()); + Assertions.assertEquals("Authenticated endpoints not available.", builder.build().getStatus().getDescription()); + Assertions.assertEquals(Status.DOWN, builder.build().getDetails().get("gateway")); + } +} diff --git a/discovery-service/src/test/resources/application.yml b/discovery-service/src/test/resources/application.yml index 0855b45e01..36acf0dd37 100644 --- a/discovery-service/src/test/resources/application.yml +++ b/discovery-service/src/test/resources/application.yml @@ -47,6 +47,14 @@ management: health: defaults: enabled: false + endpoint: + health: + status: + order: "DOWN,PARTIAL,UP" + http-mapping: + DOWN: 503 + PARTIAL: 200 + show-details: always --- spring: profiles: https