diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackAsyncClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackAsyncClient.java index 2aac2dfe06..5cd7ab009c 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackAsyncClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackAsyncClient.java @@ -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.FirewallAsyncClient; import org.jclouds.cloudstack.features.NATAsyncClient; import org.jclouds.cloudstack.features.NetworkAsyncClient; import org.jclouds.cloudstack.features.OfferingAsyncClient; @@ -93,4 +94,10 @@ public interface CloudStackAsyncClient { */ @Delegate NATAsyncClient getNATClient(); + + /** + * Provides asynchronous access to Firewall features. + */ + @Delegate + FirewallAsyncClient getFirewallClient(); } diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackClient.java index b4084bbc64..c2b66dc5dd 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackClient.java @@ -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.FirewallClient; import org.jclouds.cloudstack.features.NATClient; import org.jclouds.cloudstack.features.NetworkClient; import org.jclouds.cloudstack.features.OfferingClient; @@ -96,4 +97,10 @@ public interface CloudStackClient { */ @Delegate NATClient getNATClient(); + + /** + * Provides synchronous access to Firewall features. + */ + @Delegate + FirewallClient getFirewallClient(); } diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java index e1473263fd..5be91a90c9 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java @@ -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.FirewallAsyncClient; +import org.jclouds.cloudstack.features.FirewallClient; import org.jclouds.cloudstack.features.NATAsyncClient; import org.jclouds.cloudstack.features.NATClient; import org.jclouds.cloudstack.features.NetworkAsyncClient; @@ -73,6 +75,7 @@ public class CloudStackRestClientModule extends RestClientModule { +public class PortForwardingRule implements Comparable { public static Builder builder() { return new Builder(); } @@ -92,8 +92,8 @@ public class IPForwardingRule implements Comparable { return this; } - public IPForwardingRule build() { - return new IPForwardingRule(id, IPAddress, IPAddressId, privatePort, protocol, publicPort, state, + public PortForwardingRule build() { + return new PortForwardingRule(id, IPAddress, IPAddressId, privatePort, protocol, publicPort, state, virtualMachineDisplayName, virtualMachineId, virtualMachineName); } } @@ -107,7 +107,7 @@ public class IPForwardingRule implements Comparable { private int privatePort; private String protocol; @SerializedName("publicport") - public int publicPort; + public String publicPort; private String state; @SerializedName("virtualmachinedisplayname") private String virtualMachineDisplayName; @@ -116,7 +116,12 @@ public class IPForwardingRule implements Comparable { @SerializedName("virtualmachinename") private String virtualMachineName; - public IPForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, String protocol, + // for deserializer + PortForwardingRule() { + + } + + public PortForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, String protocol, int publicPort, String state, String virtualMachineDisplayName, long virtualMachineId, String virtualMachineName) { this.id = id; @@ -124,7 +129,7 @@ public class IPForwardingRule implements Comparable { this.IPAddressId = iPAddressId; this.privatePort = privatePort; this.protocol = protocol; - this.publicPort = publicPort; + this.publicPort = publicPort+""; this.state = state; this.virtualMachineDisplayName = virtualMachineDisplayName; this.virtualMachineId = virtualMachineId; @@ -132,7 +137,7 @@ public class IPForwardingRule implements Comparable { } @Override - public int compareTo(IPForwardingRule arg0) { + public int compareTo(PortForwardingRule arg0) { return new Long(id).compareTo(arg0.getId()); } @@ -181,7 +186,7 @@ public class IPForwardingRule implements Comparable { * @return the public port for the port forwarding rule */ public int getPublicPort() { - return publicPort; + return Integer.parseInt(publicPort); } /** @@ -225,7 +230,7 @@ public class IPForwardingRule implements Comparable { 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 + ((publicPort == null) ? 0 : publicPort.hashCode()); result = prime * result + ((state == null) ? 0 : state.hashCode()); result = prime * result + ((virtualMachineDisplayName == null) ? 0 : virtualMachineDisplayName.hashCode()); result = prime * result + (int) (virtualMachineId ^ (virtualMachineId >>> 32)); @@ -241,7 +246,7 @@ public class IPForwardingRule implements Comparable { return false; if (getClass() != obj.getClass()) return false; - IPForwardingRule other = (IPForwardingRule) obj; + PortForwardingRule other = (PortForwardingRule) obj; if (IPAddress == null) { if (other.IPAddress != null) return false; @@ -258,7 +263,10 @@ public class IPForwardingRule implements Comparable { return false; } else if (!protocol.equals(other.protocol)) return false; - if (publicPort != other.publicPort) + if (publicPort == null) { + if (other.publicPort != null) + return false; + } else if (!publicPort.equals(other.publicPort)) return false; if (state == null) { if (other.state != null) diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/FirewallAsyncClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/FirewallAsyncClient.java new file mode 100644 index 0000000000..6d59997fb8 --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/FirewallAsyncClient.java @@ -0,0 +1,84 @@ +/** + * + * 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.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.PortForwardingRule; +import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions; +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.ReturnVoidOnNotFoundOr404; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * Provides asynchronous access to cloudstack via their REST API. + *

+ * + * @see FirewallClient + * @see + * @author Adrian Cole + */ +@RequestFilters(QuerySigner.class) +@QueryParams(keys = "response", values = "json") +public interface FirewallAsyncClient { + + /** + * @see FirewallClient#listPortForwardingRules + */ + @GET + @QueryParams(keys = "command", values = "listPortForwardingRules") + @Unwrap(depth = 2) + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) + ListenableFuture> listPortForwardingRules(ListPortForwardingRulesOptions... options); + + /** + * @see FirewallClient#createPortForwardingRuleForVirtualMachine + */ + @GET + @QueryParams(keys = "command", values = "createPortForwardingRule") + @Unwrap + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture createPortForwardingRuleForVirtualMachine( + @QueryParam("virtualmachineid") long virtualMachineId, @QueryParam("ipaddressid") long IPAddressId, + @QueryParam("protocol") String protocol, @QueryParam("privateport") int privatePort, + @QueryParam("publicport") int publicPort); + + /** + * @see FirewallClient#deletePortForwardingRule + */ + @GET + @QueryParams(keys = "command", values = "deletePortForwardingRule") + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + ListenableFuture deletePortForwardingRule(@QueryParam("id") long id); + +} diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/FirewallClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/FirewallClient.java new file mode 100644 index 0000000000..044319ef1d --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/FirewallClient.java @@ -0,0 +1,76 @@ +/** + * + * 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.features; + +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.options.ListPortForwardingRulesOptions; +import org.jclouds.concurrent.Timeout; + +/** + * Provides synchronous access to CloudStack PortForwardingRule features. + *

+ * + * @see PortForwardingRuleAsyncClient + * @see + * @author Adrian Cole + */ +@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) +public interface FirewallClient { + /** + * List the port forwarding rules + * + * @param options + * if present, how to constrain the list. + * @return PortForwardingRulees matching query, or empty set, if no PortForwardingRulees are + * found + */ + Set listPortForwardingRules(ListPortForwardingRulesOptions... options); + + /** + * Creates an port forwarding rule + * + * @param virtualMachineId + * the ID of the virtual machine for the port forwarding rule + * @param IPAddressId + * the public IP address id of the forwarding rule, already associated via + * associatePort + * @param protocol + * the protocol for the rule. Valid values are TCP or UDP. + * @param privatePort + * the private port of the port forwarding rule + * @param publicPort + * the public port of the port forwarding rule + * @return response used to track creation + */ + AsyncCreateResponse createPortForwardingRuleForVirtualMachine(long virtualMachineId, long IPAddressId, + String protocol, int privatePort, int publicPort); + + /** + * Deletes an port forwarding rule + * + * @param id + * the id of the forwarding rule + */ + void deletePortForwardingRule(long id); +} 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 23ff38f883..9fc49d69bf 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.IPForwardingRule; +import org.jclouds.cloudstack.domain.PortForwardingRule; import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.options.CreateIPForwardingRuleOptions; import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions; @@ -61,7 +61,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 +71,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 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 4892451e8c..a7ec7463f0 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.IPForwardingRule; +import org.jclouds.cloudstack.domain.PortForwardingRule; 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 */ - IPForwardingRule getIPForwardingRule(long id); + PortForwardingRule getIPForwardingRule(long id); /** * Creates an ip forwarding rule diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateIPForwardingRuleOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateIPForwardingRuleOptions.java index 7c56aeba56..6e2eff063f 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateIPForwardingRuleOptions.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateIPForwardingRuleOptions.java @@ -46,7 +46,7 @@ public class CreateIPForwardingRuleOptions extends BaseHttpRequestOptions { public static class Builder { /** - * @see CreateIPForwardingRuleOptions#endPort + * @see CreatePortForwardingRuleOptions#endPort */ public static CreateIPForwardingRuleOptions endPort(int endPort) { CreateIPForwardingRuleOptions options = new CreateIPForwardingRuleOptions(); diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java index 8e4f1e8902..defdc7004e 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/DeployVirtualMachineOptions.java @@ -47,7 +47,7 @@ public class DeployVirtualMachineOptions extends BaseHttpRequestOptions { */ public DeployVirtualMachineOptions accountInDomain(String account, long domain) { this.queryParameters.replaceValues("account", ImmutableSet.of(account)); - this.queryParameters.replaceValues("domain", ImmutableSet.of(domain + "")); + this.queryParameters.replaceValues("domainid", ImmutableSet.of(domain + "")); return this; } diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptions.java index 3ee83da617..b7ef133ec9 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptions.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptions.java @@ -57,9 +57,13 @@ public class ListIPForwardingRulesOptions extends BaseHttpRequestOptions { * @param account * the account associated with the ip forwarding rule. Must be used with the domainId * parameter. + * + * @param domain + * domain id */ - public ListIPForwardingRulesOptions account(String account) { + public ListIPForwardingRulesOptions accountInDomain(String account, long domain) { this.queryParameters.replaceValues("account", ImmutableSet.of(account)); + this.queryParameters.replaceValues("domainid", ImmutableSet.of(domain + "")); return this; } @@ -86,11 +90,11 @@ public class ListIPForwardingRulesOptions extends BaseHttpRequestOptions { public static class Builder { /** - * @see ListIPForwardingRulesOptions#account + * @see ListIPForwardingRulesOptions#accountInDomain */ - public static ListIPForwardingRulesOptions account(String account) { + public static ListIPForwardingRulesOptions accountInDomain(String account, long domain) { ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions(); - return options.account(account); + return options.accountInDomain(account, domain); } /** diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListPortForwardingRulesOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListPortForwardingRulesOptions.java new file mode 100644 index 0000000000..7bd6a1b11c --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListPortForwardingRulesOptions.java @@ -0,0 +1,97 @@ +/** + * + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +import com.google.common.collect.ImmutableSet; + +/** + * Options used to control what port forwarding rules are returned + * + * @see + * @author Adrian Cole + */ +public class ListPortForwardingRulesOptions extends BaseHttpRequestOptions { + + public static final ListPortForwardingRulesOptions NONE = new ListPortForwardingRulesOptions(); + + /** + * @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 ListPortForwardingRulesOptions domainId(long domainId) { + this.queryParameters.replaceValues("domainid", ImmutableSet.of(domainId + "")); + return this; + + } + + /** + * @param account + * the account associated with the port forwarding rule. Must be used with the domainId + * parameter. + * + * @param domain + * domain id + */ + public ListPortForwardingRulesOptions accountInDomain(String account, long domain) { + this.queryParameters.replaceValues("account", ImmutableSet.of(account)); + this.queryParameters.replaceValues("domainid", ImmutableSet.of(domain + "")); + return this; + } + + /** + * @param IPAddressId + * list the rule belonging to this public ip address + */ + public ListPortForwardingRulesOptions IPAddressId(long IPAddressId) { + this.queryParameters.replaceValues("ipaddressid", ImmutableSet.of(IPAddressId + "")); + return this; + + } + + public static class Builder { + /** + * @see ListPortForwardingRulesOptions#accountInDomain + */ + public static ListPortForwardingRulesOptions accountInDomain(String account, long domain) { + ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions(); + return options.accountInDomain(account, domain); + } + + /** + * @see ListPortForwardingRulesOptions#IPAddressId + */ + public static ListPortForwardingRulesOptions IPAddressId(long IPAddressId) { + ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions(); + return options.IPAddressId(IPAddressId); + } + + /** + * @see ListPortForwardingRulesOptions#domainId + */ + public static ListPortForwardingRulesOptions domainId(long id) { + ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions(); + return options.domainId(id); + } + + } +} diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallAsyncClientTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallAsyncClientTest.java new file mode 100644 index 0000000000..f0629d26f4 --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallAsyncClientTest.java @@ -0,0 +1,124 @@ +/** + * + * 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.features; + +import java.io.IOException; +import java.lang.reflect.Method; + +import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions; +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.rest.functions.MapHttp4xxCodesToExceptions; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +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 FirewallAsyncClient} + * + * @author Adrian Cole + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire +@Test(groups = "unit", testName = "FirewallAsyncClientTest") +public class FirewallAsyncClientTest extends BaseCloudStackAsyncClientTest { + public void testListPortForwardingRules() throws SecurityException, NoSuchMethodException, IOException { + Method method = FirewallAsyncClient.class.getMethod("listPortForwardingRules", + ListPortForwardingRulesOptions[].class); + HttpRequest httpRequest = processor.createRequest(method); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listPortForwardingRules 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 testListPortForwardingRulesOptions() throws SecurityException, NoSuchMethodException, IOException { + Method method = FirewallAsyncClient.class.getMethod("listPortForwardingRules", + ListPortForwardingRulesOptions[].class); + HttpRequest httpRequest = processor.createRequest(method, ListPortForwardingRulesOptions.Builder.IPAddressId(3)); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listPortForwardingRules&ipaddressid=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 testCreatePortForwardingRuleForVirtualMachine() throws SecurityException, NoSuchMethodException, + IOException { + Method method = FirewallAsyncClient.class.getMethod("createPortForwardingRuleForVirtualMachine", long.class, + long.class, String.class, int.class, int.class); + HttpRequest httpRequest = processor.createRequest(method, 6, 7, "tcp", 22, 22); + + assertRequestLineEquals( + httpRequest, + "GET http://localhost:8080/client/api?response=json&command=createPortForwardingRule&virtualmachineid=6&protocol=tcp&ipaddressid=7&privateport=22&publicport=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 testDeletePortForwardingRule() throws SecurityException, NoSuchMethodException, IOException { + Method method = FirewallAsyncClient.class.getMethod("deletePortForwardingRule", long.class); + HttpRequest httpRequest = processor.createRequest(method, 5); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=deletePortForwardingRule&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> createTypeLiteral() { + return new TypeLiteral>() { + }; + } +} 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 new file mode 100644 index 0000000000..936c8f6d7a --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientLiveTest.java @@ -0,0 +1,120 @@ +/** + * + * 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 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.find; +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.PortForwardingRule; +import org.jclouds.cloudstack.domain.PublicIPAddress; +import org.jclouds.cloudstack.domain.VirtualMachine; +import org.jclouds.net.IPSocket; +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 FirewallClientLiveTest} + * + * @author Adrian Cole + */ +@Test(groups = "live", sequential = true, testName = "FirewallClientLiveTest") +public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest { + private PublicIPAddress ip = null; + private VirtualMachine vm; + private PortForwardingRule rule; + + @BeforeGroups(groups = "live") + public void setupClient() { + super.setupClient(); + prefix += "rule"; + ip = AddressClientLiveTest.createPublicIPAddress(client, jobComplete); + vm = VirtualMachineClientLiveTest.createVirtualMachine(client, jobComplete, virtualMachineRunning); + } + + public void testCreatePortForwardingRule() throws Exception { + AsyncCreateResponse job = client.getFirewallClient().createPortForwardingRuleForVirtualMachine(vm.getId(), + ip.getId(), "tcp", 22, 22); + assert jobComplete.apply(job.getJobId()); + rule = findRuleWithId(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.getFirewallClient().deletePortForwardingRule(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 testListPortForwardingRules() throws Exception { + Set response = client.getFirewallClient().listPortForwardingRules(); + assert null != response; + assertTrue(response.size() >= 0); + for (final PortForwardingRule rule : response) { + PortForwardingRule newDetails = findRuleWithId(rule.getId()); + assertEquals(rule.getId(), newDetails.getId()); + checkRule(rule); + } + } + + private PortForwardingRule findRuleWithId(final long id) { + return find(client.getFirewallClient().listPortForwardingRules(), new Predicate() { + + @Override + public boolean apply(PortForwardingRule arg0) { + return arg0.getId() == id; + } + + }); + } + + protected void checkRule(PortForwardingRule rule) { + assertEquals(rule.getId(), findRuleWithId(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.getVirtualMachineId() > 0 : rule; + assert rule.getVirtualMachineName() != null : rule; + } +} diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATClientLiveTest.java index a0520d26eb..3780efc9cc 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NATClientLiveTest.java @@ -26,7 +26,7 @@ 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.PortForwardingRule; import org.jclouds.cloudstack.domain.PublicIPAddress; import org.jclouds.cloudstack.domain.VirtualMachine; import org.jclouds.cloudstack.options.ListIPForwardingRulesOptions; @@ -44,7 +44,7 @@ import org.testng.annotations.Test; public class NATClientLiveTest extends BaseCloudStackClientLiveTest { private PublicIPAddress ip = null; private VirtualMachine vm; - private IPForwardingRule rule; + private PortForwardingRule rule; @BeforeGroups(groups = "live") public void setupClient() { @@ -84,18 +84,18 @@ public class NATClientLiveTest extends BaseCloudStackClientLiveTest { } public void testListIPForwardingRules() throws Exception { - Set response = client.getNATClient().listIPForwardingRules(); + Set response = client.getNATClient().listIPForwardingRules(); assert null != response; assertTrue(response.size() >= 0); - for (IPForwardingRule rule : response) { - IPForwardingRule newDetails = getOnlyElement(client.getNATClient().listIPForwardingRules( + for (PortForwardingRule rule : response) { + PortForwardingRule newDetails = getOnlyElement(client.getNATClient().listIPForwardingRules( ListIPForwardingRulesOptions.Builder.id(rule.getId()))); assertEquals(rule.getId(), newDetails.getId()); checkRule(rule); } } - protected void checkRule(IPForwardingRule rule) { + protected void checkRule(PortForwardingRule rule) { assertEquals(rule.getId(), client.getNATClient().getIPForwardingRule(rule.getId()).getId()); assert rule.getId() > 0 : rule; assert rule.getIPAddress() != null : rule; diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ListPortForwardingRuleResponseTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ListPortForwardingRuleResponseTest.java new file mode 100644 index 0000000000..72b6179568 --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ListPortForwardingRuleResponseTest.java @@ -0,0 +1,75 @@ +/** + * + * 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.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.Set; + +import org.jclouds.cloudstack.domain.PortForwardingRule; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue; +import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Sets; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit") +public class ListPortForwardingRuleResponseTest { + + Injector i = Guice.createInjector(new GsonModule() { + + @Override + protected void configure() { + bind(DateAdapter.class).to(Iso8601DateAdapter.class); + super.configure(); + } + + }); + + public void test() { + InputStream is = getClass().getResourceAsStream("/listportforwardingrulesresponse.json"); + + Set expects = ImmutableSortedSet. of(PortForwardingRule.builder().id(15) + .privatePort(22).protocol("tcp").publicPort(2022).virtualMachineId(3).virtualMachineName("i-3-3-VM") + .IPAddressId(3).IPAddress("72.52.126.32").state("Active").build(), PortForwardingRule.builder().id(18) + .privatePort(22).protocol("tcp").publicPort(22).virtualMachineId(89).virtualMachineName("i-3-89-VM") + .IPAddressId(34).IPAddress("72.52.126.63").state("Active").build()); + + UnwrapOnlyNestedJsonValue> parser = i.getInstance(Key + .get(new TypeLiteral>>() { + })); + Set response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); + + assertEquals(Sets.newTreeSet(response), expects); + } + +} diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptionsTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptionsTest.java index 76e37a2b43..540c319a5d 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptionsTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListIPForwardingRulesOptionsTest.java @@ -19,7 +19,7 @@ package org.jclouds.cloudstack.options; -import static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.account; +import static org.jclouds.cloudstack.options.ListIPForwardingRulesOptions.Builder.accountInDomain; 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; @@ -49,13 +49,15 @@ public class ListIPForwardingRulesOptionsTest { } public void testAccount() { - ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().account("account"); + ListIPForwardingRulesOptions options = new ListIPForwardingRulesOptions().accountInDomain("account", 6); assertEquals(ImmutableList.of("account"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid")); } public void testAccountStatic() { - ListIPForwardingRulesOptions options = account("account"); + ListIPForwardingRulesOptions options = accountInDomain("account", 6); assertEquals(ImmutableList.of("account"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid")); } public void testName() { diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListPortForwardingRulesOptionsTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListPortForwardingRulesOptionsTest.java new file mode 100644 index 0000000000..d1dab4c9ad --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListPortForwardingRulesOptionsTest.java @@ -0,0 +1,71 @@ +/** + * + * 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.options; + +import static org.jclouds.cloudstack.options.ListPortForwardingRulesOptions.Builder.IPAddressId; +import static org.jclouds.cloudstack.options.ListPortForwardingRulesOptions.Builder.accountInDomain; +import static org.jclouds.cloudstack.options.ListPortForwardingRulesOptions.Builder.domainId; +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +/** + * Tests behavior of {@code ListPortForwardingRulesOptions} + * + * @author Adrian Cole + */ +@Test(groups = "unit") +public class ListPortForwardingRulesOptionsTest { + + public void testAccount() { + ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions().accountInDomain("account", 6); + assertEquals(ImmutableList.of("account"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testAccountStatic() { + ListPortForwardingRulesOptions options = accountInDomain("account", 6); + assertEquals(ImmutableList.of("account"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testName() { + ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions().IPAddressId(9); + assertEquals(ImmutableList.of("9"), options.buildQueryParameters().get("ipaddressid")); + } + + public void testNameStatic() { + ListPortForwardingRulesOptions options = IPAddressId(9); + assertEquals(ImmutableList.of("9"), options.buildQueryParameters().get("ipaddressid")); + } + + public void testDomainId() { + ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions().domainId(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testDomainIdStatic() { + ListPortForwardingRulesOptions options = domainId(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("domainid")); + } + +} diff --git a/sandbox-apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json b/sandbox-apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json new file mode 100644 index 0000000000..54d5a076df --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json @@ -0,0 +1 @@ +{ "listportforwardingrulesresponse" : { "portforwardingrule" : [ {"id":18,"privateport":"22","protocol":"tcp","publicport":"22","virtualmachineid":89,"virtualmachinename":"i-3-89-VM","ipaddressid":34,"ipaddress":"72.52.126.63","state":"Active"}, {"id":15,"privateport":"22","protocol":"tcp","publicport":"2022","virtualmachineid":3,"virtualmachinename":"i-3-3-VM","ipaddressid":3,"ipaddress":"72.52.126.32","state":"Active"} ] } } \ No newline at end of file