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 3c48bc6b7134ff6194ee51565bcf12f1e6b97c3f Author: Andrea Cosentino <[email protected]> AuthorDate: Fri Nov 4 10:47:50 2022 +0100 CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - DynamoDB Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-ddb/pom.xml | 4 + .../component/aws2/ddb/Ddb2ClientHealthCheck.java | 79 +++++++++++++++ .../camel/component/aws2/ddb/Ddb2Component.java | 2 - .../aws2/ddb/Ddb2ComponentVerifierExtension.java | 106 --------------------- .../camel/component/aws2/ddb/Ddb2Endpoint.java | 13 +++ 5 files changed, 96 insertions(+), 108 deletions(-) diff --git a/components/camel-aws/camel-aws2-ddb/pom.xml b/components/camel-aws/camel-aws2-ddb/pom.xml index 4f7c1a937ba..30dbc996204 100644 --- a/components/camel-aws/camel-aws2-ddb/pom.xml +++ b/components/camel-aws/camel-aws2-ddb/pom.xml @@ -76,5 +76,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-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2ClientHealthCheck.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2ClientHealthCheck.java new file mode 100644 index 00000000000..ae3ef251bf8 --- /dev/null +++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2ClientHealthCheck.java @@ -0,0 +1,79 @@ +/* + * 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.ddb; + +import org.apache.camel.component.aws2.ddb.client.Ddb2ClientFactory; +import org.apache.camel.component.aws2.ddb.client.Ddb2InternalClient; +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.dynamodb.DynamoDbClient; + +import java.util.Map; + +public class Ddb2ClientHealthCheck extends AbstractHealthCheck { + + private final Ddb2Endpoint ddb2Endpoint; + private final String clientId; + + public Ddb2ClientHealthCheck(Ddb2Endpoint ddb2Endpoint, String clientId) { + super("camel", "aws2-ddb-client-" + clientId); + this.ddb2Endpoint = ddb2Endpoint; + 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) { + Ddb2Configuration configuration = ddb2Endpoint.getConfiguration(); + if (ObjectHelper.isNotEmpty(configuration.getRegion())) { + if (!DynamoDbClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) { + builder.message("The service is not supported in this region"); + builder.down(); + return; + } + } + try { + Ddb2InternalClient ddb2Client = Ddb2ClientFactory.getDynamoDBClient(configuration); + ddb2Client.getDynamoDBClient().listTables(); + } 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-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Component.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Component.java index 157eaca3cdb..1d8713b204d 100644 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Component.java +++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Component.java @@ -40,8 +40,6 @@ public class Ddb2Component extends DefaultComponent { public Ddb2Component(CamelContext context) { super(context); - - registerExtension(new Ddb2ComponentVerifierExtension()); } @Override diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2ComponentVerifierExtension.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2ComponentVerifierExtension.java deleted file mode 100644 index 5f18d40b384..00000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2ComponentVerifierExtension.java +++ /dev/null @@ -1,106 +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.ddb; - -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.core.exception.SdkClientException; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.dynamodb.DynamoDbClient; -import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; - -public class Ddb2ComponentVerifierExtension extends DefaultComponentVerifierExtension { - - public Ddb2ComponentVerifierExtension() { - this("aws2-ddb"); - } - - public Ddb2ComponentVerifierExtension(String scheme) { - super(scheme); - } - - // ********************************* - // Parameters validation - // ********************************* - - /** - * Basic check of the parameters (they are not empty) - * - * @param parameters - * @return - */ - @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 - // ********************************* - - /** - * To verify the connectivity, we will try a basic test connection to extract the list of tables and see if it fails - * - * @param parameters - * @return - */ - @Override - protected Result verifyConnectivity(Map<String, Object> parameters) { - ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY); - - try { - Ddb2Configuration configuration = setProperties(new Ddb2Configuration(), parameters); - if (!DynamoDbClient.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()); - DynamoDbClientBuilder clientBuilder = DynamoDbClient.builder(); - DynamoDbClient client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)) - .region(Region.of(configuration.getRegion())).build(); - client.listTables(); - } catch (SdkClientException e) { - ResultErrorBuilder errorBuilder - = ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage()) - .detail("aws_ddb_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-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java index cace6fcfb72..c527f410fa7 100644 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java +++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java @@ -25,6 +25,8 @@ import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.aws2.ddb.client.Ddb2ClientFactory; +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.ScheduledPollEndpoint; @@ -56,6 +58,9 @@ public class Ddb2Endpoint extends ScheduledPollEndpoint { private static final Logger LOG = LoggerFactory.getLogger(Ddb2Endpoint.class); + private ComponentsHealthCheckRepository healthCheckRepository; + private Ddb2ClientHealthCheck clientHealthCheck; + @UriParam private Ddb2Configuration configuration; @@ -80,6 +85,14 @@ public class Ddb2Endpoint extends ScheduledPollEndpoint { public void doStart() throws Exception { super.doStart(); + healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(getCamelContext(), + ComponentsHealthCheckRepository.REPOSITORY_ID, ComponentsHealthCheckRepository.class); + + if (healthCheckRepository != null) { + clientHealthCheck = new Ddb2ClientHealthCheck(this, getId()); + healthCheckRepository.addHealthCheck(clientHealthCheck); + } + ddbClient = configuration.getAmazonDDBClient() != null ? configuration.getAmazonDDBClient() : Ddb2ClientFactory.getDynamoDBClient(configuration).getDynamoDBClient();
