fixed ebs volume parsing

This commit is contained in:
Adrian Cole 2010-09-15 09:25:47 -07:00
parent be69174a20
commit 7b360cad41
3 changed files with 129 additions and 10 deletions

View File

@ -138,7 +138,8 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
@Override
public Volume apply(Entry<String, EbsBlockDevice> from) {
return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(),
instance.getRootDeviceName().equals(from.getKey()), true);
instance.getRootDeviceName() != null
&& instance.getRootDeviceName().equals(from.getKey()), true);
}
});
@ -207,16 +208,21 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
private Location getLocationForAvailabilityZone(final RunningInstance instance) {
final String locationId = instance.getAvailabilityZone();
try {
Location location = Iterables.find(locations.get(), new Predicate<Location>() {
Location location = Iterables.find(locations.get(), new Predicate<Location>() {
@Override
public boolean apply(Location input) {
return input.getId().equals(locationId);
}
@Override
public boolean apply(Location input) {
return input.getId().equals(locationId);
}
});
return location;
});
return location;
} catch (NoSuchElementException e) {
logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
return null;
}
}
@VisibleForTesting

View File

@ -35,17 +35,25 @@ import org.jclouds.aws.domain.Region;
import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.compute.domain.EC2Hardware;
import org.jclouds.aws.ec2.compute.domain.RegionAndName;
import org.jclouds.aws.ec2.domain.Attachment;
import org.jclouds.aws.ec2.domain.AvailabilityZone;
import org.jclouds.aws.ec2.domain.Image;
import org.jclouds.aws.ec2.domain.InstanceState;
import org.jclouds.aws.ec2.domain.InstanceType;
import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.domain.RootDeviceType;
import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.domain.RunningInstance.EbsBlockDevice;
import org.jclouds.aws.ec2.services.AMIClient;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
@ -54,6 +62,8 @@ import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
@ -70,6 +80,100 @@ public class RunningInstanceToNodeMetadataTest {
}
DateService dateService = new SimpleDateFormatDateService();
@SuppressWarnings( { "unchecked", "rawtypes" })
@Test
public void testApplyWithEBSWhenBootIsInstanceStoreAndAvailabilityZoneNotFound() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
AMIClient amiClient = createMock(AMIClient.class);
expect(client.getAMIServices()).andReturn(amiClient).atLeastOnce();
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M1_SMALL));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class);
expect(instance.getId()).andReturn("i-3d640055").atLeastOnce();
expect(instance.getGroupIds()).andReturn(ImmutableSet.of("default")).atLeastOnce();
expect(instance.getKeyName()).andReturn("jclouds#tag#us-east-1#50").atLeastOnce();
expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING);
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1d", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn("174.129.1.50");
expect(instance.getPrivateIpAddress()).andReturn("10.202.117.241");
expect(instance.getRegion()).andReturn(Region.US_EAST_1).atLeastOnce();
expect(jcImage.getOperatingSystem()).andReturn(createMock(OperatingSystem.class)).atLeastOnce();
expect(instance.getImageId()).andReturn("ami-1515f07c").atLeastOnce();
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn(
(Set) ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn(
new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass"));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
expect(instance.getInstanceType()).andReturn(InstanceType.M1_SMALL).atLeastOnce();
expect(instance.getEbsBlockDevices()).andReturn(
ImmutableMap.<String, EbsBlockDevice> of("/dev/sdg", new EbsBlockDevice("vol-1f20d376",
Attachment.Status.ATTACHED, dateService.iso8601DateParse("2009-12-11T16:32:46.000Z"), false),
"/dev/sdj", new EbsBlockDevice("vol-c0eb78aa", Attachment.Status.ATTACHED, dateService
.iso8601DateParse("2010-06-17T10:43:28.000Z"), false)));
expect(instance.getRootDeviceType()).andReturn(RootDeviceType.INSTANCE_STORE);
expect(instance.getRootDeviceName()).andReturn(null).atLeastOnce();
replay(imageMap);
replay(client);
replay(amiClient);
replay(credentialsMap);
replay(credentialProvider);
replay(instance);
replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getTag(), "NOTAG-i-3d640055");
assertEquals(metadata.getLocation(), null);
assertEquals(metadata.getImageId(), "us-east-1/ami-1515f07c");
assertEquals(metadata.getHardware().getId(), "m1.small");
assertEquals(metadata.getHardware().getName(), "m1.small");
assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false),//
new VolumeImpl("vol-1f20d376", Volume.Type.SAN, null, "/dev/sdg", false, true),//
new VolumeImpl("vol-c0eb78aa", Volume.Type.SAN, null, "/dev/sdj", false, true)));
assertEquals(metadata.getCredentials(), new Credentials("user", "pass"));
verify(imageMap);
verify(jcImage);
verify(client);
verify(amiClient);
verify(credentialsMap);
verify(credentialProvider);
verify(instance);
}
@SuppressWarnings("unchecked")
@Test
public void testImageNotFoundAndLazyReturnsNull() throws UnknownHostException {

View File

@ -119,9 +119,11 @@ public class VolumeImpl implements Volume {
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
int result = 1;
result = prime * result + (bootDevice ? 1231 : 1237);
result = prime * result + ((device == null) ? 0 : device.hashCode());
result = prime * result + (durable ? 1231 : 1237);
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((size == null) ? 0 : size.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
@ -131,11 +133,13 @@ public class VolumeImpl implements Volume {
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
VolumeImpl other = (VolumeImpl) obj;
if (bootDevice != other.bootDevice)
return false;
if (device == null) {
if (other.device != null)
return false;
@ -143,6 +147,11 @@ public class VolumeImpl implements Volume {
return false;
if (durable != other.durable)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (size == null) {
if (other.size != null)
return false;