This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 1a6a807 CAMEL-12150 - Camel-AWS EC2: Add the ability to specify credentials and region at component level 1a6a807 is described below commit 1a6a807c0f0cc0678ef83ef7f1c839283189879a Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Wed Jan 17 11:15:00 2018 +0100 CAMEL-12150 - Camel-AWS EC2: Add the ability to specify credentials and region at component level --- .../camel-aws/src/main/docs/aws-ec2-component.adoc | 14 +- .../camel/component/aws/ec2/EC2Component.java | 67 +++++++++- .../camel/component/aws/ec2/EC2Configuration.java | 15 ++- .../aws/ec2/EC2ComponentConfigurationTest.java | 25 ++++ .../ec2/springboot/EC2ComponentConfiguration.java | 147 +++++++++++++++++++++ 5 files changed, 265 insertions(+), 3 deletions(-) diff --git a/components/camel-aws/src/main/docs/aws-ec2-component.adoc b/components/camel-aws/src/main/docs/aws-ec2-component.adoc index 897fec9..db18894 100644 --- a/components/camel-aws/src/main/docs/aws-ec2-component.adoc +++ b/components/camel-aws/src/main/docs/aws-ec2-component.adoc @@ -25,7 +25,19 @@ You can append query options to the URI in the following format, // component options: START -The AWS EC2 component has no options. +The AWS EC2 component supports 5 options which are listed below. + + + +[width="100%",cols="2,5,^1,2",options="header"] +|=== +| Name | Description | Default | Type +| *configuration* (advanced) | The AWS EC2 default configuration | | EC2Configuration +| *region* (producer) | The region in which EC2 client needs to work | | String +| *accessKey* (producer) | Amazon AWS Access Key | | String +| *secretKey* (producer) | Amazon AWS Secret Key | | String +| *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean +|=== // component options: END 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 index c2df816..ba16ab9 100644 --- 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 @@ -21,12 +21,23 @@ import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.impl.DefaultComponent; +import org.apache.camel.spi.Metadata; +import org.apache.camel.util.ObjectHelper; /** * For working with Amazon's Elastic Compute Cloud (EC2). */ public class EC2Component extends DefaultComponent { + @Metadata + private String accessKey; + @Metadata + private String secretKey; + @Metadata + private String region; + @Metadata(label = "advanced") + private EC2Configuration configuration; + public EC2Component() { this(null); } @@ -34,14 +45,24 @@ public class EC2Component extends DefaultComponent { public EC2Component(CamelContext context) { super(context); + this.configuration = new EC2Configuration(); registerExtension(new EC2ComponentVerifierExtension()); } @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - EC2Configuration configuration = new EC2Configuration(); + EC2Configuration configuration = this.configuration.copy(); setProperties(configuration, parameters); + if (ObjectHelper.isEmpty(configuration.getAccessKey())) { + setAccessKey(accessKey); + } + if (ObjectHelper.isEmpty(configuration.getSecretKey())) { + setSecretKey(secretKey); + } + if (ObjectHelper.isEmpty(configuration.getRegion())) { + setRegion(region); + } if (configuration.getAmazonEc2Client() == null && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) { throw new IllegalArgumentException("amazonEC2Client or accessKey and secretKey must be specified"); } @@ -49,5 +70,49 @@ public class EC2Component extends DefaultComponent { EC2Endpoint endpoint = new EC2Endpoint(uri, this, configuration); return endpoint; } + + public EC2Configuration getConfiguration() { + return configuration; + } + + /** + * The AWS EC2 default configuration + */ + public void setConfiguration(EC2Configuration configuration) { + this.configuration = configuration; + } + + /** + * The region in which EC2 client needs to work + */ + public String getRegion() { + return configuration.getRegion(); + } + + public void setRegion(String region) { + configuration.setRegion(region); + } + + public String getAccessKey() { + return configuration.getAccessKey(); + } + + /** + * Amazon AWS Access Key + */ + public void setAccessKey(String accessKey) { + configuration.setAccessKey(accessKey); + } + + public String getSecretKey() { + return configuration.getSecretKey(); + } + + /** + * Amazon AWS Secret Key + */ + public void setSecretKey(String secretKey) { + configuration.setSecretKey(secretKey); + } } 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 index acc437f..ad8bd03 100644 --- 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 @@ -18,13 +18,14 @@ package org.apache.camel.component.aws.ec2; import com.amazonaws.services.ec2.AmazonEC2Client; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; import org.apache.camel.spi.UriPath; @UriParams -public class EC2Configuration { +public class EC2Configuration implements Cloneable { @UriPath(description = "Logical name") @Metadata(required = "true") private String label; @@ -135,4 +136,16 @@ public class EC2Configuration { public void setRegion(String region) { this.region = region; } + + // ************************************************* + // + // ************************************************* + + public EC2Configuration copy() { + try { + return (EC2Configuration)super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeCamelException(e); + } + } } 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 index ac7da91..d20fd27 100644 --- 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 @@ -16,6 +16,7 @@ */ package org.apache.camel.component.aws.ec2; +import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2Client; import org.apache.camel.impl.JndiRegistry; @@ -77,6 +78,30 @@ public class EC2ComponentConfigurationTest extends CamelTestSupport { component.createEndpoint("aws-ec2://TestDomain?amazonEc2Client=#amazonEc2Client"); } + @Test + public void createEndpointWithComponentElements() throws Exception { + EC2Component component = new EC2Component(context); + component.setAccessKey("XXX"); + component.setSecretKey("YYY"); + EC2Endpoint endpoint = (EC2Endpoint)component.createEndpoint("aws-ec2://testDomain"); + + assertEquals("XXX", endpoint.getConfiguration().getAccessKey()); + assertEquals("YYY", endpoint.getConfiguration().getSecretKey()); + } + + @Test + public void createEndpointWithComponentAndEndpointElements() throws Exception { + EC2Component component = new EC2Component(context); + component.setAccessKey("XXX"); + component.setSecretKey("YYY"); + component.setRegion(Regions.US_WEST_1.toString()); + EC2Endpoint endpoint = (EC2Endpoint)component.createEndpoint("aws-s3://testDomain?accessKey=xxxxxx&secretKey=yyyyy®ion=US_EAST_1"); + + assertEquals("xxxxxx", endpoint.getConfiguration().getAccessKey()); + assertEquals("yyyyy", endpoint.getConfiguration().getSecretKey()); + assertEquals("US_EAST_1", endpoint.getConfiguration().getRegion()); + } + @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); diff --git a/platforms/spring-boot/components-starter/camel-aws-starter/src/main/java/org/apache/camel/component/aws/ec2/springboot/EC2ComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-aws-starter/src/main/java/org/apache/camel/component/aws/ec2/springboot/EC2ComponentConfiguration.java index 6dcbd07..c4896dd 100644 --- a/platforms/spring-boot/components-starter/camel-aws-starter/src/main/java/org/apache/camel/component/aws/ec2/springboot/EC2ComponentConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-aws-starter/src/main/java/org/apache/camel/component/aws/ec2/springboot/EC2ComponentConfiguration.java @@ -17,8 +17,11 @@ package org.apache.camel.component.aws.ec2.springboot; import javax.annotation.Generated; +import com.amazonaws.services.ec2.AmazonEC2Client; +import org.apache.camel.component.aws.ec2.EC2Operations; import org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; /** * The aws-ec2 is used for managing Amazon EC2 instances. @@ -32,12 +35,61 @@ public class EC2ComponentConfiguration ComponentConfigurationPropertiesCommon { /** + * The AWS EC2 default configuration + */ + private EC2ConfigurationNestedConfiguration configuration; + /** + * The region in which EC2 client needs to work + */ + private String region; + /** + * Amazon AWS Access Key + */ + private String accessKey; + /** + * Amazon AWS Secret Key + */ + private String secretKey; + /** * Whether the component should resolve property placeholders on itself when * starting. Only properties which are of String type can use property * placeholders. */ private Boolean resolvePropertyPlaceholders = true; + public EC2ConfigurationNestedConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration( + EC2ConfigurationNestedConfiguration configuration) { + this.configuration = configuration; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + + 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 Boolean getResolvePropertyPlaceholders() { return resolvePropertyPlaceholders; } @@ -46,4 +98,99 @@ public class EC2ComponentConfiguration Boolean resolvePropertyPlaceholders) { this.resolvePropertyPlaceholders = resolvePropertyPlaceholders; } + + public static class EC2ConfigurationNestedConfiguration { + public static final Class CAMEL_NESTED_CLASS = org.apache.camel.component.aws.ec2.EC2Configuration.class; + /** + * To use a existing configured AmazonEC2Client as client + */ + @NestedConfigurationProperty + private AmazonEC2Client amazonEc2Client; + /** + * Amazon AWS Access Key + */ + private String accessKey; + /** + * Amazon AWS Secret Key + */ + private String secretKey; + /** + * The region with which the AWS-EC2 client wants to work with. + */ + private String amazonEc2Endpoint; + /** + * The operation to perform. It can be createAndRunInstances, + * startInstances, stopInstances, terminateInstances, describeInstances, + * describeInstancesStatus, rebootInstances, monitorInstances, + * unmonitorInstances, createTags or deleteTags + */ + private EC2Operations operation; + private String proxyHost; + private Integer proxyPort; + private String region; + + 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; + } + + public String getProxyHost() { + return proxyHost; + } + + public void setProxyHost(String proxyHost) { + this.proxyHost = proxyHost; + } + + public Integer getProxyPort() { + return proxyPort; + } + + public void setProxyPort(Integer proxyPort) { + this.proxyPort = proxyPort; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + } } \ No newline at end of file -- To stop receiving notification emails like this one, please contact ['"commits@camel.apache.org" <commits@camel.apache.org>'].