Complete the Firewall Client API

This commit is contained in:
andreisavu 2012-01-09 18:33:45 +02:00
parent 56aa4e5ece
commit 3ff038bf31
9 changed files with 752 additions and 66 deletions

View File

@ -0,0 +1,260 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.domain;
import com.google.gson.annotations.SerializedName;
/**
* @author Andrei Savu
*/
public class FirewallRule implements Comparable<FirewallRule> {
public static enum Protocol {
TCP,
UDP,
ICMP,
UNKNOWN;
public static Protocol fromValue(String value) {
try {
return valueOf(value.toUpperCase());
} catch(IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return name().toUpperCase();
}
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id;
private String CIDRs;
private int startPort;
private int endPort;
private String icmpCode;
private String icmpType;
private String ipAddress;
private String ipAddressId;
private Protocol protocol;
private String state;
public Builder id(long id) {
this.id = id;
return this;
}
public Builder CIDRs(String CIDRs) {
this.CIDRs = CIDRs;
return this;
}
public Builder startPort(int startPort) {
this.startPort = startPort;
return this;
}
public Builder endPort(int endPort) {
this.endPort = endPort;
return this;
}
public Builder icmpCode(String icmpCode) {
this.icmpCode = icmpCode;
return this;
}
public Builder icmpType(String icmpType) {
this.icmpType = icmpType;
return this;
}
public Builder ipAddress(String ipAddress) {
this.ipAddress = ipAddress;
return this;
}
public Builder ipAddressId(String ipAddressId) {
this.ipAddressId = ipAddressId;
return this;
}
public Builder protocol(Protocol protocol) {
this.protocol = protocol;
return this;
}
public Builder state(String state) {
this.state = state;
return this;
}
public FirewallRule build() {
return new FirewallRule(id, CIDRs, startPort, endPort, icmpCode,
icmpType, ipAddress, ipAddressId, protocol, state);
}
}
private long id;
@SerializedName("cidrlist")
private String CIDRs;
@SerializedName("startport")
private int startPort;
@SerializedName("endport")
private int endPort;
@SerializedName("icmpcode")
private String icmpCode;
@SerializedName("icmptype")
private String icmpType;
@SerializedName("ipaddress")
private String ipAddress;
@SerializedName("ipaddressid")
private String ipAddressId;
private Protocol protocol;
private String state;
public FirewallRule(long id, String CIDRs, int startPort, int endPort,
String icmpCode, String icmpType, String ipAddress, String ipAddressId,
Protocol protocol, String state) {
this.id = id;
this.CIDRs = CIDRs;
this.startPort = startPort;
this.endPort = endPort;
this.icmpCode = icmpCode;
this.icmpType = icmpType;
this.ipAddress = ipAddress;
this.ipAddressId = ipAddressId;
this.protocol = protocol;
this.state = state;
}
@Override
public int compareTo(FirewallRule arg0) {
return new Long(id).compareTo(arg0.getId());
}
public long getId() {
return id;
}
public String getCIDRs() {
return CIDRs;
}
public int getStartPort() {
return startPort;
}
public int getEndPort() {
return endPort;
}
public String getIcmpCode() {
return icmpCode;
}
public String getIcmpType() {
return icmpType;
}
public String getIpAddress() {
return ipAddress;
}
public String getIpAddressId() {
return ipAddressId;
}
public Protocol getProtocol() {
return protocol;
}
public String getState() {
return state;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FirewallRule that = (FirewallRule) o;
if (endPort != that.endPort) return false;
if (id != that.id) return false;
if (startPort != that.startPort) return false;
if (CIDRs != null ? !CIDRs.equals(that.CIDRs) : that.CIDRs != null)
return false;
if (icmpCode != null ? !icmpCode.equals(that.icmpCode) : that.icmpCode != null)
return false;
if (icmpType != null ? !icmpType.equals(that.icmpType) : that.icmpType != null)
return false;
if (ipAddress != null ? !ipAddress.equals(that.ipAddress) : that.ipAddress != null)
return false;
if (ipAddressId != null ? !ipAddressId.equals(that.ipAddressId) : that.ipAddressId != null)
return false;
if (protocol != null ? !protocol.equals(that.protocol) : that.protocol != null)
return false;
if (state != null ? !state.equals(that.state) : that.state != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (CIDRs != null ? CIDRs.hashCode() : 0);
result = 31 * result + startPort;
result = 31 * result + endPort;
result = 31 * result + (icmpCode != null ? icmpCode.hashCode() : 0);
result = 31 * result + (icmpType != null ? icmpType.hashCode() : 0);
result = 31 * result + (ipAddress != null ? ipAddress.hashCode() : 0);
result = 31 * result + (ipAddressId != null ? ipAddressId.hashCode() : 0);
result = 31 * result + (protocol != null ? protocol.hashCode() : 0);
result = 31 * result + (state != null ? state.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "FirewallRule{" +
"id=" + id +
", CIDRs='" + CIDRs + '\'' +
", startPort=" + startPort +
", endPort=" + endPort +
", icmpCode='" + icmpCode + '\'' +
", icmpType='" + icmpType + '\'' +
", ipAddress='" + ipAddress + '\'' +
", ipAddressId='" + ipAddressId + '\'' +
", protocol='" + protocol + '\'' +
", state='" + state + '\'' +
'}';
}
}

View File

@ -26,15 +26,20 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.CreateFirewallRuleOptions;
import org.jclouds.cloudstack.options.ListFirewallRulesOptions;
import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.OnlyElement;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
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;
@ -42,15 +47,54 @@ import com.google.common.util.concurrent.ListenableFuture;
/**
* Provides asynchronous access to cloudstack via their REST API.
* <p/>
*
*
* @author Adrian Cole
* @see FirewallClient
* @see <a href="http://download.cloud.com/releases/2.2.0/api_2.2.12/TOC_User.html" />
* @author Adrian Cole
*/
@RequestFilters(QuerySigner.class)
@QueryParams(keys = "response", values = "json")
public interface FirewallAsyncClient {
/**
* @see FirewallClient#listFirewallRules
*/
@GET
@QueryParams(keys = "command", values = "listFirewallRules")
@SelectJson("firewallrule")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<FirewallRule>> listFirewallRules(ListFirewallRulesOptions... options);
/**
* @see FirewallClient#getFirewallRule
*/
@GET
@QueryParams(keys = "command", values = "listFirewallRules")
@SelectJson("firewallrule")
@OnlyElement
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<FirewallRule> getFirewallRule(@QueryParam("id") long id);
/**
* @see FirewallClient#createFirewallRule
*/
@GET
@QueryParams(keys = "command", values = "createFirewallRule")
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<AsyncCreateResponse> createFirewallRule(@QueryParam("ipaddressid") long ipAddressId,
@QueryParam("protocol") FirewallRule.Protocol protocol, CreateFirewallRuleOptions... options);
/**
* @see FirewallClient#deleteFirewallRule
*/
@GET
@QueryParams(keys = "command", values = "deleteFirewallRule")
@ExceptionParser(ReturnVoidOnNotFoundOr404.class)
ListenableFuture<Void> deleteFirewallRule(@QueryParam("id") long id);
/**
* @see FirewallClient#listPortForwardingRules
*/
@ -61,6 +105,17 @@ public interface FirewallAsyncClient {
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<PortForwardingRule>> listPortForwardingRules(ListPortForwardingRulesOptions... options);
/**
* @see FirewallClient#getPortForwardingRule
*/
@GET
@QueryParams(keys = "command", values = "listPortForwardingRules")
@SelectJson("portforwardingrule")
@OnlyElement
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<PortForwardingRule> getPortForwardingRule(@QueryParam("id") long id);
/**
* @see FirewallClient#createPortForwardingRuleForVirtualMachine
*/
@ -69,9 +124,9 @@ public interface FirewallAsyncClient {
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<AsyncCreateResponse> createPortForwardingRuleForVirtualMachine(
@QueryParam("virtualmachineid") long virtualMachineId, @QueryParam("ipaddressid") long IPAddressId,
@QueryParam("protocol") String protocol, @QueryParam("privateport") int privatePort,
@QueryParam("publicport") int publicPort);
@QueryParam("virtualmachineid") long virtualMachineId, @QueryParam("ipaddressid") long IPAddressId,
@QueryParam("protocol") String protocol, @QueryParam("privateport") int privatePort,
@QueryParam("publicport") int publicPort);
/**
* @see FirewallClient#deletePortForwardingRule

View File

@ -22,7 +22,10 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.options.CreateFirewallRuleOptions;
import org.jclouds.cloudstack.options.ListFirewallRulesOptions;
import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions;
import org.jclouds.concurrent.Timeout;
@ -36,6 +39,50 @@ import org.jclouds.concurrent.Timeout;
*/
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface FirewallClient {
/**
* List the firewall rules
*
* @param options
* if present, how to constrain the list.
* @return
* set of firewall rules or empty set if no rules are found
*/
Set<FirewallRule> listFirewallRules(ListFirewallRulesOptions... options);
/**
* Get a firewall rule by ID
*
* @param id
* the ID of the firewall rule
* @return
* firewall rule instance or null
*/
FirewallRule getFirewallRule(long id);
/**
* Create new firewall rule for a specific IP address
*
* @param ipAddressId
* the IP address id of the port forwarding rule
* @param protocol
* the protocol for the firewall rule. Valid values are TCP/UDP/ICMP
* @param options
* optional arguments for firewall rule creation
* @return
*/
AsyncCreateResponse createFirewallRule(long ipAddressId, FirewallRule.Protocol protocol,
CreateFirewallRuleOptions... options);
/**
* Deletes a firewall rule
*
* @param id
* the ID of the firewall rule
*/
Void deleteFirewallRule(long id);
/**
* List the port forwarding rules
*
@ -46,6 +93,16 @@ public interface FirewallClient {
*/
Set<PortForwardingRule> listPortForwardingRules(ListPortForwardingRulesOptions... options);
/**
* Get a port forwarding rule by ID
*
* @param id
* port forwarding rule ID
* @return
* rule instance or null
*/
PortForwardingRule getPortForwardingRule(long id);
/**
* Creates an port forwarding rule
*
@ -71,5 +128,5 @@ public interface FirewallClient {
* @param id
* the id of the forwarding rule
*/
void deletePortForwardingRule(long id);
Void deletePortForwardingRule(long id);
}

View File

@ -0,0 +1,126 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.options;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import org.jclouds.http.options.BaseHttpRequestOptions;
import java.util.Set;
/**
* Options used to control how a firewall rule is created
*
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/global_admin/createFirewallRule.html"
* />
* @author Andrei Savu
*/
public class CreateFirewallRuleOptions extends BaseHttpRequestOptions {
public static final CreateFirewallRuleOptions NONE = new CreateFirewallRuleOptions();
/**
* @param CIDRs
* the list of CIDRs to forward traffic from
*/
public CreateFirewallRuleOptions CIDRs(Set<String> CIDRs) {
this.queryParameters.replaceValues("id", ImmutableSet.of(Joiner.on(",").join(CIDRs)));
return this;
}
/**
* @param startPort
* the starting port of firewall rule
*/
public CreateFirewallRuleOptions startPort(int startPort) {
this.queryParameters.replaceValues("startport", ImmutableSet.of(startPort + ""));
return this;
}
/**
* @param endPort
* the ending port of firewall rule
*/
public CreateFirewallRuleOptions endPort(int endPort) {
this.queryParameters.replaceValues("endport", ImmutableSet.of(endPort + ""));
return this;
}
/**
* @param icmpCode
* error code for this icmp message
*/
public CreateFirewallRuleOptions icmpCode(String icmpCode) {
this.queryParameters.replaceValues("icmpcode", ImmutableSet.of(icmpCode));
return this;
}
/**
* @param icmpType
* type of the icmp message being sent
*/
public CreateFirewallRuleOptions icmpType(String icmpType) {
this.queryParameters.replaceValues("icmptype", ImmutableSet.of(icmpType));
return this;
}
public static class Builder {
/**
* @see CreateFirewallRuleOptions#CIDRs
*/
public static CreateFirewallRuleOptions CIDRs(Set<String> CIDRs) {
CreateFirewallRuleOptions options = new CreateFirewallRuleOptions();
return options.CIDRs(CIDRs);
}
/**
* @see CreateFirewallRuleOptions#startPort
*/
public static CreateFirewallRuleOptions startPort(int startPort) {
CreateFirewallRuleOptions options = new CreateFirewallRuleOptions();
return options.startPort(startPort);
}
/**
* @see CreateFirewallRuleOptions#endPort
*/
public static CreateFirewallRuleOptions endPort(int endPort) {
CreateFirewallRuleOptions options = new CreateFirewallRuleOptions();
return options.endPort(endPort);
}
/**
* @see CreateFirewallRuleOptions#icmpCode
*/
public static CreateFirewallRuleOptions icmpCode(String icmpCode) {
CreateFirewallRuleOptions options = new CreateFirewallRuleOptions();
return options.icmpCode(icmpCode);
}
/**
* @see CreateFirewallRuleOptions#icmpType
*/
public static CreateFirewallRuleOptions icmpType(String icmpType) {
CreateFirewallRuleOptions options = new CreateFirewallRuleOptions();
return options.icmpType(icmpType);
}
}
}

View File

@ -0,0 +1,147 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.options;
import com.google.common.collect.ImmutableSet;
/**
* Options used to control what firewall rules are returned
*
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/global_admin/listFirewallRules.html"
* />
* @author Andrei Savu
*/
public class ListFirewallRulesOptions extends AccountInDomainOptions {
public static final ListFirewallRulesOptions NONE = new ListFirewallRulesOptions();
/**
* @param id
* firewall rule ID
*/
public ListFirewallRulesOptions id(long id) {
this.queryParameters.replaceValues("id", ImmutableSet.of(id + ""));
return this;
}
/**
* @param ipAddressId
* the id of IP address of the firwall services
*/
public ListFirewallRulesOptions ipAddressId(long ipAddressId) {
this.queryParameters.replaceValues("ipaddressid", ImmutableSet.of(ipAddressId + ""));
return this;
}
/**
* @param keyword
* list by keyword
*/
public ListFirewallRulesOptions keyword(String keyword) {
this.queryParameters.replaceValues("keyword", ImmutableSet.of(keyword));
return this;
}
public ListFirewallRulesOptions page(long page) {
this.queryParameters.replaceValues("page", ImmutableSet.of(page + ""));
return this;
}
public ListFirewallRulesOptions pageSize(long pageSize) {
this.queryParameters.replaceValues("pagesize", ImmutableSet.of(pageSize + ""));
return this;
}
public static class Builder {
/**
* @see ListFirewallRulesOptions#id
*/
public static ListFirewallRulesOptions id(long id) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.id(id);
}
/**
* @see ListFirewallRulesOptions#ipAddressId
*/
public static ListFirewallRulesOptions ipAddressId(long ipAddressId) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.ipAddressId(ipAddressId);
}
/**
* @see ListFirewallRulesOptions#keyword
*/
public static ListFirewallRulesOptions keyword(String keyword) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.keyword(keyword);
}
/**
* @see ListFirewallRulesOptions#page
*/
public static ListFirewallRulesOptions page(long page) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.page(page);
}
/**
* @see ListFirewallRulesOptions#pageSize
*/
public static ListFirewallRulesOptions pageSize(long pageSize) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.pageSize(pageSize);
}
/**
* @see ListFirewallRulesOptions#accountInDomain
*/
public static ListFirewallRulesOptions accountInDomain(String account, long domain) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.accountInDomain(account, domain);
}
/**
* @see ListFirewallRulesOptions#domainId
*/
public static ListFirewallRulesOptions domainId(long id) {
ListFirewallRulesOptions options = new ListFirewallRulesOptions();
return options.domainId(id);
}
}
/**
* {@inheritDoc}
*/
@Override
public ListFirewallRulesOptions accountInDomain(String account, long domain) {
return ListFirewallRulesOptions.class.cast(super.accountInDomain(account, domain));
}
/**
* {@inheritDoc}
*/
@Override
public ListFirewallRulesOptions domainId(long domainId) {
return ListFirewallRulesOptions.class.cast(super.domainId(domainId));
}
}

View File

@ -22,27 +22,53 @@ import com.google.common.collect.ImmutableSet;
/**
* Options used to control what port forwarding rules are returned
*
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api/user/listIpForwardingRules.html"
* />
*
* @author Adrian Cole
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/global_admin/listPortForwardingRules.html"
* />
*/
public class ListPortForwardingRulesOptions extends AccountInDomainOptions {
public static final ListPortForwardingRulesOptions NONE = new ListPortForwardingRulesOptions();
/**
* @param IPAddressId
* list the rule belonging to this public ip address
* @param id
* lists rule with the specified ID
*/
public ListPortForwardingRulesOptions IPAddressId(long IPAddressId) {
public ListPortForwardingRulesOptions id(long id) {
this.queryParameters.replaceValues("id", ImmutableSet.of(id + ""));
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#id
*/
public static ListPortForwardingRulesOptions id(long id) {
ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions();
return options.id(id);
}
/**
* @see ListPortForwardingRulesOptions#ipAddressId
*/
public static ListPortForwardingRulesOptions ipAddressId(long ipAddressId) {
ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions();
return options.ipAddressId(ipAddressId);
}
/**
* @see ListPortForwardingRulesOptions#accountInDomain
*/
@ -51,14 +77,6 @@ public class ListPortForwardingRulesOptions extends AccountInDomainOptions {
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
*/
@ -66,7 +84,6 @@ public class ListPortForwardingRulesOptions extends AccountInDomainOptions {
ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions();
return options.domainId(id);
}
}
/**

View File

@ -64,7 +64,7 @@ public class FirewallAsyncClientTest extends BaseCloudStackAsyncClientTest<Firew
public void testListPortForwardingRulesOptions() throws SecurityException, NoSuchMethodException, IOException {
Method method = FirewallAsyncClient.class.getMethod("listPortForwardingRules",
ListPortForwardingRulesOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, ListPortForwardingRulesOptions.Builder.IPAddressId(3));
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");

View File

@ -19,18 +19,20 @@
package org.jclouds.cloudstack.features;
import static com.google.common.collect.Iterables.find;
import static org.jclouds.cloudstack.predicates.NetworkPredicates.supportsPortForwarding;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.NoSuchElementException;
import java.util.Set;
import com.google.common.base.Predicates;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.Network;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.domain.PublicIPAddress;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.predicates.NetworkPredicates;
import org.jclouds.net.IPSocket;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
@ -38,16 +40,21 @@ import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import javax.annotation.Nullable;
/**
* Tests behavior of {@code FirewallClientLiveTest}
*
*
* @author Adrian Cole
*/
@Test(groups = "live", singleThreaded = true, testName = "FirewallClientLiveTest")
public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest {
private PublicIPAddress ip = null;
private VirtualMachine vm;
private PortForwardingRule rule;
private FirewallRule firewallRule;
private PortForwardingRule portForwardingRule;
private Network network;
private boolean networksDisabled;
@ -56,13 +63,25 @@ public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest {
super.setupClient();
prefix += "rule";
try {
network = find(client.getNetworkClient().listNetworks(), NetworkPredicates.supportsPortForwarding());
network = find(client.getNetworkClient().listNetworks(), Predicates.and(supportsPortForwarding(),
new Predicate<Network>() {
@Override
public boolean apply(@Nullable Network network) {
return network.isDefault()
&& !network.isSecurityGroupEnabled()
&& network.getAccount().equals(user.getAccount());
}
}));
Long defaultTemplate = (imageId != null && !"".equals(imageId)) ? new Long(imageId) : null;
vm = VirtualMachineClientLiveTest.createVirtualMachineInNetwork(network,
defaultTemplateOrPreferredInZone(defaultTemplate, client, network.getZoneId()), client, jobComplete,
virtualMachineRunning);
defaultTemplateOrPreferredInZone(defaultTemplate, client, network.getZoneId()),
client, jobComplete, virtualMachineRunning);
if (vm.getPassword() != null && !loginCredentials.hasPasswordOption())
loginCredentials = loginCredentials.toBuilder().password(vm.getPassword()).build();
} catch (NoSuchElementException e) {
networksDisabled = true;
}
@ -71,30 +90,57 @@ public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest {
public void testCreatePortForwardingRule() throws Exception {
if (networksDisabled)
return;
while (rule == null) {
while (portForwardingRule == null) {
ip = reuseOrAssociate.apply(network);
try {
AsyncCreateResponse job = client.getFirewallClient().createPortForwardingRuleForVirtualMachine(vm.getId(),
ip.getId(), "tcp", 22, 22);
ip.getId(), "tcp", 22, 22);
assertTrue(jobComplete.apply(job.getJobId()));
rule = findRuleWithId(job.getId());
portForwardingRule = client.getFirewallClient().getPortForwardingRule(job.getId());
} catch (IllegalStateException e) {
// very likely an ip conflict, so retry;
}
}
assertEquals(rule.getIPAddressId(), ip.getId());
assertEquals(rule.getVirtualMachineId(), vm.getId());
assertEquals(rule.getPublicPort(), 22);
assertEquals(rule.getProtocol(), "tcp");
checkRule(rule);
assertEquals(portForwardingRule.getIPAddressId(), ip.getId());
assertEquals(portForwardingRule.getVirtualMachineId(), vm.getId());
assertEquals(portForwardingRule.getPublicPort(), 22);
assertEquals(portForwardingRule.getProtocol(), "tcp");
checkPortForwardingRule(portForwardingRule);
checkSSH(new IPSocket(ip.getIPAddress(), 22));
}
@Test(dependsOnMethods = "testCreatePortForwardingRule")
public void testListPortForwardingRules() throws Exception {
Set<PortForwardingRule> response = client.getFirewallClient().listPortForwardingRules();
assert null != response;
assertTrue(response.size() >= 0);
for (final PortForwardingRule rule : response) {
PortForwardingRule newDetails = client.getFirewallClient().getPortForwardingRule(rule.getId());
assertEquals(rule.getId(), newDetails.getId());
checkPortForwardingRule(rule);
}
}
@Test(dependsOnMethods = "testCreatePortForwardingRule")
public void testCreateFirewallRule() {
}
@Test(dependsOnMethods = "testCreateFirewallRule")
public void testListFirewallRules() {
Set<FirewallRule> rules = client.getFirewallClient().listFirewallRules();
assert rules != null;
assertTrue(rules.size() >= 0);
// TODO: check each rule
}
@AfterGroups(groups = "live")
protected void tearDown() {
if (rule != null) {
client.getFirewallClient().deletePortForwardingRule(rule.getId());
if (portForwardingRule != null) {
client.getFirewallClient().deletePortForwardingRule(portForwardingRule.getId());
}
if (vm != null) {
jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId()));
@ -105,30 +151,8 @@ public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest {
super.tearDown();
}
public void testListPortForwardingRules() throws Exception {
Set<PortForwardingRule> 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<PortForwardingRule>() {
@Override
public boolean apply(PortForwardingRule arg0) {
return arg0.getId() == id;
}
});
}
protected void checkRule(PortForwardingRule rule) {
assertEquals(rule.getId(), findRuleWithId(rule.getId()).getId());
protected void checkPortForwardingRule(PortForwardingRule rule) {
assertEquals(rule.getId(), client.getFirewallClient().getPortForwardingRule(rule.getId()).getId());
assert rule.getId() > 0 : rule;
assert rule.getIPAddress() != null : rule;
assert rule.getIPAddressId() > 0 : rule;

View File

@ -18,9 +18,9 @@
*/
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.jclouds.cloudstack.options.ListPortForwardingRulesOptions.Builder.ipAddressId;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
@ -48,12 +48,12 @@ public class ListPortForwardingRulesOptionsTest {
}
public void testName() {
ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions().IPAddressId(9);
ListPortForwardingRulesOptions options = new ListPortForwardingRulesOptions().ipAddressId(9);
assertEquals(ImmutableList.of("9"), options.buildQueryParameters().get("ipaddressid"));
}
public void testNameStatic() {
ListPortForwardingRulesOptions options = IPAddressId(9);
ListPortForwardingRulesOptions options = ipAddressId(9);
assertEquals(ImmutableList.of("9"), options.buildQueryParameters().get("ipaddressid"));
}