From 3af4d3a5bb345253c58d8ff8fbdbaff0307653f0 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Tue, 10 Jan 2012 03:24:00 +0200 Subject: [PATCH] Use enum for *Rule.state --- .../config/CloudStackParserModule.java | 4 +-- .../cloudstack/domain/FirewallRule.java | 35 ++++++++++++++++--- .../cloudstack/domain/PortForwardingRule.java | 34 +++++++++++++++--- .../features/FirewallClientExpectTest.java | 26 ++++++++------ .../parse/ListFirewallRulesResponseTest.java | 6 ++-- .../ListPortForwardingRulesResponseTest.java | 10 +++--- 6 files changed, 86 insertions(+), 29 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackParserModule.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackParserModule.java index 155bbf05e7..065662b09a 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackParserModule.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackParserModule.java @@ -90,7 +90,7 @@ public class CloudStackParserModule extends AbstractModule { private PortForwardingRule.Protocol protocol; @SerializedName("publicport") public int publicPort; - private String state; + private PortForwardingRule.State state; @SerializedName("virtualmachinedisplayname") private String virtualMachineDisplayName; @SerializedName("virtualmachineid") @@ -148,7 +148,7 @@ public class CloudStackParserModule extends AbstractModule { @SerializedName("ipaddressid") private long ipAddressId; private FirewallRule.Protocol protocol; - private String state; + private FirewallRule.State state; } } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/FirewallRule.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/FirewallRule.java index f8248d74bd..baf5fe6f3c 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/FirewallRule.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/FirewallRule.java @@ -18,8 +18,10 @@ */ package org.jclouds.cloudstack.domain; +import com.google.common.base.CaseFormat; import com.google.common.collect.ImmutableSet; import com.google.gson.annotations.SerializedName; +import org.omg.PortableInterceptor.ACTIVE; import java.util.Set; @@ -48,6 +50,29 @@ public class FirewallRule implements Comparable { } } + 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() { return new Builder(); } @@ -66,7 +91,7 @@ public class FirewallRule implements Comparable { private long ipAddressId; private Protocol protocol; - private String state; + private State state; public Builder id(long id) { this.id = id; @@ -113,7 +138,7 @@ public class FirewallRule implements Comparable { return this; } - public Builder state(String state) { + public Builder state(State state) { this.state = state; return this; } @@ -140,11 +165,11 @@ public class FirewallRule implements Comparable { @SerializedName("ipaddressid") private long ipAddressId; private Protocol protocol; - private String state; + private State state; public FirewallRule(long id, Set CIDRs, int startPort, int endPort, String icmpCode, String icmpType, String ipAddress, long ipAddressId, - Protocol protocol, String state) { + Protocol protocol, State state) { this.id = id; this.CIDRs = ImmutableSet.copyOf(CIDRs); this.startPort = startPort; @@ -198,7 +223,7 @@ public class FirewallRule implements Comparable { return protocol; } - public String getState() { + public State getState() { return state; } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/PortForwardingRule.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/PortForwardingRule.java index 420f71efc8..67c8ddb8ce 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/PortForwardingRule.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/PortForwardingRule.java @@ -20,6 +20,7 @@ package org.jclouds.cloudstack.domain; import java.util.Set; +import com.google.common.base.CaseFormat; import com.google.common.collect.ImmutableSet; import com.google.gson.annotations.SerializedName; @@ -48,6 +49,29 @@ public class PortForwardingRule implements Comparable { } } + 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() { return new Builder(); } @@ -59,7 +83,7 @@ public class PortForwardingRule implements Comparable { private int privatePort; private Protocol protocol; public int publicPort; - private String state; + private State state; private String virtualMachineDisplayName; public long virtualMachineId; private String virtualMachineName; @@ -97,7 +121,7 @@ public class PortForwardingRule implements Comparable { return this; } - public Builder state(String state) { + public Builder state(State state) { this.state = state; return this; } @@ -148,7 +172,7 @@ public class PortForwardingRule implements Comparable { private Protocol protocol; @SerializedName("publicport") public int publicPort; - private String state; + private State state; @SerializedName("virtualmachinedisplayname") private String virtualMachineDisplayName; @SerializedName("virtualmachineid") @@ -163,7 +187,7 @@ public class PortForwardingRule implements Comparable { private int publicEndPort; 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 CIDRs, int privateEndPort, int publicEndPort) { this.id = id; this.IPAddress = iPAddress; @@ -230,7 +254,7 @@ public class PortForwardingRule implements Comparable { /** * @return the state of the rule */ - public String getState() { + public State getState() { return state; } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java index 8fc61770bb..616c5efdd1 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/FirewallClientExpectTest.java @@ -66,11 +66,14 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest assertEquals(client.listFirewallRules(), ImmutableSet.of( 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) - .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) - .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), 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() ); } @@ -201,10 +204,12 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest assertEquals(client.listPortForwardingRules(), ImmutableSet.of( - 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(), - 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()) + 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(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).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 cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); assertEquals(client.getPortForwardingRule(15), - 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()); + 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(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).build()); } public void testGetPortForwardingRuleWhenResponseIs404() { diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java index f009dbe742..1a67ca39c6 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListFirewallRulesResponseTest.java @@ -61,11 +61,11 @@ public class ListFirewallRulesResponseTest extends BaseSetParserTest CIDRs = ImmutableSet.of("0.0.0.0/0"); return ImmutableSet.of( 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) - .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) - .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() ); } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListPortForwardingRulesResponseTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListPortForwardingRulesResponseTest.java index 62e74eb2b1..5079c7c6a5 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListPortForwardingRulesResponseTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListPortForwardingRulesResponseTest.java @@ -61,10 +61,12 @@ public class ListPortForwardingRulesResponseTest extends BaseSetParserTest expected() { Set cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); return ImmutableSet. of( - 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(), - 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()); + 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(PortForwardingRule.State.ACTIVE).CIDRs(cidrs).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()); } }