Repository: camel Updated Branches: refs/heads/master 6845fd8cf -> a5b189d72
CAMEL-8825 Add support to AWS EC2 inside Camel-AWS component Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a5b189d7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a5b189d7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a5b189d7 Branch: refs/heads/master Commit: a5b189d726d2d6e2a34e9bc820337340b636828f Parents: 6845fd8 Author: Andrea Cosentino <[email protected]> Authored: Thu May 14 09:35:50 2015 +0200 Committer: Andrea Cosentino <[email protected]> Committed: Tue Jun 2 14:45:48 2015 +0200 ---------------------------------------------------------------------- .../camel/component/aws/ec2/EC2Component.java | 51 +++++ .../component/aws/ec2/EC2Configuration.java | 77 +++++++ .../camel/component/aws/ec2/EC2Constants.java | 34 +++ .../camel/component/aws/ec2/EC2Endpoint.java | 97 +++++++++ .../camel/component/aws/ec2/EC2Operations.java | 25 +++ .../camel/component/aws/ec2/EC2Producer.java | 205 +++++++++++++++++++ .../services/org/apache/camel/component/aws-ec2 | 18 ++ .../component/aws/ec2/AmazonEC2ClientMock.java | 136 ++++++++++++ .../aws/ec2/EC2ComponentConfigurationTest.java | 58 ++++++ .../aws/ec2/EC2ComponentSpringTest.java | 136 ++++++++++++ .../component/aws/ec2/EC2OperationsTest.java | 45 ++++ .../component/aws/ec2/EC2ProducerTest.java | 180 ++++++++++++++++ .../EC2ComponentIntegrationTest.java | 100 +++++++++ .../aws/ec2/EC2ComponentSpringTest-context.xml | 43 ++++ 14 files changed, 1205 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Component.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Component.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Component.java new file mode 100644 index 0000000..8378943 --- /dev/null +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Component.java @@ -0,0 +1,51 @@ +/** + * 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.ec2; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.impl.UriEndpointComponent; + +/** + * Defines the <a href="http://aws.amazon.com/ec2/">AWS EC2 Component</a> + */ +public class EC2Component extends UriEndpointComponent { + + public EC2Component() { + super(EC2Endpoint.class); + } + + public EC2Component(CamelContext context) { + super(context, EC2Endpoint.class); + } + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + EC2Configuration configuration = new EC2Configuration(); + setProperties(configuration, parameters); + + if (configuration.getAmazonEc2Client() == null && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) { + throw new IllegalArgumentException("amazonEC2Client or accessKey and secretKey must be specified"); + } + + EC2Endpoint endpoint = new EC2Endpoint(uri, this, configuration); + return endpoint; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Configuration.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Configuration.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Configuration.java new file mode 100644 index 0000000..67e3941 --- /dev/null +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Configuration.java @@ -0,0 +1,77 @@ +/** + * 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.ec2; + +import com.amazonaws.services.ec2.AmazonEC2Client; + +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriParams; + +@UriParams +public class EC2Configuration { + + @UriParam + private AmazonEC2Client amazonEc2Client; + @UriParam + private String accessKey; + @UriParam + private String secretKey; + @UriParam + private String amazonEc2Endpoint; + @UriParam + private EC2Operations operation; + + public AmazonEC2Client getAmazonEc2Client() { + return amazonEc2Client; + } + + public void setAmazonEc2Client(AmazonEC2Client amazonEc2Client) { + this.amazonEc2Client = amazonEc2Client; + } + + public String getAccessKey() { + return accessKey; + } + + public void setAccessKey(String accessKey) { + this.accessKey = accessKey; + } + + public String getSecretKey() { + return secretKey; + } + + public void setSecretKey(String secretKey) { + this.secretKey = secretKey; + } + + public String getAmazonEc2Endpoint() { + return amazonEc2Endpoint; + } + + public void setAmazonEc2Endpoint(String amazonEc2Endpoint) { + this.amazonEc2Endpoint = amazonEc2Endpoint; + } + + public EC2Operations getOperation() { + return operation; + } + + public void setOperation(EC2Operations operation) { + this.operation = operation; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/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 new file mode 100644 index 0000000..a929e44 --- /dev/null +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Constants.java @@ -0,0 +1,34 @@ +/** + * 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.ec2; + +/** + * Constants used in Camel AWS EC2 module + * + */ +public interface EC2Constants { + + String IMAGE_ID = "CamelAwsEC2ImageId"; + String INSTANCE_TYPE = "CamelAwsEC2InstanceType"; + String OPERATION = "CamelAwsEC2Operation"; + String INSTANCE_MIN_COUNT = "CamelAwsEC2InstanceMinCount"; + String INSTANCE_MAX_COUNT = "CamelAwsEC2InstanceMaxCount"; + String INSTANCE_MONITORING = "CamelAwsEC2InstanceMonitoring"; + String INSTANCE_KERNEL_ID = "CamelAwsEC2InstanceKernelId"; + String INSTANCE_EBS_OPTIMIZED = "CamelAwsEC2InstanceEbsOptimized"; + String INSTANCES_IDS = "CamelAwsEC2InstancesIds"; +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Endpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Endpoint.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Endpoint.java new file mode 100644 index 0000000..a47a9ac --- /dev/null +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Endpoint.java @@ -0,0 +1,97 @@ +/** + * 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.ec2; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.ec2.AmazonEC2Client; +import com.amazonaws.services.ec2.model.DescribeImagesRequest; +import com.amazonaws.services.ec2.model.InstanceType; +import com.amazonaws.services.ec2.model.RunInstancesRequest; +import com.amazonaws.services.simpledb.AmazonSimpleDB; +import com.amazonaws.services.simpledb.AmazonSimpleDBClient; +import com.amazonaws.services.simpledb.model.CreateDomainRequest; +import com.amazonaws.services.simpledb.model.DomainMetadataRequest; +import com.amazonaws.services.simpledb.model.NoSuchDomainException; + +import org.apache.camel.CamelContext; +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.aws.s3.S3Endpoint; +import org.apache.camel.impl.ScheduledPollEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Defines the <a href="http://camel.apache.org/aws.html">AWS EC2 Endpoint</a>. + */ +@UriEndpoint(scheme = "aws-ec2", title = "AWS EC2", syntax = "aws-ec2:label", producerOnly = true, label = "cloud") +public class EC2Endpoint extends ScheduledPollEndpoint { + + private static final Logger LOG = LoggerFactory.getLogger(EC2Endpoint.class); + + private AmazonEC2Client ec2Client; + + @UriParam + private EC2Configuration configuration; + + public EC2Endpoint(String uri, Component component, EC2Configuration configuration) { + super(uri, component); + this.configuration = configuration; + } + + public Consumer createConsumer(Processor processor) throws Exception { + throw new UnsupportedOperationException("You cannot receive messages from this endpoint"); + } + + public Producer createProducer() throws Exception { + return new EC2Producer(this); + } + + public boolean isSingleton() { + return true; + } + + @Override + public void doStart() throws Exception { + super.doStart(); + + ec2Client = configuration.getAmazonEc2Client() != null ? configuration.getAmazonEc2Client() : createEc2Client(); + if (ObjectHelper.isNotEmpty(configuration.getAmazonEc2Endpoint())) { + ec2Client.setEndpoint(configuration.getAmazonEc2Endpoint()); + } + } + + public EC2Configuration getConfiguration() { + return configuration; + } + + public AmazonEC2Client getEc2Client() { + return ec2Client; + } + + AmazonEC2Client createEc2Client() { + AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey()); + AmazonEC2Client client = new AmazonEC2Client(credentials); + return client; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/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 new file mode 100644 index 0000000..d3d7af1 --- /dev/null +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Operations.java @@ -0,0 +1,25 @@ +/** + * 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.ec2; + +public enum EC2Operations { + + createAndRunInstances, + startInstances, + stopInstances, + terminateInstances +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/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 new file mode 100644 index 0000000..9ad9bf9 --- /dev/null +++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ec2/EC2Producer.java @@ -0,0 +1,205 @@ +/** + * 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.ec2; + +import java.util.Collection; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.services.ec2.AmazonEC2Client; +import com.amazonaws.services.ec2.model.InstanceType; +import com.amazonaws.services.ec2.model.RunInstancesRequest; +import com.amazonaws.services.ec2.model.RunInstancesResult; +import com.amazonaws.services.ec2.model.StartInstancesRequest; +import com.amazonaws.services.ec2.model.StartInstancesResult; +import com.amazonaws.services.ec2.model.StopInstancesRequest; +import com.amazonaws.services.ec2.model.StopInstancesResult; +import com.amazonaws.services.ec2.model.TerminateInstancesRequest; +import com.amazonaws.services.ec2.model.TerminateInstancesResult; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.URISupport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Producer which sends messages to the Amazon EC2 Service + * <a href="http://aws.amazon.com/ec2/">AWS EC2</a> + */ +public class EC2Producer extends DefaultProducer { + + private static final Logger LOG = LoggerFactory.getLogger(EC2Producer.class); + + public EC2Producer(Endpoint endpoint) { + super(endpoint); + } + + public void process(Exchange exchange) throws Exception { + switch (determineOperation(exchange)) { + case createAndRunInstances: + createAndRunInstance(getEndpoint().getEc2Client(), exchange); + break; + case startInstances: + startInstances(getEndpoint().getEc2Client(), exchange); + break; + case stopInstances: + stopInstances(getEndpoint().getEc2Client(), exchange); + break; + case terminateInstances: + terminateInstances(getEndpoint().getEc2Client(), exchange); + break; + default: + throw new IllegalArgumentException("Unsupported operation"); + } + } + + private EC2Operations determineOperation(Exchange exchange) { + EC2Operations operation = exchange.getIn().getHeader(EC2Constants.OPERATION, EC2Operations.class); + if (operation == null) { + operation = getConfiguration().getOperation(); + } + return operation; + } + + protected EC2Configuration getConfiguration() { + return getEndpoint().getConfiguration(); + } + + @Override + public String toString() { + return "EC2Producer[" + URISupport.sanitizeUri(getEndpoint().getEndpointUri()) + "]"; + } + + @Override + public EC2Endpoint getEndpoint() { + return (EC2Endpoint) super.getEndpoint(); + } + + private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) { + String ami; + InstanceType instanceType; + int minCount; + int maxCount; + boolean monitoring; + String kernelId; + boolean ebsOptimized; + RunInstancesRequest request = new RunInstancesRequest(); + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) { + ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class); + request.withImageId(ami); + } else { + throw new IllegalArgumentException("AMI must be specified"); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) { + instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class); + request.withInstanceType(instanceType.toString()); + } else { + throw new IllegalArgumentException("Instance Type must be specified"); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) { + minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class); + request.withMinCount(minCount); + } else { + throw new IllegalArgumentException("Min instances count must be specified"); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) { + maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class); + request.withMaxCount(maxCount); + } else { + throw new IllegalArgumentException("Max instances count must be specified"); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) { + monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class); + request.withMonitoring(monitoring); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) { + kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class); + request.withKernelId(kernelId); + } + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) { + ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class); + request.withEbsOptimized(ebsOptimized); + } + RunInstancesResult result; + try { + result = ec2Client.runInstances(request); + } catch (AmazonServiceException ase) { + LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode()); + throw ase; + } + exchange.getIn().setBody(result); + } + + private void startInstances(AmazonEC2Client ec2Client, Exchange exchange) { + Collection instanceIds; + StartInstancesRequest request = new StartInstancesRequest(); + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) { + instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class); + request.withInstanceIds(instanceIds); + } else { + throw new IllegalArgumentException("Instances Ids must be specified"); + } + StartInstancesResult result; + try { + result = ec2Client.startInstances(request); + } catch (AmazonServiceException ase) { + LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode()); + throw ase; + } + exchange.getIn().setBody(result); + } + + private void stopInstances(AmazonEC2Client ec2Client, Exchange exchange) { + Collection instanceIds; + StopInstancesRequest request = new StopInstancesRequest(); + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) { + instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class); + request.withInstanceIds(instanceIds); + } else { + throw new IllegalArgumentException("Instances Ids must be specified"); + } + StopInstancesResult result; + try { + result = ec2Client.stopInstances(request); + } catch (AmazonServiceException ase) { + LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode()); + throw ase; + } + exchange.getIn().setBody(result); + } + + private void terminateInstances(AmazonEC2Client ec2Client, Exchange exchange) { + Collection instanceIds; + TerminateInstancesRequest request = new TerminateInstancesRequest(); + if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) { + instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class); + request.withInstanceIds(instanceIds); + } else { + throw new IllegalArgumentException("Instances Ids must be specified"); + } + TerminateInstancesResult result; + try { + result = ec2Client.terminateInstances(request); + } catch (AmazonServiceException ase) { + LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode()); + throw ase; + } + exchange.getIn().setBody(result); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-ec2 ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-ec2 b/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-ec2 new file mode 100644 index 0000000..9a2bcfa --- /dev/null +++ b/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-ec2 @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +class=org.apache.camel.component.aws.ec2.EC2Component http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/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 new file mode 100644 index 0000000..bf8db56 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/AmazonEC2ClientMock.java @@ -0,0 +1,136 @@ +/** + * 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.ec2; + +import java.util.ArrayList; +import java.util.Collection; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.ec2.AmazonEC2Client; +import com.amazonaws.services.ec2.model.Instance; +import com.amazonaws.services.ec2.model.InstanceState; +import com.amazonaws.services.ec2.model.InstanceStateChange; +import com.amazonaws.services.ec2.model.InstanceStateName; +import com.amazonaws.services.ec2.model.Reservation; +import com.amazonaws.services.ec2.model.RunInstancesRequest; +import com.amazonaws.services.ec2.model.RunInstancesResult; +import com.amazonaws.services.ec2.model.StartInstancesRequest; +import com.amazonaws.services.ec2.model.StartInstancesResult; +import com.amazonaws.services.ec2.model.StopInstancesRequest; +import com.amazonaws.services.ec2.model.StopInstancesResult; +import com.amazonaws.services.ec2.model.TerminateInstancesRequest; +import com.amazonaws.services.ec2.model.TerminateInstancesResult; + +public class AmazonEC2ClientMock extends AmazonEC2Client { + + public AmazonEC2ClientMock() { + super(new BasicAWSCredentials("user", "secret")); + } + + @Override + public RunInstancesResult runInstances(RunInstancesRequest runInstancesRequest) { + RunInstancesResult result = new RunInstancesResult(); + if (runInstancesRequest.getImageId().equals("test-1")) { + Reservation res = new Reservation(); + res.setOwnerId("1"); + res.setRequesterId("user-test"); + res.setReservationId("res-1"); + Collection<Instance> instances = new ArrayList(); + Instance ins = new Instance(); + ins.setImageId(runInstancesRequest.getImageId()); + ins.setInstanceType(runInstancesRequest.getInstanceType()); + ins.setInstanceId("instance-1"); + instances.add(ins); + res.setInstances(instances); + result.setReservation(res); + } else { + throw new AmazonServiceException("The image-id doesn't exists"); + } + return result; + + } + + @Override + public StartInstancesResult startInstances(StartInstancesRequest startInstancesRequest) { + StartInstancesResult result = new StartInstancesResult(); + if (startInstancesRequest.getInstanceIds().get(0).equals("test-1")) { + Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>(); + InstanceStateChange sc = new InstanceStateChange(); + InstanceState previousState = new InstanceState(); + previousState.setCode(80); + previousState.setName(InstanceStateName.Stopped); + InstanceState newState = new InstanceState(); + newState.setCode(16); + newState.setName(InstanceStateName.Running); + sc.setPreviousState(previousState); + sc.setCurrentState(newState); + sc.setInstanceId("test-1"); + coll.add(sc); + result.setStartingInstances(coll); + } else { + throw new AmazonServiceException("The image-id doesn't exists"); + } + return result; + } + + @Override + public StopInstancesResult stopInstances(StopInstancesRequest stopInstancesRequest) { + StopInstancesResult result = new StopInstancesResult(); + if (stopInstancesRequest.getInstanceIds().get(0).equals("test-1")) { + Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>(); + InstanceStateChange sc = new InstanceStateChange(); + InstanceState previousState = new InstanceState(); + previousState.setCode(80); + previousState.setName(InstanceStateName.Running); + InstanceState newState = new InstanceState(); + newState.setCode(16); + newState.setName(InstanceStateName.Stopped); + sc.setPreviousState(previousState); + sc.setCurrentState(newState); + sc.setInstanceId("test-1"); + coll.add(sc); + result.setStoppingInstances(coll); + } else { + throw new AmazonServiceException("The image-id doesn't exists"); + } + return result; + } + + @Override + public TerminateInstancesResult terminateInstances(TerminateInstancesRequest terminateInstancesRequest) { + TerminateInstancesResult result = new TerminateInstancesResult(); + if (terminateInstancesRequest.getInstanceIds().contains("test-1")) { + Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>(); + InstanceStateChange sc = new InstanceStateChange(); + InstanceState previousState = new InstanceState(); + previousState.setCode(80); + previousState.setName(InstanceStateName.Running); + InstanceState newState = new InstanceState(); + newState.setCode(16); + newState.setName(InstanceStateName.Terminated); + sc.setPreviousState(previousState); + sc.setCurrentState(newState); + sc.setInstanceId("test-1"); + coll.add(sc); + result.setTerminatingInstances(coll); + } else { + throw new AmazonServiceException("The image-id doesn't exists"); + } + return result; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentConfigurationTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentConfigurationTest.java new file mode 100644 index 0000000..a7c441c --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentConfigurationTest.java @@ -0,0 +1,58 @@ +/** + * 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.ec2; + +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class EC2ComponentConfigurationTest extends CamelTestSupport { + + @Test + public void createEndpointWithMinimalConfiguration() throws Exception { + EC2Component component = new EC2Component(context); + EC2Endpoint endpoint = (EC2Endpoint) component.createEndpoint( + "aws-ec2://TestDomain?accessKey=xxx&secretKey=yyy"); + + assertEquals("xxx", endpoint.getConfiguration().getAccessKey()); + assertEquals("yyy", endpoint.getConfiguration().getSecretKey()); + assertNull(endpoint.getConfiguration().getAmazonEc2Client()); + } + + @Test(expected = IllegalArgumentException.class) + public void createEndpointWithoutDomainName() throws Exception { + EC2Component component = new EC2Component(context); + component.createEndpoint("aws-ec2:// "); + } + + @Test(expected = IllegalArgumentException.class) + public void createEndpointWithoutAmazonSDBClientConfiguration() throws Exception { + EC2Component component = new EC2Component(context); + component.createEndpoint("aws-ec2://TestDomain"); + } + + @Test(expected = IllegalArgumentException.class) + public void createEndpointWithoutAccessKeyConfiguration() throws Exception { + EC2Component component = new EC2Component(context); + component.createEndpoint("aws-ec2://TestDomain?secretKey=yyy"); + } + + @Test(expected = IllegalArgumentException.class) + public void createEndpointWithoutSecretKeyConfiguration() throws Exception { + EC2Component component = new EC2Component(context); + component.createEndpoint("aws-ec2://TestDomain?accessKey=xxx"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest.java new file mode 100644 index 0000000..82b3484 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest.java @@ -0,0 +1,136 @@ +/** + * 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.ec2; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import com.amazonaws.services.ec2.model.InstanceStateName; +import com.amazonaws.services.ec2.model.InstanceType; +import com.amazonaws.services.ec2.model.RunInstancesResult; +import com.amazonaws.services.ec2.model.StartInstancesResult; +import com.amazonaws.services.ec2.model.StopInstancesResult; +import com.amazonaws.services.ec2.model.TerminateInstancesResult; +import com.amazonaws.services.simpledb.model.Attribute; +import com.amazonaws.services.simpledb.model.DeletableItem; +import com.amazonaws.services.simpledb.model.Item; +import com.amazonaws.services.simpledb.model.ReplaceableAttribute; +import com.amazonaws.services.simpledb.model.ReplaceableItem; +import com.amazonaws.services.simpledb.model.UpdateCondition; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.impl.DefaultProducerTemplate; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class EC2ComponentSpringTest extends CamelSpringTestSupport { + + private AmazonEC2ClientMock amazonEc2Client; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + + amazonEc2Client = context.getRegistry().lookupByNameAndType("amazonEC2Client", AmazonEC2ClientMock.class); + } + + @Test + public void createAndRunInstances() { + + Exchange exchange = template.request("direct:createAndRun", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(EC2Constants.OPERATION, EC2Operations.createAndRunInstances); + exchange.getIn().setHeader(EC2Constants.IMAGE_ID, "test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MIN_COUNT, 1); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MAX_COUNT, 1); + } + }); + + RunInstancesResult resultGet = (RunInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getReservation().getInstances().get(0).getImageId(), "test-1"); + assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceType(), InstanceType.T2Micro.toString()); + assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceId(), "instance-1"); + } + + @Test + public void startInstances() { + + Exchange exchange = template.request("direct:start", 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); + } + }); + + StartInstancesResult resultGet = (StartInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getStartingInstances().get(0).getInstanceId(), "test-1"); + assertEquals(resultGet.getStartingInstances().get(0).getPreviousState().getName(), InstanceStateName.Stopped.toString()); + assertEquals(resultGet.getStartingInstances().get(0).getCurrentState().getName(), InstanceStateName.Running.toString()); + } + + @Test + public void stopInstances() { + + Exchange exchange = template.request("direct:stop", 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); + } + }); + + StopInstancesResult resultGet = (StopInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getStoppingInstances().get(0).getInstanceId(), "test-1"); + assertEquals(resultGet.getStoppingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString()); + assertEquals(resultGet.getStoppingInstances().get(0).getCurrentState().getName(), InstanceStateName.Stopped.toString()); + } + + @Test + public void terminateInstances() { + + Exchange exchange = template.request("direct:terminate", 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); + } + }); + + TerminateInstancesResult resultGet = (TerminateInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getTerminatingInstances().get(0).getInstanceId(), "test-1"); + assertEquals(resultGet.getTerminatingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString()); + assertEquals(resultGet.getTerminatingInstances().get(0).getCurrentState().getName(), InstanceStateName.Terminated.toString()); + } + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext( + "org/apache/camel/component/aws/ec2/EC2ComponentSpringTest-context.xml"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/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 new file mode 100644 index 0000000..7cb3bdb --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2OperationsTest.java @@ -0,0 +1,45 @@ +/** + * 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.ec2; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class EC2OperationsTest { + + @Test + public void supportedOperationCount() { + assertEquals(4, EC2Operations.values().length); + } + + @Test + public void valueOf() { + assertEquals(EC2Operations.createAndRunInstances, EC2Operations.valueOf("createAndRunInstances")); + assertEquals(EC2Operations.startInstances, EC2Operations.valueOf("startInstances")); + assertEquals(EC2Operations.stopInstances, EC2Operations.valueOf("stopInstances")); + assertEquals(EC2Operations.terminateInstances, EC2Operations.valueOf("terminateInstances")); + } + + @Test + public void testToString() { + assertEquals(EC2Operations.createAndRunInstances.toString(), "createAndRunInstances"); + assertEquals(EC2Operations.startInstances.toString(), "startInstances"); + assertEquals(EC2Operations.stopInstances.toString(), "stopInstances"); + assertEquals(EC2Operations.terminateInstances.toString(), "terminateInstances"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/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 new file mode 100644 index 0000000..c0dd4af --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/EC2ProducerTest.java @@ -0,0 +1,180 @@ +/** + * 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.ec2; + +import java.util.ArrayList; +import java.util.Collection; + +import com.amazonaws.services.ec2.model.InstanceStateName; +import com.amazonaws.services.ec2.model.InstanceType; +import com.amazonaws.services.ec2.model.RunInstancesResult; +import com.amazonaws.services.ec2.model.StartInstancesResult; +import com.amazonaws.services.ec2.model.StopInstancesResult; +import com.amazonaws.services.ec2.model.TerminateInstancesResult; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class EC2ProducerTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:result") + private MockEndpoint mock; + + @Test + public void ec2CreateAndRunTest() throws Exception { + + mock.expectedMessageCount(1); + Exchange exchange = template.request("direct:createAndRun", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(EC2Constants.OPERATION, EC2Operations.createAndRunInstances); + exchange.getIn().setHeader(EC2Constants.IMAGE_ID, "test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MIN_COUNT, 1); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MAX_COUNT, 1); + } + }); + + assertMockEndpointsSatisfied(); + + RunInstancesResult resultGet = (RunInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getReservation().getInstances().get(0).getImageId(), "test-1"); + assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceType(), InstanceType.T2Micro.toString()); + assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceId(), "instance-1"); + } + + @Test + public void ec2CreateAndRunKoTest() throws Exception { + + mock.expectedMessageCount(0); + Exchange exchange = template.request("direct:createAndRun", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(EC2Constants.OPERATION, EC2Operations.createAndRunInstances); + exchange.getIn().setHeader(EC2Constants.IMAGE_ID, "test-2"); + exchange.getIn().setHeader(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MIN_COUNT, 1); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MAX_COUNT, 1); + } + }); + + assertMockEndpointsSatisfied(); + } + + + @Test + public void ec2StartTest() throws Exception { + + mock.expectedMessageCount(1); + Exchange exchange = template.request("direct:start", 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); + } + }); + + assertMockEndpointsSatisfied(); + + StartInstancesResult resultGet = (StartInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getStartingInstances().get(0).getInstanceId(), "test-1"); + assertEquals(resultGet.getStartingInstances().get(0).getPreviousState().getName(), InstanceStateName.Stopped.toString()); + assertEquals(resultGet.getStartingInstances().get(0).getCurrentState().getName(), InstanceStateName.Running.toString()); + } + + @Test + public void ec2StopTest() throws Exception { + + mock.expectedMessageCount(1); + Exchange exchange = template.request("direct:stop", 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); + } + }); + + assertMockEndpointsSatisfied(); + + StopInstancesResult resultGet = (StopInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getStoppingInstances().get(0).getInstanceId(), "test-1"); + assertEquals(resultGet.getStoppingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString()); + assertEquals(resultGet.getStoppingInstances().get(0).getCurrentState().getName(), InstanceStateName.Stopped.toString()); + } + + + @Test + public void ec2TerminateTest() throws Exception { + + mock.expectedMessageCount(1); + Exchange exchange = template.request("direct:terminate", 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); + } + }); + + assertMockEndpointsSatisfied(); + + TerminateInstancesResult resultGet = (TerminateInstancesResult) exchange.getIn().getBody(); + assertEquals(resultGet.getTerminatingInstances().get(0).getInstanceId(), "test-1"); + assertEquals(resultGet.getTerminatingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString()); + assertEquals(resultGet.getTerminatingInstances().get(0).getCurrentState().getName(), InstanceStateName.Terminated.toString()); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + + AmazonEC2ClientMock clientMock = new AmazonEC2ClientMock(); + + registry.bind("amazonEc2Client", clientMock); + + return registry; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:createAndRun") + .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=createAndRunInstances") + .to("mock:result"); + from("direct:start") + .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=startInstances") + .to("mock:result"); + from("direct:stop") + .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=stopInstances") + .to("mock:result"); + from("direct:terminate") + .to("aws-ec2://test?amazonEc2Client=#amazonEc2Client&operation=terminateInstances") + .to("mock:result"); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/integration/EC2ComponentIntegrationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/integration/EC2ComponentIntegrationTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/integration/EC2ComponentIntegrationTest.java new file mode 100644 index 0000000..79feb55 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ec2/integration/EC2ComponentIntegrationTest.java @@ -0,0 +1,100 @@ +/** + * 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.ec2.integration; + +import java.util.ArrayList; +import java.util.Collection; + +import com.amazonaws.services.ec2.model.InstanceType; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.aws.ec2.EC2Constants; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore("Must be manually tested. Provide your own accessKey and secretKey!") +public class EC2ComponentIntegrationTest extends CamelTestSupport { + + @Test + public void createAndRunInstancesTest() { + + template.send("direct:createAndRun", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(EC2Constants.IMAGE_ID, "ami-fd65ba94"); + exchange.getIn().setHeader(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MIN_COUNT, 1); + exchange.getIn().setHeader(EC2Constants.INSTANCE_MAX_COUNT, 1); + } + }); + } + + @Test + public void stopInstances() { + + template.send("direct:stop", new Processor() { + public void process(Exchange exchange) throws Exception { + Collection l = new ArrayList(); + l.add("test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l); + } + }); + } + + @Test + public void startInstances() { + + template.send("direct:start", new Processor() { + public void process(Exchange exchange) throws Exception { + Collection l = new ArrayList(); + l.add("test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l); + } + }); + } + + @Test + public void terminateInstances() { + + template.send("direct:terminate", new Processor() { + public void process(Exchange exchange) throws Exception { + Collection l = new ArrayList(); + l.add("test-1"); + exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l); + } + }); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:createAndRun") + .to("aws-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=createAndRunInstances"); + from("direct:stop") + .to("aws-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=stopInstances"); + from("direct:start") + .to("aws-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=startInstances"); + from("direct:terminate") + .to("aws-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=terminateInstances"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a5b189d7/components/camel-aws/src/test/resources/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/resources/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest-context.xml b/components/camel-aws/src/test/resources/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest-context.xml new file mode 100644 index 0000000..af0042d --- /dev/null +++ b/components/camel-aws/src/test/resources/org/apache/camel/component/aws/ec2/EC2ComponentSpringTest-context.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:createAndRun"/> + <to uri="aws-ec2://Test?amazonEc2Client=#amazonEc2Client&operation=createAndRunInstances"/> + </route> + <route> + <from uri="direct:start"/> + <to uri="aws-ec2://Test?amazonEc2Client=#amazonEc2Client&operation=startInstances"/> + </route> + <route> + <from uri="direct:stop"/> + <to uri="aws-ec2://Test?amazonEc2Client=#amazonEc2Client&operation=stopInstances"/> + </route> + <route> + <from uri="direct:terminate"/> + <to uri="aws-ec2://Test?amazonEc2Client=#amazonEc2Client&operation=terminateInstances"/> + </route> + </camelContext> + + <bean id="amazonEc2Client" class="org.apache.camel.component.aws.ec2.AmazonEC2ClientMock"/> +</beans> \ No newline at end of file
