various cleanups to get ec2 subnet api up to latest

This commit is contained in:
Adrian Cole 2013-02-24 14:40:31 -08:00
parent 3c6a2b4318
commit 7e270c5ddb
12 changed files with 445 additions and 423 deletions

View File

@ -24,22 +24,17 @@ import java.util.Map;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
/**
* Amazon EC2 VPCs contain one or more subnets.
*
* @see <a
* href="http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html"
* >doc</a>
* @see <a href="http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html" >doc</a>
*
* @author Adrian Cole
* @author Andrew Bayer
*/
public class Subnet {
public final class Subnet {
public static enum State {
/**
@ -71,16 +66,15 @@ public class Subnet {
private final String availabilityZone;
private final Map<String, String> tags;
public Subnet(String subnetId, State subnetState, String vpcId, String cidrBlock,
int availableIpAddressCount, String availabilityZone, Map<String, String> tags) {
private Subnet(String subnetId, State subnetState, String vpcId, String cidrBlock, int availableIpAddressCount,
String availabilityZone, ImmutableMap<String, String> tags) {
this.subnetId = checkNotNull(subnetId, "subnetId");
this.subnetState = checkNotNull(subnetState, "subnetState for %s", subnetId);
this.vpcId = checkNotNull(vpcId, "vpcId for %s", subnetId);
this.cidrBlock = checkNotNull(cidrBlock, "cidrBlock for %s", subnetId);
this.availableIpAddressCount = availableIpAddressCount;
this.availabilityZone = checkNotNull(availabilityZone, "availabilityZone for %s", subnetId);
this.tags = ImmutableMap.<String, String> copyOf(checkNotNull(tags, "tags for %s", subnetId));
this.tags = checkNotNull(tags, "tags for %s", subnetId);
}
/**
@ -132,43 +126,30 @@ public class Subnet {
return tags;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hashCode(subnetId, vpcId, availabilityZone);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
if (obj == null || getClass() != obj.getClass())
return false;
if (getClass() != obj.getClass())
return false;
Subnet other = (Subnet) obj;
return Objects.equal(this.subnetId, other.subnetId)
&& Objects.equal(this.vpcId, other.vpcId)
&& Objects.equal(this.availabilityZone, other.availabilityZone);
Subnet that = Subnet.class.cast(obj);
return Objects.equal(this.subnetId, that.subnetId) && Objects.equal(this.vpcId, that.vpcId)
&& Objects.equal(this.availabilityZone, that.availabilityZone);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return string().toString();
}
private final ToStringHelper string() {
return Objects.toStringHelper(this).omitNullValues().add("subnetId", subnetId)
.add("subnetState", subnetState).add("vpcId", vpcId)
.add("cidrBlock", cidrBlock).add("availableIpAddressCount", availableIpAddressCount)
return Objects.toStringHelper(this).omitNullValues().add("subnetId", subnetId).add("subnetState", subnetState)
.add("vpcId", vpcId).add("cidrBlock", cidrBlock).add("availableIpAddressCount", availableIpAddressCount)
.add("availabilityZone", availabilityZone).add("tags", tags);
}
@ -177,17 +158,17 @@ public class Subnet {
}
public Builder toBuilder() {
return builder().fromSubnet(this);
return builder().from(this);
}
public static class Builder {
public final static class Builder {
private String subnetId;
private State subnetState;
private String vpcId;
private String cidrBlock;
private int availableIpAddressCount;
private String availabilityZone;
private Map<String, String> tags = Maps.newLinkedHashMap();
private ImmutableMap.Builder<String, String> tags = ImmutableMap.<String, String> builder();
/**
* @see Subnet#getSubnetId()
@ -241,32 +222,34 @@ public class Subnet {
* @see Subnet#getTags()
*/
public Builder tags(Map<String, String> tags) {
this.tags = ImmutableMap.copyOf(checkNotNull(tags, "tags"));
this.tags.putAll(checkNotNull(tags, "tags"));
return this;
}
/**
* @see Subnet#getTags()
*/
public Builder tag(String key) {
return tag(key, "");
}
/**
* @see Subnet#getTags()
*/
public Builder tag(String key, String value) {
if (key != null)
this.tags.put(key, Strings.nullToEmpty(value));
this.tags.put(checkNotNull(key, "key"), checkNotNull(value, "value"));
return this;
}
public Subnet build() {
return new Subnet(subnetId, subnetState, vpcId, cidrBlock, availableIpAddressCount,
availabilityZone, tags);
return new Subnet(subnetId, subnetState, vpcId, cidrBlock, availableIpAddressCount, availabilityZone,
tags.build());
}
public Builder fromSubnet(Subnet in) {
return this.subnetId(in.getSubnetId())
.subnetState(in.getSubnetState())
.vpcId(in.getVpcId())
.cidrBlock(in.getCidrBlock())
.availableIpAddressCount(in.getAvailableIpAddressCount())
.availabilityZone(in.getAvailabilityZone())
.tags(in.getTags());
public Builder from(Subnet in) {
return this.subnetId(in.getSubnetId()).subnetState(in.getSubnetState()).vpcId(in.getVpcId())
.cidrBlock(in.getCidrBlock()).availableIpAddressCount(in.getAvailableIpAddressCount())
.availabilityZone(in.getAvailabilityZone()).tags(in.getTags());
}
}
}

View File

@ -18,7 +18,6 @@
*/
package org.jclouds.ec2.features;
import java.util.Map;
import org.jclouds.ec2.domain.Subnet;
import org.jclouds.ec2.util.SubnetFilterBuilder;
import org.jclouds.rest.annotations.SinceApiVersion;
@ -27,13 +26,10 @@ import com.google.common.collect.FluentIterable;
import com.google.common.collect.Multimap;
/**
* To help you manage your Amazon EC2 instances, images, and other Amazon EC2
* resources, you can assign your own metadata to each resource in the form of
* tags.
* To help you manage your Amazon EC2 instances, images, and other Amazon EC2 resources, you can assign your own
* metadata to each resource in the form of tags.
*
* @see <a
* href="http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html"
* >doc</a>
* @see <a href="http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html" >doc</a>
* @see SubnetAsyncApi
* @author Adrian Cole
* @author Andrew Bayer
@ -57,7 +53,7 @@ public interface SubnetApi {
* <h4>example</h4>
*
* <pre>
* subnets = subnetApi.filter(new SubnetFilterBuilder().vpcId("vpc-1a2b3c4d").build());
* subnets = subnetApi.filter(new SubnetFilterBuilder().vpcId(&quot;vpc-1a2b3c4d&quot;).build());
* </pre>
*
* @param filter

View File

@ -20,8 +20,6 @@ package org.jclouds.ec2.features;
import static org.jclouds.aws.reference.FormParameters.ACTION;
import java.util.Map;
import javax.inject.Named;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
@ -47,8 +45,7 @@ import com.google.common.util.concurrent.ListenableFuture;
* Provides access to Amazon EC2 via the Query API
* <p/>
*
* @see <a
* href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSubnets.html"
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSubnets.html"
* >doc</a>
* @see SubnetApi
* @author Adrian Cole

View File

@ -18,31 +18,25 @@
*/
package org.jclouds.ec2.xml;
import static org.jclouds.util.SaxUtils.currentOrNull;
import static org.jclouds.util.SaxUtils.equalsOrSuffix;
import java.util.Set;
import org.jclouds.ec2.domain.Subnet;
import org.jclouds.http.functions.ParseSax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
/**
* @see <a
* href="http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSubnets.html"
* >xml</a>
* @see <a href="http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSubnets.html" >xml</a>
*
* @author Adrian Cole
* @author Andrew Bayer
*/
public class DescribeSubnetsResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<FluentIterable<Subnet>> {
public class DescribeSubnetsResponseHandler extends
ParseSax.HandlerForGeneratedRequestWithResult<FluentIterable<Subnet>> {
private final SubnetHandler subnetHandler;
private StringBuilder currentText = new StringBuilder();
@ -55,19 +49,13 @@ public class DescribeSubnetsResponseHandler extends ParseSax.HandlerForGenerated
this.subnetHandler = subnetHandler;
}
/**
* {@inheritDoc}
*/
@Override
public FluentIterable<Subnet> getResult() {
return FluentIterable.from(subnets.build());
}
/**
* {@inheritDoc}
*/
@Override
public void startElement(String url, String name, String qName, Attributes attributes) throws SAXException {
public void startElement(String url, String name, String qName, Attributes attributes) {
if (equalsOrSuffix(qName, "subnetSet")) {
inSubnetSet = true;
} else if (inSubnetSet) {
@ -78,11 +66,8 @@ public class DescribeSubnetsResponseHandler extends ParseSax.HandlerForGenerated
}
}
/**
* {@inheritDoc}
*/
@Override
public void endElement(String uri, String name, String qName) throws SAXException {
public void endElement(String uri, String name, String qName) {
if (equalsOrSuffix(qName, "subnetSet")) {
inSubnetSet = false;
} else if (equalsOrSuffix(qName, "tagSet")) {
@ -97,9 +82,6 @@ public class DescribeSubnetsResponseHandler extends ParseSax.HandlerForGenerated
currentText = new StringBuilder();
}
/**
* {@inheritDoc}
*/
@Override
public void characters(char ch[], int start, int length) {
if (inSubnetSet) {
@ -108,5 +90,4 @@ public class DescribeSubnetsResponseHandler extends ParseSax.HandlerForGenerated
currentText.append(ch, start, length);
}
}
}

View File

@ -24,24 +24,18 @@ import static org.jclouds.util.SaxUtils.equalsOrSuffix;
import javax.inject.Inject;
import org.jclouds.ec2.domain.Subnet;
import org.jclouds.ec2.domain.Subnet.State;
import org.jclouds.http.functions.ParseSax;
import org.xml.sax.Attributes;
import com.google.common.base.Supplier;
/**
* @see <a
* href="http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-ItemType-SubnetType.html"
* >xml</a>
* @see <a href="http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-ItemType-SubnetType.html" >xml</a>
*
* @author Adrian Cole
* @author Andrew Bayer
*/
public class SubnetHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Subnet> {
private StringBuilder currentText = new StringBuilder();
private Subnet.Builder builder = newBuilder();
private Subnet.Builder builder = Subnet.builder();
private final TagSetHandler tagSetHandler;
private boolean inTagSet;
@ -50,9 +44,6 @@ public class SubnetHandler extends ParseSax.HandlerForGeneratedRequestWithResult
this.tagSetHandler = tagSetHandler;
}
/**
* {@inheritDoc}
*/
@Override
public Subnet getResult() {
try {
@ -62,9 +53,6 @@ public class SubnetHandler extends ParseSax.HandlerForGeneratedRequestWithResult
}
}
/**
* {@inheritDoc}
*/
@Override
public void startElement(String uri, String name, String qName, Attributes attrs) {
if (equalsOrSuffix(qName, "tagSet")) {
@ -75,9 +63,6 @@ public class SubnetHandler extends ParseSax.HandlerForGeneratedRequestWithResult
}
}
/**
* {@inheritDoc}
*/
@Override
public void endElement(String uri, String name, String qName) {
if (equalsOrSuffix(qName, "tagSet")) {
@ -101,11 +86,6 @@ public class SubnetHandler extends ParseSax.HandlerForGeneratedRequestWithResult
currentText = new StringBuilder();
}
/**
* {@inheritDoc}
*/
@Override
public void characters(char ch[], int start, int length) {
if (inTagSet) {
@ -114,9 +94,4 @@ public class SubnetHandler extends ParseSax.HandlerForGeneratedRequestWithResult
currentText.append(ch, start, length);
}
}
private Subnet.Builder newBuilder() {
return Subnet.builder();
}
}

View File

@ -27,11 +27,9 @@ import org.jclouds.ec2.internal.BaseEC2ApiExpectTest;
import org.jclouds.ec2.parse.DescribeSubnetsResponseTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.rest.annotations.SinceApiVersion;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
@ -51,20 +49,16 @@ public class SubnetApiExpectTest extends BaseEC2ApiExpectTest<EC2Api> {
return props;
}
HttpRequest list = HttpRequest.builder()
.method("POST")
HttpRequest list = HttpRequest.builder().method("POST")
.endpoint("https://ec2.us-east-1.amazonaws.com/")
.addHeader("Host", "ec2.us-east-1.amazonaws.com")
.payload(
payloadFromStringWithContentType(
"Action=DescribeSubnets" +
"&Signature=Uuafp9lnYQmMUcf/JE1epPTQVCSMPqfns%2BwlZssUsi4%3D" +
"&SignatureMethod=HmacSHA256" +
"&SignatureVersion=2" +
"&Timestamp=2012-04-16T15%3A54%3A08.897Z" +
"&Version=2011-01-01" +
"&AWSAccessKeyId=identity",
"application/x-www-form-urlencoded"))
.addFormParam("Action", "DescribeSubnets")
.addFormParam("Signature", "Uuafp9lnYQmMUcf/JE1epPTQVCSMPqfns%2BwlZssUsi4%3D")
.addFormParam("SignatureMethod", "HmacSHA256")
.addFormParam("SignatureVersion", "2")
.addFormParam("Timestamp", "2012-04-16T15%3A54%3A08.897Z")
.addFormParam("Version", "2011-01-01")
.addFormParam("AWSAccessKeyId", "identity")
.build();
public void testListWhenResponseIs2xx() throws Exception {
@ -88,23 +82,18 @@ public class SubnetApiExpectTest extends BaseEC2ApiExpectTest<EC2Api> {
assertEquals(apiWhenDontExist.getSubnetApi().get().list().toSet(), ImmutableSet.of());
}
HttpRequest filter =
HttpRequest.builder()
.method("POST")
HttpRequest filter = HttpRequest.builder().method("POST")
.endpoint("https://ec2.us-east-1.amazonaws.com/")
.addHeader("Host", "ec2.us-east-1.amazonaws.com")
.payload(payloadFromStringWithContentType(
"Action=DescribeSubnets" +
"&Filter.1.Name=subnet-id" +
"&Filter.1.Value.1=subnet-9d4a7b6c" +
"&Signature=%2Bp34YACfLk9km1H3eALnDmrkst9FhJttojVSf7VztLk%3D" +
"&SignatureMethod=HmacSHA256" +
"&SignatureVersion=2" +
"&Timestamp=2012-04-16T15%3A54%3A08.897Z" +
"&Version=2011-01-01" +
"&AWSAccessKeyId=identity",
"application/x-www-form-urlencoded"))
.build();
.addFormParam("Action", "DescribeSubnets")
.addFormParam("Filter.1.Name", "subnet-id")
.addFormParam("Filter.1.Value.1", "subnet-9d4a7b6c")
.addFormParam("Signature", "%2Bp34YACfLk9km1H3eALnDmrkst9FhJttojVSf7VztLk%3D")
.addFormParam("SignatureMethod", "HmacSHA256")
.addFormParam("SignatureVersion", "2")
.addFormParam("Timestamp", "2012-04-16T15%3A54%3A08.897Z")
.addFormParam("Version", "2011-01-01")
.addFormParam("AWSAccessKeyId", "identity").build();
public void testFilterWhenResponseIs2xx() throws Exception {

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.ec2.features.internal;
package org.jclouds.ec2.features;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;
@ -24,24 +24,16 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Logger.getAnonymousLogger;
import static org.jclouds.util.Predicates2.retry;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.logging.Logger;
import org.jclouds.ec2.domain.Subnet;
import org.jclouds.ec2.features.SubnetApi;
import org.jclouds.ec2.internal.BaseEC2ApiLiveTest;
import org.jclouds.ec2.util.SubnetFilterBuilder;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
@ -52,8 +44,6 @@ import com.google.common.collect.ImmutableSet;
*/
@Test(groups = "live")
public class SubnetApiLiveTest extends BaseEC2ApiLiveTest {
private Subnet subnet;
private void checkSubnet(Subnet subnet) {
getAnonymousLogger().info(format("subnet %s vpc: %s", subnet.getSubnetId(), subnet.getVpcId()));

View File

@ -27,6 +27,12 @@ import javax.inject.Singleton;
import org.jclouds.ec2.EC2AsyncClient;
import org.jclouds.ec2.EC2Client;
import org.jclouds.ec2.config.EC2RestClientModule;
import org.jclouds.ec2.features.SubnetApi;
import org.jclouds.ec2.features.SubnetAsyncApi;
import org.jclouds.ec2.features.TagApi;
import org.jclouds.ec2.features.TagAsyncApi;
import org.jclouds.ec2.features.WindowsApi;
import org.jclouds.ec2.features.WindowsAsyncApi;
import org.jclouds.ec2.services.AMIAsyncClient;
import org.jclouds.ec2.services.AMIClient;
import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient;
@ -76,6 +82,9 @@ public class NovaEC2RestClientModule extends EC2RestClientModule<NovaEC2Client,
.put(WindowsClient.class, WindowsAsyncClient.class)//
.put(AvailabilityZoneAndRegionClient.class, AvailabilityZoneAndRegionAsyncClient.class)//
.put(ElasticBlockStoreClient.class, ElasticBlockStoreAsyncClient.class)//
.put(WindowsApi.class, WindowsAsyncApi.class)//
.put(TagApi.class, TagAsyncApi.class)//
.put(SubnetApi.class, SubnetAsyncApi.class)//
.build();
public NovaEC2RestClientModule() {

View File

@ -0,0 +1,33 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.openstack.nova.ec2.features;
import org.jclouds.ec2.features.SubnetApiLiveTest;
import org.testng.annotations.Test;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", testName = "NovaSubnetApiLiveTest")
public class NovaSubnetApiLiveTest extends SubnetApiLiveTest {
public NovaSubnetApiLiveTest() {
provider = "cloudstack-ec2";
}
}

View File

@ -0,0 +1,33 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.cloudstack.ec2.features;
import org.jclouds.ec2.features.SubnetApiLiveTest;
import org.testng.annotations.Test;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", testName = "CloudStackSubnetApiLiveTest")
public class CloudStackSubnetApiLiveTest extends SubnetApiLiveTest {
public CloudStackSubnetApiLiveTest() {
provider = "cloudstack-ec2";
}
}

View File

@ -46,6 +46,8 @@ import org.jclouds.aws.ec2.services.SpotInstanceClient;
import org.jclouds.ec2.EC2AsyncClient;
import org.jclouds.ec2.EC2Client;
import org.jclouds.ec2.config.EC2RestClientModule;
import org.jclouds.ec2.features.SubnetApi;
import org.jclouds.ec2.features.SubnetAsyncApi;
import org.jclouds.ec2.features.TagApi;
import org.jclouds.ec2.features.TagAsyncApi;
import org.jclouds.ec2.features.WindowsApi;
@ -94,6 +96,7 @@ public class AWSEC2RestClientModule extends EC2RestClientModule<AWSEC2Client, AW
.put(SpotInstanceClient.class, SpotInstanceAsyncClient.class)//
.put(WindowsApi.class, WindowsAsyncApi.class)//
.put(TagApi.class, TagAsyncApi.class)//
.put(SubnetApi.class, SubnetAsyncApi.class)//
.build();
public AWSEC2RestClientModule() {

View File

@ -0,0 +1,33 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.aws.ec2.features;
import org.jclouds.ec2.features.SubnetApiLiveTest;
import org.testng.annotations.Test;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", testName = "AWSSubnetApiLiveTest")
public class AWSSubnetApiLiveTest extends SubnetApiLiveTest {
public AWSSubnetApiLiveTest() {
provider = "aws-ec2";
}
}