Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For EC2, Introduce "allocationId" to IP Address and a new describeRouteTablesWithFilter method for searching RouteTables #202

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
private final String region;
@Nullable
private final String instanceId;
private final String allocationId;
private final String publicIp;
private final Map<String, String> tags;

public PublicIpInstanceIdPair(final String region, final String publicIp, @Nullable final String instanceId,
@Nullable final Map<String, String> tags) {
@Nullable final String allocationId, @Nullable final Map<String, String> tags) {
this.region = checkNotNull(region, "region");
this.instanceId = instanceId;
this.allocationId = allocationId;
this.publicIp = checkNotNull(publicIp, "publicIp");
this.tags = tags == null ? ImmutableMap.<String, String> of() : ImmutableMap.copyOf(tags);
}
Expand Down Expand Up @@ -71,6 +73,13 @@ public String getInstanceId() {
return instanceId;
}

/**
* The ID of the IP allocation (e.g., eipalloc-0ca038968f2a2c986).
*/
public String getAllocationId() {
return allocationId;
}

/**
* The public IP address.
*/
Expand All @@ -87,6 +96,7 @@ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode());
result = prime * result + ((allocationId == null) ? 0 : allocationId.hashCode());
result = prime * result + ((publicIp == null) ? 0 : publicIp.hashCode());
result = prime * result + ((region == null) ? 0 : region.hashCode());
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
Expand All @@ -107,6 +117,11 @@ public boolean equals(Object obj) {
return false;
} else if (!instanceId.equals(other.instanceId))
return false;
if (allocationId == null) {
if (other.allocationId != null)
return false;
} else if (!allocationId.equals(other.allocationId))
return false;
if (publicIp == null) {
if (other.publicIp != null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ void releaseAddressInRegion(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
@FormParam("PublicIp") String publicIp);

/**
* Releases an elastic IP address associated with your identity.
*
* @param region
* Elastic IP addresses are tied to a Region and cannot be mapped across Regions.
* @param allocationId
* The Allocation ID (e.g., eipalloc-0ca038968f2a2c986) of the IP address that you are releasing from your identity.
*
* @see #allocateAddress
* @see #describeAddresses
* @see #associateAddress
* @see #disassociateAddress
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-ReleaseAddress.html"
*/
@Named("ReleaseAddress")
@POST
@Path("/")
@FormParams(keys = ACTION, values = "ReleaseAddress")
void releaseAddressInRegionByAllocationId(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
@FormParam("AllocationId") String allocationId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the corresponding unit test to the ElasticIPAddressApiTest?


/**
* Lists elastic IP addresses assigned to your identity or provides information about a specific
* address.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DescribeAddressesResponseHandler extends HandlerForGeneratedRequest
@Inject
@Region
Supplier<String> defaultRegion;
private String allocationId;
private String instanceId;
private final TagSetHandler tagSetHandler;
private boolean inTagSet;
Expand Down Expand Up @@ -80,14 +81,17 @@ public void endElement(final String uri, final String name, final String qName)
ipAddress = currentOrNull();
} else if (qName.equals("instanceId")) {
instanceId = currentOrNull();
} else if (qName.equals("allocationId")) {
allocationId = currentOrNull();
} else if (qName.equals("item")) {
String region = AWSUtils.findRegionInArgsOrNull(getRequest());
if (region == null)
region = defaultRegion.get();

pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, tagResults));
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, allocationId, tagResults));
ipAddress = null;
instanceId = null;
allocationId = null;
tagResults = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testReturnsPublicIpOnMatch() throws Exception {

expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
expect(ipClient.describeAddressesInRegion("region")).andReturn(
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null)))
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null, null)))
.atLeastOnce();

replay(client);
Expand Down Expand Up @@ -85,7 +85,7 @@ public void testReturnsNullWhenNotAssigned() throws Exception {
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();

expect(ipClient.describeAddressesInRegion("region")).andReturn(
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null)))
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null, null)))
.atLeastOnce();

replay(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void testApplyInputStream() throws UnknownHostException {

Set<PublicIpInstanceIdPair> result = factory.create(handler).parse(is);

assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98",
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98", "eipalloc-0ca038968f2a2c986",
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null, null,
Collections.<String, String> emptyMap())));
}

Expand All @@ -61,9 +61,9 @@ public void testApplyInputStreamWithTags() throws UnknownHostException {
Set<PublicIpInstanceIdPair> result = factory.create(handler).parse(is);

assertEquals(result.size(), 3);
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98",
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "54.76.27.192", null,
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98", "eipalloc-0ca038968f2a2c986",
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null, null,
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "54.76.27.192", null, null,
ImmutableMap.of("Name", "value-fa97d19c", "Empty", ""))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jclouds.aws.ec2.xml.DescribeRouteTablesResponseHandler;
import org.jclouds.aws.ec2.xml.ReturnValueHandler;
import org.jclouds.aws.filters.FormSigner;
import org.jclouds.ec2.binders.BindFiltersToIndexedFormParams;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.rest.annotations.BinderParam;
Expand All @@ -44,6 +45,7 @@
import org.jclouds.rest.annotations.XMLResponseParser;

import com.google.common.collect.FluentIterable;
import com.google.common.collect.Multimap;

/**
* Provides access to AWS Route Table services.
Expand Down Expand Up @@ -267,6 +269,10 @@ boolean deleteRoute(
/**
* Describes route tables.
* @param region The region to search for route tables.
* @param routeTableIds One or more identifiers for existing RouteTable instances
*
* @return a set of RouteTable objects that matched the routeTableIds passed
*
*/
@Named("DescribeRouteTables")
@POST
Expand All @@ -276,4 +282,21 @@ boolean deleteRoute(
FluentIterable<RouteTable> describeRouteTables(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
@BinderParam(BindRouteTableIdsToIndexedFormParams.class) String... routeTableIds);

/**
* Describes route tables.
* @param region The region to search for route tables.
* @param filter One or more filters utilized to search for RouteTable instances
*
* @link <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html">...</a>
*/
@Named("DescribeRouteTables")
@POST
@FormParams(keys = ACTION, values = "DescribeRouteTables")
@XMLResponseParser(DescribeRouteTablesResponseHandler.class)
@Fallback(Fallbacks.EmptyFluentIterableOnNotFoundOr404.class)
FluentIterable<RouteTable> describeRouteTablesWithFilter(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
@BinderParam(BindFiltersToIndexedFormParams.class) Multimap<String, String> filter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the corresponding unit tests to the RouteTableApiMockTest?


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.ImmutableMultimap;

/**
* Tests behavior of {@link RouteTableApi}
Expand Down Expand Up @@ -106,6 +107,22 @@ public void testDescribe() {
}
});
assertTrue(vpcRT.isPresent(), "Could not find VPC " + vpc.id() + " in described route tables");

//Now test the Find by Filter version of the describeRouteTables
final FluentIterable<RouteTable> routeTablesByFilter = routeTableApi.describeRouteTablesWithFilter(TEST_REGION,
ImmutableMultimap.<String, String>builder()
.put("vpc-id", vpc.id())
.build()
);
assertNotNull(routeTablesByFilter, "Failed to return list of routeTablesByFilter");
Optional<RouteTable> vpcRTByFilter = Iterables.tryFind(routeTablesByFilter, new Predicate<RouteTable>() {
@Override public boolean apply(RouteTable input) {
return vpc.id().equals(input.vpcId());
}
});
assertTrue(vpcRTByFilter.isPresent(), "Could not find VPC " + vpc.id() + " in described route tables");


RouteTable rt = vpcRT.get();
assertEquals(rt.associationSet().size(), 1,
"Route for test VPC has wrong number of associations, should be 1: " + rt.associationSet());
Expand Down
Loading