You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/11/21 13:51:21 UTC

[camel] branch master updated: CAMEL-12012 - Camel-AWS: Add component verifiers like S3 for all the AWS components - Lambda Component

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 3d8b6fd  CAMEL-12012 - Camel-AWS: Add component verifiers like S3 for all the AWS components - Lambda Component
3d8b6fd is described below

commit 3d8b6fd8457c9145c36853fa2dfd7bb2c283d66e
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Tue Nov 21 14:50:51 2017 +0100

    CAMEL-12012 - Camel-AWS: Add component verifiers like S3 for all the AWS components - Lambda Component
---
 .../src/main/docs/aws-lambda-component.adoc        |  3 +-
 .../component/aws/lambda/LambdaComponent.java      |  3 +
 .../lambda/LambdaComponentVerifierExtension.java   | 86 ++++++++++++++++++++++
 .../component/aws/lambda/LambdaConfiguration.java  | 13 ++++
 .../LambdaComponentVerifierExtensionTest.java      | 73 ++++++++++++++++++
 5 files changed, 177 insertions(+), 1 deletion(-)

diff --git a/components/camel-aws/src/main/docs/aws-lambda-component.adoc b/components/camel-aws/src/main/docs/aws-lambda-component.adoc
index d8a92dc..66c1539 100644
--- a/components/camel-aws/src/main/docs/aws-lambda-component.adoc
+++ b/components/camel-aws/src/main/docs/aws-lambda-component.adoc
@@ -54,13 +54,14 @@ with the following path and query parameters:
 | *function* | *Required* Name of the Lambda function. |  | String
 |===
 
-==== Query Parameters (8 parameters):
+==== Query Parameters (9 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
 | *awsLambdaEndpoint* (producer) | The region with which the AWS-Lambda client wants to work with. |  | String
 | *operation* (producer) | *Required* The operation to perform. It can be listFunctions getFunction createFunction deleteFunction or invokeFunction |  | LambdaOperations
+| *region* (producer) | Amazon AWS Region |  | String
 | *awsLambdaClient* (advanced) | To use a existing configured AwsLambdaClient as client |  | AWSLambda
 | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). | false | boolean
 | *proxyHost* (proxy) | To define a proxy host when instantiating the Lambda client |  | String
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponent.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponent.java
index 9f958ab..b90b574 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponent.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponent.java
@@ -26,10 +26,13 @@ import org.apache.camel.util.ObjectHelper;
 public class LambdaComponent extends DefaultComponent {
 
     public LambdaComponent() {
+        this(null);
     }
 
     public LambdaComponent(CamelContext context) {
         super(context);
+        
+        registerExtension(new LambdaComponentVerifierExtension());
     }
 
     @Override
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponentVerifierExtension.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponentVerifierExtension.java
new file mode 100644
index 0000000..9d9ca14
--- /dev/null
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaComponentVerifierExtension.java
@@ -0,0 +1,86 @@
+/**
+ * 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.aws.lambda;
+
+import java.util.Map;
+
+import com.amazonaws.SdkClientException;
+import com.amazonaws.auth.AWSCredentials;
+import com.amazonaws.auth.AWSCredentialsProvider;
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.services.lambda.AWSLambda;
+import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
+
+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;
+
+public class LambdaComponentVerifierExtension extends DefaultComponentVerifierExtension {
+
+    public LambdaComponentVerifierExtension() {
+        this("aws-lambda");
+    }
+
+    public LambdaComponentVerifierExtension(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 {
+            LambdaConfiguration configuration = setProperties(new LambdaConfiguration(), parameters);
+            AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey());
+            AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
+            AWSLambda client = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).withRegion(configuration.getRegion()).build();
+            client.listFunctions();
+        } catch (SdkClientException e) {
+            ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage())
+                .detail("aws_lambda_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/src/main/java/org/apache/camel/component/aws/lambda/LambdaConfiguration.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaConfiguration.java
index e672da6..1908d1c 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaConfiguration.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaConfiguration.java
@@ -37,6 +37,8 @@ public class LambdaConfiguration {
     private String accessKey;
     @UriParam(label = "security", secret = true)
     private String secretKey;
+    @UriParam(label = "producer")
+    private String region;
     @UriParam(label = "proxy")
     private String proxyHost;
     @UriParam(label = "proxy")
@@ -88,6 +90,17 @@ public class LambdaConfiguration {
         this.secretKey = secretKey;
     }
 
+    public String getRegion() {
+        return region;
+    }
+
+    /**
+     * Amazon AWS Region
+     */
+    public void setRegion(String region) {
+        this.region = region;
+    }
+
     public String getAwsLambdaEndpoint() {
         return awsLambdaEndpoint;
     }
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentVerifierExtensionTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentVerifierExtensionTest.java
new file mode 100644
index 0000000..4be6e1b
--- /dev/null
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentVerifierExtensionTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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.aws.lambda;
+
+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.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class LambdaComponentVerifierExtensionTest extends CamelTestSupport {
+
+    // *************************************************
+    // Tests (parameters)
+    // *************************************************
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testParameters() throws Exception {
+        Component component = context().getComponent("aws-lambda");
+
+        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("function", "myfunction");
+        parameters.put("operation", "invokeFunction");
+
+        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
+
+        Assert.assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus());
+    }
+
+    @Test
+    public void testConnectivity() throws Exception {
+        Component component = context().getComponent("aws-lambda");
+        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("function", "myfunction");
+        parameters.put("operation", "invokeFunction");
+
+        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
+
+        Assert.assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].