fixed where TemplateOptions was not permitted in ec2

This commit is contained in:
Adrian Cole 2010-05-22 23:13:45 -07:00
parent 050af3c3f7
commit dbd42ce7ac
2 changed files with 29 additions and 22 deletions

View File

@ -19,7 +19,7 @@
package org.jclouds.aws.ec2.compute.strategy; package org.jclouds.aws.ec2.compute.strategy;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static org.jclouds.aws.ec2.options.RunInstancesOptions.Builder.*; import static org.jclouds.aws.ec2.options.RunInstancesOptions.Builder.asType;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -37,6 +37,7 @@ import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.options.RunInstancesOptions; import org.jclouds.aws.ec2.options.RunInstancesOptions;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.TemplateOptions;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -72,13 +73,9 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
"unexpected image type. should be EC2Size, was: " + template.getSize().getClass()); "unexpected image type. should be EC2Size, was: " + template.getSize().getClass());
EC2Size ec2Size = EC2Size.class.cast(template.getSize()); EC2Size ec2Size = EC2Size.class.cast(template.getSize());
checkArgument(template.getOptions() instanceof EC2TemplateOptions, String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, template
"unexpected options type. should be EC2Options, was: " .getOptions());
+ template.getOptions().getClass()); Set<String> groups = getSecurityGroupsForTagAndOptions(region, tag, template.getOptions());
EC2TemplateOptions options = EC2TemplateOptions.class.cast(template.getOptions());
String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options);
Set<String> groups = getSecurityGroupsForTagAndOptions(region, tag, options);
RunInstancesOptions instanceOptions = asType(ec2Size.getInstanceType())// RunInstancesOptions instanceOptions = asType(ec2Size.getInstanceType())//
.withSecurityGroups(groups)// .withSecurityGroups(groups)//
@ -92,9 +89,16 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
@VisibleForTesting @VisibleForTesting
String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String tag, String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String tag,
EC2TemplateOptions options) { TemplateOptions options) {
String keyPairName = options.getKeyPair(); String keyPairName = null;
if (keyPairName == null && options.shouldAutomaticallyCreateKeyPair()) { boolean shouldAutomaticallyCreateKeyPair = true;
if (options instanceof EC2TemplateOptions) {
keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair();
if (keyPairName == null)
shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options)
.shouldAutomaticallyCreateKeyPair();
}
if (keyPairName == null && shouldAutomaticallyCreateKeyPair) {
RegionAndName regionAndName = new RegionAndName(region, tag); RegionAndName regionAndName = new RegionAndName(region, tag);
KeyPair keyPair = createUniqueKeyPair.apply(regionAndName); KeyPair keyPair = createUniqueKeyPair.apply(regionAndName);
// get or create incidental resources // get or create incidental resources
@ -110,7 +114,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
@VisibleForTesting @VisibleForTesting
Set<String> getSecurityGroupsForTagAndOptions(String region, @Nullable String tag, Set<String> getSecurityGroupsForTagAndOptions(String region, @Nullable String tag,
EC2TemplateOptions options) { TemplateOptions options) {
Set<String> groups = Sets.newLinkedHashSet(); Set<String> groups = Sets.newLinkedHashSet();
if (tag != null) { if (tag != null) {
@ -119,12 +123,15 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
RegionNameAndIngressRules regionNameAndIngessRulesForMarkerGroup; RegionNameAndIngressRules regionNameAndIngessRulesForMarkerGroup;
if (options.getGroupIds().size() == 0) { if (options instanceof EC2TemplateOptions
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, && EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) {
markerGroup, options.getInboundPorts(), true);
} else {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region,
markerGroup, new int[] {}, false); markerGroup, new int[] {}, false);
groups.addAll(EC2TemplateOptions.class.cast(options).getGroupIds());
} else {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region,
markerGroup, options.getInboundPorts(), true);
} }
if (!securityGroupMap.containsKey(regionNameAndIngessRulesForMarkerGroup)) { if (!securityGroupMap.containsKey(regionNameAndIngessRulesForMarkerGroup)) {
@ -132,7 +139,6 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
createSecurityGroupIfNeeded.apply(regionNameAndIngessRulesForMarkerGroup)); createSecurityGroupIfNeeded.apply(regionNameAndIngessRulesForMarkerGroup));
} }
} }
groups.addAll(options.getGroupIds());
return groups; return groups;
} }
} }

View File

@ -38,6 +38,7 @@ import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.options.RunInstancesOptions; import org.jclouds.aws.ec2.options.RunInstancesOptions;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.TemplateOptions;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
@ -63,12 +64,12 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class, new Method[] { CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class, new Method[] {
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod("createNewKeyPairUnlessUserSpecifiedOtherwise", .getDeclaredMethod("createNewKeyPairUnlessUserSpecifiedOtherwise",
String.class, String.class, EC2TemplateOptions.class), String.class, String.class, TemplateOptions.class),
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod("getSecurityGroupsForTagAndOptions", .getDeclaredMethod("getSecurityGroupsForTagAndOptions",
String.class, String.class, EC2TemplateOptions.class) }); String.class, String.class, TemplateOptions.class) });
EC2TemplateOptions options = createMock(EC2TemplateOptions.class); TemplateOptions options = createMock(TemplateOptions.class);
Template template = createMock(Template.class); Template template = createMock(Template.class);
// setup expectations // setup expectations