diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClient.java index cb6afd06df..9a75d0962f 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClient.java @@ -18,13 +18,13 @@ */ package org.jclouds.cloudstack.features; -import java.util.Set; - import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; +import java.util.Set; +import com.google.common.util.concurrent.ListenableFuture; import org.jclouds.cloudstack.domain.SshKeyPair; import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.options.ListSSHKeyPairsOptions; @@ -37,11 +37,9 @@ import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; -import com.google.common.util.concurrent.ListenableFuture; - /** * Provides asynchronous access to CloudStack SSHKeyPair features. - * + * * @author Vijay Kiran * @see > listSSHKeyPairs(ListSSHKeyPairsOptions... options); + /** + * @see SSHKeyPairClient#registerSSHKeyPair + */ + @GET + @QueryParams(keys = "command", values = "registerSSHKeyPair") + @SelectJson("keypair") + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture registerSSHKeyPair(@QueryParam("name") String name, @QueryParam("publickey") String publicKey); + /** * @see SSHKeyPairClient#createSSHKeyPair */ diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairClient.java index 88497c37c6..75613d965d 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SSHKeyPairClient.java @@ -28,47 +28,53 @@ import org.jclouds.concurrent.Timeout; /** * Provides synchronous access to CloudStack SSHKeyPair features. - * + * + * @author Vijay Kiran * @see - * @author Vijay Kiran */ @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) public interface SSHKeyPairClient { /** * Returns a list of {@link SshKeyPair}s registered by current user. - * - * @param options - * if present, how to constrain the list + * + * @param options if present, how to constrain the list * @return Set of {@link SshKeyPair}s matching the current constrains or * empty set if no SshKeyPairs found. */ Set listSSHKeyPairs(ListSSHKeyPairsOptions... options); + + /** + * Registers a {@link SshKeyPair} with the given name and public kay material. + * + * @param name of the keypair + * @param publicKey Public key material of the keypair + * @return Created SshKeyPair. + */ + SshKeyPair registerSSHKeyPair(String name, String publicKey); + /** * Creates a {@link SshKeyPair} with specified name. - * - * @param name - * of the SshKeyPair. + * + * @param name of the SshKeyPair. * @return Created SshKeyPair. */ SshKeyPair createSSHKeyPair(String name); /** * Retrieves the {@link SSHKeyPairClient} with given name. - * - * @param name - * name of the key pair + * + * @param name name of the key pair * @return SSH Key pair or null if not found. */ SshKeyPair getSSHKeyPair(String name); /** * Deletes the {@link SSHKeyPairClient} with given name. - * - * @param name - * name of the key pair + * + * @param name name of the key pair * @return */ void deleteSSHKeyPair(String name); diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClientTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClientTest.java index 68086bf13f..6ddc763d5b 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClientTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairAsyncClientTest.java @@ -20,24 +20,26 @@ package org.jclouds.cloudstack.features; import java.io.IOException; import java.lang.reflect.Method; +import java.net.URLEncoder; +import com.google.common.base.Functions; +import com.google.inject.TypeLiteral; import org.jclouds.cloudstack.options.ListSSHKeyPairsOptions; +import org.jclouds.crypto.SshKeys; import org.jclouds.functions.IdentityFunction; import org.jclouds.http.HttpRequest; import org.jclouds.http.functions.ParseFirstJsonValueNamed; import org.jclouds.http.functions.ReleasePayloadAndReturn; +import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.testng.annotations.Test; -import com.google.common.base.Functions; -import com.google.inject.TypeLiteral; - /** * Tests behavior of {@code SSHKeyPairAsyncClient} - * + * * @author Vijay Kiran */ @Test(groups = "unit", testName = "SSHKeyPairAsyncClientTest") @@ -95,6 +97,25 @@ public class SSHKeyPairAsyncClientTest extends BaseCloudStackAsyncClientTest> createTypeLiteral() { return new TypeLiteral>() { diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairClientLiveTest.java index ecefc9cccf..5123b2e8cf 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SSHKeyPairClientLiveTest.java @@ -23,11 +23,12 @@ import static org.testng.Assert.assertEquals; import java.util.Set; import org.jclouds.cloudstack.domain.SshKeyPair; +import org.jclouds.crypto.SshKeys; import org.testng.annotations.Test; /** * Tests behavior of {@code SSHKeyPairClient} - * + * * @author Vijay Kiran */ @Test(groups = "live", singleThreaded = true, testName = "SSHKeyPairClientLiveTest") @@ -52,6 +53,18 @@ public class SSHKeyPairClientLiveTest extends BaseCloudStackClientLiveTest { sshKeyPair = null; } + public void testRegisterDeleteSSHKeyPair() { + final String publickey = SshKeys.generate().get("public"); + sshKeyPair = client.getSSHKeyPairClient().registerSSHKeyPair(prefix + "jclouds-keypair", publickey); + checkSSHKeyPair(sshKeyPair); + client.getSSHKeyPairClient().deleteSSHKeyPair(sshKeyPair.getName()); + assertEquals(client.getSSHKeyPairClient().getSSHKeyPair(sshKeyPair.getName()), null); + // Set the keypair to null , if the delete test is passed. + sshKeyPair = null; + + } + + protected void checkSSHKeyPair(SshKeyPair pair) { assert pair.getName() != null : pair; assertEquals(pair.toString(), client.getSSHKeyPairClient().getSSHKeyPair(pair.getName()).toString());