mirror of https://github.com/apache/jclouds.git
Merge branch 'group-name' of https://github.com/doo/jclouds
* 'group-name' of https://github.com/doo/jclouds: If group name can't be parsed from a group ID, try the key name
This commit is contained in:
commit
05ec5045f1
|
@ -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<RunningInstance,
|
|||
|
||||
@VisibleForTesting
|
||||
String getGroupForInstance(final RunningInstance instance) {
|
||||
String group = parseGroupFrom(instance, instance.getGroupIds());
|
||||
if(group == null && instance.getKeyName() != null) {
|
||||
// when not using a generated security group, e.g. in VPC, try from key:
|
||||
group = parseGroupFrom(instance, Sets.newHashSet(instance.getKeyName()));
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
private String parseGroupFrom(final RunningInstance instance, final Set<String> data) {
|
||||
String group = null;
|
||||
try {
|
||||
group = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate<String>() {
|
||||
group = Iterables.getOnlyElement(Iterables.filter(data, new Predicate<String>() {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
|
|
@ -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.<Hardware> of(), ImmutableSet
|
||||
.<Location> of(), ImmutableSet.<Image> of(), ImmutableMap.<String, Credentials> of())
|
||||
.apply(instance).getGroup());
|
||||
}
|
||||
|
||||
private RunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
|
||||
final ImmutableSet<Location> locations, Map<String, Credentials> credentialStore,
|
||||
Map<InstanceState, NodeState> instanceToNodeState, LoadingCache<RegionAndName, Image> instanceToImage) {
|
||||
|
|
Loading…
Reference in New Issue