JCLOUDS-1427: Fix Elastic IP deserialization when IP has tags

This commit is contained in:
Lucia Monte 2018-06-20 11:42:24 +02:00 committed by Ignasi Barrera
parent e4f1823fd5
commit 6b4be0d686
5 changed files with 112 additions and 19 deletions

View File

@ -18,8 +18,12 @@ package org.jclouds.ec2.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.collect.ImmutableMap;
/**
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-DescribeAddressesResponseInfoType.html"
@ -31,11 +35,14 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
@Nullable
private final String instanceId;
private final String publicIp;
private final Map<String, String> tags;
public PublicIpInstanceIdPair(String region, String publicIp, @Nullable String instanceId) {
public PublicIpInstanceIdPair(final String region, final String publicIp, @Nullable final String instanceId,
@Nullable final Map<String, String> tags) {
this.region = checkNotNull(region, "region");
this.instanceId = instanceId;
this.publicIp = checkNotNull(publicIp, "publicIp");
this.tags = tags == null ? ImmutableMap.<String, String> of() : ImmutableMap.copyOf(tags);
}
/**
@ -53,8 +60,8 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
* {@inheritDoc}
*/
@Override
public int compareTo(PublicIpInstanceIdPair o) {
return (this == o) ? 0 : getPublicIp().compareTo(o.getPublicIp());
public int compareTo(final PublicIpInstanceIdPair o) {
return this == o ? 0 : getPublicIp().compareTo(o.getPublicIp());
}
/**
@ -70,6 +77,10 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
public String getPublicIp() {
return publicIp;
}
public Map<String, String> getTags() {
return tags;
}
@Override
public int hashCode() {
@ -78,6 +89,7 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
result = prime * result + ((instanceId == null) ? 0 : instanceId.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());
return result;
}
@ -105,7 +117,11 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
return false;
} else if (!region.equals(other.region))
return false;
if (tags == null) {
if (other.tags != null)
return false;
} else if (!tags.equals(other.tags))
return false;
return true;
}
}

View File

@ -16,6 +16,9 @@
*/
package org.jclouds.ec2.xml;
import static org.jclouds.util.SaxUtils.equalsOrSuffix;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
@ -26,12 +29,12 @@ import org.jclouds.ec2.domain.PublicIpInstanceIdPair;
import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult;
import org.jclouds.location.Region;
import org.jclouds.logging.Logger;
import org.xml.sax.Attributes;
import com.google.common.base.Supplier;
import com.google.common.collect.Sets;
public class DescribeAddressesResponseHandler extends
HandlerForGeneratedRequestWithResult<Set<PublicIpInstanceIdPair>> {
public class DescribeAddressesResponseHandler extends HandlerForGeneratedRequestWithResult<Set<PublicIpInstanceIdPair>> {
@Resource
protected Logger logger = Logger.NULL;
@ -42,14 +45,38 @@ public class DescribeAddressesResponseHandler extends
@Region
Supplier<String> defaultRegion;
private String instanceId;
private final TagSetHandler tagSetHandler;
private boolean inTagSet;
private Map<String, String> tagResults;
@Inject
DescribeAddressesResponseHandler(final TagSetHandler tagSetHandler) {
this.tagSetHandler = tagSetHandler;
}
@Override
public void startElement(final String uri, final String name, final String qName, final Attributes attrs) {
if (equalsOrSuffix(qName, "tagSet")) {
inTagSet = true;
}
if (inTagSet) {
tagSetHandler.startElement(uri, name, qName, attrs);
}
}
protected String currentOrNull() {
String returnVal = currentText.toString().trim();
return returnVal.equals("") ? null : returnVal;
}
public void endElement(String uri, String name, String qName) {
if (qName.equals("publicIp")) {
@Override
public void endElement(final String uri, final String name, final String qName) {
if (equalsOrSuffix(qName, "tagSet")) {
inTagSet = false;
tagResults = tagSetHandler.getResult();
} else if (inTagSet) {
tagSetHandler.endElement(uri, name, qName);
} else if (qName.equals("publicIp")) {
ipAddress = currentOrNull();
} else if (qName.equals("instanceId")) {
instanceId = currentOrNull();
@ -57,15 +84,23 @@ public class DescribeAddressesResponseHandler extends
String region = AWSUtils.findRegionInArgsOrNull(getRequest());
if (region == null)
region = defaultRegion.get();
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId));
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, tagResults));
ipAddress = null;
instanceId = null;
tagResults = null;
}
currentText.setLength(0);
}
public void characters(char[] ch, int start, int length) {
currentText.append(ch, start, length);
@Override
public void characters(final char[] ch, final int start, final int length) {
if (inTagSet) {
tagSetHandler.characters(ch, start, length);
} else {
currentText.append(ch, start, length);
}
}
@Override

View File

@ -41,8 +41,8 @@ public class LoadPublicIpForInstanceOrNullTest {
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")))
.atLeastOnce();
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null)))
.atLeastOnce();
replay(client);
replay(ipClient);
@ -85,7 +85,7 @@ public class LoadPublicIpForInstanceOrNullTest {
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)))
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null)))
.atLeastOnce();
replay(client);

View File

@ -20,13 +20,15 @@ import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Set;
import org.jclouds.ec2.domain.PublicIpInstanceIdPair;
import org.jclouds.http.functions.ParseSax;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
* Tests behavior of {@code DescribeAddressesResponseHandler}
@ -43,13 +45,29 @@ public class DescribeAddressesResponseHandlerTest extends BaseEC2HandlerTest {
addDefaultRegionToHandler(handler);
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,
Collections.<String, String> emptyMap())));
}
public void testApplyInputStreamWithTags() throws UnknownHostException {
assertEquals(result, ImmutableList.of(new PublicIpInstanceIdPair(defaultRegion,
"67.202.55.255", "i-f15ebb98"), new PublicIpInstanceIdPair(defaultRegion,
"67.202.55.233", null)));
InputStream is = getClass().getResourceAsStream("/describe_addresses_with_tags.xml");
DescribeAddressesResponseHandler handler = injector.getInstance(DescribeAddressesResponseHandler.class);
addDefaultRegionToHandler(handler);
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,
ImmutableMap.of("Name", "value-fa97d19c", "Empty", ""))));
}
private void addDefaultRegionToHandler(ParseSax.HandlerWithResult<?> handler) {
private void addDefaultRegionToHandler(final ParseSax.HandlerWithResult<?> handler) {
handler.setContext(request);
}
}

View File

@ -0,0 +1,24 @@
<DescribeAddressesResponse xmlns="http://ec2.amazonaws.com/doc/2009-11-30/">
<addressesSet>
<item>
<instanceId>i-f15ebb98</instanceId>
<publicIp>67.202.55.255</publicIp>
</item>
<item>
<publicIp>67.202.55.233</publicIp>
</item>
<item>
<publicIp>54.76.27.192</publicIp>
<tagSet>
<item>
<key>Name</key>
<value>value-fa97d19c</value>
</item>
<item>
<key>Empty</key>
<value/>
</item>
</tagSet>
</item>
</addressesSet>
</DescribeAddressesResponse>