Issue 1121: Don't generate SecurityGroup when user specifies NovaTemplateOptions.securityGroupNames

This commit is contained in:
Adrian Cole 2012-11-12 22:33:36 -08:00
parent a7f39f5acd
commit caa8fdee20
6 changed files with 82 additions and 24 deletions

View File

@ -106,7 +106,8 @@ public class NovaComputeServiceAdapter implements
CreateServerOptions options = new CreateServerOptions(); CreateServerOptions options = new CreateServerOptions();
options.metadata(metadataAndTagsAsCommaDelimitedValue(template.getOptions())); options.metadata(metadataAndTagsAsCommaDelimitedValue(template.getOptions()));
options.securityGroupNames(templateOptions.getSecurityGroupNames()); if (templateOptions.getSecurityGroupNames().isPresent())
options.securityGroupNames(templateOptions.getSecurityGroupNames().get());
options.userData(templateOptions.getUserData()); options.userData(templateOptions.getUserData());
Optional<String> privateKey = Optional.absent(); Optional<String> privateKey = Optional.absent();

View File

@ -67,7 +67,8 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
if (to instanceof NovaTemplateOptions) { if (to instanceof NovaTemplateOptions) {
NovaTemplateOptions eTo = NovaTemplateOptions.class.cast(to); NovaTemplateOptions eTo = NovaTemplateOptions.class.cast(to);
eTo.autoAssignFloatingIp(shouldAutoAssignFloatingIp()); eTo.autoAssignFloatingIp(shouldAutoAssignFloatingIp());
eTo.securityGroupNames(getSecurityGroupNames()); if (getSecurityGroupNames().isPresent())
eTo.securityGroupNames(getSecurityGroupNames().get());
eTo.generateKeyPair(shouldGenerateKeyPair()); eTo.generateKeyPair(shouldGenerateKeyPair());
eTo.keyPairName(getKeyPairName()); eTo.keyPairName(getKeyPairName());
if (getUserData() != null) { if (getUserData() != null) {
@ -77,7 +78,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
} }
protected boolean autoAssignFloatingIp = false; protected boolean autoAssignFloatingIp = false;
protected Set<String> securityGroupNames = ImmutableSet.of(); protected Optional<Set<String>> securityGroupNames = Optional.absent();
protected boolean generateKeyPair = false; protected boolean generateKeyPair = false;
protected String keyPairName; protected String keyPairName;
protected byte[] userData; protected byte[] userData;
@ -106,8 +107,8 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
ToStringHelper toString = super.string(); ToStringHelper toString = super.string();
if (!autoAssignFloatingIp) if (!autoAssignFloatingIp)
toString.add("autoAssignFloatingIp", autoAssignFloatingIp); toString.add("autoAssignFloatingIp", autoAssignFloatingIp);
if (securityGroupNames.size() != 0) if (securityGroupNames.isPresent())
toString.add("securityGroupNames", securityGroupNames); toString.add("securityGroupNames", securityGroupNames.get());
if (generateKeyPair) if (generateKeyPair)
toString.add("generateKeyPair", generateKeyPair); toString.add("generateKeyPair", generateKeyPair);
toString.add("keyPairName", keyPairName); toString.add("keyPairName", keyPairName);
@ -155,7 +156,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
public NovaTemplateOptions securityGroupNames(Iterable<String> securityGroupNames) { public NovaTemplateOptions securityGroupNames(Iterable<String> securityGroupNames) {
for (String groupName : checkNotNull(securityGroupNames, "securityGroupNames")) for (String groupName : checkNotNull(securityGroupNames, "securityGroupNames"))
Preconditions2.checkNotEmpty(groupName, "all security groups must be non-empty"); Preconditions2.checkNotEmpty(groupName, "all security groups must be non-empty");
this.securityGroupNames = ImmutableSet.copyOf(securityGroupNames); this.securityGroupNames = Optional.<Set<String>> of(ImmutableSet.copyOf(securityGroupNames));
return this; return this;
} }
@ -192,9 +193,12 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
} }
/** /**
* if unset, generate a default group prefixed with {@link jclouds#} according
* to {@link #getInboundPorts()}
*
* @see org.jclouds.openstack.nova.v2_0.options.CreateServerOptions#getSecurityGroupNames * @see org.jclouds.openstack.nova.v2_0.options.CreateServerOptions#getSecurityGroupNames
*/ */
public Set<String> getSecurityGroupNames() { public Optional<Set<String>> getSecurityGroupNames() {
return securityGroupNames; return securityGroupNames;
} }

View File

@ -128,18 +128,21 @@ public class ApplyNovaTemplateOptionsCreateNodesWithGroupEncodedIntoNameThenAddT
boolean securityGroupExtensionPresent = novaApi.getSecurityGroupExtensionForZone(zone).isPresent(); boolean securityGroupExtensionPresent = novaApi.getSecurityGroupExtensionForZone(zone).isPresent();
List<Integer> inboundPorts = Ints.asList(templateOptions.getInboundPorts()); List<Integer> inboundPorts = Ints.asList(templateOptions.getInboundPorts());
if (templateOptions.getSecurityGroupNames().size() > 0) { if (templateOptions.getSecurityGroupNames().isPresent()
checkArgument(novaApi.getSecurityGroupExtensionForZone(zone).isPresent(), && templateOptions.getSecurityGroupNames().get().size() > 0) {
checkArgument(securityGroupExtensionPresent,
"Security groups are required by options, but the extension is not available! options: %s", "Security groups are required by options, but the extension is not available! options: %s",
templateOptions); templateOptions);
} else if (securityGroupExtensionPresent && inboundPorts.size() > 0) { } else if (securityGroupExtensionPresent) {
String securityGroupName = namingConvention.create().sharedNameForGroup(group); if (!templateOptions.getSecurityGroupNames().isPresent() && inboundPorts.size() > 0) {
try { String securityGroupName = namingConvention.create().sharedNameForGroup(group);
securityGroupCache.get(new ZoneSecurityGroupNameAndPorts(zone, securityGroupName, inboundPorts)); try {
} catch (ExecutionException e) { securityGroupCache.get(new ZoneSecurityGroupNameAndPorts(zone, securityGroupName, inboundPorts));
throw Throwables.propagate(e.getCause()); } catch (ExecutionException e) {
throw Throwables.propagate(e.getCause());
}
templateOptions.securityGroupNames(securityGroupName);
} }
templateOptions.securityGroupNames(securityGroupName);
} }
return super.execute(group, count, mutableTemplate, goodNodes, badNodes, customizationResponses); return super.execute(group, count, mutableTemplate, goodNodes, badNodes, customizationResponses);

View File

@ -293,14 +293,13 @@ public class CreateServerOptions implements MapBinder {
} }
/** /**
*
* Security groups the user specified to run servers with.
* *
* <h3>Note</h3> * <h3>Note</h3>
* *
* This requires that {@link NovaApi#getSecurityGroupExtensionForZone(String)} to return * This requires that {@link NovaApi#getSecurityGroupExtensionForZone(String)} to return
* {@link Optional#isPresent present} * {@link Optional#isPresent present}
*
* @return security groups the user specified to run servers with; zero length will create an
* implicit group starting with {@code jclouds#}
*/ */
public Set<String> getSecurityGroupNames() { public Set<String> getSecurityGroupNames() {
return securityGroupNames; return securityGroupNames;

View File

@ -328,4 +328,54 @@ public class NovaComputeServiceExpectTest extends BaseNovaComputeServiceExpectTe
assertEquals(node.getCredentials().getPrivateKey(), null); assertEquals(node.getCredentials().getPrivateKey(), null);
} }
@Test
public void testCreateNodeWhileUserSpecifiesKeyPairAndUserSpecifiedGroups() throws Exception {
Builder<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder()
.putAll(defaultTemplateTryStack);
requestResponseMap.put(list, notFound);
requestResponseMap.put(serverDetail, serverDetailResponse);
HttpRequest createServerWithSuppliedKeyPairAndGroup = HttpRequest
.builder()
.method("POST")
.endpoint("https://nova-api.trystack.org:9774/v1.1/3456/servers")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken)
.payload(
payloadFromStringWithContentType(
"{\"server\":{\"name\":\"test-0\",\"imageRef\":\"14\",\"flavorRef\":\"1\",\"key_name\":\"fooPair\",\"security_groups\":[{\"name\":\"mygroup\"}]}}",
"application/json")).build();
HttpResponse createdServer = HttpResponse.builder().statusCode(202).message("HTTP/1.1 202 Accepted")
.payload(payloadFromResourceWithContentType("/new_server.json", "application/json; charset=UTF-8")).build();
requestResponseMap.put(createServerWithSuppliedKeyPairAndGroup, createdServer);
ComputeService apiThatCreatesNode = requestsSendResponses(requestResponseMap.build(), new AbstractModule() {
@Override
protected void configure() {
// predicatable node names
final AtomicInteger suffix = new AtomicInteger();
bind(new TypeLiteral<Supplier<String>>() {
}).toInstance(new Supplier<String>() {
@Override
public String get() {
return suffix.getAndIncrement() + "";
}
});
}
});
NodeMetadata node = Iterables.getOnlyElement(apiThatCreatesNode.createNodesInGroup("test", 1,
keyPairName("fooPair").securityGroupNames("mygroup").blockUntilRunning(false)));
// we don't have access to this private key
assertEquals(node.getCredentials().getPrivateKey(), null);
}
} }

View File

@ -32,6 +32,7 @@ import java.io.IOException;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
/** /**
@ -57,14 +58,14 @@ public class NovaTemplateOptionsTest {
public void testsecurityGroupNamesIterable() { public void testsecurityGroupNamesIterable() {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();
options.securityGroupNames(ImmutableSet.of("group1", "group2")); options.securityGroupNames(ImmutableSet.of("group1", "group2"));
assertEquals(options.getSecurityGroupNames(), ImmutableSet.of("group1", "group2")); assertEquals(options.getSecurityGroupNames(), Optional.of(ImmutableSet.of("group1", "group2")));
} }
@Test @Test
public void testsecurityGroupNamesIterableStatic() { public void testsecurityGroupNamesIterableStatic() {
NovaTemplateOptions options = securityGroupNames(ImmutableSet.of("group1", "group2")); NovaTemplateOptions options = securityGroupNames(ImmutableSet.of("group1", "group2"));
assertEquals(options.getSecurityGroupNames(), ImmutableSet.of("group1", "group2")); assertEquals(options.getSecurityGroupNames(), Optional.of(ImmutableSet.of("group1", "group2")));
} }
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
@ -77,20 +78,20 @@ public class NovaTemplateOptionsTest {
public void testsecurityGroupNamesVarArgs() { public void testsecurityGroupNamesVarArgs() {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();
options.securityGroupNames("group1", "group2"); options.securityGroupNames("group1", "group2");
assertEquals(options.getSecurityGroupNames(), ImmutableSet.of("group1", "group2")); assertEquals(options.getSecurityGroupNames(), Optional.of(ImmutableSet.of("group1", "group2")));
} }
@Test @Test
public void testDefaultGroupsVarArgsEmpty() { public void testDefaultGroupsVarArgsEmpty() {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();
assertEquals(options.getSecurityGroupNames(), ImmutableSet.of()); assertEquals(options.getSecurityGroupNames(), Optional.absent());
} }
@Test @Test
public void testsecurityGroupNamesVarArgsStatic() { public void testsecurityGroupNamesVarArgsStatic() {
NovaTemplateOptions options = securityGroupNames("group1", "group2"); NovaTemplateOptions options = securityGroupNames("group1", "group2");
assertEquals(options.getSecurityGroupNames(), ImmutableSet.of("group1", "group2")); assertEquals(options.getSecurityGroupNames(), Optional.of(ImmutableSet.of("group1", "group2")));
} }
@Test @Test