diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadataTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadataTest.java index e2681832c5..6d85b9376c 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadataTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadataTest.java @@ -29,6 +29,7 @@ import javax.annotation.Nullable; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeState; import org.jclouds.compute.domain.OperatingSystem; @@ -108,11 +109,12 @@ public class RunningInstanceToNodeMetadataTest { ImmutableSet. of(), ImmutableMap. of()); RunningInstance server = firstInstanceFromResource("/describe_instances_running.xml"); - - assertEquals(parser.apply(server), new NodeMetadataBuilder().hostname("ip-10-243-42-70").state(NodeState.RUNNING) - .privateAddresses(ImmutableSet.of("10.243.42.70")).publicAddresses(ImmutableSet.of("174.129.81.68")) - .imageId("us-east-1/ami-82e4b5c7").id("us-east-1/i-0799056f").providerId("i-0799056f").location(provider) - .build()); + NodeMetadata expected = new NodeMetadataBuilder().hostname("ip-10-243-42-70").state(NodeState.RUNNING) + .privateAddresses(ImmutableSet.of("10.243.42.70")).publicAddresses(ImmutableSet.of("174.129.81.68")) + .imageId("us-east-1/ami-82e4b5c7").id("us-east-1/i-0799056f").providerId("i-0799056f") + .location(provider).build(); + + assertEquals(parser.apply(server), expected); } @Test diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java index 566204247f..c221468dc3 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java @@ -120,10 +120,10 @@ public abstract class BaseAWSReservationHandler extends HandlerForGeneratedRe } else if (equalsOrSuffix(qName, "groupName")) { switch (itemDepth) { case 2: - builder.securityGroupIdToName(groupId, currentOrNull(currentText)); + reservationGroupIdToNames.put(groupId, currentOrNull(currentText)); break; case 3: - reservationGroupIdToNames.put(groupId, currentOrNull(currentText)); + builder.securityGroupIdToName(groupId, currentOrNull(currentText)); break; } groupId = null; diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/functions/AWSRunningInstanceToNodeMetadataTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/functions/AWSRunningInstanceToNodeMetadataTest.java new file mode 100644 index 0000000000..08e807adc2 --- /dev/null +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/functions/AWSRunningInstanceToNodeMetadataTest.java @@ -0,0 +1,157 @@ +/** + * + * Copyright (C) 2011 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.jclouds.aws.ec2.compute.functions; + +import static org.testng.Assert.assertEquals; + +import java.util.Map; +import java.util.Set; + +import org.jclouds.aws.ec2.domain.AWSRunningInstance; +import org.jclouds.aws.ec2.domain.MonitoringState; +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadataBuilder; +import org.jclouds.compute.domain.NodeState; +import org.jclouds.date.DateService; +import org.jclouds.domain.Credentials; +import org.jclouds.domain.Location; +import org.jclouds.domain.LocationBuilder; +import org.jclouds.domain.LocationScope; +import org.jclouds.ec2.compute.config.EC2ComputeServiceDependenciesModule; +import org.jclouds.ec2.compute.domain.RegionAndName; +import org.jclouds.ec2.domain.Attachment; +import org.jclouds.ec2.domain.BlockDevice; +import org.jclouds.ec2.domain.InstanceState; +import org.jclouds.ec2.domain.RootDeviceType; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.inject.Guice; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit") +public class AWSRunningInstanceToNodeMetadataTest { + + private static final String defaultRegion = "us-east-1"; + static Location provider = new LocationBuilder().scope(LocationScope.REGION).id(defaultRegion).description( + defaultRegion).build(); + + private DateService dateService; + + @BeforeTest + protected void setUpInjector() { + dateService = Guice.createInjector().getInstance(DateService.class); + assert dateService != null; + } + + @Test + public void test2Nodes() { + + AWSRunningInstanceToNodeMetadata parser = createNodeParser(ImmutableSet. of(), ImmutableSet + . of(), ImmutableSet. of(), ImmutableMap. of()); + + ImmutableSet contents = ImmutableSet.of(new AWSRunningInstance.Builder() + .region(defaultRegion).instanceId("i-911444f0").imageId("ami-63be790a").instanceState( + InstanceState.RUNNING).privateDnsName("ip-10-212-81-7.ec2.internal").dnsName( + "ec2-174-129-173-155.compute-1.amazonaws.com").keyName("jclouds#zkclustertest#us-east-1#23") + .amiLaunchIndex("0").instanceType("t1.micro").launchTime( + dateService.iso8601DateParse("2011-08-16T13:40:50.000Z")).availabilityZone("us-east-1c") + .kernelId("aki-427d952b").monitoringState(MonitoringState.DISABLED).privateIpAddress("10.212.81.7") + .ipAddress("174.129.173.155").securityGroupIdToNames( + ImmutableMap. of("sg-ef052b86", "jclouds#zkclustertest#us-east-1")) + .rootDeviceType(RootDeviceType.EBS).rootDeviceName("/dev/sda1").device( + "/dev/sda1", + new BlockDevice("vol-5829fc32", Attachment.Status.ATTACHED, dateService + .iso8601DateParse("2011-08-16T13:41:19.000Z"), true)) + .virtualizationType("paravirtual").build(),// + new AWSRunningInstance.Builder().region(defaultRegion).instanceId("i-931444f2").imageId("ami-63be790a") + .instanceState(InstanceState.RUNNING).privateDnsName("ip-10-212-185-8.ec2.internal").dnsName( + "ec2-50-19-207-248.compute-1.amazonaws.com").keyName( + "jclouds#zkclustertest#us-east-1#23").amiLaunchIndex("0").instanceType("t1.micro") + .launchTime(dateService.iso8601DateParse("2011-08-16T13:40:50.000Z")).availabilityZone( + "us-east-1c").kernelId("aki-427d952b").monitoringState(MonitoringState.DISABLED) + .privateIpAddress("10.212.185.8").ipAddress("50.19.207.248").securityGroupIdToNames( + ImmutableMap. of("sg-ef052b86", "jclouds#zkclustertest#us-east-1")) + .rootDeviceType(RootDeviceType.EBS).rootDeviceName("/dev/sda1").device( + "/dev/sda1", + new BlockDevice("vol-5029fc3a", Attachment.Status.ATTACHED, dateService + .iso8601DateParse("2011-08-16T13:41:19.000Z"), true)).virtualizationType( + "paravirtual").build()); + + assertEquals(parser.apply(Iterables.get(contents, 0)), new NodeMetadataBuilder().state(NodeState.RUNNING).group( + "zkclustertest").hostname("ip-10-212-81-7").privateAddresses(ImmutableSet.of("10.212.81.7")) + .publicAddresses(ImmutableSet.of("174.129.173.155")).imageId("us-east-1/ami-63be790a").id( + "us-east-1/i-911444f0").providerId("i-911444f0").build()); + assertEquals(parser.apply(Iterables.get(contents, 1)), new NodeMetadataBuilder().state(NodeState.RUNNING).group( + "zkclustertest").hostname("ip-10-212-185-8").privateAddresses(ImmutableSet.of("10.212.185.8")) + .publicAddresses(ImmutableSet.of("50.19.207.248")).imageId("us-east-1/ami-63be790a").id( + "us-east-1/i-931444f2").providerId("i-931444f2").build()); + } + + protected AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet hardware, + final ImmutableSet locations, Set images, + Map credentialStore) { + Map instanceToNodeState = EC2ComputeServiceDependenciesModule.instanceToNodeState; + + Map instanceToImage = Maps.uniqueIndex(images, new Function() { + + @Override + public RegionAndName apply(Image from) { + return new RegionAndName(from.getLocation().getId(), from.getProviderId()); + } + + }); + + return createNodeParser(hardware, locations, credentialStore, instanceToNodeState, instanceToImage); + } + + private AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet hardware, + final ImmutableSet locations, Map credentialStore, + Map instanceToNodeState, Map instanceToImage) { + Supplier> locationSupplier = new Supplier>() { + + @Override + public Set get() { + return locations; + } + + }; + Supplier> hardwareSupplier = new Supplier>() { + + @Override + public Set get() { + return hardware; + } + + }; + AWSRunningInstanceToNodeMetadata parser = new AWSRunningInstanceToNodeMetadata(instanceToNodeState, + credentialStore, instanceToImage, locationSupplier, hardwareSupplier); + return parser; + } + +} diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java index 5ae799a931..004af6d716 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java @@ -41,6 +41,7 @@ import org.jclouds.location.Region; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.inject.AbstractModule; import com.google.inject.Guice; @@ -84,6 +85,49 @@ public class AWSDescribeInstancesResponseHandlerTest extends BaseEC2HandlerTest assertEquals(result.toString(), contents.toString()); } + + public void testWhenRunningLatest() throws UnknownHostException { + + Set> contents = ImmutableSet.of(new Reservation( + defaultRegion, ImmutableSet.of("jclouds#ec2-s#us-east-1"), ImmutableSet.of( + new AWSRunningInstance.Builder().region(defaultRegion).instanceId("i-911444f0").imageId( + "ami-63be790a").instanceState(InstanceState.RUNNING).privateDnsName( + "ip-10-212-81-7.ec2.internal").dnsName("ec2-174-129-173-155.compute-1.amazonaws.com") + .keyName("jclouds#zkclustertest#us-east-1#23").amiLaunchIndex("0").instanceType( + "t1.micro").launchTime( + dateService.iso8601DateParse("2011-08-16T13:40:50.000Z")).availabilityZone( + "us-east-1c").kernelId("aki-427d952b").monitoringState( + MonitoringState.DISABLED).privateIpAddress("10.212.81.7").ipAddress( + "174.129.173.155").securityGroupIdToNames( + ImmutableMap. of("sg-ef052b86", + "jclouds#zkclustertest#us-east-1")).rootDeviceType( + RootDeviceType.EBS).rootDeviceName("/dev/sda1").device( + "/dev/sda1", + new BlockDevice("vol-5829fc32", Attachment.Status.ATTACHED, dateService + .iso8601DateParse("2011-08-16T13:41:19.000Z"), true)) + .virtualizationType("paravirtual").build(),// + new AWSRunningInstance.Builder().region(defaultRegion).instanceId("i-931444f2").imageId( + "ami-63be790a").instanceState(InstanceState.RUNNING).privateDnsName( + "ip-10-212-185-8.ec2.internal").dnsName("ec2-50-19-207-248.compute-1.amazonaws.com") + .keyName("jclouds#zkclustertest#us-east-1#23").amiLaunchIndex("0").instanceType( + "t1.micro").launchTime( + dateService.iso8601DateParse("2011-08-16T13:40:50.000Z")).availabilityZone( + "us-east-1c").kernelId("aki-427d952b").monitoringState( + MonitoringState.DISABLED).privateIpAddress("10.212.185.8").ipAddress( + "50.19.207.248").securityGroupIdToNames( + ImmutableMap. of("sg-ef052b86", + "jclouds#zkclustertest#us-east-1")).rootDeviceType( + RootDeviceType.EBS).rootDeviceName("/dev/sda1").device( + "/dev/sda1", + new BlockDevice("vol-5029fc3a", Attachment.Status.ATTACHED, dateService + .iso8601DateParse("2011-08-16T13:41:19.000Z"), true)) + .virtualizationType("paravirtual").build()), defaultRegion, defaultRegion, + defaultRegion)); + + Set> result = parseAWSRunningInstances("/describe_instances_latest.xml"); + + assertEquals(result.toString(), contents.toString()); + } public void testParseNoNPE() { parseAWSRunningInstances("/describe_instances_1.xml"); diff --git a/providers/aws-ec2/src/test/resources/describe_instances_latest.xml b/providers/aws-ec2/src/test/resources/describe_instances_latest.xml index b0044b3a30..2e5d7bb06b 100644 --- a/providers/aws-ec2/src/test/resources/describe_instances_latest.xml +++ b/providers/aws-ec2/src/test/resources/describe_instances_latest.xml @@ -1,33 +1,34 @@ + - e32bb361-415e-484b-8651-5fd0c0a85fab + a03c1896-0543-485f-a732-ebc83873a3ca - r-6d62c002 + r-0f4c2160 993194456877 - sg-1f1c3976 - jclouds#ec2-s#us-east-1 + sg-ef052b86 + jclouds#zkclustertest#us-east-1 - i-5f833d3e - ami-8e1fece7 + i-911444f0 + ami-63be790a 16 running - domU-12-31-38-04-25-D8.compute-1.internal - ec2-50-16-149-221.compute-1.amazonaws.com + ip-10-212-81-7.ec2.internal + ec2-174-129-173-155.compute-1.amazonaws.com - jclouds#ec2-s#us-east-1#38 + jclouds#zkclustertest#us-east-1#23 0 t1.micro - 2011-07-29T04:08:19.000Z + 2011-08-16T13:40:50.000Z - us-east-1d + us-east-1c default @@ -35,12 +36,12 @@ disabled - 10.220.38.38 - 50.16.149.221 + 10.212.81.7 + 174.129.173.155 - sg-1f1c3976 - jclouds#ec2-s#us-east-1 + sg-ef052b86 + jclouds#zkclustertest#us-east-1 x86_64 @@ -50,9 +51,59 @@ /dev/sda1 - vol-4bc3da20 + vol-5829fc32 attached - 2011-07-29T04:08:40.000Z + 2011-08-16T13:41:19.000Z + true + + + + paravirtual + + xen + + + i-931444f2 + ami-63be790a + + 16 + running + + ip-10-212-185-8.ec2.internal + ec2-50-19-207-248.compute-1.amazonaws.com + + jclouds#zkclustertest#us-east-1#23 + 1 + + t1.micro + 2011-08-16T13:40:50.000Z + + us-east-1c + + default + + aki-427d952b + + disabled + + 10.212.185.8 + 50.19.207.248 + + + sg-ef052b86 + jclouds#zkclustertest#us-east-1 + + + x86_64 + ebs + /dev/sda1 + + + /dev/sda1 + + vol-5029fc3a + attached + 2011-08-16T13:41:19.000Z true @@ -64,4 +115,4 @@ - + \ No newline at end of file