remap public ip to private ip if incorrectly set by ec2 clone api

This commit is contained in:
Adrian Cole 2011-12-07 12:58:08 -08:00
parent 03db8b0d45
commit d81d1680f7
2 changed files with 49 additions and 6 deletions

View File

@ -19,12 +19,14 @@
package org.jclouds.ec2.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterables.filter;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Map.Entry;
import javax.annotation.Resource;
import javax.inject.Inject;
@ -48,15 +50,18 @@ import org.jclouds.ec2.domain.InstanceState;
import org.jclouds.ec2.domain.RootDeviceType;
import org.jclouds.ec2.domain.RunningInstance;
import org.jclouds.logging.Logger;
import org.jclouds.util.NullSafeCollections;
import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.util.concurrent.UncheckedExecutionException;
/**
@ -94,7 +99,7 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
return builder.build();
}
protected NodeMetadataBuilder buildInstance(RunningInstance instance, NodeMetadataBuilder builder) {
protected NodeMetadataBuilder buildInstance(final RunningInstance instance, NodeMetadataBuilder builder) {
builder.providerId(instance.getId());
builder.id(instance.getRegion() + "/" + instance.getId());
String group = getGroupForInstance(instance);
@ -104,8 +109,19 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
addCredentialsForInstance(builder, instance);
builder.state(instanceToNodeState.get(instance.getInstanceState()));
builder.publicAddresses(NullSafeCollections.nullSafeSet(instance.getIpAddress()));
builder.privateAddresses(NullSafeCollections.nullSafeSet(instance.getPrivateIpAddress()));
// collect all ip addresses into one bundle in case the api mistakenly put a private address
// into the public address field
Builder<String> addressesBuilder = ImmutableSet.<String> builder();
if (Strings.emptyToNull(instance.getIpAddress()) != null)
addressesBuilder.add(instance.getIpAddress());
if (Strings.emptyToNull(instance.getPrivateIpAddress()) != null)
addressesBuilder.add(instance.getPrivateIpAddress());
Set<String> addresses = addressesBuilder.build();
builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
builder.hardware(parseHardware(instance));
Location location = getLocationForAvailabilityZoneOrRegion(instance);
builder.location(location);
@ -126,7 +142,8 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
}
protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
builder.credentials(LoginCredentials.builder(credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId())).build());
builder.credentials(LoginCredentials.builder(
credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId())).build());
}
protected Hardware parseHardware(final RunningInstance instance) {

View File

@ -69,7 +69,33 @@ public class RunningInstanceToNodeMetadataTest {
}
}
@Test
public void testPrivateIpAddressIncorrectlyInPublicAddressFieldGoesToPrivateAddressCollection() {
RunningInstance instance = RunningInstance.builder().instanceId("id").imageId("image").instanceType("m1.small")
.instanceState(InstanceState.RUNNING).region("us-east-1").ipAddress("10.1.1.1").build();
RunningInstanceToNodeMetadata parser = createNodeParser(ImmutableSet.<Hardware> of(), ImmutableSet
.<Location> of(), ImmutableSet.<Image> of(), ImmutableMap.<String, Credentials> of());
assertEquals(parser.apply(instance), new NodeMetadataBuilder().state(NodeState.RUNNING).publicAddresses(
ImmutableSet.<String> of()).privateAddresses(ImmutableSet.of("10.1.1.1")).id("us-east-1/id").imageId(
"us-east-1/image").providerId("id").build());
}
@Test
public void testPublicIpAddressIncorrectlyInPrivateAddressFieldGoesToPublicAddressCollection() {
RunningInstance instance = RunningInstance.builder().instanceId("id").imageId("image").instanceType("m1.small")
.instanceState(InstanceState.RUNNING).region("us-east-1").privateIpAddress("1.1.1.1").build();
RunningInstanceToNodeMetadata parser = createNodeParser(ImmutableSet.<Hardware> of(), ImmutableSet
.<Location> of(), ImmutableSet.<Image> of(), ImmutableMap.<String, Credentials> of());
assertEquals(parser.apply(instance), new NodeMetadataBuilder().state(NodeState.RUNNING).privateAddresses(
ImmutableSet.<String> of()).publicAddresses(ImmutableSet.of("1.1.1.1")).id("us-east-1/id").imageId(
"us-east-1/image").providerId("id").build());
}
static Location provider = new LocationBuilder().scope(LocationScope.REGION).id("us-east-1")
.description("us-east-1").build();