This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3529a789a4a24a2f1828c2d4667184720d23c0a0 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Wed Nov 2 18:27:08 2022 +0100 CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - Fix check for region before looking for enabled services - CloudWatch Signed-off-by: Andrea Cosentino <anco...@gmail.com> --- components/camel-aws/camel-aws2-cw/pom.xml | 8 ++ .../component/aws2/cw/Cw2ClientHealthCheck.java | 80 +++++++++++++++++++ .../camel/component/aws2/cw/Cw2Component.java | 1 - .../aws2/cw/Cw2ComponentVerifierExtension.java | 93 ---------------------- .../camel/component/aws2/cw/Cw2Endpoint.java | 17 ++++ .../aws2/cw/CwComponentVerifierExtensionTest.java | 91 --------------------- 6 files changed, 105 insertions(+), 185 deletions(-) diff --git a/components/camel-aws/camel-aws2-cw/pom.xml b/components/camel-aws/camel-aws2-cw/pom.xml index 8926c8e5c4b..8c26a4ccec7 100644 --- a/components/camel-aws/camel-aws2-cw/pom.xml +++ b/components/camel-aws/camel-aws2-cw/pom.xml @@ -40,6 +40,10 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-health</artifactId> + </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>cloudwatch</artifactId> @@ -76,5 +80,9 @@ <type>test-jar</type> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-health</artifactId> + </dependency> </dependencies> </project> diff --git a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2ClientHealthCheck.java b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2ClientHealthCheck.java new file mode 100644 index 00000000000..fc0904f2c16 --- /dev/null +++ b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2ClientHealthCheck.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.component.aws2.cw; + +import org.apache.camel.component.aws2.cw.client.Cw2ClientFactory; +import org.apache.camel.component.aws2.cw.client.Cw2InternalClient; +import org.apache.camel.health.HealthCheckResultBuilder; +import org.apache.camel.impl.health.AbstractHealthCheck; +import org.apache.camel.util.ObjectHelper; +import software.amazon.awssdk.awscore.exception.AwsServiceException; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; +import software.amazon.awssdk.services.cloudwatch.model.ListDashboardsRequest; + +import java.util.Map; + +public class Cw2ClientHealthCheck extends AbstractHealthCheck { + + private final Cw2Endpoint cw2Endpoint; + private final String clientId; + + public Cw2ClientHealthCheck(Cw2Endpoint cw2Endpoint, String clientId) { + super("camel", "aws2-cw-client-" + clientId); + this.cw2Endpoint = cw2Endpoint; + this.clientId = clientId; + } + + @Override + public boolean isLiveness() { + // this health check is only readiness + return false; + } + + @Override + protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) { + Cw2Configuration configuration = cw2Endpoint.getConfiguration(); + if (ObjectHelper.isNotEmpty(configuration.getRegion())) { + if (!CloudWatchClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) { + builder.message("The service is not supported in this region"); + builder.down(); + return; + } + } + try { + Cw2InternalClient cw2Client = Cw2ClientFactory.getCloudWatchClient(configuration); + cw2Client.getCloudWatchClient().listDashboards(ListDashboardsRequest.builder().build()); + } catch (AwsServiceException e) { + builder.message(e.getMessage()); + builder.error(e); + if (ObjectHelper.isNotEmpty(e.statusCode())) { + builder.detail(SERVICE_STATUS_CODE, e.statusCode()); + } + if (ObjectHelper.isNotEmpty(e.awsErrorDetails().errorCode())) { + builder.detail(SERVICE_ERROR_CODE, e.awsErrorDetails().errorCode()); + } + builder.down(); + return; + } catch (Exception e) { + builder.error(e); + builder.down(); + return; + } + builder.up(); + } +} diff --git a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Component.java b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Component.java index a4048d8d734..1b3333b2b55 100644 --- a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Component.java +++ b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Component.java @@ -39,7 +39,6 @@ public class Cw2Component extends DefaultComponent { public Cw2Component(CamelContext context) { super(context); - registerExtension(new Cw2ComponentVerifierExtension()); } @Override diff --git a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2ComponentVerifierExtension.java b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2ComponentVerifierExtension.java deleted file mode 100644 index 249bcaf0fe9..00000000000 --- a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2ComponentVerifierExtension.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.cw; - -import java.util.Map; - -import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension; -import org.apache.camel.component.extension.verifier.ResultBuilder; -import org.apache.camel.component.extension.verifier.ResultErrorBuilder; -import org.apache.camel.component.extension.verifier.ResultErrorHelper; -import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; -import software.amazon.awssdk.awscore.exception.AwsServiceException; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; -import software.amazon.awssdk.services.cloudwatch.CloudWatchClientBuilder; - -public class Cw2ComponentVerifierExtension extends DefaultComponentVerifierExtension { - - public Cw2ComponentVerifierExtension() { - this("aws2-cw"); - } - - public Cw2ComponentVerifierExtension(String scheme) { - super(scheme); - } - - // ********************************* - // Parameters validation - // ********************************* - - @Override - protected Result verifyParameters(Map<String, Object> parameters) { - - ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS) - .error(ResultErrorHelper.requiresOption("accessKey", parameters)) - .error(ResultErrorHelper.requiresOption("secretKey", parameters)) - .error(ResultErrorHelper.requiresOption("region", parameters)); - - // Validate using the catalog - - super.verifyParametersAgainstCatalog(builder, parameters); - - return builder.build(); - } - - // ********************************* - // Connectivity validation - // ********************************* - - @Override - protected Result verifyConnectivity(Map<String, Object> parameters) { - ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY); - try { - Cw2Configuration configuration = setProperties(new Cw2Configuration(), parameters); - if (!CloudWatchClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) { - ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription( - VerificationError.StandardCode.ILLEGAL_PARAMETER, "The service is not supported in this region"); - return builder.error(errorBuilder.build()).build(); - } - AwsBasicCredentials cred = AwsBasicCredentials.create(configuration.getAccessKey(), configuration.getSecretKey()); - CloudWatchClientBuilder clientBuilder = CloudWatchClient.builder(); - CloudWatchClient client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)) - .region(Region.of(configuration.getRegion())).build(); - client.listMetrics(); - } catch (AwsServiceException e) { - ResultErrorBuilder errorBuilder - = ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage()) - .detail("aws_cw_exception_message", e.getMessage()) - .detail(VerificationError.ExceptionAttribute.EXCEPTION_CLASS, e.getClass().getName()) - .detail(VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE, e); - - builder.error(errorBuilder.build()); - } catch (Exception e) { - builder.error(ResultErrorBuilder.withException(e).build()); - } - return builder.build(); - } -} diff --git a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java index 4bd17681875..5c1dfec0808 100644 --- a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java +++ b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java @@ -22,6 +22,8 @@ import org.apache.camel.Consumer; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.component.aws2.cw.client.Cw2ClientFactory; +import org.apache.camel.health.HealthCheckHelper; +import org.apache.camel.impl.health.ComponentsHealthCheckRepository; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.support.DefaultEndpoint; @@ -38,6 +40,8 @@ public class Cw2Endpoint extends DefaultEndpoint { @UriParam private Cw2Configuration configuration; private CloudWatchClient cloudWatchClient; + private ComponentsHealthCheckRepository healthCheckRepository; + private Cw2ClientHealthCheck clientHealthCheck; public Cw2Endpoint(String uri, Component component, Cw2Configuration configuration) { super(uri, component); @@ -72,6 +76,19 @@ public class Cw2Endpoint extends DefaultEndpoint { super.doStop(); } + @Override + public void doStart() throws Exception { + super.doStart(); + + healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(getCamelContext(), + ComponentsHealthCheckRepository.REPOSITORY_ID, ComponentsHealthCheckRepository.class); + + if (healthCheckRepository != null) { + clientHealthCheck = new Cw2ClientHealthCheck(this, getId()); + healthCheckRepository.addHealthCheck(clientHealthCheck); + } + } + public Cw2Configuration getConfiguration() { return configuration; } diff --git a/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwComponentVerifierExtensionTest.java b/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwComponentVerifierExtensionTest.java deleted file mode 100644 index 7981cf63a22..00000000000 --- a/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwComponentVerifierExtensionTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.cw; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.Component; -import org.apache.camel.component.extension.ComponentVerifierExtension; -import org.apache.camel.test.junit5.CamelTestSupport; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class CwComponentVerifierExtensionTest extends CamelTestSupport { - - // ************************************************* - // Tests (parameters) - // ************************************************* - @Override - public boolean isUseRouteBuilder() { - return false; - } - - @Test - public void testParameters() { - Component component = context().getComponent("aws2-cw"); - - ComponentVerifierExtension verifier - = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); - - Map<String, Object> parameters = new HashMap<>(); - parameters.put("secretKey", "l"); - parameters.put("accessKey", "k"); - parameters.put("region", "l"); - parameters.put("namespace", "test"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivity() { - Component component = context().getComponent("aws2-cw"); - ComponentVerifierExtension verifier - = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); - - Map<String, Object> parameters = new HashMap<>(); - parameters.put("secretKey", "l"); - parameters.put("accessKey", "k"); - parameters.put("region", "US_EAST_1"); - parameters.put("namespace", "test"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - } - - @Test - public void testConnectivityAndRegion() { - Component component = context().getComponent("aws2-cw"); - ComponentVerifierExtension verifier - = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); - - Map<String, Object> parameters = new HashMap<>(); - parameters.put("secretKey", "l"); - parameters.put("accessKey", "k"); - parameters.put("region", "test"); - parameters.put("namespace", "test"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - } - -}