first attempt at fixing/refactoring destroyNodesWithTag for EC2

This commit is contained in:
Alex Yarmula 2010-05-07 12:51:11 -07:00
parent 7ccc18c0f8
commit bb52dfd9a2
1 changed files with 27 additions and 21 deletions

View File

@ -18,15 +18,21 @@
*/
package org.jclouds.aws.ec2.compute;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Sets;
import org.jclouds.Constants;
import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.compute.config.EC2ComputeServiceContextModule.GetRegionFromNodeOrDefault;
@ -41,6 +47,7 @@ import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.Size;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.internal.BaseComputeService;
import org.jclouds.compute.predicates.NodePredicates;
import org.jclouds.compute.strategy.DestroyNodeStrategy;
import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
import org.jclouds.compute.strategy.ListNodesStrategy;
@ -110,15 +117,14 @@ public class EC2ComputeService extends BaseComputeService {
@Override
public void destroyNodesWithTag(String tag) {
super.destroyNodesWithTag(tag);
Iterable<? extends NodeMetadata> nodes = Iterables.filter(listNodesWithTag(tag),
new Predicate<NodeMetadata>() {
Set<String> regions = Sets.newHashSet(Iterables.transform(listNodesWithTag(tag),
new Function<NodeMetadata, String>() {
@Override
public boolean apply(NodeMetadata input) {
return input.getState() == NodeState.TERMINATED;
public String apply(@Nullable NodeMetadata nodeMetadata) {
return getRegionFromNodeOrDefault.apply(nodeMetadata);
}
});
if (Iterables.size(nodes) > 0) {
String region = getRegionFromNodeOrDefault.apply(Iterables.get(nodes, 0));
}));
for(String region : regions) {
deleteKeyPair(region, tag);
deleteSecurityGroup(region, tag);
}