Use enum for *Rule.state

This commit is contained in:
andreisavu 2012-01-10 03:24:00 +02:00
parent b5bba81d05
commit 3af4d3a5bb
6 changed files with 86 additions and 29 deletions

View File

@ -90,7 +90,7 @@ public class CloudStackParserModule extends AbstractModule {
private PortForwardingRule.Protocol protocol; private PortForwardingRule.Protocol protocol;
@SerializedName("publicport") @SerializedName("publicport")
public int publicPort; public int publicPort;
private String state; private PortForwardingRule.State state;
@SerializedName("virtualmachinedisplayname") @SerializedName("virtualmachinedisplayname")
private String virtualMachineDisplayName; private String virtualMachineDisplayName;
@SerializedName("virtualmachineid") @SerializedName("virtualmachineid")
@ -148,7 +148,7 @@ public class CloudStackParserModule extends AbstractModule {
@SerializedName("ipaddressid") @SerializedName("ipaddressid")
private long ipAddressId; private long ipAddressId;
private FirewallRule.Protocol protocol; private FirewallRule.Protocol protocol;
private String state; private FirewallRule.State state;
} }
} }

View File

@ -18,8 +18,10 @@
*/ */
package org.jclouds.cloudstack.domain; package org.jclouds.cloudstack.domain;
import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import org.omg.PortableInterceptor.ACTIVE;
import java.util.Set; import java.util.Set;
@ -48,6 +50,29 @@ public class FirewallRule implements Comparable<FirewallRule> {
} }
} }
public static enum State {
STAGED, // Rule been created but has never got through network rule conflict detection.
// Rules in this state can not be sent to network elements.
ADD, // Add means the rule has been created and has gone through network rule conflict detection.
ACTIVE, // Rule has been sent to the network elements and reported to be active.
DELETEING, // Revoke means this rule has been revoked. If this rule has been sent to the
// network elements, the rule will be deleted from database.
UNKNOWN;
public static State fromValue(String value) {
try {
return valueOf(value.toUpperCase());
} catch(IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name());
}
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -66,7 +91,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
private long ipAddressId; private long ipAddressId;
private Protocol protocol; private Protocol protocol;
private String state; private State state;
public Builder id(long id) { public Builder id(long id) {
this.id = id; this.id = id;
@ -113,7 +138,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
return this; return this;
} }
public Builder state(String state) { public Builder state(State state) {
this.state = state; this.state = state;
return this; return this;
} }
@ -140,11 +165,11 @@ public class FirewallRule implements Comparable<FirewallRule> {
@SerializedName("ipaddressid") @SerializedName("ipaddressid")
private long ipAddressId; private long ipAddressId;
private Protocol protocol; private Protocol protocol;
private String state; private State state;
public FirewallRule(long id, Set<String> CIDRs, int startPort, int endPort, public FirewallRule(long id, Set<String> CIDRs, int startPort, int endPort,
String icmpCode, String icmpType, String ipAddress, long ipAddressId, String icmpCode, String icmpType, String ipAddress, long ipAddressId,
Protocol protocol, String state) { Protocol protocol, State state) {
this.id = id; this.id = id;
this.CIDRs = ImmutableSet.copyOf(CIDRs); this.CIDRs = ImmutableSet.copyOf(CIDRs);
this.startPort = startPort; this.startPort = startPort;
@ -198,7 +223,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
return protocol; return protocol;
} }
public String getState() { public State getState() {
return state; return state;
} }

View File

@ -20,6 +20,7 @@ package org.jclouds.cloudstack.domain;
import java.util.Set; import java.util.Set;
import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
@ -48,6 +49,29 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
} }
} }
public static enum State {
STAGED, // Rule been created but has never got through network rule conflict detection.
// Rules in this state can not be sent to network elements.
ADD, // Add means the rule has been created and has gone through network rule conflict detection.
ACTIVE, // Rule has been sent to the network elements and reported to be active.
DELETEING, // Revoke means this rule has been revoked. If this rule has been sent to the
// network elements, the rule will be deleted from database.
UNKNOWN;
public static State fromValue(String value) {
try {
return valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name());
}
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -59,7 +83,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
private int privatePort; private int privatePort;
private Protocol protocol; private Protocol protocol;
public int publicPort; public int publicPort;
private String state; private State state;
private String virtualMachineDisplayName; private String virtualMachineDisplayName;
public long virtualMachineId; public long virtualMachineId;
private String virtualMachineName; private String virtualMachineName;
@ -97,7 +121,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
return this; return this;
} }
public Builder state(String state) { public Builder state(State state) {
this.state = state; this.state = state;
return this; return this;
} }
@ -148,7 +172,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
private Protocol protocol; private Protocol protocol;
@SerializedName("publicport") @SerializedName("publicport")
public int publicPort; public int publicPort;
private String state; private State state;
@SerializedName("virtualmachinedisplayname") @SerializedName("virtualmachinedisplayname")
private String virtualMachineDisplayName; private String virtualMachineDisplayName;
@SerializedName("virtualmachineid") @SerializedName("virtualmachineid")
@ -163,7 +187,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
private int publicEndPort; private int publicEndPort;
public PortForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, Protocol protocol, public PortForwardingRule(long id, String iPAddress, long iPAddressId, int privatePort, Protocol protocol,
int publicPort, String state, String virtualMachineDisplayName, long virtualMachineId, int publicPort, State 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;
this.IPAddress = iPAddress; this.IPAddress = iPAddress;
@ -230,7 +254,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
/** /**
* @return the state of the rule * @return the state of the rule
*/ */
public String getState() { public State getState() {
return state; return state;
} }

View File

@ -66,11 +66,14 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
assertEquals(client.listFirewallRules(), assertEquals(client.listFirewallRules(),
ImmutableSet.of( ImmutableSet.of(
FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30)
.endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs(CIDRs).build(), .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE)
.CIDRs(CIDRs).build(),
FirewallRule.builder().id(2016).protocol(FirewallRule.Protocol.TCP).startPort(22) FirewallRule.builder().id(2016).protocol(FirewallRule.Protocol.TCP).startPort(22)
.endPort(22).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs(CIDRs).build(), .endPort(22).ipAddressId(2).ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE)
.CIDRs(CIDRs).build(),
FirewallRule.builder().id(10).protocol(FirewallRule.Protocol.TCP).startPort(22) FirewallRule.builder().id(10).protocol(FirewallRule.Protocol.TCP).startPort(22)
.endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state("Active").CIDRs(CIDRs).build() .endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state(FirewallRule.State.ACTIVE)
.CIDRs(CIDRs).build()
)); ));
} }
@ -114,7 +117,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
assertEquals(client.getFirewallRule(2017), assertEquals(client.getFirewallRule(2017),
FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30)
.endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active") .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE)
.CIDRs(ImmutableSet.of("0.0.0.0/0")).build() .CIDRs(ImmutableSet.of("0.0.0.0/0")).build()
); );
} }
@ -201,10 +204,12 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
assertEquals(client.listPortForwardingRules(), assertEquals(client.listPortForwardingRules(),
ImmutableSet.<PortForwardingRule>of( ImmutableSet.<PortForwardingRule>of(
PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(2022).virtualMachineId(3) PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP)
.virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(), .publicPort(2022).virtualMachineId(3).virtualMachineName("i-3-3-VM").IPAddressId(3)
PortForwardingRule.builder().id(18).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(22).virtualMachineId(89) .IPAddress("72.52.126.32").state(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).build(),
.virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build()) 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(PortForwardingRule.State.ACTIVE).build())
); );
} }
@ -249,8 +254,9 @@ 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(PortForwardingRule.Protocol.TCP).publicPort(2022).virtualMachineId(3) PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP)
.virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build()); .publicPort(2022).virtualMachineId(3).virtualMachineName("i-3-3-VM").IPAddressId(3)
.IPAddress("72.52.126.32").state(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).build());
} }
public void testGetPortForwardingRuleWhenResponseIs404() { public void testGetPortForwardingRuleWhenResponseIs404() {

View File

@ -61,11 +61,11 @@ public class ListFirewallRulesResponseTest extends BaseSetParserTest<FirewallRul
Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0"); Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0");
return ImmutableSet.of( return ImmutableSet.of(
FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30)
.endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs(CIDRs).build(), .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE).CIDRs(CIDRs).build(),
FirewallRule.builder().id(2016).protocol(FirewallRule.Protocol.TCP).startPort(22) FirewallRule.builder().id(2016).protocol(FirewallRule.Protocol.TCP).startPort(22)
.endPort(22).ipAddressId(2).ipAddress("10.27.27.51").state("Active").CIDRs(CIDRs).build(), .endPort(22).ipAddressId(2).ipAddress("10.27.27.51").state(FirewallRule.State.ACTIVE).CIDRs(CIDRs).build(),
FirewallRule.builder().id(10).protocol(FirewallRule.Protocol.TCP).startPort(22) FirewallRule.builder().id(10).protocol(FirewallRule.Protocol.TCP).startPort(22)
.endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state("Active").CIDRs(CIDRs).build() .endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state(FirewallRule.State.ACTIVE).CIDRs(CIDRs).build()
); );
} }

View File

@ -61,10 +61,12 @@ 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(PortForwardingRule.Protocol.TCP).publicPort(2022).virtualMachineId(3) PortForwardingRule.builder().id(15).privatePort(22).protocol(PortForwardingRule.Protocol.TCP)
.virtualMachineName("i-3-3-VM").IPAddressId(3).IPAddress("72.52.126.32").state("Active").CIDRs(cidrs).build(), .publicPort(2022).virtualMachineId(3).virtualMachineName("i-3-3-VM").IPAddressId(3)
PortForwardingRule.builder().id(18).privatePort(22).protocol(PortForwardingRule.Protocol.TCP).publicPort(22).virtualMachineId(89) .IPAddress("72.52.126.32").state(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).build(),
.virtualMachineName("i-3-89-VM").IPAddressId(34).IPAddress("72.52.126.63").state("Active").build()); 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(PortForwardingRule.State.ACTIVE).build());
} }
} }