From de19a639c46bb4f4eeb80536afff09b712a4636f Mon Sep 17 00:00:00 2001 From: andreisavu Date: Sun, 13 Nov 2011 17:55:43 +0200 Subject: [PATCH] Allow user to reference a keypair when creating a new virtual machine --- .../options/CloudStackTemplateOptions.java | 21 ++++++++++++++ .../CloudStackComputeServiceAdapter.java | 19 +++++++++++-- ...oudStackComputeServiceAdapterLiveTest.java | 28 +++++++++++++++++-- .../CloudStackComputeServiceLiveTest.java | 2 +- .../CloudStackTemplateOptionsTest.java | 7 +++++ .../features/OfferingClientLiveTest.java | 1 - 6 files changed, 70 insertions(+), 8 deletions(-) diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java index e2125fc440..0d3869ddce 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/options/CloudStackTemplateOptions.java @@ -55,6 +55,7 @@ import com.google.common.collect.Sets; public class CloudStackTemplateOptions extends TemplateOptions implements Cloneable { protected Set securityGroupIds = Sets. 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 /** diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java index 6d4163b4b4..cc6c6ef39f 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java @@ -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