First-cut implementation of SSHKeyPair for cloudstack

This commit is contained in:
vijaykiran 2011-10-29 16:01:26 +02:00
parent fd9b49873b
commit 60f70a0589
10 changed files with 486 additions and 147 deletions

View File

@ -18,120 +18,112 @@
*/
package org.jclouds.cloudstack;
import org.jclouds.cloudstack.features.AccountAsyncClient;
import org.jclouds.cloudstack.features.AddressAsyncClient;
import org.jclouds.cloudstack.features.AsyncJobAsyncClient;
import org.jclouds.cloudstack.features.ConfigurationAsyncClient;
import org.jclouds.cloudstack.features.FirewallAsyncClient;
import org.jclouds.cloudstack.features.GuestOSAsyncClient;
import org.jclouds.cloudstack.features.HypervisorAsyncClient;
import org.jclouds.cloudstack.features.LoadBalancerAsyncClient;
import org.jclouds.cloudstack.features.NATAsyncClient;
import org.jclouds.cloudstack.features.NetworkAsyncClient;
import org.jclouds.cloudstack.features.OfferingAsyncClient;
import org.jclouds.cloudstack.features.SecurityGroupAsyncClient;
import org.jclouds.cloudstack.features.TemplateAsyncClient;
import org.jclouds.cloudstack.features.VirtualMachineAsyncClient;
import org.jclouds.cloudstack.features.ZoneAsyncClient;
import org.jclouds.cloudstack.features.*;
import org.jclouds.rest.annotations.Delegate;
/**
* Provides asynchronous access to CloudStack via their REST API.
* <p/>
*
* @author Adrian Cole
* @see CloudStackClient
* @see <a href="http://download.cloud.com/releases/2.2.0/api/TOC_User.html" />
* @author Adrian Cole
*/
public interface CloudStackAsyncClient {
/**
* Provides asynchronous access to Zone features.
*/
@Delegate
ZoneAsyncClient getZoneClient();
/**
* Provides asynchronous access to Zone features.
*/
@Delegate
ZoneAsyncClient getZoneClient();
/**
* Provides asynchronous access to Template features.
*/
@Delegate
TemplateAsyncClient getTemplateClient();
/**
* Provides asynchronous access to Template features.
*/
@Delegate
TemplateAsyncClient getTemplateClient();
/**
* Provides asynchronous access to Service, Disk, and Network Offering features.
*/
@Delegate
OfferingAsyncClient getOfferingClient();
/**
* Provides asynchronous access to Service, Disk, and Network Offering features.
*/
@Delegate
OfferingAsyncClient getOfferingClient();
/**
* Provides asynchronous access to Network features.
*/
@Delegate
NetworkAsyncClient getNetworkClient();
/**
* Provides asynchronous access to Network features.
*/
@Delegate
NetworkAsyncClient getNetworkClient();
/**
* Provides asynchronous access to VirtualMachine features.
*/
@Delegate
VirtualMachineAsyncClient getVirtualMachineClient();
/**
* Provides asynchronous access to VirtualMachine features.
*/
@Delegate
VirtualMachineAsyncClient getVirtualMachineClient();
/**
* Provides asynchronous access to SecurityGroup features.
*/
@Delegate
SecurityGroupAsyncClient getSecurityGroupClient();
/**
* Provides asynchronous access to SecurityGroup features.
*/
@Delegate
SecurityGroupAsyncClient getSecurityGroupClient();
/**
* Provides asynchronous access to AsyncJob features.
*/
@Delegate
AsyncJobAsyncClient getAsyncJobClient();
/**
* Provides asynchronous access to AsyncJob features.
*/
@Delegate
AsyncJobAsyncClient getAsyncJobClient();
/**
* Provides asynchronous access to Address features.
*/
@Delegate
AddressAsyncClient getAddressClient();
/**
* Provides asynchronous access to Address features.
*/
@Delegate
AddressAsyncClient getAddressClient();
/**
* Provides asynchronous access to NAT features.
*/
@Delegate
NATAsyncClient getNATClient();
/**
* Provides asynchronous access to NAT features.
*/
@Delegate
NATAsyncClient getNATClient();
/**
* Provides asynchronous access to Firewall features.
*/
@Delegate
FirewallAsyncClient getFirewallClient();
/**
* Provides asynchronous access to Firewall features.
*/
@Delegate
FirewallAsyncClient getFirewallClient();
/**
* Provides asynchronous access to LoadBalancer features.
*/
@Delegate
LoadBalancerAsyncClient getLoadBalancerClient();
/**
* Provides asynchronous access to LoadBalancer features.
*/
@Delegate
LoadBalancerAsyncClient getLoadBalancerClient();
/**
* Provides asynchronous access to GuestOS features.
*/
@Delegate
GuestOSAsyncClient getGuestOSClient();
/**
* Provides asynchronous access to GuestOS features.
*/
@Delegate
GuestOSAsyncClient getGuestOSClient();
/**
* Provides asynchronous access to Hypervisor features.
*/
@Delegate
HypervisorAsyncClient getHypervisorClient();
/**
* Provides asynchronous access to Hypervisor features.
*/
@Delegate
HypervisorAsyncClient getHypervisorClient();
/**
* Provides asynchronous access to Configuration features.
*/
@Delegate
ConfigurationAsyncClient getConfigurationClient();
/**
* Provides asynchronous access to Configuration features.
*/
@Delegate
ConfigurationAsyncClient getConfigurationClient();
/**
* Provides asynchronous access to Account features.
*/
@Delegate
AccountAsyncClient getAccountClient();
/**
* Provides asynchronous access to Account features.
*/
@Delegate
AccountAsyncClient getAccountClient();
/**
* Provides asynchronous access to SSH Keypairs
*/
@Delegate
SSHKeyPairAsyncClient getSSHKeyPairClient();
}

View File

@ -18,26 +18,12 @@
*/
package org.jclouds.cloudstack;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.features.AccountClient;
import org.jclouds.cloudstack.features.AddressClient;
import org.jclouds.cloudstack.features.AsyncJobClient;
import org.jclouds.cloudstack.features.ConfigurationClient;
import org.jclouds.cloudstack.features.FirewallClient;
import org.jclouds.cloudstack.features.GuestOSClient;
import org.jclouds.cloudstack.features.HypervisorClient;
import org.jclouds.cloudstack.features.LoadBalancerClient;
import org.jclouds.cloudstack.features.NATClient;
import org.jclouds.cloudstack.features.NetworkClient;
import org.jclouds.cloudstack.features.OfferingClient;
import org.jclouds.cloudstack.features.SecurityGroupClient;
import org.jclouds.cloudstack.features.TemplateClient;
import org.jclouds.cloudstack.features.VirtualMachineClient;
import org.jclouds.cloudstack.features.ZoneClient;
import org.jclouds.cloudstack.features.*;
import org.jclouds.concurrent.Timeout;
import org.jclouds.rest.annotations.Delegate;
import java.util.concurrent.TimeUnit;
/**
* Provides synchronous access to CloudStack.
* <p/>
@ -137,4 +123,11 @@ public interface CloudStackClient {
*/
@Delegate
AccountClient getAccountClient();
/**
* Provides synchronous access to SSH Keypairs
*/
@Delegate
SSHKeyPairClient getSSHKeyPairClient();
}

View File

@ -18,40 +18,10 @@
*/
package org.jclouds.cloudstack.config;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import org.jclouds.cloudstack.CloudStackAsyncClient;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.features.AccountAsyncClient;
import org.jclouds.cloudstack.features.AccountClient;
import org.jclouds.cloudstack.features.AddressAsyncClient;
import org.jclouds.cloudstack.features.AddressClient;
import org.jclouds.cloudstack.features.AsyncJobAsyncClient;
import org.jclouds.cloudstack.features.AsyncJobClient;
import org.jclouds.cloudstack.features.ConfigurationAsyncClient;
import org.jclouds.cloudstack.features.ConfigurationClient;
import org.jclouds.cloudstack.features.FirewallAsyncClient;
import org.jclouds.cloudstack.features.FirewallClient;
import org.jclouds.cloudstack.features.GuestOSAsyncClient;
import org.jclouds.cloudstack.features.GuestOSClient;
import org.jclouds.cloudstack.features.HypervisorAsyncClient;
import org.jclouds.cloudstack.features.HypervisorClient;
import org.jclouds.cloudstack.features.LoadBalancerAsyncClient;
import org.jclouds.cloudstack.features.LoadBalancerClient;
import org.jclouds.cloudstack.features.NATAsyncClient;
import org.jclouds.cloudstack.features.NATClient;
import org.jclouds.cloudstack.features.NetworkAsyncClient;
import org.jclouds.cloudstack.features.NetworkClient;
import org.jclouds.cloudstack.features.OfferingAsyncClient;
import org.jclouds.cloudstack.features.OfferingClient;
import org.jclouds.cloudstack.features.SecurityGroupAsyncClient;
import org.jclouds.cloudstack.features.SecurityGroupClient;
import org.jclouds.cloudstack.features.TemplateAsyncClient;
import org.jclouds.cloudstack.features.TemplateClient;
import org.jclouds.cloudstack.features.VirtualMachineAsyncClient;
import org.jclouds.cloudstack.features.VirtualMachineClient;
import org.jclouds.cloudstack.features.ZoneAsyncClient;
import org.jclouds.cloudstack.features.ZoneClient;
import org.jclouds.cloudstack.features.*;
import org.jclouds.cloudstack.handlers.CloudStackErrorHandler;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.RequiresHttp;
@ -65,7 +35,7 @@ import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.config.RestClientModule;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
/**
* Configures the cloudstack connection.
@ -92,6 +62,7 @@ public class CloudStackRestClientModule extends RestClientModule<CloudStackClien
.put(HypervisorClient.class, HypervisorAsyncClient.class)//
.put(ConfigurationClient.class, ConfigurationAsyncClient.class)//
.put(AccountClient.class, AccountAsyncClient.class)//
.put(SSHKeyPairClient.class, SSHKeyPairAsyncClient.class)//
.build();
public CloudStackRestClientModule() {

View File

@ -0,0 +1,116 @@
package org.jclouds.cloudstack.domain;
import com.google.gson.annotations.SerializedName;
public class SSHKeyPair implements Comparable<SSHKeyPair> {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String fingerprint;
private String name;
private String privateKey;
public Builder fingerprint(String fingerprint) {
this.fingerprint = fingerprint;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder privateKey(String privateKey) {
this.privateKey = privateKey;
return this;
}
public SSHKeyPair build() {
return new SSHKeyPair(fingerprint, name, privateKey);
}
}
// for deserialization
SSHKeyPair() {
}
private String fingerprint;
private String name;
@SerializedName("privatekey")
private String privateKey;
public SSHKeyPair(String fingerprint, String name, String privateKey) {
this.fingerprint = fingerprint;
this.name = name;
this.privateKey = privateKey;
}
public String getFingerprint() {
return fingerprint;
}
public String getName() {
return name;
}
public String getPrivateKey() {
return privateKey;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((fingerprint == null) ? 0 : fingerprint.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((privateKey == null) ? 0 : privateKey.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SSHKeyPair other = (SSHKeyPair) obj;
if (fingerprint == null) {
if (other.fingerprint != null)
return false;
} else if (!fingerprint.equals(other.fingerprint))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (privateKey == null) {
if (other.privateKey != null)
return false;
} else if (!privateKey.equals(other.privateKey))
return false;
return true;
}
@Override
public String toString() {
return "[fingerprint=" + fingerprint + ", name=" + name + "]";
}
@Override
public int compareTo(SSHKeyPair arg0) {
return fingerprint.compareTo(arg0.getFingerprint());
}
}

View File

@ -0,0 +1,47 @@
/**
* 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.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
@RequestFilters(QuerySigner.class)
@QueryParams(keys = "response", values = "json")
public interface SSHKeyPairAsyncClient {
/**
* @see AccountClient#listAccounts
*/
@GET
@QueryParams(keys = "command", values = "listSSHKeyPairs")
// @SelectJson("")
// @Consumes(MediaType.APPLICATION_JSON)
// @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
// ListenableFuture<Set<SSHKeyPair>> listSSHKeyPairs();
ListenableFuture<String> listSSHKeyPairs();
@GET
@QueryParams(keys = "command", values="createSSHKeyPair")
ListenableFuture<String> createSSHKeyPair(@QueryParam("name") String name);
}

View File

@ -0,0 +1,22 @@
package org.jclouds.cloudstack.features;
import org.jclouds.concurrent.Timeout;
import java.util.concurrent.TimeUnit;
/**
* Provides synchronous access to CloudStack SSHKeyPair features.
* <p/>
*
* @author Adrian Cole
* @see <a href="http://download.cloud.com/releases/2.2.0/api_2.2.8/user/listSSHKeyPairs.html" />
*/
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface SSHKeyPairClient {
String listSSHKeyPairs();
String createSSHKeyPair(String name);
}

View File

@ -0,0 +1,46 @@
/**
* 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.options;
import com.google.common.collect.ImmutableSet;
import org.jclouds.http.options.BaseHttpRequestOptions;
public class ListSSHKeyPairsOptions extends BaseHttpRequestOptions {
public static final ListSSHKeyPairsOptions NONE = new ListSSHKeyPairsOptions();
/**
* @param name the SSHKeyPair name
*/
public ListSSHKeyPairsOptions name(String name) {
this.queryParameters.replaceValues("name", ImmutableSet.of(name));
return this;
}
public static class Builder {
/**
* @see ListSSHKeyPairsOptions#name
*/
public static ListSSHKeyPairsOptions name(String name) {
ListSSHKeyPairsOptions options = new ListSSHKeyPairsOptions();
return options.name(name);
}
}
}

View File

@ -18,23 +18,23 @@
*/
package org.jclouds.cloudstack;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import com.google.inject.TypeLiteral;
import org.jclouds.cloudstack.features.BaseCloudStackAsyncClientTest;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.inject.TypeLiteral;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/**
* Tests behavior of {@code CloudStackAsyncClient}
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
// NOTE:without testName, this will not call @Before* and fail w/NPE during
// surefire
@Test(groups = "unit", testName = "CloudStackAsyncClientTest")
public class CloudStackAsyncClientTest extends BaseCloudStackAsyncClientTest<CloudStackAsyncClient> {
@ -57,6 +57,7 @@ public class CloudStackAsyncClientTest extends BaseCloudStackAsyncClientTest<Clo
assert syncClient.getHypervisorClient() != null;
assert syncClient.getConfigurationClient() != null;
assert syncClient.getAccountClient() != null;
assert syncClient.getSSHKeyPairClient() != null;
}
public void testAsync() throws SecurityException, NoSuchMethodException, InterruptedException, ExecutionException {
@ -75,6 +76,7 @@ public class CloudStackAsyncClientTest extends BaseCloudStackAsyncClientTest<Clo
assert asyncClient.getHypervisorClient() != null;
assert asyncClient.getConfigurationClient() != null;
assert asyncClient.getAccountClient() != null;
assert asyncClient.getSSHKeyPairClient() != null;
}
@Override

View File

@ -0,0 +1,103 @@
/**
* 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.features;
import java.io.IOException;
import java.lang.reflect.Method;
import org.jclouds.cloudstack.options.ListSSHKeyPairsOptions;
import org.jclouds.functions.IdentityFunction;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
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 Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during
// surefire
@Test(groups = "unit", testName = "SSHKeyPairAsyncClientTest")
public class SSHKeyPairAsyncClientTest extends BaseCloudStackAsyncClientTest<SSHKeyPairAsyncClient> {
public void testListSSHKeyPairs() throws SecurityException, NoSuchMethodException, IOException {
Method method = SSHKeyPairAsyncClient.class.getMethod("listSSHKeyPairs", ListSSHKeyPairsOptions[].class);
HttpRequest httpRequest = processor.createRequest(method);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listSSHKeyPairs HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testListSSHKeyPairsOptions() throws SecurityException, NoSuchMethodException, IOException {
Method method = SSHKeyPairAsyncClient.class.getMethod("listSSHKeyPairs", ListSSHKeyPairsOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, ListSSHKeyPairsOptions.Builder.name("jclouds"));
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listSSHKeyPairs&SSHKeyPair=jclouds&domainid=123 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testGetSSHKeyPair() throws SecurityException, NoSuchMethodException, IOException {
Method method = SSHKeyPairAsyncClient.class.getMethod("getSSHKeyPair", long.class);
HttpRequest httpRequest = processor.createRequest(method, 3l);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listSSHKeyPairs&id=3 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest,
Functions.compose(IdentityFunction.INSTANCE, IdentityFunction.INSTANCE).getClass());
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<SSHKeyPairAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<SSHKeyPairAsyncClient>>() {
};
}
}

View File

@ -0,0 +1,47 @@
/**
* 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.features;
import org.jclouds.cloudstack.domain.SSHKeyPair;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code SSHKeyPairClient}
*
* @author Adrian Cole
*/
@Test(groups = "live", singleThreaded = true, testName = "SSHKeyPairClientLiveTest")
public class SSHKeyPairClientLiveTest extends BaseCloudStackClientLiveTest {
public void testListSSHKeyPairs() {
System.out.println(client.getSSHKeyPairClient().listSSHKeyPairs());
}
public void testCreateSSHKeyPair() {
final String sshKeyPair = client.getSSHKeyPairClient().createSSHKeyPair("vijay-test");
System.out.println(sshKeyPair);
}
protected void checkSSHKeyPair(SSHKeyPair pair) {
assert pair.getName() != null : pair;
// assertEquals(pair.toString(),
// client.getSSHKeyPairClient().getSSHKeyPair(pair.getName()).toString());
}
}