diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadata.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadata.java index 69876dc4b1..877ed21638 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadata.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/RunningInstanceToNodeMetadata.java @@ -62,6 +62,7 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import com.google.common.collect.ImmutableSet.Builder; import com.google.common.util.concurrent.UncheckedExecutionException; @@ -186,21 +187,28 @@ public class RunningInstanceToNodeMetadata implements Function data) { String group = null; try { - group = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate() { + group = Iterables.getOnlyElement(Iterables.filter(data, new Predicate() { @Override public boolean apply(String input) { - return input.startsWith("jclouds#") && input.endsWith("#" + instance.getRegion()); + return input.startsWith("jclouds#") && input.contains("#" + instance.getRegion()); } - - })).substring(8).replaceAll("#" + instance.getRegion() + "$", ""); + })).split("#")[1]; } catch (NoSuchElementException e) { - logger.debug("no group parsed from %s's security groups: %s", instance.getId(), instance.getGroupIds()); + logger.debug("no group parsed from %s's data: %s", instance.getId(), data); } catch (IllegalArgumentException e) { - logger.debug("too many groups match %s; %s's security groups: %s", "jclouds#", instance.getId(), instance - .getGroupIds()); + logger.debug("too many groups match %s; %s's data: %s", "jclouds#", instance.getId(), data); } return group; } 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 03987761c4..455bd8a60b 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 @@ -217,6 +217,20 @@ public class RunningInstanceToNodeMetadataTest { .hardware(m1_small32().build()).location(provider).build()); } + @Test + public void testGroupNameIsSetWhenCustomKeyNameIsSetAndSecurityGroupIsGenerated() { + checkGroupName(RunningInstance.builder().instanceId("id").imageId("image").instanceType("m1.small") + .instanceState(InstanceState.RUNNING).region("us-east-1").keyName("custom-key") + .groupId("jclouds#groupname#us-east-1").build()); + } + + @Test + public void testGroupNameIsSetWhenCustomSecurityGroupIsSetAndKeyNameIsGenerated() { + checkGroupName(RunningInstance.builder().instanceId("id").imageId("image").instanceType("m1.small") + .instanceState(InstanceState.RUNNING).region("us-east-1").groupId("custom-sec") + .keyName("jclouds#groupname#us-east-1#23").build()); + } + protected RunningInstance firstInstanceFromResource(String resource) { RunningInstance server = Iterables.get(Iterables.get(DescribeInstancesResponseHandlerTest .parseRunningInstances(resource), 0), 0); @@ -239,6 +253,12 @@ public class RunningInstanceToNodeMetadataTest { return createNodeParser(hardware, locations, credentialStore, instanceToNodeState, instanceToImage); } + private void checkGroupName(RunningInstance instance) { + assertEquals("groupname", createNodeParser(ImmutableSet. of(), ImmutableSet + . of(), ImmutableSet. of(), ImmutableMap. of()) + .apply(instance).getGroup()); + } + private RunningInstanceToNodeMetadata createNodeParser(final ImmutableSet hardware, final ImmutableSet locations, Map credentialStore, Map instanceToNodeState, LoadingCache instanceToImage) {