cloudstack registerSSHKeyPair implementation

This commit is contained in:
vijaykiran 2011-11-07 12:22:22 +01:00
parent 98c3116edc
commit f0f77e7ce7
4 changed files with 72 additions and 24 deletions

View File

@ -18,13 +18,13 @@
*/ */
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import java.util.Set;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; 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.domain.SshKeyPair;
import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.ListSSHKeyPairsOptions; 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.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import com.google.common.util.concurrent.ListenableFuture;
/** /**
* Provides asynchronous access to CloudStack SSHKeyPair features. * Provides asynchronous access to CloudStack SSHKeyPair features.
* *
* @author Vijay Kiran * @author Vijay Kiran
* @see <a * @see <a
* href="http://download.cloud.com/releases/2.2.0/api_2.2.8/TOC_User.html" * href="http://download.cloud.com/releases/2.2.0/api_2.2.8/TOC_User.html"
@ -60,6 +58,15 @@ public interface SSHKeyPairAsyncClient {
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<SshKeyPair>> listSSHKeyPairs(ListSSHKeyPairsOptions... options); ListenableFuture<Set<SshKeyPair>> listSSHKeyPairs(ListSSHKeyPairsOptions... options);
/**
* @see SSHKeyPairClient#registerSSHKeyPair
*/
@GET
@QueryParams(keys = "command", values = "registerSSHKeyPair")
@SelectJson("keypair")
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<SshKeyPair> registerSSHKeyPair(@QueryParam("name") String name, @QueryParam("publickey") String publicKey);
/** /**
* @see SSHKeyPairClient#createSSHKeyPair * @see SSHKeyPairClient#createSSHKeyPair
*/ */

View File

@ -28,47 +28,53 @@ import org.jclouds.concurrent.Timeout;
/** /**
* Provides synchronous access to CloudStack SSHKeyPair features. * Provides synchronous access to CloudStack SSHKeyPair features.
* *
* @author Vijay Kiran
* @see <a * @see <a
* href="http://download.cloud.com/releases/2.2.0/api_2.2.8/TOC_User.html" * href="http://download.cloud.com/releases/2.2.0/api_2.2.8/TOC_User.html"
* /> * />
* @author Vijay Kiran
*/ */
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface SSHKeyPairClient { public interface SSHKeyPairClient {
/** /**
* Returns a list of {@link SshKeyPair}s registered by current user. * Returns a list of {@link SshKeyPair}s registered by current user.
* *
* @param options * @param options if present, how to constrain the list
* if present, how to constrain the list
* @return Set of {@link SshKeyPair}s matching the current constrains or * @return Set of {@link SshKeyPair}s matching the current constrains or
* empty set if no SshKeyPairs found. * empty set if no SshKeyPairs found.
*/ */
Set<SshKeyPair> listSSHKeyPairs(ListSSHKeyPairsOptions... options); Set<SshKeyPair> 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. * Creates a {@link SshKeyPair} with specified name.
* *
* @param name * @param name of the SshKeyPair.
* of the SshKeyPair.
* @return Created SshKeyPair. * @return Created SshKeyPair.
*/ */
SshKeyPair createSSHKeyPair(String name); SshKeyPair createSSHKeyPair(String name);
/** /**
* Retrieves the {@link SSHKeyPairClient} with given name. * Retrieves the {@link SSHKeyPairClient} with given name.
* *
* @param name * @param name name of the key pair
* name of the key pair
* @return SSH Key pair or null if not found. * @return SSH Key pair or null if not found.
*/ */
SshKeyPair getSSHKeyPair(String name); SshKeyPair getSSHKeyPair(String name);
/** /**
* Deletes the {@link SSHKeyPairClient} with given name. * Deletes the {@link SSHKeyPairClient} with given name.
* *
* @param name * @param name name of the key pair
* name of the key pair
* @return * @return
*/ */
void deleteSSHKeyPair(String name); void deleteSSHKeyPair(String name);

View File

@ -20,24 +20,26 @@ package org.jclouds.cloudstack.features;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; 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.cloudstack.options.ListSSHKeyPairsOptions;
import org.jclouds.crypto.SshKeys;
import org.jclouds.functions.IdentityFunction; import org.jclouds.functions.IdentityFunction;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed; import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.http.functions.ReleasePayloadAndReturn; import org.jclouds.http.functions.ReleasePayloadAndReturn;
import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Functions;
import com.google.inject.TypeLiteral;
/** /**
* Tests behavior of {@code SSHKeyPairAsyncClient} * Tests behavior of {@code SSHKeyPairAsyncClient}
* *
* @author Vijay Kiran * @author Vijay Kiran
*/ */
@Test(groups = "unit", testName = "SSHKeyPairAsyncClientTest") @Test(groups = "unit", testName = "SSHKeyPairAsyncClientTest")
@ -95,6 +97,25 @@ public class SSHKeyPairAsyncClientTest extends BaseCloudStackAsyncClientTest<SSH
} }
public void testRegisterSSHKeyPair() throws SecurityException, NoSuchMethodException, IOException {
Method method = SSHKeyPairAsyncClient.class.getMethod("registerSSHKeyPair", String.class, String.class);
String publicKey = URLEncoder.encode(SshKeys.generate().get("public"), "UTF-8");
HttpRequest httpRequest = processor.createRequest(method, "jclouds-keypair", publicKey);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=registerSSHKeyPair&name=jclouds-keypair&publickey="
+ publicKey
+ " HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testDeleteSSHKeyPair() throws SecurityException, NoSuchMethodException, IOException { public void testDeleteSSHKeyPair() throws SecurityException, NoSuchMethodException, IOException {
Method method = SSHKeyPairAsyncClient.class.getMethod("deleteSSHKeyPair", String.class); Method method = SSHKeyPairAsyncClient.class.getMethod("deleteSSHKeyPair", String.class);
HttpRequest httpRequest = processor.createRequest(method, "jclouds-keypair"); HttpRequest httpRequest = processor.createRequest(method, "jclouds-keypair");
@ -112,6 +133,7 @@ public class SSHKeyPairAsyncClientTest extends BaseCloudStackAsyncClientTest<SSH
} }
@Override @Override
protected TypeLiteral<RestAnnotationProcessor<SSHKeyPairAsyncClient>> createTypeLiteral() { protected TypeLiteral<RestAnnotationProcessor<SSHKeyPairAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<SSHKeyPairAsyncClient>>() { return new TypeLiteral<RestAnnotationProcessor<SSHKeyPairAsyncClient>>() {

View File

@ -23,11 +23,12 @@ import static org.testng.Assert.assertEquals;
import java.util.Set; import java.util.Set;
import org.jclouds.cloudstack.domain.SshKeyPair; import org.jclouds.cloudstack.domain.SshKeyPair;
import org.jclouds.crypto.SshKeys;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
* Tests behavior of {@code SSHKeyPairClient} * Tests behavior of {@code SSHKeyPairClient}
* *
* @author Vijay Kiran * @author Vijay Kiran
*/ */
@Test(groups = "live", singleThreaded = true, testName = "SSHKeyPairClientLiveTest") @Test(groups = "live", singleThreaded = true, testName = "SSHKeyPairClientLiveTest")
@ -52,6 +53,18 @@ public class SSHKeyPairClientLiveTest extends BaseCloudStackClientLiveTest {
sshKeyPair = null; 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) { protected void checkSSHKeyPair(SshKeyPair pair) {
assert pair.getName() != null : pair; assert pair.getName() != null : pair;
assertEquals(pair.toString(), client.getSSHKeyPairClient().getSSHKeyPair(pair.getName()).toString()); assertEquals(pair.toString(), client.getSSHKeyPairClient().getSSHKeyPair(pair.getName()).toString());