openstack-nova: setup user syntax for implicit keypair option

This commit is contained in:
Adam Lowe 2012-03-21 12:46:06 +00:00
parent 70c1d4c05c
commit e6a949005b
8 changed files with 69 additions and 16 deletions

View File

@ -22,6 +22,7 @@ import static org.jclouds.Constants.PROPERTY_ENDPOINT;
import static org.jclouds.Constants.PROPERTY_ISO3166_CODES; import static org.jclouds.Constants.PROPERTY_ISO3166_CODES;
import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_TERMINATED; import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_TERMINATED;
import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS; import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS;
import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_AUTO_GENERATE_KEYPAIRS;
import java.util.Properties; import java.util.Properties;
@ -39,6 +40,7 @@ public class HPCloudComputePropertiesBuilder extends NovaPropertiesBuilder {
properties.setProperty(PROPERTY_ISO3166_CODES, "US-NV"); properties.setProperty(PROPERTY_ISO3166_CODES, "US-NV");
properties.setProperty(PROPERTY_ENDPOINT, "https://region-a.geo-1.identity.hpcloudsvc.com:35357"); properties.setProperty(PROPERTY_ENDPOINT, "https://region-a.geo-1.identity.hpcloudsvc.com:35357");
properties.setProperty(PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS, "true"); properties.setProperty(PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS, "true");
properties.setProperty(PROPERTY_NOVA_AUTO_GENERATE_KEYPAIRS, "true");
// deallocating ip addresses can take a while // deallocating ip addresses can take a while
properties.setProperty(PROPERTY_TIMEOUT_NODE_TERMINATED, 60 * 1000 + ""); properties.setProperty(PROPERTY_TIMEOUT_NODE_TERMINATED, 60 * 1000 + "");
return properties; return properties;

View File

@ -21,6 +21,7 @@ package org.jclouds.openstack.nova.v1_1;
import static org.jclouds.Constants.PROPERTY_API_VERSION; import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.Constants.PROPERTY_ENDPOINT; import static org.jclouds.Constants.PROPERTY_ENDPOINT;
import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS; import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS;
import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_AUTO_GENERATE_KEYPAIRS;
import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_TIMEOUT_SECURITYGROUP_PRESENT; import static org.jclouds.openstack.nova.v1_1.reference.NovaConstants.PROPERTY_NOVA_TIMEOUT_SECURITYGROUP_PRESENT;
import java.util.Properties; import java.util.Properties;
@ -44,6 +45,7 @@ public class NovaPropertiesBuilder extends PropertiesBuilder {
properties.setProperty(KeystoneProperties.VERSION, "2.0"); properties.setProperty(KeystoneProperties.VERSION, "2.0");
properties.setProperty(PROPERTY_API_VERSION, "1.1"); properties.setProperty(PROPERTY_API_VERSION, "1.1");
properties.setProperty(PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS, "false"); properties.setProperty(PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS, "false");
properties.setProperty(PROPERTY_NOVA_AUTO_GENERATE_KEYPAIRS, "false");
properties.setProperty(PROPERTY_NOVA_TIMEOUT_SECURITYGROUP_PRESENT, "500"); properties.setProperty(PROPERTY_NOVA_TIMEOUT_SECURITYGROUP_PRESENT, "500");
return properties; return properties;
} }

View File

@ -135,9 +135,11 @@ public class NovaComputeServiceContextModule
@Override @Override
protected TemplateOptions provideTemplateOptions(Injector injector, TemplateOptions options) { protected TemplateOptions provideTemplateOptions(Injector injector, TemplateOptions options) {
return options.as(NovaTemplateOptions.class).autoAssignFloatingIp( return options.as(NovaTemplateOptions.class)
injector.getInstance(Key.get(boolean.class, Names .autoAssignFloatingIp(injector.getInstance(
.named(NovaConstants.PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS)))); Key.get(boolean.class, Names.named(NovaConstants.PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS))))
.generateKeyPair(injector.getInstance(
Key.get(boolean.class, Names.named(NovaConstants.PROPERTY_NOVA_AUTO_GENERATE_KEYPAIRS))));
} }
@Provides @Provides

View File

@ -70,6 +70,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
} }
private boolean autoAssignFloatingIp = false; private boolean autoAssignFloatingIp = false;
private boolean generateKeyPair = false;
private Set<String> securityGroupNames = ImmutableSet.of(); private Set<String> securityGroupNames = ImmutableSet.of();
public static final NovaTemplateOptions NONE = new NovaTemplateOptions(); public static final NovaTemplateOptions NONE = new NovaTemplateOptions();
@ -82,16 +83,24 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
return this; return this;
} }
/**
* @see #shouldGenerateKeyPair()
*/
public NovaTemplateOptions generateKeyPair(boolean enable) {
this.generateKeyPair = enable;
return this;
}
/** /**
* *
* @see CreateServerOptions#getSecurityGroupNames * @see org.jclouds.openstack.nova.v1_1.options.CreateServerOptions#getSecurityGroupNames
*/ */
public NovaTemplateOptions securityGroupNames(String... securityGroupNames) { public NovaTemplateOptions securityGroupNames(String... securityGroupNames) {
return securityGroupNames(ImmutableSet.copyOf(checkNotNull(securityGroupNames, "securityGroupNames"))); return securityGroupNames(ImmutableSet.copyOf(checkNotNull(securityGroupNames, "securityGroupNames")));
} }
/** /**
* @see CreateServerOptions#getSecurityGroupNames * @see org.jclouds.openstack.nova.v1_1.options.CreateServerOptions#getSecurityGroupNames
*/ */
public NovaTemplateOptions securityGroupNames(Iterable<String> securityGroupNames) { public NovaTemplateOptions securityGroupNames(Iterable<String> securityGroupNames) {
for (String groupName : checkNotNull(securityGroupNames, "securityGroupNames")) for (String groupName : checkNotNull(securityGroupNames, "securityGroupNames"))
@ -113,7 +122,19 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
} }
/** /**
* @see CreateServerOptions#getSecurityGroupNames * <h3>Note</h3>
*
* This requires that {@link NovaClient#getKeyPairExtensionForZone(String)} to return
* {@link Optional#isPresent present}
*
* @return true if auto generation of keypairs is enabled
*/
public boolean shouldGenerateKeyPair() {
return generateKeyPair;
}
/**
* @see org.jclouds.openstack.nova.v1_1.options.CreateServerOptions#getSecurityGroupNames
*/ */
public Set<String> getSecurityGroupNames() { public Set<String> getSecurityGroupNames() {
return securityGroupNames; return securityGroupNames;
@ -129,7 +150,14 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
} }
/** /**
* @see CreateServerOptions#getSecurityGroupNames * @see NovaTemplateOptions#shouldGenerateKeyPair()
*/
public static NovaTemplateOptions generateKeyPair(boolean enable) {
return new NovaTemplateOptions().generateKeyPair(enable);
}
/**
* @see org.jclouds.openstack.nova.v1_1.options.CreateServerOptions#getSecurityGroupNames
*/ */
public static NovaTemplateOptions securityGroupNames(String... groupNames) { public static NovaTemplateOptions securityGroupNames(String... groupNames) {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();
@ -137,7 +165,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
} }
/** /**
* @see CreateServerOptions#getSecurityGroupNames * @see org.jclouds.openstack.nova.v1_1.options.CreateServerOptions#getSecurityGroupNames
*/ */
public static NovaTemplateOptions securityGroupNames(Iterable<String> groupNames) { public static NovaTemplateOptions securityGroupNames(Iterable<String> groupNames) {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();

View File

@ -106,6 +106,12 @@ public class ApplyNovaTemplateOptionsCreateNodesWithGroupEncodedIntoNameThenAddT
templateOptions); templateOptions);
} }
if (templateOptions.shouldGenerateKeyPair()) {
checkArgument(novaClient.getKeyPairExtensionForZone(zone).isPresent(),
"Key Pairs are required by options, but the extension is not available! options: %s",
templateOptions);
}
boolean securityGroupExensionPresent = novaClient.getSecurityGroupExtensionForZone(zone).isPresent(); boolean securityGroupExensionPresent = novaClient.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().size() > 0) {

View File

@ -232,7 +232,7 @@ public class CreateServerOptions implements MapBinder {
public static class Builder { public static class Builder {
/** /**
* @see CreateServerOptions#withFile(String,byte []) * @see CreateServerOptions#withFile(String, byte[])
*/ */
public static CreateServerOptions withFile(String path, byte[] contents) { public static CreateServerOptions withFile(String path, byte[] contents) {
CreateServerOptions options = new CreateServerOptions(); CreateServerOptions options = new CreateServerOptions();

View File

@ -36,4 +36,10 @@ public class NovaConstants {
*/ */
public static final String PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS = "jclouds.openstack-nova.auto-allocate-floating-ips"; public static final String PROPERTY_NOVA_AUTO_ALLOCATE_FLOATING_IPS = "jclouds.openstack-nova.auto-allocate-floating-ips";
/**
* Whenever a node is created, automatically generate keypairs for groups, as needed, also
* delete the keypair(s) when the last node in the group is destroyed.
*/
public static final String PROPERTY_NOVA_AUTO_GENERATE_KEYPAIRS = "jclouds.openstack-nova.auto-generate-keypairs";
} }

View File

@ -18,12 +18,7 @@
*/ */
package org.jclouds.openstack.nova.v1_1.compute.options; package org.jclouds.openstack.nova.v1_1.compute.options;
import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.authorizePublicKey; import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.*;
import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.autoAssignFloatingIp;
import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.blockOnPort;
import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.inboundPorts;
import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.installPrivateKey;
import static org.jclouds.openstack.nova.v1_1.compute.options.NovaTemplateOptions.Builder.securityGroupNames;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
@ -110,6 +105,18 @@ public class NovaTemplateOptionsTest {
assert options.shouldAutoAssignFloatingIp(); assert options.shouldAutoAssignFloatingIp();
} }
@Test
public void testGenerateKeyPair() {
NovaTemplateOptions options = new NovaTemplateOptions().generateKeyPair(true);
assert options.shouldGenerateKeyPair();
}
@Test
public void testGenerateKeyPairStatic() {
NovaTemplateOptions options = generateKeyPair(true);
assert options.shouldGenerateKeyPair();
}
// superclass tests // superclass tests
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testinstallPrivateKeyBadFormat() { public void testinstallPrivateKeyBadFormat() {