added nat functionality to cloudstack

This commit is contained in:
Adrian Cole 2011-02-20 13:39:56 -08:00
parent 3c0cffc54f
commit 8b8cf92dd1
15 changed files with 1130 additions and 25 deletions

View File

@ -21,6 +21,7 @@ package org.jclouds.cloudstack;
import org.jclouds.cloudstack.features.AddressAsyncClient;
import org.jclouds.cloudstack.features.AsyncJobAsyncClient;
import org.jclouds.cloudstack.features.NATAsyncClient;
import org.jclouds.cloudstack.features.NetworkAsyncClient;
import org.jclouds.cloudstack.features.OfferingAsyncClient;
import org.jclouds.cloudstack.features.SecurityGroupAsyncClient;
@ -86,4 +87,10 @@ public interface CloudStackAsyncClient {
*/
@Delegate
AddressAsyncClient getAddressClient();
/**
* Provides asynchronous access to NAT features.
*/
@Delegate
NATAsyncClient getNATClient();
}

View File

@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.features.AddressClient;
import org.jclouds.cloudstack.features.AsyncJobClient;
import org.jclouds.cloudstack.features.NATClient;
import org.jclouds.cloudstack.features.NetworkClient;
import org.jclouds.cloudstack.features.OfferingClient;
import org.jclouds.cloudstack.features.SecurityGroupClient;
@ -89,4 +90,10 @@ public interface CloudStackClient {
*/
@Delegate
AddressClient getAddressClient();
/**
* Provides synchronous access to NAT features.
*/
@Delegate
NATClient getNATClient();
}

View File

@ -27,6 +27,8 @@ 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.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;
@ -70,6 +72,7 @@ public class CloudStackRestClientModule extends RestClientModule<CloudStackClien
.put(SecurityGroupClient.class, SecurityGroupAsyncClient.class)//
.put(AsyncJobClient.class, AsyncJobAsyncClient.class)//
.put(AddressClient.class, AddressAsyncClient.class)//
.put(NATClient.class, NATAsyncClient.class)//
.build();
public CloudStackRestClientModule() {

View File

@ -0,0 +1,291 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.domain;
import com.google.gson.annotations.SerializedName;
/**
*
* @author Adrian Cole
*/
public class IPForwardingRule implements Comparable<IPForwardingRule> {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id;
private String IPAddress;
private long IPAddressId;
private int privatePort;
private String protocol;
public int publicPort;
private String state;
private String virtualMachineDisplayName;
public long virtualMachineId;
private String virtualMachineName;
public Builder id(long id) {
this.id = id;
return this;
}
public Builder IPAddress(String IPAddress) {
this.IPAddress = IPAddress;
return this;
}
public Builder IPAddressId(long IPAddressId) {
this.IPAddressId = IPAddressId;
return this;
}
public Builder privatePort(int privatePort) {
this.privatePort = privatePort;
return this;
}
public Builder protocol(String protocol) {
this.protocol = protocol;
return this;
}
public Builder publicPort(int publicPort) {
this.publicPort = publicPort;
return this;
}
public Builder state(String state) {
this.state = state;
return this;
}
public Builder virtualMachineDisplayName(String virtualMachineDisplayName) {
this.virtualMachineDisplayName = virtualMachineDisplayName;
return this;
}
public Builder virtualMachineId(long virtualMachineId) {
this.virtualMachineId = virtualMachineId;
return this;
}
public Builder virtualMachineName(String virtualMachineName) {
this.virtualMachineName = virtualMachineName;
return this;
}
public IPForwardingRule build() {
return new IPForwardingRule(id, IPAddress, IPAddressId, privatePort, protocol, publicPort, state,
virtualMachineDisplayName, virtualMachineId, virtualMachineName);
}
}
private long id;
@SerializedName("ipaddress")
private String IPAddress;
@SerializedName("ipaddressid")
private long IPAddressId;
@SerializedName("privateport")
private int privatePort;
private String protocol;
@SerializedName("publicport")
public int publicPort;
private String state;
@SerializedName("virtualmachinedisplayname")
private String virtualMachineDisplayName;
@SerializedName("virtualmachineid")
public long virtualMachineId;
@SerializedName("virtualmachinename")
private String virtualMachineName;
public IPForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, String protocol,
int publicPort, String state, String virtualMachineDisplayName, long virtualMachineId,
String virtualMachineName) {
this.id = id;
this.IPAddress = iPAddress;
this.IPAddressId = iPAddressId;
this.privatePort = privatePort;
this.protocol = protocol;
this.publicPort = publicPort;
this.state = state;
this.virtualMachineDisplayName = virtualMachineDisplayName;
this.virtualMachineId = virtualMachineId;
this.virtualMachineName = virtualMachineName;
}
@Override
public int compareTo(IPForwardingRule arg0) {
return new Long(id).compareTo(arg0.getId());
}
/**
*
* @return the ID of the port forwarding rule
*/
public long getId() {
return id;
}
/**
*
* @return the public ip address for the port forwarding rule
*/
public String getIPAddress() {
return IPAddress;
}
/**
*
* @return the public ip address id for the port forwarding rule
*/
public long getIPAddressId() {
return IPAddressId;
}
/**
*
* @return the private port for the port forwarding rule
*/
public int getPrivatePort() {
return privatePort;
}
/**
*
* @return the protocol of the port forwarding rule
*/
public String getProtocol() {
return protocol;
}
/**
*
* @return the public port for the port forwarding rule
*/
public int getPublicPort() {
return publicPort;
}
/**
*
* @return the state of the rule
*/
public String getState() {
return state;
}
/**
*
* @return the VM display name for the port forwarding rule
*/
public String getVirtualMachineDisplayName() {
return virtualMachineDisplayName;
}
/**
*
* @return the VM ID for the port forwarding rule
*/
public long getVirtualMachineId() {
return virtualMachineId;
}
/**
*
* @return the VM name for the port forwarding rule
*/
public String getVirtualMachineName() {
return virtualMachineName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((IPAddress == null) ? 0 : IPAddress.hashCode());
result = prime * result + (int) (IPAddressId ^ (IPAddressId >>> 32));
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + privatePort;
result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
result = prime * result + publicPort;
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + ((virtualMachineDisplayName == null) ? 0 : virtualMachineDisplayName.hashCode());
result = prime * result + (int) (virtualMachineId ^ (virtualMachineId >>> 32));
result = prime * result + ((virtualMachineName == null) ? 0 : virtualMachineName.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;
IPForwardingRule other = (IPForwardingRule) obj;
if (IPAddress == null) {
if (other.IPAddress != null)
return false;
} else if (!IPAddress.equals(other.IPAddress))
return false;
if (IPAddressId != other.IPAddressId)
return false;
if (id != other.id)
return false;
if (privatePort != other.privatePort)
return false;
if (protocol == null) {
if (other.protocol != null)
return false;
} else if (!protocol.equals(other.protocol))
return false;
if (publicPort != other.publicPort)
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
if (virtualMachineDisplayName == null) {
if (other.virtualMachineDisplayName != null)
return false;
} else if (!virtualMachineDisplayName.equals(other.virtualMachineDisplayName))
return false;
if (virtualMachineId != other.virtualMachineId)
return false;
if (virtualMachineName == null) {
if (other.virtualMachineName != null)
return false;
} else if (!virtualMachineName.equals(other.virtualMachineName))
return false;
return true;
}
@Override
public String toString() {
return "[IPAddress=" + IPAddress + ", IPAddressId=" + IPAddressId + ", id=" + id + ", privatePort=" + privatePort
+ ", protocol=" + protocol + ", publicPort=" + publicPort + ", state=" + state
+ ", virtualMachineDisplayName=" + virtualMachineDisplayName + ", virtualMachineId=" + virtualMachineId
+ ", virtualMachineName=" + virtualMachineName + "]";
}
}

View File

@ -0,0 +1,96 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions;
import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.Unwrap;
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 via their REST API.
* <p/>
*
* @see NATClient
* @see <a href="http://download.cloud.com/releases/2.2.0/api/TOC_User.html" />
* @author Adrian Cole
*/
@RequestFilters(QuerySigner.class)
@QueryParams(keys = "response", values = "json")
public interface NATAsyncClient {
/**
* @see NATClient#listIPForwardingRules
*/
@GET
@QueryParams(keys = "command", values = "listIpForwardingRules")
@Unwrap(depth = 2)
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<IPForwardingRule>> listIPForwardingRules(ListIPForwardingRulesOptions... options);
/**
* @see NATClient#getIPForwardingRule
*/
@GET
@QueryParams(keys = "command", values = "listIpForwardingRules")
@Unwrap(depth = 3, edgeCollection = Set.class)
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<IPForwardingRule> getIPForwardingRule(@QueryParam("id") long id);
/**
* @see NATClient#createIPForwardingRuleForVirtualMachine
*/
@GET
@QueryParams(keys = "command", values = "createIpForwardingRule")
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<AsyncCreateResponse> createIPForwardingRuleForVirtualMachine(
@QueryParam("virtualmachineid") long virtualMachineId, @QueryParam("ipaddressid") long IPAddressId,
@QueryParam("protocol") String protocol, @QueryParam("startport") int startPort,
CreateIPForwardingRuleOptions... options);
/**
* @see NATClient#deleteIpForwardingRule
*/
@GET
@QueryParams(keys = "command", values = "deleteIpForwardingRule")
@ExceptionParser(ReturnVoidOnNotFoundOr404.class)
ListenableFuture<Void> deleteIPForwardingRule(@QueryParam("id") long id);
}

View File

@ -0,0 +1,83 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions;
import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions;
import org.jclouds.concurrent.Timeout;
/**
* Provides synchronous access to CloudStack IPForwardingRule features.
* <p/>
*
* @see IPForwardingRuleAsyncClient
* @see <a href="http://download.cloud.com/releases/2.2.0/api/TOC_User.html" />
* @author Adrian Cole
*/
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface NATClient {
/**
* List the ip forwarding rules
*
* @param options
* if present, how to constrain the list.
* @return IPForwardingRulees matching query, or empty set, if no IPForwardingRulees are found
*/
Set<IPForwardingRule> listIPForwardingRules(ListIPForwardingRulesOptions... options);
/**
* get a specific IPForwardingRule by id
*
* @param id
* IPForwardingRule to get
* @return IPForwardingRule or null if not found
*/
IPForwardingRule getIPForwardingRule(long id);
/**
* Creates an ip forwarding rule
*
* @param virtualMachineId
* the virtual machine this rule applies to.
*
* @param IPAddressId
* the public IP address id of the forwarding rule, already associated via associateIp
* @param protocol
* the protocol for the rule. Valid values are TCP or UDP.
* @param startPort
* the start port for the rule
* @return response used to track creation
*/
AsyncCreateResponse createIPForwardingRuleForVirtualMachine(long virtualMachineId, long IPAddressId,
String protocol, int startPort, CreateIPForwardingRuleOptions... options);
/**
* Deletes an ip forwarding rule
*
* @param id
* the id of the forwarding rule
*/
void deleteIPForwardingRule(long id);
}

View File

@ -0,0 +1,56 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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 org.jclouds.http.options.BaseHttpRequestOptions;
import com.google.common.collect.ImmutableSet;
/**
* Options used to create an ip forwarding rule
*
* @see <a href="http://download.cloud.com/releases/2.2.0/api/user/createIpForwardingRule.html" />
* @author Adrian Cole
*/
public class CreateIPForwardingRuleOptions extends BaseHttpRequestOptions {
public static final CreateIPForwardingRuleOptions NONE = new CreateIPForwardingRuleOptions();
/**
* @param endPort
* the end port for the rule
*/
public CreateIPForwardingRuleOptions endPort(int endPort) {
this.queryParameters.replaceValues("endport", ImmutableSet.of(endPort + ""));
return this;
}
public static class Builder {
/**
* @see CreateIPForwardingRuleOptions#endPort
*/
public static CreateIPForwardingRuleOptions endPort(int endPort) {
CreateIPForwardingRuleOptions options = new CreateIPForwardingRuleOptions();
return options.endPort(endPort);
}
}
}

View File

@ -0,0 +1,128 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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 org.jclouds.http.options.BaseHttpRequestOptions;
import com.google.common.collect.ImmutableSet;
/**
* Options used to control what ip forwarding rules are returned
*
* @see <a href="http://download.cloud.com/releases/2.2.0/api/user/listIpForwardingRules.html" />
* @author Adrian Cole
*/
public class ListIPForwardingRulesOptions extends BaseHttpRequestOptions {
public static final ListIPForwardingRulesOptions NONE = new ListIPForwardingRulesOptions();
/**
* @param id
* Lists rule with the specified ID.
*/
public ListIPForwardingRulesOptions id(long id) {
this.queryParameters.replaceValues("id", ImmutableSet.of(id + ""));
return this;
}
/**
* @param domainId
* Lists all rules for this id. If used with the account parameter, returns all rules
* for an account in the specified domain ID.
*/
public ListIPForwardingRulesOptions domainId(long domainId) {
this.queryParameters.replaceValues("domainid", ImmutableSet.of(domainId + ""));
return this;
}
/**
* @param account
* the account associated with the ip forwarding rule. Must be used with the domainId
* parameter.
*/
public ListIPForwardingRulesOptions account(String account) {
this.queryParameters.replaceValues("account", ImmutableSet.of(account));
return this;
}
/**
* @param IPAddressId
* list the rule belonging to this public ip address
*/
public ListIPForwardingRulesOptions IPAddressId(long IPAddressId) {
this.queryParameters.replaceValues("ipaddressid", ImmutableSet.of(IPAddressId + ""));
return this;
}
/**
* @param virtualMachineId
* Lists all rules applied to the specified Vm.
*/
public ListIPForwardingRulesOptions virtualMachineId(long virtualMachineId) {
this.queryParameters.replaceValues("virtualmachineid", ImmutableSet.of(virtualMachineId + ""));
return this;
}
public static class Builder {
/**
* @see ListIPForwardingRulesOptions#account
*/
public static ListIPForwardingRulesOptions account(String account) {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions();
return options.account(account);
}
/**
* @see ListIPForwardingRulesOptions#IPAddressId
*/
public static ListIPForwardingRulesOptions IPAddressId(long IPAddressId) {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions();
return options.IPAddressId(IPAddressId);
}
/**
* @see ListIPForwardingRulesOptions#domainId
*/
public static ListIPForwardingRulesOptions domainId(long id) {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions();
return options.domainId(id);
}
/**
* @see ListIPForwardingRulesOptions#id
*/
public static ListIPForwardingRulesOptions id(long id) {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions();
return options.id(id);
}
/**
* @see ListIPForwardingRulesOptions#virtualMachineId
*/
public static ListIPForwardingRulesOptions virtualMachineId(long virtualMachineId) {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions();
return options.virtualMachineId(virtualMachineId);
}
}
}

View File

@ -19,7 +19,7 @@
package org.jclouds.cloudstack.features;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
@ -27,7 +27,9 @@ import static org.testng.Assert.assertTrue;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.PublicIPAddress;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.cloudstack.options.ListPublicIPAddressesOptions;
@ -37,6 +39,8 @@ import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
/**
* Tests behavior of {@code PublicIPAddressClientLiveTest}
*
@ -54,13 +58,28 @@ public class AddressClientLiveTest extends BaseCloudStackClientLiveTest {
}
public void testAssociateDisassociatePublicIPAddress() throws Exception {
final Zone zone = get(client.getZoneClient().listZones(), 0);
ip = createPublicIPAddress(client, jobComplete);
checkIP(ip);
}
public static PublicIPAddress createPublicIPAddress(CloudStackClient client, RetryablePredicate<Long> jobComplete) {
Zone zone = find(client.getZoneClient().listZones(), new Predicate<Zone>() {
@Override
public boolean apply(Zone arg0) {
return arg0.getNetworkType() == NetworkType.ADVANCED;
}
@Override
public String toString() {
return "networkType(ADVANCED)";
}
});
AsyncCreateResponse job = client.getAddressClient().associateIPAddress(zone.getId());
assert jobComplete.apply(job.getJobId());
ip = client.getAddressClient().getPublicIPAddress(job.getId());
PublicIPAddress ip = client.getAddressClient().getPublicIPAddress(job.getId());
assertEquals(ip.getZoneId(), zone.getId());
checkIP(ip);
return ip;
}
@AfterGroups(groups = "live")

View File

@ -27,6 +27,10 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.Constants;
import org.jclouds.cloudstack.CloudStackAsyncClient;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.predicates.JobComplete;
import org.jclouds.cloudstack.predicates.VirtualMachineDestroyed;
import org.jclouds.cloudstack.predicates.VirtualMachineRunning;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
@ -55,6 +59,9 @@ public class BaseCloudStackClientLiveTest {
protected String endpoint;
protected String apiversion;
protected Predicate<IPSocket> socketTester;
protected RetryablePredicate<Long> jobComplete;
protected RetryablePredicate<VirtualMachine> virtualMachineRunning;
protected RetryablePredicate<VirtualMachine> virtualMachineDestroyed;
protected void setupCredentials() {
identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider
@ -87,6 +94,11 @@ public class BaseCloudStackClientLiveTest {
client = context.getApi();
socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 180, 1, TimeUnit.SECONDS);
jobComplete = new RetryablePredicate<Long>(new JobComplete(client), 600, 5, TimeUnit.SECONDS);
virtualMachineRunning = new RetryablePredicate<VirtualMachine>(new VirtualMachineRunning(client), 600, 5,
TimeUnit.SECONDS);
virtualMachineDestroyed = new RetryablePredicate<VirtualMachine>(new VirtualMachineDestroyed(client), 600, 5,
TimeUnit.SECONDS);
}
@AfterGroups(groups = "live")

View File

@ -0,0 +1,163 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.CreateIPForwardingRuleOptions;
import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ReleasePayloadAndReturn;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValueInSet;
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.inject.TypeLiteral;
/**
* Tests behavior of {@code NATAsyncClient}
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "NATAsyncClientTest")
public class NATAsyncClientTest extends BaseCloudStackAsyncClientTest<NATAsyncClient> {
public void testListIPForwardingRules() throws SecurityException, NoSuchMethodException, IOException {
Method method = NATAsyncClient.class.getMethod("listIPForwardingRules", ListIPForwardingRulesOptions[].class);
HttpRequest httpRequest = processor.createRequest(method);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listIpForwardingRules HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testListIPForwardingRulesOptions() throws SecurityException, NoSuchMethodException, IOException {
Method method = NATAsyncClient.class.getMethod("listIPForwardingRules", ListIPForwardingRulesOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, ListIPForwardingRulesOptions.Builder
.virtualMachineId(3));
assertRequestLineEquals(
httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listIpForwardingRules&virtualmachineid=3 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testGetIPForwardingRule() throws SecurityException, NoSuchMethodException, IOException {
Method method = NATAsyncClient.class.getMethod("getIPForwardingRule", long.class);
HttpRequest httpRequest = processor.createRequest(method, 5);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listIpForwardingRules&id=5 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValueInSet.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testCreateIPForwardingRuleForVirtualMachine() throws SecurityException, NoSuchMethodException,
IOException {
Method method = NATAsyncClient.class.getMethod("createIPForwardingRuleForVirtualMachine", long.class, long.class,
String.class, int.class, CreateIPForwardingRuleOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, 6, 7, "tcp", 22);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=createIpForwardingRule&virtualmachineid=6&protocol=tcp&ipaddressid=7&startport=22 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testCreateIPForwardingRuleForVirtualMachineOptions() throws SecurityException, NoSuchMethodException,
IOException {
Method method = NATAsyncClient.class.getMethod("createIPForwardingRuleForVirtualMachine", long.class, long.class,
String.class, int.class, CreateIPForwardingRuleOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, 6, 7, "tcp", 22, CreateIPForwardingRuleOptions.Builder
.endPort(22));
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=createIpForwardingRule&virtualmachineid=6&protocol=tcp&ipaddressid=7&startport=22&endport=22 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testDeleteIPForwardingRule() throws SecurityException, NoSuchMethodException, IOException {
Method method = NATAsyncClient.class.getMethod("deleteIPForwardingRule", long.class);
HttpRequest httpRequest = processor.createRequest(method, 5);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=deleteIpForwardingRule&id=5 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<NATAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<NATAsyncClient>>() {
};
}
}

View File

@ -0,0 +1,112 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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 agred 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.
* Se the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.cloudstack.features;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.domain.PublicIPAddress;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions;
import org.jclouds.net.IPSocket;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code NATClientLiveTest}
*
* @author Adrian Cole
*/
@Test(groups = "live", sequential = true, testName = "NATClientLiveTest")
public class NATClientLiveTest extends BaseCloudStackClientLiveTest {
private PublicIPAddress ip = null;
private VirtualMachine vm;
private IPForwardingRule rule;
@BeforeGroups(groups = "live")
public void setupClient() {
super.setupClient();
prefix += "rule";
ip = AddressClientLiveTest.createPublicIPAddress(client, jobComplete);
vm = VirtualMachineClientLiveTest.createVirtualMachine(client, jobComplete, virtualMachineRunning);
}
public void testCreateIPForwardingRule() throws Exception {
// TODO check for 1-1 Nat feature
AsyncCreateResponse job = client.getNATClient().createIPForwardingRuleForVirtualMachine(vm.getId(), ip.getId(),
"tcp", 22);
assert jobComplete.apply(job.getJobId());
rule = client.getNATClient().getIPForwardingRule(job.getId());
assertEquals(rule.getIPAddressId(), ip.getId());
assertEquals(rule.getVirtualMachineId(), vm.getId());
assertEquals(rule.getPublicPort(), 22);
assertEquals(rule.getProtocol(), "tcp");
checkRule(rule);
IPSocket socket = new IPSocket(ip.getIPAddress(), 22);
socketTester.apply(socket);
}
@AfterGroups(groups = "live")
protected void tearDown() {
if (rule != null) {
client.getNATClient().deleteIPForwardingRule(rule.getId());
}
if (ip != null) {
client.getAddressClient().disassociateIPAddress(ip.getId());
}
if (vm != null) {
assert jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId()));
}
super.tearDown();
}
public void testListIPForwardingRules() throws Exception {
Set<IPForwardingRule> response = client.getNATClient().listIPForwardingRules();
assert null != response;
assertTrue(response.size() >= 0);
for (IPForwardingRule rule : response) {
IPForwardingRule newDetails = getOnlyElement(client.getNATClient().listIPForwardingRules(
ListIPForwardingRulesOptions.Builder.id(rule.getId())));
assertEquals(rule.getId(), newDetails.getId());
checkRule(rule);
}
}
protected void checkRule(IPForwardingRule rule) {
assertEquals(rule.getId(), client.getNATClient().getIPForwardingRule(rule.getId()).getId());
assert rule.getId() > 0 : rule;
assert rule.getIPAddress() != null : rule;
assert rule.getIPAddressId() > 0 : rule;
assert rule.getPrivatePort() > 0 : rule;
assert rule.getProtocol() != null : rule;
assert rule.getPublicPort() > 0 : rule;
assert rule.getState() != null : rule;
assert rule.getVirtualMachineDisplayName() != null : rule;
assert rule.getVirtualMachineId() > 0 : rule;
assert rule.getVirtualMachineName() != null : rule;
}
}

View File

@ -26,8 +26,8 @@ import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.GuestIPType;
import org.jclouds.cloudstack.domain.NIC;
@ -40,12 +40,8 @@ import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
import org.jclouds.cloudstack.options.ListVirtualMachinesOptions;
import org.jclouds.cloudstack.predicates.JobComplete;
import org.jclouds.cloudstack.predicates.VirtualMachineDestroyed;
import org.jclouds.cloudstack.predicates.VirtualMachineRunning;
import org.jclouds.predicates.RetryablePredicate;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
@ -60,19 +56,6 @@ import com.google.common.collect.Ordering;
@Test(groups = "live", sequential = true, testName = "VirtualMachineClientLiveTest")
public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
private VirtualMachine vm = null;
private RetryablePredicate<Long> jobComplete;
private RetryablePredicate<VirtualMachine> virtualMachineRunning;
private RetryablePredicate<VirtualMachine> virtualMachineDestroyed;
@BeforeGroups(groups = "live")
public void setupClient() {
super.setupClient();
jobComplete = new RetryablePredicate<Long>(new JobComplete(client), 600, 5, TimeUnit.SECONDS);
virtualMachineRunning = new RetryablePredicate<VirtualMachine>(new VirtualMachineRunning(client), 600, 5,
TimeUnit.SECONDS);
virtualMachineDestroyed = new RetryablePredicate<VirtualMachine>(new VirtualMachineDestroyed(client), 600, 5,
TimeUnit.SECONDS);
}
static final Ordering<ServiceOffering> DEFAULT_SIZE_ORDERING = new Ordering<ServiceOffering>() {
public int compare(ServiceOffering left, ServiceOffering right) {
@ -81,7 +64,8 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
}
};
public void testCreateDestroyVirtualMachine() throws Exception {
public static VirtualMachine createVirtualMachine(CloudStackClient client, RetryablePredicate<Long> jobComplete,
RetryablePredicate<VirtualMachine> virtualMachineRunning) {
final Zone zone = get(client.getZoneClient().listZones(), 0);
long serviceOfferingId = DEFAULT_SIZE_ORDERING.min(client.getOfferingClient().listServiceOfferings()).getId();
@ -121,11 +105,19 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
AsyncCreateResponse job = client.getVirtualMachineClient().deployVirtualMachine(serviceOfferingId, templateId,
zone.getId(), options);
assert jobComplete.apply(job.getJobId());
vm = client.getVirtualMachineClient().getVirtualMachine(job.getId());
VirtualMachine vm = client.getVirtualMachineClient().getVirtualMachine(job.getId());
if (vm.isPasswordEnabled())
assert vm.getPassword() != null : vm;
assert virtualMachineRunning.apply(vm);
assertEquals(vm.getServiceOfferingId(), serviceOfferingId);
assertEquals(vm.getTemplateId(), templateId);
assertEquals(vm.getZoneId(), zone.getId());
return vm;
}
public void testCreateDestroyVirtualMachine() throws Exception {
vm = createVirtualMachine(client, jobComplete, virtualMachineRunning);
assertEquals(vm.getRootDeviceType(), "NetworkFilesystem");
checkVm(vm);
}

View File

@ -0,0 +1,46 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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 static org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions.Builder.endPort;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
/**
* Tests behavior of {@code CreateIPForwardingRuleOptions}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class CreateIPForwardingRuleOptionsTest {
public void testNetworkId() {
CreateIPForwardingRuleOptions options = new CreateIPForwardingRuleOptions().endPort(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("endport"));
}
public void testNetworkIdStatic() {
CreateIPForwardingRuleOptions options = endPort(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("endport"));
}
}

View File

@ -0,0 +1,90 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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 static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.account;
import static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.domainId;
import static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.id;
import static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.IPAddressId;
import static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.virtualMachineId;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
/**
* Tests behavior of {@code ListIPForwardingRulesOptions}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ListIPForwardingRulesOptionsTest {
public void testId() {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().id(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id"));
}
public void testIdStatic() {
ListIPForwardingRulesOptions options = id(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id"));
}
public void testAccount() {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().account("account");
assertEquals(ImmutableList.of("account"), options.buildQueryParameters().get("account"));
}
public void testAccountStatic() {
ListIPForwardingRulesOptions options = account("account");
assertEquals(ImmutableList.of("account"), options.buildQueryParameters().get("account"));
}
public void testName() {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().IPAddressId(9);
assertEquals(ImmutableList.of("9"), options.buildQueryParameters().get("ipaddressid"));
}
public void testNameStatic() {
ListIPForwardingRulesOptions options = IPAddressId(9);
assertEquals(ImmutableList.of("9"), options.buildQueryParameters().get("ipaddressid"));
}
public void testDomainId() {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().domainId(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid"));
}
public void testDomainIdStatic() {
ListIPForwardingRulesOptions options = domainId(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid"));
}
public void testVirtualMachineId() {
ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().virtualMachineId(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("virtualmachineid"));
}
public void testVirtualMachineIdStatic() {
ListIPForwardingRulesOptions options = virtualMachineId(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("virtualmachineid"));
}
}