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 2020/04/09 11:01:57 UTC

[camel] 08/10: CAMEL-14868 - Camel-AWS2-*: Where possible, give the possiblity to the end user to pass an AWS Request pojo as body, aws2-ec2 remaining operations

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

commit bde80f96bb28b3aaf98eb6ab7f7039434faaaf18
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Apr 9 12:20:09 2020 +0200

    CAMEL-14868 - Camel-AWS2-*: Where possible, give the possiblity to the end user to pass an AWS Request pojo as body, aws2-ec2 remaining operations
---
 .../camel/component/aws2/ec2/AWS2EC2Producer.java  | 222 ++++++++++++++-------
 1 file changed, 147 insertions(+), 75 deletions(-)

diff --git a/components/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java b/components/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
index a6b509f..15238d4 100644
--- a/components/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
+++ b/components/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
@@ -425,7 +425,7 @@ public class AWS2EC2Producer extends DefaultProducer {
         Collection<String> instanceIds;
         if (getConfiguration().isPojoRequest()) {
             if (ObjectHelper.isNotEmpty(exchange.getIn().getBody())) {
-                if (exchange.getIn().getBody() instanceof DescribeInstanceStatusRequest) {
+                if (exchange.getIn().getBody() instanceof RebootInstancesRequest) {
                     Object payload = exchange.getIn().getBody();
                     try {
                         LOG.trace("Rebooting instances with Ids [{}] ", ((RebootInstancesRequest)payload).instanceIds());
@@ -457,103 +457,175 @@ public class AWS2EC2Producer extends DefaultProducer {
     @SuppressWarnings("unchecked")
     private void monitorInstances(Ec2Client ec2Client, Exchange exchange) {
         Collection<String> instanceIds;
-        MonitorInstancesRequest.Builder builder = MonitorInstancesRequest.builder();
-        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
-            instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
-            builder.instanceIds(instanceIds);
+        if (getConfiguration().isPojoRequest()) {
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getBody())) {
+                if (exchange.getIn().getBody() instanceof MonitorInstancesRequest) {
+                    Object payload = exchange.getIn().getBody();
+                    MonitorInstancesResponse result;
+                    try {
+                        result = ec2Client.monitorInstances((MonitorInstancesRequest)payload);
+                    } catch (AwsServiceException ase) {
+                        LOG.trace("Monitor Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
+                        throw ase;
+                    }
+                    LOG.trace("Start Monitoring instances with Ids [{}] ", ((MonitorInstancesRequest)payload).instanceIds());
+                    Message message = getMessageForResponse(exchange);
+                    message.setBody(result);
+                }
+            }
         } else {
-            throw new IllegalArgumentException("Instances Ids must be specified");
-        }
-        MonitorInstancesResponse result;
-        try {
-            result = ec2Client.monitorInstances(builder.build());
-        } catch (AwsServiceException ase) {
-            LOG.trace("Monitor Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
-            throw ase;
+            MonitorInstancesRequest.Builder builder = MonitorInstancesRequest.builder();
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
+                instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
+                builder.instanceIds(instanceIds);
+            } else {
+                throw new IllegalArgumentException("Instances Ids must be specified");
+            }
+            MonitorInstancesResponse result;
+            try {
+                result = ec2Client.monitorInstances(builder.build());
+            } catch (AwsServiceException ase) {
+                LOG.trace("Monitor Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
+                throw ase;
+            }
+            LOG.trace("Start Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
+            Message message = getMessageForResponse(exchange);
+            message.setBody(result);
         }
-        LOG.trace("Start Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
-        Message message = getMessageForResponse(exchange);
-        message.setBody(result);
     }
 
     @SuppressWarnings("unchecked")
     private void unmonitorInstances(Ec2Client ec2Client, Exchange exchange) {
         Collection<String> instanceIds;
-        UnmonitorInstancesRequest.Builder builder = UnmonitorInstancesRequest.builder();
-        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
-            instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
-            builder.instanceIds(instanceIds);
+        if (getConfiguration().isPojoRequest()) {
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getBody())) {
+                if (exchange.getIn().getBody() instanceof UnmonitorInstancesRequest) {
+                    Object payload = exchange.getIn().getBody();
+                    UnmonitorInstancesResponse result;
+                    try {
+                        result = ec2Client.unmonitorInstances((UnmonitorInstancesRequest)payload);
+                    } catch (AwsServiceException ase) {
+                        LOG.trace("Unmonitor Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
+                        throw ase;
+                    }
+                    LOG.trace("Stop Monitoring instances with Ids [{}] ", ((UnmonitorInstancesRequest)payload).instanceIds());
+                    Message message = getMessageForResponse(exchange);
+                    message.setBody(result);
+                }
+            }
         } else {
-            throw new IllegalArgumentException("Instances Ids must be specified");
-        }
-        UnmonitorInstancesResponse result;
-        try {
-            result = ec2Client.unmonitorInstances(builder.build());
-        } catch (AwsServiceException ase) {
-            LOG.trace("Unmonitor Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
-            throw ase;
+            UnmonitorInstancesRequest.Builder builder = UnmonitorInstancesRequest.builder();
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
+                instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
+                builder.instanceIds(instanceIds);
+            } else {
+                throw new IllegalArgumentException("Instances Ids must be specified");
+            }
+            UnmonitorInstancesResponse result;
+            try {
+                result = ec2Client.unmonitorInstances(builder.build());
+            } catch (AwsServiceException ase) {
+                LOG.trace("Unmonitor Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
+                throw ase;
+            }
+            LOG.trace("Stop Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
+            Message message = getMessageForResponse(exchange);
+            message.setBody(result);
         }
-        LOG.trace("Stop Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
-        Message message = getMessageForResponse(exchange);
-        message.setBody(result);
     }
 
     @SuppressWarnings("unchecked")
     private void createTags(Ec2Client ec2Client, Exchange exchange) {
         Collection<String> instanceIds;
-        Collection<Tag> tags;
-        CreateTagsRequest.Builder builder = CreateTagsRequest.builder();
-        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
-            instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
-            builder.resources(instanceIds);
-        } else {
-            throw new IllegalArgumentException("Instances Ids must be specified");
-        }
-        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS))) {
-            tags = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS, Collection.class);
-            builder.tags(tags);
+        if (getConfiguration().isPojoRequest()) {
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getBody())) {
+                if (exchange.getIn().getBody() instanceof CreateTagsRequest) {
+                    Object payload = exchange.getIn().getBody();
+                    CreateTagsResponse result;
+                    try {
+                        result = ec2Client.createTags((CreateTagsRequest)payload);
+                    } catch (AwsServiceException ase) {
+                        LOG.trace("Create tags command returned the error code {}", ase.awsErrorDetails().errorCode());
+                        throw ase;
+                    }
+                    LOG.trace("Created tags [{}] ", ((CreateTagsRequest)payload).tags());
+                    Message message = getMessageForResponse(exchange);
+                    message.setBody(result);
+                }
+            }
         } else {
-            throw new IllegalArgumentException("Tags must be specified");
-        }
-        CreateTagsResponse result;
-        try {
-            result = ec2Client.createTags(builder.build());
-        } catch (AwsServiceException ase) {
-            LOG.trace("Create tags command returned the error code {}", ase.awsErrorDetails().errorCode());
-            throw ase;
+            Collection<Tag> tags;
+            CreateTagsRequest.Builder builder = CreateTagsRequest.builder();
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
+                instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
+                builder.resources(instanceIds);
+            } else {
+                throw new IllegalArgumentException("Instances Ids must be specified");
+            }
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS))) {
+                tags = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS, Collection.class);
+                builder.tags(tags);
+            } else {
+                throw new IllegalArgumentException("Tags must be specified");
+            }
+            CreateTagsResponse result;
+            try {
+                result = ec2Client.createTags(builder.build());
+            } catch (AwsServiceException ase) {
+                LOG.trace("Create tags command returned the error code {}", ase.awsErrorDetails().errorCode());
+                throw ase;
+            }
+            LOG.trace("Created tags [{}] on resources with Ids [{}] ", Arrays.toString(tags.toArray()), Arrays.toString(instanceIds.toArray()));
+            Message message = getMessageForResponse(exchange);
+            message.setBody(result);
         }
-        LOG.trace("Created tags [{}] on resources with Ids [{}] ", Arrays.toString(tags.toArray()), Arrays.toString(instanceIds.toArray()));
-        Message message = getMessageForResponse(exchange);
-        message.setBody(result);
     }
 
     @SuppressWarnings("unchecked")
     private void deleteTags(Ec2Client ec2Client, Exchange exchange) {
         Collection<String> instanceIds;
-        Collection<Tag> tags;
-        DeleteTagsRequest.Builder builder = DeleteTagsRequest.builder();
-        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
-            instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
-            builder.resources(instanceIds);
-        } else {
-            throw new IllegalArgumentException("Instances Ids must be specified");
-        }
-        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS))) {
-            tags = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS, Collection.class);
-            builder.tags(tags);
+        if (getConfiguration().isPojoRequest()) {
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getBody())) {
+                if (exchange.getIn().getBody() instanceof DeleteTagsRequest) {
+                    Object payload = exchange.getIn().getBody();
+                    DeleteTagsResponse result;
+                    try {
+                        result = ec2Client.deleteTags((DeleteTagsRequest)payload);
+                    } catch (AwsServiceException ase) {
+                        LOG.trace("Delete tags command returned the error code {}", ase.awsErrorDetails().errorCode());
+                        throw ase;
+                    }
+                    LOG.trace("Delete tags [{}]  ", ((DeleteTagsRequest)payload).tags());
+                    Message message = getMessageForResponse(exchange);
+                    message.setBody(result);
+                }
+            }
         } else {
-            throw new IllegalArgumentException("Tags must be specified");
-        }
-        DeleteTagsResponse result;
-        try {
-            result = ec2Client.deleteTags(builder.build());
-        } catch (AwsServiceException ase) {
-            LOG.trace("Delete tags command returned the error code {}", ase.awsErrorDetails().errorCode());
-            throw ase;
+            Collection<Tag> tags;
+            DeleteTagsRequest.Builder builder = DeleteTagsRequest.builder();
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS))) {
+                instanceIds = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_IDS, Collection.class);
+                builder.resources(instanceIds);
+            } else {
+                throw new IllegalArgumentException("Instances Ids must be specified");
+            }
+            if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS))) {
+                tags = exchange.getIn().getHeader(AWS2EC2Constants.INSTANCES_TAGS, Collection.class);
+                builder.tags(tags);
+            } else {
+                throw new IllegalArgumentException("Tags must be specified");
+            }
+            DeleteTagsResponse result;
+            try {
+                result = ec2Client.deleteTags(builder.build());
+            } catch (AwsServiceException ase) {
+                LOG.trace("Delete tags command returned the error code {}", ase.awsErrorDetails().errorCode());
+                throw ase;
+            }
+            LOG.trace("Delete tags [{}] on resources with Ids [{}] ", Arrays.toString(tags.toArray()), Arrays.toString(instanceIds.toArray()));
+            Message message = getMessageForResponse(exchange);
+            message.setBody(result);
         }
-        LOG.trace("Delete tags [{}] on resources with Ids [{}] ", Arrays.toString(tags.toArray()), Arrays.toString(instanceIds.toArray()));
-        Message message = getMessageForResponse(exchange);
-        message.setBody(result);
     }
 
     public static Message getMessageForResponse(final Exchange exchange) {