camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [camel] 37/45: CAMEL-12884 - Camel-AWS Lambda: Add support for event source mapping, list operation
Date Thu, 18 Oct 2018 16:03:01 GMT
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 98614df9748c6c7cea304c508a610c7f08d2361a
Author: Andrea Cosentino <ancosen@gmail.com>
AuthorDate: Wed Oct 17 13:31:54 2018 +0200

    CAMEL-12884 - Camel-AWS Lambda: Add support for event source mapping, list operation
---
 .../component/aws/lambda/LambdaOperations.java     |  3 ++-
 .../camel/component/aws/lambda/LambdaProducer.java | 27 ++++++++++++++++++++++
 .../aws/lambda/AmazonLambdaClientMock.java         | 12 +++++++++-
 .../aws/lambda/LambdaComponentSpringTest.java      | 13 +++++++++++
 .../component/aws/lambda/LambdaOperationsTest.java |  4 +++-
 .../component/aws/lambda/LambdaProducerTest.java   | 16 +++++++++++++
 .../lambda/LambdaComponentSpringTest-context.xml   |  5 ++++
 7 files changed, 77 insertions(+), 3 deletions(-)

diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaOperations.java
b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaOperations.java
index 907c9bf..a565373 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaOperations.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaOperations.java
@@ -25,5 +25,6 @@ public enum LambdaOperations {
     invokeFunction,
     updateFunction,
     createEventSourceMapping,
-    deleteEventSourceMapping
+    deleteEventSourceMapping,
+    listEventSourceMapping
 }
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaProducer.java
b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaProducer.java
index ac167a28..bf9baa6 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaProducer.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/lambda/LambdaProducer.java
@@ -40,6 +40,8 @@ import com.amazonaws.services.lambda.model.GetFunctionRequest;
 import com.amazonaws.services.lambda.model.GetFunctionResult;
 import com.amazonaws.services.lambda.model.InvokeRequest;
 import com.amazonaws.services.lambda.model.InvokeResult;
+import com.amazonaws.services.lambda.model.ListEventSourceMappingsRequest;
+import com.amazonaws.services.lambda.model.ListEventSourceMappingsResult;
 import com.amazonaws.services.lambda.model.ListFunctionsResult;
 import com.amazonaws.services.lambda.model.TracingConfig;
 import com.amazonaws.services.lambda.model.UpdateFunctionCodeRequest;
@@ -95,6 +97,9 @@ public class LambdaProducer extends DefaultProducer {
         case deleteEventSourceMapping:
             deleteEventSourceMapping(getEndpoint().getAwsLambdaClient(), exchange);
             break;
+        case listEventSourceMapping:
+            listEventSourceMapping(getEndpoint().getAwsLambdaClient(), exchange);
+            break;
         default:
             throw new IllegalArgumentException("Unsupported operation");
         }
@@ -408,6 +413,28 @@ public class LambdaProducer extends DefaultProducer {
         message.setBody(result);
     }
     
+    private void listEventSourceMapping(AWSLambda lambdaClient, Exchange exchange) {
+        ListEventSourceMappingsResult result;
+        try {
+            ListEventSourceMappingsRequest request = new ListEventSourceMappingsRequest().withFunctionName(getConfiguration().getFunction());
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(LambdaConstants.SDK_CLIENT_EXECUTION_TIMEOUT)))
{
+                Integer timeout = exchange.getIn().getHeader(LambdaConstants.SDK_CLIENT_EXECUTION_TIMEOUT,
Integer.class);
+                request.withSdkClientExecutionTimeout(timeout);
+            }
+
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(LambdaConstants.SDK_REQUEST_TIMEOUT)))
{
+                Integer timeout = exchange.getIn().getHeader(LambdaConstants.SDK_REQUEST_TIMEOUT,
Integer.class);
+                request.withSdkRequestTimeout(timeout);
+            }
+            result = lambdaClient.listEventSourceMappings(request);
+        } catch (AmazonServiceException ase) {
+            LOG.trace("listEventSourceMapping command returned the error code {}", ase.getErrorCode());
+            throw ase;
+        }
+        Message message = getMessageForResponse(exchange);
+        message.setBody(result);
+    }
+    
     private LambdaOperations determineOperation(Exchange exchange) {
         LambdaOperations operation = exchange.getIn().getHeader(LambdaConstants.OPERATION,
LambdaOperations.class);
         if (operation == null) {
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/AmazonLambdaClientMock.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/AmazonLambdaClientMock.java
index 8398068..909f910 100644
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/AmazonLambdaClientMock.java
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/AmazonLambdaClientMock.java
@@ -40,6 +40,7 @@ import com.amazonaws.services.lambda.model.DeleteFunctionConcurrencyRequest;
 import com.amazonaws.services.lambda.model.DeleteFunctionConcurrencyResult;
 import com.amazonaws.services.lambda.model.DeleteFunctionRequest;
 import com.amazonaws.services.lambda.model.DeleteFunctionResult;
+import com.amazonaws.services.lambda.model.EventSourceMappingConfiguration;
 import com.amazonaws.services.lambda.model.FunctionConfiguration;
 import com.amazonaws.services.lambda.model.GetAccountSettingsRequest;
 import com.amazonaws.services.lambda.model.GetAccountSettingsResult;
@@ -238,7 +239,16 @@ public class AmazonLambdaClientMock extends AbstractAWSLambda {
 
     @Override
     public ListEventSourceMappingsResult listEventSourceMappings(ListEventSourceMappingsRequest
listEventSourceMappingsRequest) {
-        throw new UnsupportedOperationException();
+        ListEventSourceMappingsResult result = new ListEventSourceMappingsResult();
+        List<EventSourceMappingConfiguration> confList = new ArrayList<EventSourceMappingConfiguration>();
+        EventSourceMappingConfiguration conf = new EventSourceMappingConfiguration();
+        conf.setBatchSize(100);
+        conf.setFunctionArn("arn:aws:lambda:eu-central-1:643534317684:function:" + listEventSourceMappingsRequest.getFunctionName());
+        conf.setState("Enabled");
+        conf.setEventSourceArn("arn:aws:sqs:eu-central-1:643534317684:testqueue");
+        confList.add(conf);
+        result.setEventSourceMappings(confList);
+        return result;
     }
 
     @Override
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest.java
index fb3412e..d2d60c5 100644
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest.java
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest.java
@@ -23,6 +23,7 @@ import com.amazonaws.services.lambda.model.CreateFunctionResult;
 import com.amazonaws.services.lambda.model.DeleteEventSourceMappingResult;
 import com.amazonaws.services.lambda.model.DeleteFunctionResult;
 import com.amazonaws.services.lambda.model.GetFunctionResult;
+import com.amazonaws.services.lambda.model.ListEventSourceMappingsResult;
 import com.amazonaws.services.lambda.model.ListFunctionsResult;
 import com.amazonaws.util.IOUtils;
 import org.apache.camel.Exchange;
@@ -143,7 +144,19 @@ public class LambdaComponentSpringTest extends CamelSpringTestSupport
{
         DeleteEventSourceMappingResult result = exchange.getOut().getBody(DeleteEventSourceMappingResult.class);
         assertTrue(result.getState().equalsIgnoreCase("Deleting"));
     }
+    
+    @Test
+    public void lambdaListEventSourceMappingTest() throws Exception {
+        Exchange exchange = template.send("direct:listEventSourceMapping", ExchangePattern.InOut,
new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+            }
+        });
+        assertMockEndpointsSatisfied();
 
+        ListEventSourceMappingsResult result = exchange.getOut().getBody(ListEventSourceMappingsResult.class);
+        assertEquals(result.getEventSourceMappings().get(0).getFunctionArn(), "arn:aws:lambda:eu-central-1:643534317684:function:GetHelloWithName");
+    }
 
     @Override
     protected AbstractApplicationContext createApplicationContext() {
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaOperationsTest.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaOperationsTest.java
index 420d658..b117f31 100644
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaOperationsTest.java
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaOperationsTest.java
@@ -26,7 +26,7 @@ public class LambdaOperationsTest {
 
     @Test
     public void supportedOperationCount() {
-        assertEquals(8, LambdaOperations.values().length);
+        assertEquals(9, LambdaOperations.values().length);
     }
 
     @Test
@@ -39,6 +39,7 @@ public class LambdaOperationsTest {
         assertEquals(LambdaOperations.updateFunction, LambdaOperations.valueOf("updateFunction"));
         assertEquals(LambdaOperations.createEventSourceMapping, LambdaOperations.valueOf("createEventSourceMapping"));
         assertEquals(LambdaOperations.deleteEventSourceMapping, LambdaOperations.valueOf("deleteEventSourceMapping"));
+        assertEquals(LambdaOperations.listEventSourceMapping, LambdaOperations.valueOf("listEventSourceMapping"));
     }
 
     @Test
@@ -51,5 +52,6 @@ public class LambdaOperationsTest {
         assertEquals(LambdaOperations.updateFunction.toString(), "updateFunction");
         assertEquals(LambdaOperations.createEventSourceMapping.toString(), "createEventSourceMapping");
         assertEquals(LambdaOperations.deleteEventSourceMapping.toString(), "deleteEventSourceMapping");
+        assertEquals(LambdaOperations.listEventSourceMapping.toString(), "listEventSourceMapping");
     }
 }
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaProducerTest.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaProducerTest.java
index 252a44d..b76256c 100644
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaProducerTest.java
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/lambda/LambdaProducerTest.java
@@ -24,6 +24,7 @@ import com.amazonaws.services.lambda.model.CreateFunctionResult;
 import com.amazonaws.services.lambda.model.DeleteEventSourceMappingResult;
 import com.amazonaws.services.lambda.model.DeleteFunctionResult;
 import com.amazonaws.services.lambda.model.GetFunctionResult;
+import com.amazonaws.services.lambda.model.ListEventSourceMappingsResult;
 import com.amazonaws.services.lambda.model.ListFunctionsResult;
 import com.amazonaws.services.lambda.model.UpdateFunctionCodeResult;
 import com.amazonaws.util.IOUtils;
@@ -183,6 +184,19 @@ public class LambdaProducerTest extends CamelTestSupport {
         DeleteEventSourceMappingResult result = exchange.getOut().getBody(DeleteEventSourceMappingResult.class);
         assertTrue(result.getState().equalsIgnoreCase("Deleting"));
     }
+    
+    @Test
+    public void lambdaListEventSourceMappingTest() throws Exception {
+        Exchange exchange = template.send("direct:listEventSourceMapping", ExchangePattern.InOut,
new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+            }
+        });
+        assertMockEndpointsSatisfied();
+
+        ListEventSourceMappingsResult result = exchange.getOut().getBody(ListEventSourceMappingsResult.class);
+        assertEquals(result.getEventSourceMappings().get(0).getFunctionArn(), "arn:aws:lambda:eu-central-1:643534317684:function:GetHelloWithName");
+    }
 
     @Override
     protected JndiRegistry createRegistry() throws Exception {
@@ -215,6 +229,8 @@ public class LambdaProducerTest extends CamelTestSupport {
                 from("direct:createEventSourceMapping").to("aws-lambda://GetHelloWithName?awsLambdaClient=#awsLambdaClient&operation=createEventSourceMapping").to("mock:result");
                 
                 from("direct:deleteEventSourceMapping").to("aws-lambda://GetHelloWithName?awsLambdaClient=#awsLambdaClient&operation=deleteEventSourceMapping").to("mock:result");
+                
+                from("direct:listEventSourceMapping").to("aws-lambda://GetHelloWithName?awsLambdaClient=#awsLambdaClient&operation=listEventSourceMapping").to("mock:result");
             }
         };
     }
diff --git a/components/camel-aws/src/test/resources/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest-context.xml
b/components/camel-aws/src/test/resources/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest-context.xml
index 1e7c83e..0bd71c9 100644
--- a/components/camel-aws/src/test/resources/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest-context.xml
+++ b/components/camel-aws/src/test/resources/org/apache/camel/component/aws/lambda/LambdaComponentSpringTest-context.xml
@@ -59,6 +59,11 @@
             <from uri="direct:deleteEventSourceMapping"/>
             <to uri="aws-lambda://GetHelloWithName?awsLambdaClient=#awsLambdaClient&amp;operation=deleteEventSourceMapping"/>
         </route>
+        
+        <route>
+            <from uri="direct:listEventSourceMapping"/>
+            <to uri="aws-lambda://GetHelloWithName?awsLambdaClient=#awsLambdaClient&amp;operation=listEventSourceMapping"/>
+        </route>
     </camelContext>
 
     <bean id="awsLambdaClient" class="org.apache.camel.component.aws.lambda.AmazonLambdaClientMock"/>


Mime
View raw message