Allow user to reference a keypair when creating a new virtual machine

This commit is contained in:
andreisavu 2011-11-13 17:55:43 +02:00
parent cd0086c92e
commit de19a639c4
6 changed files with 70 additions and 8 deletions

View File

@ -55,6 +55,7 @@ import com.google.common.collect.Sets;
public class CloudStackTemplateOptions extends TemplateOptions implements Cloneable {
protected Set<Long> securityGroupIds = Sets.<Long> newLinkedHashSet();
protected String keyPair;
@Override
public CloudStackTemplateOptions clone() {
@ -92,6 +93,18 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return securityGroupIds;
}
/**
* @see DeployVirtualMachineOptions#keyPair(String)
*/
public CloudStackTemplateOptions keyPair(String keyPair) {
this.keyPair = keyPair;
return this;
}
public String getKeyPair() {
return keyPair;
}
public static final CloudStackTemplateOptions NONE = new CloudStackTemplateOptions();
public static class Builder {
@ -112,6 +125,14 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return options.securityGroupIds(securityGroupIds);
}
/**
* @see CloudStackTemplateOptions#keyPair
*/
public static CloudStackTemplateOptions keyPair(String keyPair) {
CloudStackTemplateOptions options = new CloudStackTemplateOptions();
return options.keyPair(keyPair);
}
// methods that only facilitate returning the correct object type
/**

View File

@ -30,6 +30,7 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import com.google.common.base.Predicates;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
@ -85,6 +86,16 @@ public class CloudStackComputeServiceAdapter implements
if (templateOptions.getSecurityGroupIds().size() > 0)
options.securityGroupIds(templateOptions.getSecurityGroupIds());
if (templateOptions.getKeyPair() != null) {
options.keyPair(templateOptions.getKeyPair());
if (templateOptions.getRunScript() != null) {
checkArgument(
credentialStore.containsKey("keypair#" + templateOptions.getKeyPair()),
"no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)",
templateOptions.getKeyPair());
}
}
long zoneId = Long.parseLong(template.getLocation().getId());
long templateId = Long.parseLong(template.getImage().getId());
long serviceOfferingId = Long.parseLong(template.getHardware().getId());
@ -104,9 +115,11 @@ public class CloudStackComputeServiceAdapter implements
if (vm.isPasswordEnabled()) {
assert vm.getPassword() != null : vm;
Credentials credentials = new Credentials("root", vm.getPassword());
credentialStore.put("node#" + zoneId + "/" + vm.getId(), credentials);
credentialStore.put("node#" + vm.getId(), credentials);
} else {
// TODO: look for ssh key?
// assert templateOptions.getKeyPair() != null : vm;
Credentials credentials = credentialStore.get("keypair#" + templateOptions.getKeyPair());
credentialStore.put("node#" + vm.getId(), credentials);
}
return vm;
}
@ -121,7 +134,7 @@ public class CloudStackComputeServiceAdapter implements
public Iterable<Template> listImages() {
// TODO: we may need to filter these further
// we may also want to see if we can work with ssh keys
return filter(client.getTemplateClient().listTemplates(), TemplatePredicates.isPasswordEnabled());
return filter(client.getTemplateClient().listTemplates(), TemplatePredicates.isReady());
}
@Override

View File

@ -21,7 +21,9 @@ package org.jclouds.cloudstack.compute;
import static com.google.inject.name.Names.bindProperties;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.fail;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
@ -34,10 +36,12 @@ import org.jclouds.cloudstack.CloudStackPropertiesBuilder;
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
import org.jclouds.cloudstack.compute.strategy.CloudStackComputeServiceAdapter;
import org.jclouds.cloudstack.domain.ServiceOffering;
import org.jclouds.cloudstack.domain.SshKeyPair;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.features.BaseCloudStackClientLiveTest;
import org.jclouds.cloudstack.predicates.JobComplete;
import org.jclouds.cloudstack.predicates.TemplatePredicates;
import org.jclouds.compute.ComputeTestUtils;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.Template;
import org.jclouds.domain.Credentials;
@ -64,6 +68,9 @@ public class CloudStackComputeServiceAdapterLiveTest extends BaseCloudStackClien
private CloudStackComputeServiceAdapter adapter;
private VirtualMachine vm;
private String keyPairName;
private Map<String, String> keyPair;
@BeforeGroups(groups = { "live" })
public void setupClient() {
super.setupClient();
@ -84,6 +91,13 @@ public class CloudStackComputeServiceAdapterLiveTest extends BaseCloudStackClien
};
adapter = Guice.createInjector(module, new Log4JLoggingModule()).getInstance(
CloudStackComputeServiceAdapter.class);
keyPairName = prefix + "-adapter-test-keypair";
try {
keyPair = ComputeTestUtils.setupKeyPair();
} catch (IOException e) {
fail("Unable to create keypair", e);
}
}
@Test
@ -97,16 +111,24 @@ public class CloudStackComputeServiceAdapterLiveTest extends BaseCloudStackClien
String name = "node" + new Random().nextInt();
Template template = computeContext.getComputeService().templateBuilder().build();
// TODO: look at SecurityGroupClientLiveTest for how to do this
template.getOptions().as(CloudStackTemplateOptions.class).securityGroupId(3l);
client.getSSHKeyPairClient().deleteSSHKeyPair(keyPairName);
client.getSSHKeyPairClient().registerSSHKeyPair(keyPairName, keyPair.get("public"));
Map<String, Credentials> credentialStore = Maps.newLinkedHashMap();
credentialStore.put("keypair#" + keyPairName, new Credentials("root", keyPair.get("private")));
// TODO: look at SecurityGroupClientLiveTest for how to do this
template.getOptions().as(CloudStackTemplateOptions.class).keyPair(keyPairName);
vm = adapter.createNodeWithGroupEncodedIntoNameThenStoreCredentials(group, name, template, credentialStore);
// TODO: check security groups vm.getSecurityGroups(),
// check other things, like cpu correct, mem correct, image/os is correct
// (as possible)
assert credentialStore.containsKey("node#" + vm.getId()) : "credentials to log into vm not found " + vm;
assert InetAddresses.isInetAddress(vm.getIPAddress()) : vm;
doConnectViaSsh(vm, credentialStore.get("node#" + vm.getId()));
}
@ -141,7 +163,7 @@ public class CloudStackComputeServiceAdapterLiveTest extends BaseCloudStackClien
assertFalse(Iterables.isEmpty(templates));
for (org.jclouds.cloudstack.domain.Template template : templates) {
assert TemplatePredicates.isPasswordEnabled().apply(template) : template;
assert TemplatePredicates.isReady().apply(template) : template;
}
}

View File

@ -59,5 +59,5 @@ public class CloudStackComputeServiceLiveTest extends BaseComputeServiceLiveTest
assert node.getUserMetadata().equals(ImmutableMap.<String, String> of()) : String.format(
"node userMetadata did not match %s %s", userMetadata, node);
}
}

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.cloudstack.compute.options;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.keyPair;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupId;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupIds;
import static org.testng.Assert.assertEquals;
@ -73,6 +74,12 @@ public class CloudStackTemplateOptionsTest {
assertEquals(options.as(CloudStackTemplateOptions.class).getSecurityGroupIds(), ImmutableSet.of(3l));
}
@Test
public void testKeyPair() {
TemplateOptions options = keyPair("test");
assertEquals(options.as(CloudStackTemplateOptions.class).getKeyPair(), "test");
}
@Test
public void testSecurityGroupIdsNullHasDecentMessage() {
try {

View File

@ -80,7 +80,6 @@ public class OfferingClientLiveTest extends BaseCloudStackClientLiveTest {
assert offering.getId() > 0 : offering;
assert offering.getName() != null : offering;
assert offering.getCreated() != null : offering;
assert offering.getDisplayText() != null : offering;
assert offering.getCpuNumber() > 0 : offering;
assert offering.getCpuSpeed() > 0 : offering;