Merge pull request #143 from andreisavu/enable-ssh-keypair

Enable ssh keypair & fix ip forwarding rule parsing in cloudstack
This commit is contained in:
Adrian Cole 2011-11-13 08:04:29 -08:00
commit 83cbebe6de
10 changed files with 145 additions and 13 deletions

View File

@ -55,6 +55,7 @@ import com.google.common.collect.Sets;
public class CloudStackTemplateOptions extends TemplateOptions implements Cloneable { public class CloudStackTemplateOptions extends TemplateOptions implements Cloneable {
protected Set<Long> securityGroupIds = Sets.<Long> newLinkedHashSet(); protected Set<Long> securityGroupIds = Sets.<Long> newLinkedHashSet();
protected String keyPair;
@Override @Override
public CloudStackTemplateOptions clone() { public CloudStackTemplateOptions clone() {
@ -92,6 +93,18 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return securityGroupIds; 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 final CloudStackTemplateOptions NONE = new CloudStackTemplateOptions();
public static class Builder { public static class Builder {
@ -112,6 +125,14 @@ public class CloudStackTemplateOptions extends TemplateOptions implements Clonea
return options.securityGroupIds(securityGroupIds); 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 // 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.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import com.google.common.base.Predicates;
import org.jclouds.cloudstack.CloudStackClient; import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions; import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
import org.jclouds.cloudstack.domain.AsyncCreateResponse; import org.jclouds.cloudstack.domain.AsyncCreateResponse;
@ -85,6 +86,16 @@ public class CloudStackComputeServiceAdapter implements
if (templateOptions.getSecurityGroupIds().size() > 0) if (templateOptions.getSecurityGroupIds().size() > 0)
options.securityGroupIds(templateOptions.getSecurityGroupIds()); 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 zoneId = Long.parseLong(template.getLocation().getId());
long templateId = Long.parseLong(template.getImage().getId()); long templateId = Long.parseLong(template.getImage().getId());
long serviceOfferingId = Long.parseLong(template.getHardware().getId()); long serviceOfferingId = Long.parseLong(template.getHardware().getId());
@ -104,9 +115,11 @@ public class CloudStackComputeServiceAdapter implements
if (vm.isPasswordEnabled()) { if (vm.isPasswordEnabled()) {
assert vm.getPassword() != null : vm; assert vm.getPassword() != null : vm;
Credentials credentials = new Credentials("root", vm.getPassword()); Credentials credentials = new Credentials("root", vm.getPassword());
credentialStore.put("node#" + zoneId + "/" + vm.getId(), credentials); credentialStore.put("node#" + vm.getId(), credentials);
} else { } 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; return vm;
} }
@ -121,7 +134,7 @@ public class CloudStackComputeServiceAdapter implements
public Iterable<Template> listImages() { public Iterable<Template> listImages() {
// TODO: we may need to filter these further // TODO: we may need to filter these further
// we may also want to see if we can work with ssh keys // 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 @Override

View File

@ -31,8 +31,10 @@ import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions; import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions;
import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions; import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions;
import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.OnlyElement;
import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.Unwrap; import org.jclouds.rest.annotations.Unwrap;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
@ -56,7 +58,7 @@ public interface NATAsyncClient {
*/ */
@GET @GET
@QueryParams(keys = "command", values = "listIpForwardingRules") @QueryParams(keys = "command", values = "listIpForwardingRules")
@Unwrap(depth = 2) @SelectJson("ipforwardingrule")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<IPForwardingRule>> listIPForwardingRules(ListIPForwardingRulesOptions... options); ListenableFuture<Set<IPForwardingRule>> listIPForwardingRules(ListIPForwardingRulesOptions... options);
@ -66,7 +68,8 @@ public interface NATAsyncClient {
*/ */
@GET @GET
@QueryParams(keys = "command", values = "listIpForwardingRules") @QueryParams(keys = "command", values = "listIpForwardingRules")
@Unwrap(depth = 3, edgeCollection = Set.class) @SelectJson("ipforwardingrule")
@OnlyElement
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class) @ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<IPForwardingRule> getIPForwardingRule(@QueryParam("id") long id); ListenableFuture<IPForwardingRule> getIPForwardingRule(@QueryParam("id") long id);

View File

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

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.cloudstack.compute.options; 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.securityGroupId;
import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupIds; import static org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions.Builder.securityGroupIds;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
@ -73,6 +74,12 @@ public class CloudStackTemplateOptionsTest {
assertEquals(options.as(CloudStackTemplateOptions.class).getSecurityGroupIds(), ImmutableSet.of(3l)); 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 @Test
public void testSecurityGroupIdsNullHasDecentMessage() { public void testSecurityGroupIdsNullHasDecentMessage() {
try { try {

View File

@ -24,6 +24,7 @@ import java.lang.reflect.Method;
import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions; import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions;
import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions; import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.http.functions.UnwrapOnlyJsonValue; import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue; import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValueInSet; import org.jclouds.http.functions.UnwrapOnlyNestedJsonValueInSet;
@ -53,7 +54,7 @@ public class NATAsyncClientTest extends BaseCloudStackAsyncClientTest<NATAsyncCl
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValue.class); assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null); assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class); assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
@ -71,7 +72,7 @@ public class NATAsyncClientTest extends BaseCloudStackAsyncClientTest<NATAsyncCl
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValue.class); assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null); assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class); assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
@ -88,7 +89,6 @@ public class NATAsyncClientTest extends BaseCloudStackAsyncClientTest<NATAsyncCl
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValueInSet.class);
assertSaxResponseParserClassEquals(method, null); assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);

View File

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

View File

@ -0,0 +1,50 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.parse;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import java.util.Set;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ListIPForwardingRulesResponseTest extends BaseSetParserTest<IPForwardingRule> {
@Override
public String resource() {
return "/listipforwardingrulesresponse.json";
}
@Override
@SelectJson("ipforwardingrule")
public Set<IPForwardingRule> expected() {
return ImmutableSet.<IPForwardingRule> of(
IPForwardingRule.builder().id(66).protocol("tcp").startPort(22).endPort(22).virtualMachineId(58)
.virtualMachineDisplayName("i-4-58-VM").virtualMachineName("i-4-58-VM")
.IPAddressId(15).IPAddress("10.27.27.64").state("Active").build());
}
}

View File

@ -0,0 +1,17 @@
{
"listipforwardingrulesresponse": {
"count": 1,
"ipforwardingrule": [{
"id": 66,
"protocol": "tcp",
"virtualmachineid": 58,
"virtualmachinename": "i-4-58-VM",
"virtualmachinedisplayname": "i-4-58-VM",
"ipaddressid": 15,
"ipaddress": "10.27.27.64",
"startport": 22,
"endport": 22,
"state": "Active"
}]
}
}