From 90d3cf4654dc7afa10f21f7abe39473c58a985d9 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 20 Feb 2011 19:19:03 -0800 Subject: [PATCH] tested ip and port forwarding rules on cloudstack --- .../cloudstack/domain/IPForwardingRule.java | 295 ++++++++++++++++++ .../cloudstack/features/NATAsyncClient.java | 25 +- .../cloudstack/features/NATClient.java | 10 +- .../features/AsyncJobClientLiveTest.java | 5 - .../BaseCloudStackClientLiveTest.java | 33 +- .../features/FirewallClientLiveTest.java | 12 + .../features/NATAsyncClientTest.java | 34 +- .../features/NATClientLiveTest.java | 40 ++- .../VirtualMachineClientLiveTest.java | 1 - 9 files changed, 412 insertions(+), 43 deletions(-) create mode 100644 sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/IPForwardingRule.java diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/IPForwardingRule.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/IPForwardingRule.java new file mode 100644 index 0000000000..dce9660e24 --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/IPForwardingRule.java @@ -0,0 +1,295 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 { + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private long id; + private String IPAddress; + private long IPAddressId; + private int startPort; + private String protocol; + public int endPort; + 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 startPort(int startPort) { + this.startPort = startPort; + return this; + } + + public Builder protocol(String protocol) { + this.protocol = protocol; + return this; + } + + public Builder endPort(int endPort) { + this.endPort = endPort; + 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, startPort, protocol, endPort, state, + virtualMachineDisplayName, virtualMachineId, virtualMachineName); + } + } + + private long id; + @SerializedName("ipaddress") + private String IPAddress; + @SerializedName("ipaddressid") + private long IPAddressId; + @SerializedName("startport") + private int startPort; + private String protocol; + @SerializedName("endport") + public int endPort; + private String state; + @SerializedName("virtualmachinedisplayname") + private String virtualMachineDisplayName; + @SerializedName("virtualmachineid") + public long virtualMachineId; + @SerializedName("virtualmachinename") + private String virtualMachineName; + + // for deserializer + IPForwardingRule() { + + } + + public IPForwardingRule(long id, String iPAddress, long iPAddressId, int startPort, String protocol, int endPort, + String state, String virtualMachineDisplayName, long virtualMachineId, String virtualMachineName) { + this.id = id; + this.IPAddress = iPAddress; + this.IPAddressId = iPAddressId; + this.startPort = startPort; + this.protocol = protocol; + this.endPort = endPort; + 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 ip forwarding rule + */ + public long getId() { + return id; + } + + /** + * + * @return the public ip address for the ip forwarding rule + */ + public String getIPAddress() { + return IPAddress; + } + + /** + * + * @return the public ip address id for the ip forwarding rule + */ + public long getIPAddressId() { + return IPAddressId; + } + + /** + * + * @return the private port for the ip forwarding rule + */ + public int getStartPort() { + return startPort; + } + + /** + * + * @return the protocol of the ip forwarding rule + */ + public String getProtocol() { + return protocol; + } + + /** + * + * @return the public port for the ip forwarding rule + */ + public int getEndPort() { + return endPort; + } + + /** + * + * @return the state of the rule + */ + public String getState() { + return state; + } + + /** + * + * @return the VM display name for the ip forwarding rule + */ + public String getVirtualMachineDisplayName() { + return virtualMachineDisplayName; + } + + /** + * + * @return the VM ID for the ip forwarding rule + */ + public long getVirtualMachineId() { + return virtualMachineId; + } + + /** + * + * @return the VM name for the ip 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 + endPort; + result = prime * result + (int) (id ^ (id >>> 32)); + result = prime * result + ((protocol == null) ? 0 : protocol.hashCode()); + result = prime * result + startPort; + 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 (endPort != other.endPort) + return false; + if (id != other.id) + return false; + if (protocol == null) { + if (other.protocol != null) + return false; + } else if (!protocol.equals(other.protocol)) + return false; + if (startPort != other.startPort) + 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 + ", startPort=" + startPort + + ", protocol=" + protocol + ", endPort=" + endPort + ", state=" + state + + ", virtualMachineDisplayName=" + virtualMachineDisplayName + ", virtualMachineId=" + virtualMachineId + + ", virtualMachineName=" + virtualMachineName + "]"; + } + +} diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATAsyncClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATAsyncClient.java index 9fc49d69bf..35ff47ae10 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATAsyncClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATAsyncClient.java @@ -27,7 +27,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.jclouds.cloudstack.domain.AsyncCreateResponse; -import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions; import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions; @@ -37,7 +37,6 @@ 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; @@ -61,7 +60,7 @@ public interface NATAsyncClient { @Unwrap(depth = 2) @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) - ListenableFuture> listIPForwardingRules(ListIPForwardingRulesOptions... options); + ListenableFuture> listIPForwardingRules(ListIPForwardingRulesOptions... options); /** * @see NATClient#getIPForwardingRule @@ -71,7 +70,7 @@ public interface NATAsyncClient { @Unwrap(depth = 3, edgeCollection = Set.class) @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture getIPForwardingRule(@QueryParam("id") long id); + ListenableFuture getIPForwardingRule(@QueryParam("id") long id); /** * @see NATClient#createIPForwardingRuleForVirtualMachine @@ -86,11 +85,23 @@ public interface NATAsyncClient { CreateIPForwardingRuleOptions... options); /** - * @see NATClient#deleteIpForwardingRule + * @see NATClient#enableStaticNATForVirtualMachine + */ + @GET + @QueryParams(keys = "command", values = "enableStaticNat") + @Unwrap + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture enableStaticNATForVirtualMachine( + @QueryParam("virtualmachineid") long virtualMachineId, @QueryParam("ipaddressid") long IPAddressId); + + /** + * @see NATClient#deleteIPForwardingRule */ @GET @QueryParams(keys = "command", values = "deleteIpForwardingRule") - @ExceptionParser(ReturnVoidOnNotFoundOr404.class) - ListenableFuture deleteIPForwardingRule(@QueryParam("id") long id); + @Unwrap(depth = 2) + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture deleteIPForwardingRule(@QueryParam("id") long id); } diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATClient.java index a7ec7463f0..d077d8ae82 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NATClient.java @@ -23,7 +23,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import org.jclouds.cloudstack.domain.AsyncCreateResponse; -import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions; import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions; import org.jclouds.concurrent.Timeout; @@ -45,7 +45,7 @@ public interface NATClient { * if present, how to constrain the list. * @return IPForwardingRulees matching query, or empty set, if no IPForwardingRulees are found */ - Set listIPForwardingRules(ListIPForwardingRulesOptions... options); + Set listIPForwardingRules(ListIPForwardingRulesOptions... options); /** * get a specific IPForwardingRule by id @@ -54,7 +54,7 @@ public interface NATClient { * IPForwardingRule to get * @return IPForwardingRule or null if not found */ - PortForwardingRule getIPForwardingRule(long id); + IPForwardingRule getIPForwardingRule(long id); /** * Creates an ip forwarding rule @@ -79,5 +79,7 @@ public interface NATClient { * @param id * the id of the forwarding rule */ - void deleteIPForwardingRule(long id); + Long deleteIPForwardingRule(long id); + + AsyncCreateResponse enableStaticNATForVirtualMachine(long virtualMachineId, long IPAddressId); } diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java index 79766b7d9f..07b0a5de35 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java @@ -53,11 +53,6 @@ public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest { assert asyncJob.getAccountId() >= 0 : asyncJob; assert asyncJob.getCmd() != null : asyncJob; assert asyncJob.getCreated() != null : asyncJob; - // seemingly unused fields - assert asyncJob.getInstanceId() == -1 : asyncJob; - assert asyncJob.getInstanceType() == null : asyncJob; - assert asyncJob.getResultType() == null : asyncJob; - // end if (asyncJob.getProgress() > 0) { assert asyncJob.getResult() == null : asyncJob; assert asyncJob.getResultCode() == -1 : asyncJob; diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java index 87927f66cf..23fd280f00 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java @@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features; import static com.google.common.base.Preconditions.checkNotNull; +import static org.testng.Assert.assertEquals; import java.util.Properties; import java.util.concurrent.TimeUnit; @@ -31,17 +32,22 @@ 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.compute.domain.ExecResponse; +import org.jclouds.domain.Credentials; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.net.IPSocket; import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.RetryablePredicate; import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContextFactory; +import org.jclouds.ssh.SshClient; +import org.jclouds.ssh.jsch.config.JschSshClientModule; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; import com.google.inject.Module; /** @@ -62,17 +68,33 @@ public class BaseCloudStackClientLiveTest { protected RetryablePredicate jobComplete; protected RetryablePredicate virtualMachineRunning; protected RetryablePredicate virtualMachineDestroyed; - + protected SshClient.Factory sshFactory; + protected void setupCredentials() { identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider - + ".identity must be set. ex. apiKey"); + + ".identity must be set. ex. apiKey"); credential = checkNotNull(System.getProperty("test." + provider + ".credential"), "test." + provider - + ".credential must be set. ex. secretKey"); + + ".credential must be set. ex. secretKey"); endpoint = checkNotNull(System.getProperty("test." + provider + ".endpoint"), "test." + provider - + ".endpoint must be set. ex. http://localhost:8080/client/api"); + + ".endpoint must be set. ex. http://localhost:8080/client/api"); apiversion = System.getProperty("test." + provider + ".apiversion"); } + protected void checkSSH(String address, int port) { + IPSocket socket = new IPSocket(address, port); + socketTester.apply(socket); + SshClient client = sshFactory.create(socket, new Credentials("root", "password")); + try { + client.connect(); + ExecResponse exec = client.exec("echo hello"); + System.out.println(exec); + assertEquals(exec.getOutput().trim(), "hello"); + } finally { + if (client != null) + client.disconnect(); + } + } + protected Properties setupProperties() { Properties overrides = new Properties(); overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true"); @@ -90,9 +112,10 @@ public class BaseCloudStackClientLiveTest { setupCredentials(); Properties overrides = setupProperties(); context = new RestContextFactory().createContext(provider, ImmutableSet. of(new Log4JLoggingModule()), - overrides); + overrides); client = context.getApi(); + sshFactory = Guice.createInjector(new JschSshClientModule()).getInstance(SshClient.Factory.class); socketTester = new RetryablePredicate(new InetSocketAddressConnect(), 180, 1, TimeUnit.SECONDS); jobComplete = new RetryablePredicate(new JobComplete(client), 600, 5, TimeUnit.SECONDS); virtualMachineRunning = new RetryablePredicate(new VirtualMachineRunning(client), 600, 5, diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientLiveTest.java index 936c8f6d7a..b75fe1a1d8 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientLiveTest.java @@ -29,7 +29,10 @@ import org.jclouds.cloudstack.domain.AsyncCreateResponse; import org.jclouds.cloudstack.domain.PortForwardingRule; import org.jclouds.cloudstack.domain.PublicIPAddress; import org.jclouds.cloudstack.domain.VirtualMachine; +import org.jclouds.compute.domain.ExecResponse; +import org.jclouds.domain.Credentials; import org.jclouds.net.IPSocket; +import org.jclouds.ssh.SshClient; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; @@ -67,6 +70,15 @@ public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest { checkRule(rule); IPSocket socket = new IPSocket(ip.getIPAddress(), 22); socketTester.apply(socket); + SshClient client = sshFactory.create(socket, new Credentials("root", "password")); + try { + client.connect(); + ExecResponse exec = client.exec("echo hello"); + assertEquals(exec.getOutput().trim(), "hello"); + } finally { + if (client != null) + client.disconnect(); + } } @AfterGroups(groups = "live") diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATAsyncClientTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATAsyncClientTest.java index aa8a803b2d..e1222075fc 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATAsyncClientTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATAsyncClientTest.java @@ -25,14 +25,12 @@ 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; @@ -68,8 +66,7 @@ public class NATAsyncClientTest extends BaseCloudStackAsyncClientTest response = client.getNATClient().listIPForwardingRules(); + Set response = client.getNATClient().listIPForwardingRules(); assert null != response; assertTrue(response.size() >= 0); - for (PortForwardingRule rule : response) { - PortForwardingRule newDetails = getOnlyElement(client.getNATClient().listIPForwardingRules( + 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(PortForwardingRule 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.getStartPort() > 0 : rule; assert rule.getProtocol() != null : rule; - assert rule.getPublicPort() > 0 : rule; + assert rule.getEndPort() > 0 : rule; assert rule.getState() != null : rule; - assert rule.getVirtualMachineDisplayName() != null : rule; assert rule.getVirtualMachineId() > 0 : rule; assert rule.getVirtualMachineName() != null : rule; diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineClientLiveTest.java index a56b1d9876..cafd513dc3 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineClientLiveTest.java @@ -117,7 +117,6 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest { public void testCreateDestroyVirtualMachine() throws Exception { vm = createVirtualMachine(client, jobComplete, virtualMachineRunning); - assertEquals(vm.getRootDeviceType(), "NetworkFilesystem"); checkVm(vm); }