Use enum for PortForwardingRule protocol

This commit is contained in:
andreisavu 2012-01-10 02:17:39 +02:00
parent 6f463f1f79
commit ef8438d59a
8 changed files with 57 additions and 34 deletions

View File

@ -85,7 +85,7 @@ public class CloudStackParserModule extends AbstractModule {
private long IPAddressId; private long IPAddressId;
@SerializedName("privateport") @SerializedName("privateport")
private int privatePort; private int privatePort;
private String protocol; private PortForwardingRule.Protocol protocol;
@SerializedName("publicport") @SerializedName("publicport")
public int publicPort; public int publicPort;
private String state; private String state;

View File

@ -26,9 +26,30 @@ import com.google.common.collect.ImmutableSet;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
/** /**
* @author Adrian Cole * @author Adrian Cole, Andrei Savu
*/ */
public class PortForwardingRule implements Comparable<PortForwardingRule> { public class PortForwardingRule implements Comparable<PortForwardingRule> {
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().toLowerCase();
}
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -38,7 +59,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
private String IPAddress; private String IPAddress;
private long IPAddressId; private long IPAddressId;
private int privatePort; private int privatePort;
private String protocol; private Protocol protocol;
public int publicPort; public int publicPort;
private String state; private String state;
private String virtualMachineDisplayName; private String virtualMachineDisplayName;
@ -68,7 +89,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
return this; return this;
} }
public Builder protocol(String protocol) { public Builder protocol(Protocol protocol) {
this.protocol = protocol; this.protocol = protocol;
return this; return this;
} }
@ -115,7 +136,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
public PortForwardingRule build() { public PortForwardingRule build() {
return new PortForwardingRule(id, IPAddress, IPAddressId, privatePort, protocol, publicPort, state, return new PortForwardingRule(id, IPAddress, IPAddressId, privatePort, protocol, publicPort, state,
virtualMachineDisplayName, virtualMachineId, virtualMachineName, CIDRs, privateEndPort, publicEndPort); virtualMachineDisplayName, virtualMachineId, virtualMachineName, CIDRs, privateEndPort, publicEndPort);
} }
} }
@ -126,7 +147,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
private long IPAddressId; private long IPAddressId;
@SerializedName("privateport") @SerializedName("privateport")
private int privatePort; private int privatePort;
private String protocol; private Protocol protocol;
@SerializedName("publicport") @SerializedName("publicport")
public int publicPort; public int publicPort;
private String state; private String state;
@ -143,7 +164,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
@SerializedName("publicendport") @SerializedName("publicendport")
private int publicEndPort; private int publicEndPort;
public PortForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, String protocol, public PortForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, Protocol protocol,
int publicPort, String state, String virtualMachineDisplayName, long virtualMachineId, int publicPort, String state, String virtualMachineDisplayName, long virtualMachineId,
String virtualMachineName, Set<String> CIDRs, int privateEndPort, int publicEndPort) { String virtualMachineName, Set<String> CIDRs, int privateEndPort, int publicEndPort) {
this.id = id; this.id = id;
@ -197,7 +218,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
/** /**
* @return the protocol of the port forwarding rule * @return the protocol of the port forwarding rule
*/ */
public String getProtocol() { public Protocol getProtocol() {
return protocol; return protocol;
} }
@ -324,20 +345,20 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
@Override @Override
public String toString() { public String toString() {
return "PortForwardingRule{" + return "PortForwardingRule{" +
"id=" + id + "id=" + id +
", IPAddress='" + IPAddress + '\'' + ", IPAddress='" + IPAddress + '\'' +
", IPAddressId=" + IPAddressId + ", IPAddressId=" + IPAddressId +
", privatePort=" + privatePort + ", privatePort=" + privatePort +
", protocol='" + protocol + '\'' + ", protocol='" + protocol + '\'' +
", publicPort=" + publicPort + ", publicPort=" + publicPort +
", state='" + state + '\'' + ", state='" + state + '\'' +
", virtualMachineDisplayName='" + virtualMachineDisplayName + '\'' + ", virtualMachineDisplayName='" + virtualMachineDisplayName + '\'' +
", virtualMachineId=" + virtualMachineId + ", virtualMachineId=" + virtualMachineId +
", virtualMachineName='" + virtualMachineName + '\'' + ", virtualMachineName='" + virtualMachineName + '\'' +
", CIDRs=" + getCIDRs() + ", CIDRs=" + getCIDRs() +
", privateEndPort=" + privateEndPort + ", privateEndPort=" + privateEndPort +
", publicEndPort=" + publicEndPort + ", publicEndPort=" + publicEndPort +
'}'; '}';
} }
} }

View File

@ -124,7 +124,7 @@ public interface FirewallAsyncClient {
@Unwrap @Unwrap
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<AsyncCreateResponse> createPortForwardingRuleForVirtualMachine( ListenableFuture<AsyncCreateResponse> createPortForwardingRuleForVirtualMachine(
@QueryParam("ipaddressid") long ipAddressId, @QueryParam("protocol") String protocol, @QueryParam("ipaddressid") long ipAddressId, @QueryParam("protocol") PortForwardingRule.Protocol protocol,
@QueryParam("publicport") int publicPort, @QueryParam("virtualmachineid") long virtualMachineId, @QueryParam("publicport") int publicPort, @QueryParam("virtualmachineid") long virtualMachineId,
@QueryParam("privateport") int privatePort); @QueryParam("privateport") int privatePort);

View File

@ -118,7 +118,7 @@ public interface FirewallClient {
* @return response used to track creation * @return response used to track creation
*/ */
AsyncCreateResponse createPortForwardingRuleForVirtualMachine(long ipAddressId, AsyncCreateResponse createPortForwardingRuleForVirtualMachine(long ipAddressId,
String protocol, int publicPort, long virtualMachineId, int privatePort); PortForwardingRule.Protocol protocol, int publicPort, long virtualMachineId, int privatePort);
/** /**
* Deletes an port forwarding rule * Deletes an port forwarding rule

View File

@ -21,6 +21,7 @@ package org.jclouds.cloudstack.features;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions; import org.jclouds.cloudstack.options.ListPortForwardingRulesOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed; import org.jclouds.http.functions.ParseFirstJsonValueNamed;
@ -82,12 +83,12 @@ public class FirewallAsyncClientTest extends BaseCloudStackAsyncClientTest<Firew
public void testCreatePortForwardingRuleForVirtualMachine() throws SecurityException, NoSuchMethodException, public void testCreatePortForwardingRuleForVirtualMachine() throws SecurityException, NoSuchMethodException,
IOException { IOException {
Method method = FirewallAsyncClient.class.getMethod("createPortForwardingRuleForVirtualMachine", long.class, Method method = FirewallAsyncClient.class.getMethod("createPortForwardingRuleForVirtualMachine", long.class,
long.class, String.class, int.class, int.class); PortForwardingRule.Protocol.class, int.class, long.class, int.class);
HttpRequest httpRequest = processor.createRequest(method, 6, 7, "tcp", 22, 22); HttpRequest httpRequest = processor.createRequest(method, 6L, PortForwardingRule.Protocol.TCP, 22, 7L, 22);
assertRequestLineEquals( assertRequestLineEquals(
httpRequest, httpRequest,
"GET http://localhost:8080/client/api?response=json&command=createPortForwardingRule&virtualmachineid=6&protocol=tcp&ipaddressid=7&privateport=22&publicport=22 HTTP/1.1"); "GET http://localhost:8080/client/api?response=json&command=createPortForwardingRule&ipaddressid=6&publicport=22&protocol=tcp&virtualmachineid=7&privateport=22 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);

View File

@ -199,9 +199,9 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
assertEquals(client.listPortForwardingRules(), assertEquals(client.listPortForwardingRules(),
ImmutableSet.<PortForwardingRule>of( ImmutableSet.<PortForwardingRule>of(
PortForwardingRule.builder().id(15).privatePort(22).protocol("tcp").publicPort(2022).virtualMachineId(3) PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(2022).virtualMachineId(3)
.virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(), .virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(),
PortForwardingRule.builder().id(18).privatePort(22).protocol("tcp").publicPort(22).virtualMachineId(89) PortForwardingRule.builder().id(18).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(22).virtualMachineId(89)
.virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build()) .virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build())
); );
} }
@ -247,7 +247,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1");
assertEquals(client.getPortForwardingRule(15), assertEquals(client.getPortForwardingRule(15),
PortForwardingRule.builder().id(15).privatePort(22).protocol("tcp").publicPort(2022).virtualMachineId(3) PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(2022).virtualMachineId(3)
.virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build()); .virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build());
} }
@ -290,7 +290,8 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build()) .build())
.getFirewallClient(); .getFirewallClient();
AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine(2, "tcp", 22, 1234, 22); AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine(
2, PortForwardingRule.Protocol.TCP, 22, 1234, 22);
assertEquals(response.getJobId(), 2035); assertEquals(response.getJobId(), 2035);
assertEquals(response.getId(), 2015); assertEquals(response.getId(), 2015);
} }

View File

@ -96,7 +96,7 @@ public class FirewallClientLiveTest extends BaseCloudStackClientLiveTest {
ip = reuseOrAssociate.apply(network); ip = reuseOrAssociate.apply(network);
try { try {
AsyncCreateResponse job = client.getFirewallClient() AsyncCreateResponse job = client.getFirewallClient()
.createPortForwardingRuleForVirtualMachine(ip.getId(), "tcp", 22, vm.getId(), 22); .createPortForwardingRuleForVirtualMachine(ip.getId(), PortForwardingRule.Protocol.TCP, 22, vm.getId(), 22);
assertTrue(jobComplete.apply(job.getJobId())); assertTrue(jobComplete.apply(job.getJobId()));
portForwardingRule = client.getFirewallClient().getPortForwardingRule(job.getId()); portForwardingRule = client.getFirewallClient().getPortForwardingRule(job.getId());

View File

@ -61,9 +61,9 @@ public class ListPortForwardingRulesResponseTest extends BaseSetParserTest<PortF
public Set<PortForwardingRule> expected() { public Set<PortForwardingRule> expected() {
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1");
return ImmutableSet.<PortForwardingRule> of( return ImmutableSet.<PortForwardingRule> of(
PortForwardingRule.builder().id(15).privatePort(22).protocol("tcp").publicPort(2022).virtualMachineId(3) PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(2022).virtualMachineId(3)
.virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(), .virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(),
PortForwardingRule.builder().id(18).privatePort(22).protocol("tcp").publicPort(22).virtualMachineId(89) PortForwardingRule.builder().id(18).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(22).virtualMachineId(89)
.virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build()); .virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build());
} }