Issue 249: switch to use group, not key, for tag lookups; prefix jclouds created groups and keys with jclouds#tag so that we can differentiate what we've created from what the user has

This commit is contained in:
Adrian Cole 2010-05-10 21:35:59 -07:00
parent 6c14ae1831
commit 25fd278ecf
9 changed files with 238 additions and 65 deletions

View File

@ -48,6 +48,7 @@ import org.jclouds.compute.strategy.RebootNodeStrategy;
import org.jclouds.compute.strategy.RunNodesAndAddToSetStrategy; import org.jclouds.compute.strategy.RunNodesAndAddToSetStrategy;
import org.jclouds.compute.util.ComputeUtils; import org.jclouds.compute.util.ComputeUtils;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import static org.jclouds.util.Utils.*;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -88,19 +89,21 @@ public class EC2ComputeService extends BaseComputeService {
} }
private void deleteSecurityGroup(String region, String tag) { private void deleteSecurityGroup(String region, String tag) {
if (ec2Client.getSecurityGroupServices().describeSecurityGroupsInRegion(region, tag).size() > 0) { checkNotEmpty(tag, "tag");
logger.debug(">> deleting securityGroup(%s)", tag); String group = "jclouds#" + tag;
ec2Client.getSecurityGroupServices().deleteSecurityGroupInRegion(region, tag); if (ec2Client.getSecurityGroupServices().describeSecurityGroupsInRegion(region, group).size() > 0) {
logger.debug(">> deleting securityGroup(%s)", group);
ec2Client.getSecurityGroupServices().deleteSecurityGroupInRegion(region, group);
securityGroupMap.remove(new PortsRegionTag(region, tag, null)); // TODO: test this clear securityGroupMap.remove(new PortsRegionTag(region, tag, null)); // TODO: test this clear
// happens // happens
logger.debug("<< deleted securityGroup(%s)", tag); logger.debug("<< deleted securityGroup(%s)", group);
} }
} }
private void deleteKeyPair(String region, String tag) { private void deleteKeyPair(String region, String tag) {
for (KeyPair keyPair : ec2Client.getKeyPairServices().describeKeyPairsInRegion(region)) { for (KeyPair keyPair : ec2Client.getKeyPairServices().describeKeyPairsInRegion(region)) {
if (keyPair.getKeyName().matches(tag + "-[0-9]+")) { if (keyPair.getKeyName().matches("jclouds#" + tag + "-[0-9]+")) {
logger.debug(">> deleting keyPair(%s)", tag); logger.debug(">> deleting keyPair(%s)", keyPair.getKeyName());
ec2Client.getKeyPairServices().deleteKeyPairInRegion(region, keyPair.getKeyName()); ec2Client.getKeyPairServices().deleteKeyPairInRegion(region, keyPair.getKeyName());
credentialsMap.remove(new RegionTag(region, keyPair.getKeyName())); // TODO: test this credentialsMap.remove(new RegionTag(region, keyPair.getKeyName())); // TODO: test this
// clear happens // clear happens

View File

@ -66,7 +66,7 @@ public class CreateNewKeyPair implements Function<RegionTag, KeyPair> {
while (keyPair == null) { while (keyPair == null) {
try { try {
keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region,
tag + "-" + new SecureRandom().nextInt(100)); "jclouds#" + tag + "-" + new SecureRandom().nextInt(100));
logger.debug("<< created keyPair(%s)", keyPair.getKeyName()); logger.debug("<< created keyPair(%s)", keyPair.getKeyName());
} catch (AWSResponseException e) { } catch (AWSResponseException e) {
if (!e.getError().getCode().equals("InvalidKeyPair.Duplicate")) { if (!e.getError().getCode().equals("InvalidKeyPair.Duplicate")) {

View File

@ -114,11 +114,29 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
String name = null; // user doesn't determine a node name; String name = null; // user doesn't determine a node name;
URI uri = null; // no uri to get rest access to host info URI uri = null; // no uri to get rest access to host info
String tag = String.format("NOTAG-%s", instance.getId());// default
try {
tag = Iterables.getOnlyElement(
Iterables.filter(instance.getGroupIds(), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("jclouds#");
}
})).substring(8);
} catch (NoSuchElementException e) {
logger
.warn("no tag parsed from %s's groups: %s", instance.getId(), instance
.getGroupIds());
} catch (IllegalArgumentException e) {
logger.warn("too many groups match %s; %s's groups: %s", "jclouds#", instance.getId(),
instance.getGroupIds());
}
Credentials credentials = null;// default if no keypair exists Credentials credentials = null;// default if no keypair exists
String tag = String.format("NOTAG-%s", instance.getId());// default if no keypair exists
if (instance.getKeyName() != null) { if (instance.getKeyName() != null) {
tag = instance.getKeyName().replaceAll("-[0-9]+", "");
credentials = new Credentials(getLoginAccountFor(instance), getPrivateKeyOrNull(instance, credentials = new Credentials(getLoginAccountFor(instance), getPrivateKeyOrNull(instance,
tag)); tag));
} }

View File

@ -139,7 +139,8 @@ public class EC2RunNodesAndAddToSetStrategy implements RunNodesAndAddToSetStrate
credentialsMap.put(new RegionTag(region, keyPair.getKeyName()), keyPair); credentialsMap.put(new RegionTag(region, keyPair.getKeyName()), keyPair);
TemplateOptions options = template.getOptions(); TemplateOptions options = template.getOptions();
PortsRegionTag portsRegionTag = new PortsRegionTag(region, tag, options.getInboundPorts()); String group = "jclouds#" +tag;
PortsRegionTag portsRegionTag = new PortsRegionTag(region, group, options.getInboundPorts());
if (!securityGroupMap.containsKey(portsRegionTag)) { if (!securityGroupMap.containsKey(portsRegionTag)) {
securityGroupMap.put(portsRegionTag, createSecurityGroupIfNeeded.apply(portsRegionTag)); securityGroupMap.put(portsRegionTag, createSecurityGroupIfNeeded.apply(portsRegionTag));
} }
@ -148,10 +149,10 @@ public class EC2RunNodesAndAddToSetStrategy implements RunNodesAndAddToSetStrate
.debug( .debug(
">> running %d instance region(%s) zone(%s) ami(%s) type(%s) keyPair(%s) securityGroup(%s)", ">> running %d instance region(%s) zone(%s) ami(%s) type(%s) keyPair(%s) securityGroup(%s)",
count, region, zone, template.getImage().getId(), count, region, zone, template.getImage().getId(),
ec2Size.getInstanceType(), tag, tag); ec2Size.getInstanceType(), keyPair.getKeyName(), group);
RunInstancesOptions instanceOptions = withKeyName(keyPair.getKeyName())// key RunInstancesOptions instanceOptions = withKeyName(keyPair.getKeyName())// key
.asType(ec2Size.getInstanceType())// instance size .asType(ec2Size.getInstanceType())// instance size
.withSecurityGroup(tag)// group I created above .withSecurityGroup(group)// group I created above
.withAdditionalInfo(tag); .withAdditionalInfo(tag);
Reservation reservation = ec2Client.getInstanceServices().runInstancesInRegion(region, zone, Reservation reservation = ec2Client.getInstanceServices().runInstancesInRegion(region, zone,

View File

@ -27,7 +27,9 @@ import java.util.Set;
import org.jclouds.aws.ec2.domain.Attachment.Status; import org.jclouds.aws.ec2.domain.Attachment.Status;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.internal.Nullable; import com.google.inject.internal.Nullable;
/** /**
@ -45,7 +47,7 @@ public class RunningInstance implements Comparable<RunningInstance> {
private final boolean deleteOnTermination; private final boolean deleteOnTermination;
public EbsBlockDevice(String volumeId, Status attachmentStatus, Date attachTime, public EbsBlockDevice(String volumeId, Status attachmentStatus, Date attachTime,
boolean deleteOnTermination) { boolean deleteOnTermination) {
super(); super();
this.volumeId = volumeId; this.volumeId = volumeId;
this.attachmentStatus = attachmentStatus; this.attachmentStatus = attachmentStatus;
@ -116,6 +118,8 @@ public class RunningInstance implements Comparable<RunningInstance> {
} }
private final String region; private final String region;
private final Set<String> groupIds = Sets.newLinkedHashSet();
private final String amiLaunchIndex; private final String amiLaunchIndex;
@Nullable @Nullable
private final String dnsName; private final String dnsName;
@ -156,15 +160,17 @@ public class RunningInstance implements Comparable<RunningInstance> {
return (this == o) ? 0 : getId().compareTo(o.getId()); return (this == o) ? 0 : getId().compareTo(o.getId());
} }
public RunningInstance(String region, @Nullable String amiLaunchIndex, @Nullable String dnsName, public RunningInstance(String region, Iterable<String> groupIds,
String imageId, String instanceId, InstanceState instanceState, @Nullable String amiLaunchIndex, @Nullable String dnsName, String imageId,
String instanceType, @Nullable InetAddress ipAddress, @Nullable String kernelId, String instanceId, InstanceState instanceState, String instanceType,
@Nullable String keyName, Date launchTime, boolean monitoring, @Nullable InetAddress ipAddress, @Nullable String kernelId, @Nullable String keyName,
String availabilityZone, @Nullable String platform, Date launchTime, boolean monitoring, String availabilityZone,
@Nullable String privateDnsName, @Nullable InetAddress privateIpAddress, @Nullable String platform, @Nullable String privateDnsName,
Set<String> productCodes, @Nullable String ramdiskId, @Nullable String reason, @Nullable InetAddress privateIpAddress, Set<String> productCodes,
@Nullable String subnetId, @Nullable String vpcId, RootDeviceType rootDeviceType, @Nullable String ramdiskId, @Nullable String reason, @Nullable String subnetId,
@Nullable String rootDeviceName, Map<String, EbsBlockDevice> ebsBlockDevices) { @Nullable String vpcId, RootDeviceType rootDeviceType, @Nullable String rootDeviceName,
Map<String, EbsBlockDevice> ebsBlockDevices) {
Iterables.addAll(this.groupIds, checkNotNull(groupIds, "groupIds"));
this.region = checkNotNull(region, "region"); this.region = checkNotNull(region, "region");
this.amiLaunchIndex = amiLaunchIndex; // nullable on runinstances. this.amiLaunchIndex = amiLaunchIndex; // nullable on runinstances.
this.dnsName = dnsName; // nullable on runinstances. this.dnsName = dnsName; // nullable on runinstances.
@ -351,7 +357,7 @@ public class RunningInstance implements Comparable<RunningInstance> {
public String getRootDeviceName() { public String getRootDeviceName() {
return rootDeviceName; return rootDeviceName;
} }
/** /**
* EBS volumes associated with the instance. * EBS volumes associated with the instance.
*/ */
@ -359,6 +365,13 @@ public class RunningInstance implements Comparable<RunningInstance> {
return ebsBlockDevices; return ebsBlockDevices;
} }
/**
* Names of the security groups.
*/
public Set<String> getGroupIds() {
return groupIds;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@ -367,6 +380,7 @@ public class RunningInstance implements Comparable<RunningInstance> {
result = prime * result + ((availabilityZone == null) ? 0 : availabilityZone.hashCode()); result = prime * result + ((availabilityZone == null) ? 0 : availabilityZone.hashCode());
result = prime * result + ((dnsName == null) ? 0 : dnsName.hashCode()); result = prime * result + ((dnsName == null) ? 0 : dnsName.hashCode());
result = prime * result + ((ebsBlockDevices == null) ? 0 : ebsBlockDevices.hashCode()); result = prime * result + ((ebsBlockDevices == null) ? 0 : ebsBlockDevices.hashCode());
result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode());
result = prime * result + ((imageId == null) ? 0 : imageId.hashCode()); result = prime * result + ((imageId == null) ? 0 : imageId.hashCode());
result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode()); result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode());
result = prime * result + ((instanceState == null) ? 0 : instanceState.hashCode()); result = prime * result + ((instanceState == null) ? 0 : instanceState.hashCode());
@ -417,6 +431,11 @@ public class RunningInstance implements Comparable<RunningInstance> {
return false; return false;
} else if (!ebsBlockDevices.equals(other.ebsBlockDevices)) } else if (!ebsBlockDevices.equals(other.ebsBlockDevices))
return false; return false;
if (groupIds == null) {
if (other.groupIds != null)
return false;
} else if (!groupIds.equals(other.groupIds))
return false;
if (imageId == null) { if (imageId == null) {
if (other.imageId != null) if (other.imageId != null)
return false; return false;
@ -511,15 +530,15 @@ public class RunningInstance implements Comparable<RunningInstance> {
public String toString() { public String toString() {
return "RunningInstance [amiLaunchIndex=" + amiLaunchIndex + ", availabilityZone=" return "RunningInstance [amiLaunchIndex=" + amiLaunchIndex + ", availabilityZone="
+ availabilityZone + ", dnsName=" + dnsName + ", ebsBlockDevices=" + ebsBlockDevices + availabilityZone + ", dnsName=" + dnsName + ", ebsBlockDevices=" + ebsBlockDevices
+ ", imageId=" + imageId + ", instanceId=" + instanceId + ", instanceState=" + ", groupIds=" + groupIds + ", imageId=" + imageId + ", instanceId=" + instanceId
+ instanceState + ", instanceType=" + instanceType + ", ipAddress=" + ipAddress + ", instanceState=" + instanceState + ", instanceType=" + instanceType
+ ", kernelId=" + kernelId + ", keyName=" + keyName + ", launchTime=" + launchTime + ", ipAddress=" + ipAddress + ", kernelId=" + kernelId + ", keyName=" + keyName
+ ", monitoring=" + monitoring + ", platform=" + platform + ", privateDnsName=" + ", launchTime=" + launchTime + ", monitoring=" + monitoring + ", platform="
+ privateDnsName + ", privateIpAddress=" + privateIpAddress + ", productCodes=" + platform + ", privateDnsName=" + privateDnsName + ", privateIpAddress="
+ productCodes + ", ramdiskId=" + ramdiskId + ", reason=" + reason + ", region=" + privateIpAddress + ", productCodes=" + productCodes + ", ramdiskId=" + ramdiskId
+ region + ", rootDeviceName=" + rootDeviceName + ", rootDeviceType=" + ", reason=" + reason + ", region=" + region + ", rootDeviceName=" + rootDeviceName
+ rootDeviceType + ", subnetId=" + subnetId + ", vpcId=" + vpcId + "]"; + ", rootDeviceType=" + rootDeviceType + ", subnetId=" + subnetId + ", vpcId="
+ vpcId + "]";
} }
} }

View File

@ -208,11 +208,11 @@ public abstract class BaseReservationHandler<T> extends HandlerWithResult<T> {
String region = EC2Utils.findRegionInArgsOrNull(request); String region = EC2Utils.findRegionInArgsOrNull(request);
if (region == null) if (region == null)
region = defaultRegion; region = defaultRegion;
instances.add(new RunningInstance(region, amiLaunchIndex, dnsName, imageId, instanceId, instances.add(new RunningInstance(region, groupIds, amiLaunchIndex, dnsName, imageId,
instanceState, instanceType, ipAddress, kernelId, keyName, launchTime, instanceId, instanceState, instanceType, ipAddress, kernelId, keyName,
monitoring, availabilityZone, platform, privateDnsName, privateIpAddress, launchTime, monitoring, availabilityZone, platform, privateDnsName,
productCodes, ramdiskId, reason, subnetId, vpcId, rootDeviceType, rootDeviceName, privateIpAddress, productCodes, ramdiskId, reason, subnetId, vpcId,
ebsBlockDevices)); rootDeviceType, rootDeviceName, ebsBlockDevices));
this.amiLaunchIndex = null; this.amiLaunchIndex = null;
this.dnsName = null; this.dnsName = null;
this.imageId = null; this.imageId = null;

View File

@ -55,10 +55,9 @@ import com.google.common.collect.ImmutableSet;
*/ */
@Test(groups = "unit", testName = "ec2.RunningInstanceToNodeMetadataTest") @Test(groups = "unit", testName = "ec2.RunningInstanceToNodeMetadataTest")
public class RunningInstanceToNodeMetadataTest { public class RunningInstanceToNodeMetadataTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testApplyWithNoKeyPairCreatesTagOfIdPrefixedByTagAndNullCredentials() public void testApplyWithNoSecurityGroupCreatesTagOfIdPrefixedByTagAndNullCredentials()
throws UnknownHostException { throws UnknownHostException {
AMIClient amiClient = createMock(AMIClient.class); AMIClient amiClient = createMock(AMIClient.class);
Map<RegionTag, KeyPair> credentialsMap = createMock(Map.class); Map<RegionTag, KeyPair> credentialsMap = createMock(Map.class);
@ -73,6 +72,7 @@ public class RunningInstanceToNodeMetadataTest {
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
expect(instance.getId()).andReturn("id").atLeastOnce(); expect(instance.getId()).andReturn("id").atLeastOnce();
expect(instance.getGroupIds()).andReturn(ImmutableSet.<String> of()).atLeastOnce();
expect(instance.getKeyName()).andReturn(null).atLeastOnce(); expect(instance.getKeyName()).andReturn(null).atLeastOnce();
expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING); expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING);
@ -114,7 +114,64 @@ public class RunningInstanceToNodeMetadataTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testApplyWithKeyPairCreatesTagOfParsedKeyPairAndCredentialsBasedOnIt() public void testApplyWithNoKeyPairCreatesTagOfParsedSecurityGroupAndNullCredentials()
throws UnknownHostException {
AMIClient amiClient = createMock(AMIClient.class);
Map<RegionTag, KeyPair> credentialsMap = createMock(Map.class);
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
Set<org.jclouds.compute.domain.Image> images = ImmutableSet
.<org.jclouds.compute.domain.Image> of(jcImage);
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Set<Location> locations = ImmutableSet.<Location> of(location);
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class);
expect(instance.getId()).andReturn("id").atLeastOnce();
expect(instance.getGroupIds()).andReturn(ImmutableSet.of("jclouds#tag")).atLeastOnce();
expect(instance.getKeyName()).andReturn(null).atLeastOnce();
expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING);
expect(instance.getIpAddress()).andReturn(
InetAddress.getByAddress(new byte[] { 12, 10, 10, 1 }));
expect(instance.getPrivateIpAddress()).andReturn(
InetAddress.getByAddress(new byte[] { 10, 10, 10, 1 }));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
expect(instance.getImageId()).andReturn("imageId").atLeastOnce();
expect(jcImage.getId()).andReturn("imageId").atLeastOnce();
expect(jcImage.getLocation()).andReturn(location).atLeastOnce();
expect(instance.getInstanceType()).andReturn(InstanceType.C1_XLARGE).atLeastOnce();
replay(jcImage);
replay(amiClient);
replay(credentialsMap);
replay(credentialProvider);
replay(instance);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(amiClient,
credentialsMap, credentialProvider, images, locations,
new RunningInstanceToStorageMappingUnix());
NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), locations.iterator().next());
assertEquals(metadata.getImage(), jcImage);
assertEquals(metadata.getTag(), "tag");
assertEquals(metadata.getCredentials(), null);
verify(jcImage);
verify(amiClient);
verify(credentialsMap);
verify(credentialProvider);
verify(instance);
}
@SuppressWarnings("unchecked")
@Test
public void testApplyWithKeyPairCreatesTagOfParsedSecurityGroupAndCredentialsBasedOnIt()
throws UnknownHostException { throws UnknownHostException {
AMIClient amiClient = createMock(AMIClient.class); AMIClient amiClient = createMock(AMIClient.class);
Map<RegionTag, KeyPair> credentialsMap = createMock(Map.class); Map<RegionTag, KeyPair> credentialsMap = createMock(Map.class);
@ -124,7 +181,8 @@ public class RunningInstanceToNodeMetadataTest {
Image image = createMock(Image.class); Image image = createMock(Image.class);
expect(instance.getId()).andReturn("id").atLeastOnce(); expect(instance.getId()).andReturn("id").atLeastOnce();
expect(instance.getKeyName()).andReturn("keyName-100").atLeastOnce(); expect(instance.getGroupIds()).andReturn(ImmutableSet.of("jclouds#tag")).atLeastOnce();
expect(instance.getKeyName()).andReturn("jclouds#keyName").atLeastOnce();
expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING); expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING);
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
@ -149,8 +207,8 @@ public class RunningInstanceToNodeMetadataTest {
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass")); expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
expect(credentialsMap.get(new RegionTag(Region.US_EAST_1, "keyName-100"))).andReturn( expect(credentialsMap.get(new RegionTag(Region.US_EAST_1, "jclouds#keyName"))).andReturn(
new KeyPair(Region.US_EAST_1, "keyName-100", "keyFingerprint", "pass")); new KeyPair(Region.US_EAST_1, "jclouds#keyName", "keyFingerprint", "pass"));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce(); expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
@ -168,7 +226,7 @@ public class RunningInstanceToNodeMetadataTest {
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getTag(), "keyName"); assertEquals(metadata.getTag(), "tag");
assertEquals(metadata.getLocation(), location); assertEquals(metadata.getLocation(), location);
assertEquals(metadata.getImage(), jcImage); assertEquals(metadata.getImage(), jcImage);
@ -182,4 +240,74 @@ public class RunningInstanceToNodeMetadataTest {
} }
@SuppressWarnings("unchecked")
@Test
public void testApplyWithTwoSecurityGroups()
throws UnknownHostException {
AMIClient amiClient = createMock(AMIClient.class);
Map<RegionTag, KeyPair> credentialsMap = createMock(Map.class);
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class);
expect(instance.getId()).andReturn("id").atLeastOnce();
expect(instance.getGroupIds()).andReturn(ImmutableSet.of("jclouds#tag","jclouds#tag2")).atLeastOnce();
expect(instance.getKeyName()).andReturn("jclouds#keyName").atLeastOnce();
expect(instance.getInstanceState()).andReturn(InstanceState.RUNNING);
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Set<Location> locations = ImmutableSet.<Location> of(location);
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
Set<org.jclouds.compute.domain.Image> images = ImmutableSet
.<org.jclouds.compute.domain.Image> of(jcImage);
expect(instance.getIpAddress()).andReturn(
InetAddress.getByAddress(new byte[] { 12, 10, 10, 1 }));
expect(instance.getPrivateIpAddress()).andReturn(
InetAddress.getByAddress(new byte[] { 10, 10, 10, 1 }));
expect(instance.getRegion()).andReturn(Region.US_EAST_1).atLeastOnce();
expect(instance.getImageId()).andReturn("imageId").atLeastOnce();
expect(jcImage.getId()).andReturn("imageId").atLeastOnce();
expect(jcImage.getLocation()).andReturn(location).atLeastOnce();
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("imageId"))).andReturn(
ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
expect(credentialsMap.get(new RegionTag(Region.US_EAST_1, "jclouds#keyName"))).andReturn(
new KeyPair(Region.US_EAST_1, "jclouds#keyName", "keyFingerprint", "pass"));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
expect(instance.getInstanceType()).andReturn(InstanceType.C1_XLARGE).atLeastOnce();
replay(amiClient);
replay(credentialsMap);
replay(credentialProvider);
replay(instance);
replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(amiClient,
credentialsMap, credentialProvider, images, locations,
new RunningInstanceToStorageMappingUnix());
NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getTag(), "NOTAG-id");
assertEquals(metadata.getLocation(), location);
assertEquals(metadata.getImage(), jcImage);
assertEquals(metadata.getCredentials(), new Credentials("user", "pass"));
verify(jcImage);
verify(amiClient);
verify(credentialsMap);
verify(credentialProvider);
verify(instance);
}
} }

View File

@ -70,7 +70,8 @@ public class DescribeInstancesResponseHandlerTest extends BaseEC2HandlerTest {
Set<Reservation> contents = Sets.newTreeSet(); Set<Reservation> contents = Sets.newTreeSet();
contents.add(new Reservation(defaultRegion, ImmutableSet.of("adriancole.ec2ingress"), contents.add(new Reservation(defaultRegion, ImmutableSet.of("adriancole.ec2ingress"),
ImmutableSet.of(new RunningInstance(defaultRegion, "0", ImmutableSet.of(new RunningInstance(defaultRegion, ImmutableSet
.of("adriancole.ec2ingress"), "0",
"ec2-174-129-81-68.compute-1.amazonaws.com", "ami-1fd73376", "i-0799056f", "ec2-174-129-81-68.compute-1.amazonaws.com", "ami-1fd73376", "i-0799056f",
InstanceState.RUNNING, InstanceType.M1_SMALL, InetAddress InstanceState.RUNNING, InstanceType.M1_SMALL, InetAddress
.getByName("174.129.81.68"), "aki-a71cf9ce", "adriancole.ec21", .getByName("174.129.81.68"), "aki-a71cf9ce", "adriancole.ec21",
@ -92,17 +93,19 @@ public class DescribeInstancesResponseHandlerTest extends BaseEC2HandlerTest {
Set<Reservation> contents = Sets.newTreeSet(); Set<Reservation> contents = Sets.newTreeSet();
contents.add(new Reservation(defaultRegion, ImmutableSet.of("default"), ImmutableSet.of( contents.add(new Reservation(defaultRegion, ImmutableSet.of("default"), ImmutableSet.of(
new RunningInstance(defaultRegion, "23", "ec2-72-44-33-4.compute-1.amazonaws.com", new RunningInstance(defaultRegion, ImmutableSet.of("default"), "23",
"ami-6ea54007", "i-28a64341", InstanceState.RUNNING, InstanceType.M1_LARGE, "ec2-72-44-33-4.compute-1.amazonaws.com", "ami-6ea54007", "i-28a64341",
(InetAddress) null, "aki-ba3adfd3", "example-key-name", dateService InstanceState.RUNNING, InstanceType.M1_LARGE, (InetAddress) null,
"aki-ba3adfd3", "example-key-name", dateService
.iso8601DateParse("2007-08-07T11:54:42.000Z"), false, .iso8601DateParse("2007-08-07T11:54:42.000Z"), false,
AvailabilityZone.US_EAST_1B, null, "10-251-50-132.ec2.internal", null, AvailabilityZone.US_EAST_1B, null, "10-251-50-132.ec2.internal", null,
ImmutableSet.of("774F4FF8"), "ari-badbad00", null, null, null, ImmutableSet.of("774F4FF8"), "ari-badbad00", null, null, null,
RootDeviceType.INSTANCE_STORE, null, ImmutableMap RootDeviceType.INSTANCE_STORE, null, ImmutableMap
.<String, EbsBlockDevice> of()), new RunningInstance( .<String, EbsBlockDevice> of()), new RunningInstance(
defaultRegion, "23", "ec2-72-44-33-6.compute-1.amazonaws.com", defaultRegion, ImmutableSet.of("default"), "23",
"ami-6ea54007", "i-28a64435", InstanceState.RUNNING, InstanceType.M1_LARGE, "ec2-72-44-33-6.compute-1.amazonaws.com", "ami-6ea54007", "i-28a64435",
(InetAddress) null, "aki-ba3adfd3", "example-key-name", dateService InstanceState.RUNNING, InstanceType.M1_LARGE, (InetAddress) null,
"aki-ba3adfd3", "example-key-name", dateService
.iso8601DateParse("2007-08-07T11:54:42.000Z"), false, .iso8601DateParse("2007-08-07T11:54:42.000Z"), false,
AvailabilityZone.US_EAST_1B, null, "10-251-50-134.ec2.internal", null, AvailabilityZone.US_EAST_1B, null, "10-251-50-134.ec2.internal", null,
ImmutableSet.of("774F4FF8"), "ari-badbad00", null, null, null, ImmutableSet.of("774F4FF8"), "ari-badbad00", null, null, null,
@ -121,7 +124,8 @@ public class DescribeInstancesResponseHandlerTest extends BaseEC2HandlerTest {
Set<Reservation> contents = Sets.newTreeSet(); Set<Reservation> contents = Sets.newTreeSet();
contents.add(new Reservation(defaultRegion, ImmutableSet.of("adriancole.ec2ebsingress"), contents.add(new Reservation(defaultRegion, ImmutableSet.of("adriancole.ec2ebsingress"),
ImmutableSet.of(new RunningInstance(defaultRegion, "0", ImmutableSet.of(new RunningInstance(defaultRegion, ImmutableSet
.of("adriancole.ec2ebsingress"), "0",
"ec2-75-101-203-146.compute-1.amazonaws.com", "ami-849875ed", "i-e564438d", "ec2-75-101-203-146.compute-1.amazonaws.com", "ami-849875ed", "i-e564438d",
InstanceState.RUNNING, InstanceType.M1_SMALL, InetAddress InstanceState.RUNNING, InstanceType.M1_SMALL, InetAddress
.getByName("75.101.203.146"), "aki-a71cf9ce", .getByName("75.101.203.146"), "aki-a71cf9ce",

View File

@ -67,25 +67,25 @@ public class RunInstancesResponseHandlerTest extends BaseEC2HandlerTest {
InputStream is = getClass().getResourceAsStream("/ec2/run_instances.xml"); InputStream is = getClass().getResourceAsStream("/ec2/run_instances.xml");
Reservation expected = new Reservation(defaultRegion, ImmutableSortedSet.of("default"), Reservation expected = new Reservation(defaultRegion, ImmutableSortedSet.of("default"),
ImmutableSet.of(new RunningInstance(defaultRegion, "0", null, "ami-60a54009", ImmutableSet.of(new RunningInstance(defaultRegion, ImmutableSortedSet.of("default"),
"i-2ba64342", InstanceState.PENDING, InstanceType.M1_SMALL, "0", null, "ami-60a54009", "i-2ba64342", InstanceState.PENDING,
InstanceType.M1_SMALL, (InetAddress) null, null, "example-key-name",
dateService.iso8601DateParse("2007-08-07T11:51:50.000Z"), true,
AvailabilityZone.US_EAST_1B, null, null, (InetAddress) null, Sets
.<String> newTreeSet(), null, null, null, null,
RootDeviceType.INSTANCE_STORE, null, ImmutableMap
.<String, EbsBlockDevice> of()), new RunningInstance(
defaultRegion, ImmutableSortedSet.of("default"), "1", null, "ami-60a54009",
"i-2bc64242", InstanceState.PENDING, InstanceType.M1_SMALL,
(InetAddress) null, null, "example-key-name", dateService (InetAddress) null, null, "example-key-name", dateService
.iso8601DateParse("2007-08-07T11:51:50.000Z"), true, .iso8601DateParse("2007-08-07T11:51:50.000Z"), true,
AvailabilityZone.US_EAST_1B, null, null, (InetAddress) null, Sets AvailabilityZone.US_EAST_1B, null, null, (InetAddress) null, Sets
.<String> newTreeSet(), null, null, null, null, .<String> newTreeSet(), null, null, null, null,
RootDeviceType.INSTANCE_STORE, null, ImmutableMap RootDeviceType.INSTANCE_STORE, null, ImmutableMap
.<String, EbsBlockDevice> of()), new RunningInstance( .<String, EbsBlockDevice> of()), new RunningInstance(
defaultRegion, "1", null, "ami-60a54009", "i-2bc64242", defaultRegion, ImmutableSortedSet.of("default"), "2", null, "ami-60a54009",
InstanceState.PENDING, InstanceType.M1_SMALL, (InetAddress) null, null, "i-2be64332", InstanceState.PENDING, InstanceType.M1_SMALL,
"example-key-name", dateService (InetAddress) null, null, "example-key-name", dateService
.iso8601DateParse("2007-08-07T11:51:50.000Z"), true,
AvailabilityZone.US_EAST_1B, null, null, (InetAddress) null, Sets
.<String> newTreeSet(), null, null, null, null,
RootDeviceType.INSTANCE_STORE, null, ImmutableMap
.<String, EbsBlockDevice> of()), new RunningInstance(
defaultRegion, "2", null, "ami-60a54009", "i-2be64332",
InstanceState.PENDING, InstanceType.M1_SMALL, (InetAddress) null, null,
"example-key-name", dateService
.iso8601DateParse("2007-08-07T11:51:50.000Z"), true, .iso8601DateParse("2007-08-07T11:51:50.000Z"), true,
AvailabilityZone.US_EAST_1B, null, null, (InetAddress) null, Sets AvailabilityZone.US_EAST_1B, null, null, (InetAddress) null, Sets
.<String> newTreeSet(), null, null, null, null, .<String> newTreeSet(), null, null, null, null,