612: Change Multimap occurences to Maps with Iterable values

This commit is contained in:
Andrew Donald Kennedy 2011-10-06 23:42:15 +01:00
parent d4f7140071
commit 410b947160
8 changed files with 114 additions and 74 deletions

View File

@ -47,6 +47,7 @@ import org.jclouds.rest.internal.GeneratedHttpRequest;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
/**
@ -142,7 +143,7 @@ public class AWSUtils {
checkArgument(checkNotNull(input, "input") instanceof String[], "this binder is only valid for String[] : "
+ input.getClass());
String[] values = (String[]) input;
Builder<String, String> builder = ImmutableMultimap.<String, String>builder();
Builder<String, String> builder = ImmutableMultimap.<String, String> builder();
for (int i = 0; i < values.length; i++) {
builder.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() + "s[" + i + "]"));
}
@ -174,7 +175,26 @@ public class AWSUtils {
int i = 1;
for (Object k : map.keySet()) {
builder.put(prefix + "." + i + "." + keySuffix, checkNotNull(k.toString(), keySuffix.toLowerCase() + "s[" + i + "]"));
if (!map.get(k).isEmpty()) {
int j = 1;
for (Object v : map.get(k)) {
builder.put(prefix + "." + i + "." + valueSuffix + "." + j, v.toString());
j++;
}
i++;
}
ImmutableMultimap<String, String> forms = builder.build();
return forms.size() == 0 ? request : ModifyRequest.putFormParams(request, forms);
}
@SuppressWarnings("unchecked")
public static <R extends HttpRequest> R indexMapOfIterableToFormValuesWithPrefix(R request, String prefix, String keySuffix, String valueSuffix, Object input) {
checkArgument(checkNotNull(input, "input") instanceof Map<?, ?>, "this binder is only valid for Map<?,Iterable<?>>: " + input.getClass());
Map<Object, Iterable<Object>> map = (Map<Object, Iterable<Object>>) input;
Builder<String, String> builder = ImmutableMultimap.<String, String> builder();
int i = 1;
for (Object k : map.keySet()) {
builder.put(prefix + "." + i + "." + keySuffix, checkNotNull(k.toString(), keySuffix.toLowerCase() + "s[" + i + "]"));
if (!Iterables.isEmpty(map.get(k))) {
int j = 1;
for (Object v : map.get(k)) {
builder.put(prefix + "." + i + "." + valueSuffix + "." + j, v.toString());

View File

@ -20,19 +20,19 @@ package org.jclouds.aws.ec2.binders;
import static com.google.common.base.Preconditions.*;
import java.util.Map;
import org.jclouds.aws.util.AWSUtils;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import com.google.common.collect.Multimap;
/**
* @author grkvlt@apache.org
*/
public class BindTagFiltersToIndexedFormParams implements Binder {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkArgument(checkNotNull(input, "input") instanceof Multimap, "this binder is only valid for Multimap<Filtername, ?>");
return AWSUtils.indexMultimapToFormValuesWithPrefix(request, "Filter", "Name", "Value", input);
checkArgument(checkNotNull(input, "input") instanceof Map<?,?>, "this binder is only valid for Map<?, Iterable<?>>");
return AWSUtils.indexMapOfIterableToFormValuesWithPrefix(request, "Filter", "Name", "Value", input);
}
}

View File

@ -31,7 +31,7 @@ import org.jclouds.aws.ec2.binders.BindResourceIdsToIndexedFormParams;
import org.jclouds.aws.ec2.binders.BindTagFiltersToIndexedFormParams;
import org.jclouds.aws.ec2.binders.BindTagsToIndexedFormParams;
import org.jclouds.aws.ec2.domain.Tag;
import org.jclouds.aws.ec2.util.TagFilters;
import org.jclouds.aws.ec2.util.TagFilters.FilterName;
import org.jclouds.aws.ec2.xml.DescribeTagsResponseHandler;
import org.jclouds.aws.filters.FormSigner;
import org.jclouds.javax.annotation.Nullable;
@ -46,7 +46,6 @@ import org.jclouds.rest.annotations.XMLResponseParser;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.ListenableFuture;
/**
@ -80,7 +79,7 @@ public interface TagAsyncClient {
@BinderParam(BindTagsToIndexedFormParams.class) Map<String, String> tags);
/**
* @see TagClient#describeTagsInRegion(String, Multimap)
* @see TagClient#describeTagsInRegion(String, Map)
*/
@POST
@Path("/")
@ -88,5 +87,5 @@ public interface TagAsyncClient {
@XMLResponseParser(DescribeTagsResponseHandler.class)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<? extends Set<Tag>> describeTagsInRegion(@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
@BinderParam(BindTagFiltersToIndexedFormParams.class) Multimap<TagFilters.FilterName, ?> filters);
@BinderParam(BindTagFiltersToIndexedFormParams.class) Map<FilterName, Iterable<?>> filters);
}

View File

@ -23,12 +23,10 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.aws.ec2.domain.Tag;
import org.jclouds.aws.ec2.util.TagFilters;
import org.jclouds.aws.ec2.util.TagFilters.FilterName;
import org.jclouds.concurrent.Timeout;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.collect.Multimap;
/**
* Provides Tag services for EC2. For more information, refer to the Amazon EC2
* Developer Guide.
@ -46,7 +44,7 @@ public interface TagClient {
* IDs of the resources to tag.
* @param tags
* The tags to create.
* @see #describeTagsInRegion(String, Multimap)
* @see #describeTagsInRegion(String, Map)
* @see #deleteTagsInRegion(String, Iterable, Map)
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateTags.html" />
@ -63,7 +61,7 @@ public interface TagClient {
* @param tags
* The tags to delete.
*
* @see #describeTagsInRegion(String, Multimap)
* @see #describeTagsInRegion(String, Map)
* @see #createTagsInRegion(String, Iterable, Map)
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DeleteTags.html" />
@ -82,5 +80,5 @@ public interface TagClient {
* @see #createTagsInRegion(String, Iterable, Map)
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeTags.html" />
*/
Set<Tag> describeTagsInRegion(@Nullable String region, Multimap<TagFilters.FilterName, ?> filters);
Set<Tag> describeTagsInRegion(@Nullable String region, Map<FilterName, Iterable<?>> filters);
}

View File

@ -20,10 +20,14 @@ package org.jclouds.aws.ec2.util;
import static com.google.common.base.Preconditions.*;
import java.util.Map;
import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
/**
* @author grkvlt@apache.org
@ -89,52 +93,52 @@ public class TagFilters {
}
}
protected final ImmutableSetMultimap.Builder<FilterName, Object> map;
protected final Map<FilterName, Iterable<?>> map;
protected TagFilters() {
map = ImmutableSetMultimap.<FilterName, Object>builder();
map = Maps.<FilterName, Iterable<?>>newLinkedHashMap();
}
public static TagFilters filters() {
return new TagFilters();
}
public Multimap<FilterName, Object> build() {
return map.build();
public Map<FilterName, Iterable<?>> build() {
return ImmutableMap.copyOf(map);
}
public TagFilters resourceId(String resourceId) {
map.put(FilterName.RESOURCE_ID, resourceId);
put(FilterName.RESOURCE_ID, resourceId);
return this;
}
public TagFilters key(String key) {
map.put(FilterName.KEY, key);
put(FilterName.KEY, key);
return this;
}
public TagFilters keys(String...keys) {
map.putAll(FilterName.KEY, ImmutableSet.<String>copyOf(keys));
put(FilterName.KEY, ImmutableSet.<String>copyOf(keys));
return this;
}
public TagFilters keys(Iterable<String> keys) {
map.putAll(FilterName.KEY, ImmutableSet.<String>copyOf(keys));
putAll(FilterName.KEY, ImmutableSet.<String>copyOf(keys));
return this;
}
public TagFilters value(String value) {
map.put(FilterName.VALUE, value);
put(FilterName.VALUE, value);
return this;
}
public TagFilters values(String...values) {
map.putAll(FilterName.VALUE, ImmutableSet.<String>copyOf(values));
putAll(FilterName.VALUE, ImmutableSet.<String>copyOf(values));
return this;
}
public TagFilters values(Iterable<String> values) {
map.putAll(FilterName.VALUE, ImmutableSet.<String>copyOf(values));
putAll(FilterName.VALUE, ImmutableSet.<String>copyOf(values));
return this;
}
@ -187,99 +191,125 @@ public class TagFilters {
}
public TagFilters anyKey() {
return key("*");
putAll(FilterName.KEY, ImmutableSet.<String>of());
return this;
}
public TagFilters anyValue() {
return value("*");
putAll(FilterName.VALUE, ImmutableSet.<String>of());
return this;
}
public TagFilters anyResourceId() {
return resourceId("*");
putAll(FilterName.RESOURCE_TYPE, ImmutableSet.<String>of());
return this;
}
public TagFilters anyResourceType() {
map.put(FilterName.RESOURCE_TYPE, "*");
putAll(FilterName.RESOURCE_TYPE, ImmutableSet.<ResourceType>of());
return this;
}
public TagFilters resourceType(ResourceType resourceType) {
put(FilterName.RESOURCE_TYPE, resourceType);
return this;
}
public TagFilters customerGateway() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.CUSTOMER_GATEWAY);
put(FilterName.RESOURCE_TYPE, ResourceType.CUSTOMER_GATEWAY);
return this;
}
public TagFilters dhcpOptions() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.DHCP_OPTIONS);
put(FilterName.RESOURCE_TYPE, ResourceType.DHCP_OPTIONS);
return this;
}
public TagFilters image() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.IMAGE);
put(FilterName.RESOURCE_TYPE, ResourceType.IMAGE);
return this;
}
public TagFilters instance() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.INSTANCE);
put(FilterName.RESOURCE_TYPE, ResourceType.INSTANCE);
return this;
}
public TagFilters internetGateway() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.INTERNET_GATEWAY);
put(FilterName.RESOURCE_TYPE, ResourceType.INTERNET_GATEWAY);
return this;
}
public TagFilters networkAcl() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.NETWORK_ACL);
put(FilterName.RESOURCE_TYPE, ResourceType.NETWORK_ACL);
return this;
}
public TagFilters reservedInstance() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.RESERVED_INSTANCES);
put(FilterName.RESOURCE_TYPE, ResourceType.RESERVED_INSTANCES);
return this;
}
public TagFilters routeTable() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.ROUTE_TABLE);
put(FilterName.RESOURCE_TYPE, ResourceType.ROUTE_TABLE);
return this;
}
public TagFilters securityGroup() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.SECURITY_GROUP);
put(FilterName.RESOURCE_TYPE, ResourceType.SECURITY_GROUP);
return this;
}
public TagFilters snapshot() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.SNAPSHOT);
put(FilterName.RESOURCE_TYPE, ResourceType.SNAPSHOT);
return this;
}
public TagFilters instancesRequest() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.SPOT_INSTANCES_REQUEST);
put(FilterName.RESOURCE_TYPE, ResourceType.SPOT_INSTANCES_REQUEST);
return this;
}
public TagFilters subnet() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.SUBNET);
put(FilterName.RESOURCE_TYPE, ResourceType.SUBNET);
return this;
}
public TagFilters volume() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.VOLUME);
put(FilterName.RESOURCE_TYPE, ResourceType.VOLUME);
return this;
}
public TagFilters vpc() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.VPC);
put(FilterName.RESOURCE_TYPE, ResourceType.VPC);
return this;
}
public TagFilters vpnConnection() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.VPN_CONNECTION);
put(FilterName.RESOURCE_TYPE, ResourceType.VPN_CONNECTION);
return this;
}
public TagFilters vpnGateway() {
map.put(FilterName.RESOURCE_TYPE, ResourceType.VPN_GATEWAY);
put(FilterName.RESOURCE_TYPE, ResourceType.VPN_GATEWAY);
return this;
}
private void put(FilterName key, Object value) {
putAll(key, Sets.newHashSet(value));
}
private void putAll(FilterName key, Iterable<?> values) {
if (values == null || Iterables.isEmpty(values)) {
// If we add an empty or null set of values, replace the value in the map entirely
map.put(key, ImmutableSet.of());
} else {
// Add the values, to a new set if required
if (!map.containsKey(key)) {
map.put(key, Sets.newHashSet());
}
Iterable<?> entries = map.get(key);
map.put(key, Iterables.concat(entries, values));
}
}
}

View File

@ -31,7 +31,8 @@ import org.jclouds.aws.ec2.util.TagFilters.ResourceType;
import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
@ -45,31 +46,31 @@ public class BindTagFiltersToIndexedFormParamsTest {
Injector injector = Guice.createInjector();
BindTagFiltersToIndexedFormParams binder = injector.getInstance(BindTagFiltersToIndexedFormParams.class);
public void testResourceTypeWithValues() {
public void testMultipleResourceTypes() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, ImmutableSetMultimap.<FilterName, ResourceType>builder().put(FilterName.RESOURCE_TYPE, ResourceType.VPN_GATEWAY).put(FilterName.RESOURCE_TYPE, ResourceType.INTERNET_GATEWAY).build());
request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<ResourceType>>builder().put(FilterName.RESOURCE_TYPE, ImmutableSet.<ResourceType>of(ResourceType.VPN_GATEWAY, ResourceType.INTERNET_GATEWAY)).build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=resource-type&Filter.1.Value.1=vpn-gateway&Filter.1.Value.2=internet-gateway");
}
public void testMultipleKeys() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, ImmutableSetMultimap.<FilterName, String>builder().put(FilterName.KEY, "one").put(FilterName.KEY, "two").build());
request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<String>>builder().put(FilterName.KEY, ImmutableSet.<String>of("one", "two")).build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.1.Value.2=two");
}
public void testkeyWithValue() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, ImmutableSetMultimap.<FilterName, String>builder().put(FilterName.KEY, "one").put(FilterName.VALUE, "alpha").build());
request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<String>>builder().put(FilterName.KEY, ImmutableSet.<String>of("one")).put(FilterName.VALUE, ImmutableSet.<String>of("alpha")).build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.2.Name=value&Filter.2.Value.1=alpha");
}
public void testAnyKey() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, ImmutableSetMultimap.<FilterName, String>builder().put(FilterName.KEY, "*").build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=%2A");
request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<String>>builder().put(FilterName.KEY, ImmutableSet.<String>of()).build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key");
}
public void testResourceTypeWithValuesBuilder() {
public void testMultipleResourceTypesBuilder() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, TagFilters.filters().vpnGateway().internetGateway().build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=resource-type&Filter.1.Value.1=vpn-gateway&Filter.1.Value.2=internet-gateway");
@ -81,7 +82,7 @@ public class BindTagFiltersToIndexedFormParamsTest {
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.1.Value.2=two");
}
public void testkeyWithValueBuilder() {
public void testKeyWithValueBuilder() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, TagFilters.filters().keyValuePair("one", "alpha").build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.2.Name=value&Filter.2.Value.1=alpha");
@ -90,7 +91,7 @@ public class BindTagFiltersToIndexedFormParamsTest {
public void testAnyKeyBuilder() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
request = binder.bindToRequest(request, TagFilters.filters().anyKey().build());
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=%2A");
assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key");
}
@Test(expectedExceptions = IllegalArgumentException.class)

View File

@ -22,7 +22,7 @@ import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import org.jclouds.aws.ec2.util.TagFilters.FilterName;
import org.jclouds.aws.ec2.util.TagFilters;
import org.jclouds.aws.ec2.xml.DescribeTagsResponseHandler;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseSax;
@ -34,8 +34,6 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.inject.TypeLiteral;
/**
@ -79,8 +77,8 @@ public class TagAsyncClientTest extends BaseAWSEC2AsyncClientTest<TagAsyncClient
}
public void testDescribeTags() throws SecurityException, NoSuchMethodException, IOException {
Method method = TagAsyncClient.class.getMethod("describeTagsInRegion", String.class, Multimap.class);
HttpRequest request = processor.createRequest(method, null, ImmutableMultimap.<FilterName, Object>of());
Method method = TagAsyncClient.class.getMethod("describeTagsInRegion", String.class, Map.class);
HttpRequest request = processor.createRequest(method, null, TagFilters.filters().build());
assertRequestLineEquals(request, "POST https://ec2.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: ec2.us-east-1.amazonaws.com\n");
@ -95,8 +93,8 @@ public class TagAsyncClientTest extends BaseAWSEC2AsyncClientTest<TagAsyncClient
}
public void testDescribeTagsArgs() throws SecurityException, NoSuchMethodException, IOException {
Method method = TagAsyncClient.class.getMethod("describeTagsInRegion", String.class, Multimap.class);
HttpRequest request = processor.createRequest(method, null, ImmutableMultimap.builder().put(FilterName.KEY, "one").put(FilterName.KEY, "two").build());
Method method = TagAsyncClient.class.getMethod("describeTagsInRegion", String.class, Map.class);
HttpRequest request = processor.createRequest(method, null, TagFilters.filters().key("one").key("two").build());
assertRequestLineEquals(request, "POST https://ec2.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: ec2.us-east-1.amazonaws.com\n");

View File

@ -30,7 +30,7 @@ import org.jclouds.aws.ec2.AWSEC2AsyncClient;
import org.jclouds.aws.ec2.AWSEC2Client;
import org.jclouds.aws.ec2.domain.AWSRunningInstance;
import org.jclouds.aws.ec2.domain.Tag;
import org.jclouds.aws.ec2.util.TagFilters.FilterName;
import org.jclouds.aws.ec2.util.TagFilters;
import org.jclouds.aws.ec2.util.TagFilters.ResourceType;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
@ -44,7 +44,6 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
@ -126,12 +125,7 @@ public class TagClientLiveTest {
}
protected void checkTag(String resourceId, ResourceType resourceType, String key, String value) {
Set<Tag> results = client.describeTagsInRegion(null, ImmutableMultimap.<FilterName,Object>builder()
.put(FilterName.RESOURCE_ID, resourceId)
.put(FilterName.RESOURCE_TYPE, resourceType)
.put(FilterName.KEY, key)
.put(FilterName.VALUE, value)
.build());
Set<Tag> results = client.describeTagsInRegion(null, TagFilters.filters().resourceId(resourceId).resourceType(resourceType).keyValuePair(key, value).build());
assertNotNull(results);
assertEquals(results.size(), 1);
Tag tag = Iterables.getOnlyElement(results);