CAMEL-10325 Camel-Aws: EC2 component, add createTags and deleteTags operation
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d8489682 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d8489682 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d8489682 Branch: refs/heads/master Commit: d84896821d90b136fbf5638b437c1a89e43a16ae Parents: 8c58287 Author: Andrea Cosentino <anco...@gmail.com> Authored: Fri Sep 16 11:35:09 2016 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Fri Sep 16 11:48:41 2016 +0200 ---------------------------------------------------------------------- .../camel/component/aws/ec2/EC2Constants.java | 1 + .../camel/component/aws/ec2/EC2Operations.java | 4 +- .../camel/component/aws/ec2/EC2Producer.java | 66 ++++++++++++++++++++ .../component/aws/ec2/AmazonEC2ClientMock.java | 14 +++++ .../component/aws/ec2/EC2OperationsTest.java | 6 +- .../component/aws/ec2/EC2ProducerTest.java | 56 +++++++++++++++++ 6 files changed, 145 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d8489682/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Constants.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Constants.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Constants.java index 0a863bf..c7dfa65 100644 --- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Constants.java +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Constants.java @@ -35,4 +35,5 @@ public interface EC2Constants { String INSTANCES_KEY_PAIR = "CamelAwsEC2InstancesKeyPair"; String INSTANCES_CLIENT_TOKEN = "CamelAwsEC2InstancesClientToken"; String INSTANCES_PLACEMENT = "CamelAwsEC2InstancesPlacement"; + String INSTANCES_TAGS = "CamelAwsEC2InstancesTags"; } http://git-wip-us.apache.org/repos/asf/camel/blob/d8489682/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Operations.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Operations.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Operations.java index 2cc6968..079aada 100644 --- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Operations.java +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Operations.java @@ -26,5 +26,7 @@ public enum EC2Operations { describeInstancesStatus, rebootInstances, monitorInstances, - unmonitorInstances + unmonitorInstances, + createTags, + deleteTags } http://git-wip-us.apache.org/repos/asf/camel/blob/d8489682/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Producer.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Producer.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Producer.java index a021799..167d0f5 100644 --- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Producer.java +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Producer.java @@ -21,6 +21,10 @@ import java.util.Collection; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.ec2.AmazonEC2Client; +import com.amazonaws.services.ec2.model.CreateTagsRequest; +import com.amazonaws.services.ec2.model.CreateTagsResult; +import com.amazonaws.services.ec2.model.DeleteTagsRequest; +import com.amazonaws.services.ec2.model.DeleteTagsResult; import com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest; import com.amazonaws.services.ec2.model.DescribeInstanceStatusResult; import com.amazonaws.services.ec2.model.DescribeInstancesRequest; @@ -95,6 +99,12 @@ public class EC2Producer extends DefaultProducer { case unmonitorInstances: unmonitorInstances(getEndpoint().getEc2Client(), exchange); break; + case createTags: + createTags(getEndpoint().getEc2Client(), exchange); + break; + case deleteTags: + deleteTags(getEndpoint().getEc2Client(), exchange); + break; default: throw new IllegalArgumentException("Unsupported operation"); } @@ -360,4 +370,60 @@ public class EC2Producer extends DefaultProducer { Message message = getMessageForResponse(exchange); message.setBody(result); } + + private void createTags(AmazonEC2Client ec2Client, Exchange exchange) { + Collection instanceIds; + Collection tags; + CreateTagsRequest request = new CreateTagsRequest(); + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) { + instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class); + request.withResources(instanceIds); + } else { + throw new IllegalArgumentException("Instances Ids must be specified"); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS))) { + tags = exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS, Collection.class); + request.withTags(tags); + } else { + throw new IllegalArgumentException("Tags must be specified"); + } + CreateTagsResult result = new CreateTagsResult(); + try { + result = ec2Client.createTags(request); + } catch (AmazonServiceException ase) { + LOG.trace("Create tags command returned the error code {}", ase.getErrorCode()); + 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); + } + + private void deleteTags(AmazonEC2Client ec2Client, Exchange exchange) { + Collection instanceIds; + Collection tags; + DeleteTagsRequest request = new DeleteTagsRequest(); + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) { + instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class); + request.withResources(instanceIds); + } else { + throw new IllegalArgumentException("Instances Ids must be specified"); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS))) { + tags = exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS, Collection.class); + request.withTags(tags); + } else { + throw new IllegalArgumentException("Tags must be specified"); + } + DeleteTagsResult result = new DeleteTagsResult(); + try { + result = ec2Client.deleteTags(request); + } catch (AmazonServiceException ase) { + LOG.trace("Delete tags command returned the error code {}", ase.getErrorCode()); + 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); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/d8489682/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/AmazonEC2ClientMock.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/AmazonEC2ClientMock.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/AmazonEC2ClientMock.java index ef79de3..6c11b9c 100644 --- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/AmazonEC2ClientMock.java +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/AmazonEC2ClientMock.java @@ -23,6 +23,10 @@ import java.util.Iterator; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.ec2.AmazonEC2Client; +import com.amazonaws.services.ec2.model.CreateTagsRequest; +import com.amazonaws.services.ec2.model.CreateTagsResult; +import com.amazonaws.services.ec2.model.DeleteTagsRequest; +import com.amazonaws.services.ec2.model.DeleteTagsResult; import com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest; import com.amazonaws.services.ec2.model.DescribeInstanceStatusResult; import com.amazonaws.services.ec2.model.DescribeInstancesRequest; @@ -298,4 +302,14 @@ public class AmazonEC2ClientMock extends AmazonEC2Client { } return result; } + + @Override + public CreateTagsResult createTags(CreateTagsRequest createTagsRequest) { + return new CreateTagsResult(); + } + + @Override + public DeleteTagsResult deleteTags(DeleteTagsRequest deleteTagsRequest) { + return new DeleteTagsResult(); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/d8489682/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2OperationsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2OperationsTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2OperationsTest.java index d775ce8..47cfc40 100644 --- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2OperationsTest.java +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2OperationsTest.java @@ -24,7 +24,7 @@ public class EC2OperationsTest { @Test public void supportedOperationCount() { - assertEquals(9, EC2Operations.values().length); + assertEquals(11, EC2Operations.values().length); } @Test @@ -38,6 +38,8 @@ public class EC2OperationsTest { assertEquals(EC2Operations.rebootInstances, EC2Operations.valueOf("rebootInstances")); assertEquals(EC2Operations.monitorInstances, EC2Operations.valueOf("monitorInstances")); assertEquals(EC2Operations.unmonitorInstances, EC2Operations.valueOf("unmonitorInstances")); + assertEquals(EC2Operations.createTags, EC2Operations.valueOf("createTags")); + assertEquals(EC2Operations.deleteTags, EC2Operations.valueOf("deleteTags")); } @Test @@ -51,5 +53,7 @@ public class EC2OperationsTest { assertEquals(EC2Operations.rebootInstances.toString(), "rebootInstances"); assertEquals(EC2Operations.monitorInstances.toString(), "monitorInstances"); assertEquals(EC2Operations.unmonitorInstances.toString(), "unmonitorInstances"); + assertEquals(EC2Operations.createTags.toString(), "createTags"); + assertEquals(EC2Operations.deleteTags.toString(), "deleteTags"); } } http://git-wip-us.apache.org/repos/asf/camel/blob/d8489682/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ProducerTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ProducerTest.java index c04b400..896e2a3 100644 --- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ProducerTest.java +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ProducerTest.java @@ -19,6 +19,8 @@ package org.apache.camel.component.aws.ec2; import java.util.ArrayList; import java.util.Collection; +import com.amazonaws.services.ec2.model.CreateTagsResult; +import com.amazonaws.services.ec2.model.DeleteTagsResult; import com.amazonaws.services.ec2.model.DescribeInstanceStatusResult; import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.InstanceStateName; @@ -435,6 +437,54 @@ public class EC2ProducerTest extends CamelTestSupport { assertEquals(resultGet.getInstanceMonitorings().get(0).getMonitoring().getState(), MonitoringState.Disabled.toString()); } + @Test + public void ec2CreateTagsTest() throws Exception { + + mock.expectedMessageCount(1); + Exchange exchange = template.request("direct:createTags", new Processor() { + + @Override + public void process(Exchange exchange) throws Exception { + Collection l = new ArrayList(); + l.add("test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l); + Collection tags = new ArrayList(); + tags.add("pacific"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_TAGS, tags); + } + }); + + assertMockEndpointsSatisfied(); + + CreateTagsResult resultGet = (CreateTagsResult) exchange.getIn().getBody(); + + assertNotNull(resultGet); + } + + @Test + public void ec2DeleteTagsTest() throws Exception { + + mock.expectedMessageCount(1); + Exchange exchange = template.request("direct:deleteTags", new Processor() { + + @Override + public void process(Exchange exchange) throws Exception { + Collection l = new ArrayList(); + l.add("test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l); + Collection tags = new ArrayList(); + tags.add("pacific"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_TAGS, tags); + } + }); + + assertMockEndpointsSatisfied(); + + DeleteTagsResult resultGet = (DeleteTagsResult) exchange.getIn().getBody(); + + assertNotNull(resultGet); + } + @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); @@ -478,6 +528,12 @@ public class EC2ProducerTest extends CamelTestSupport { from("direct:unmonitor") .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=unmonitorInstances") .to("mock:result"); + from("direct:createTags") + .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=createTags") + .to("mock:result"); + from("direct:deleteTags") + .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=deleteTags") + .to("mock:result"); } }; }