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 2db8024827..0ac02f2c15 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 @@ -19,6 +19,8 @@ package org.jclouds.cloudstack.config; import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.Collections; import java.util.Map; import java.util.Set; @@ -27,6 +29,7 @@ import javax.inject.Singleton; import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.Account.State; import org.jclouds.cloudstack.domain.LoadBalancerRule; +import org.jclouds.cloudstack.domain.PortForwardingRule; import org.jclouds.cloudstack.domain.User; import com.google.common.collect.ImmutableMap; @@ -48,6 +51,59 @@ import com.google.inject.TypeLiteral; */ public class CloudStackParserModule extends AbstractModule { + public static class PortForwardingRuleAdaptor implements JsonSerializer, JsonDeserializer { + + public JsonElement serialize(PortForwardingRule src, Type typeOfSrc, JsonSerializationContext context) { + return context.serialize(src); + } + + public PortForwardingRule deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return apply(context. deserialize(json, PortForwardingRuleInternal.class)); + } + + public PortForwardingRule apply(PortForwardingRuleInternal in) { + Set cidrSet; + if (in.CIDRs != null) { + String[] elements = in.CIDRs.split(","); + cidrSet = Sets.newTreeSet(Arrays.asList(elements)); + } else { + cidrSet = Collections.emptySet(); + } + return PortForwardingRule.builder().id(in.id).IPAddress(in.IPAddress).IPAddressId(in.IPAddressId) + .privatePort(in.privatePort).protocol(in.protocol).publicPort(in.publicPort).state(in.state) + .virtualMachineDisplayName(in.virtualMachineDisplayName).virtualMachineId(in.virtualMachineId) + .virtualMachineName(in.virtualMachineName).CIDRs(cidrSet).privateEndPort(in.privateEndPort) + .publicEndPort(in.publicEndPort).build(); + } + + static final class PortForwardingRuleInternal { + private long id; + @SerializedName("ipaddress") + private String IPAddress; + @SerializedName("ipaddressid") + private long IPAddressId; + @SerializedName("privateport") + private int privatePort; + private String protocol; + @SerializedName("publicport") + public int publicPort; + private String state; + @SerializedName("virtualmachinedisplayname") + private String virtualMachineDisplayName; + @SerializedName("virtualmachineid") + public long virtualMachineId; + @SerializedName("virtualmachinename") + private String virtualMachineName; + @SerializedName("cidrlist") + private String CIDRs; + @SerializedName("privateendport") + private int privateEndPort; + @SerializedName("publicendport") + private int publicEndPort; + } + } + @Singleton public static class LoadBalancerRuleAdapter implements JsonSerializer, JsonDeserializer { @@ -181,7 +237,11 @@ public class CloudStackParserModule extends AbstractModule { @Override protected void configure() { bind(new TypeLiteral>() { - }).toInstance(ImmutableMap. of(Account.class, new BreakGenericSetAdapter(), LoadBalancerRule.class, new LoadBalancerRuleAdapter())); + }).toInstance(ImmutableMap. of( + Account.class, new BreakGenericSetAdapter(), + LoadBalancerRule.class, new LoadBalancerRuleAdapter(), + PortForwardingRule.class, new PortForwardingRuleAdaptor() + )); } } 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 275e5cd3c4..e0dada1791 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 @@ -137,7 +137,7 @@ public class PortForwardingRule implements Comparable { @SerializedName("virtualmachinename") private String virtualMachineName; @SerializedName("cidrlist") - private String CIDRs; + private Set CIDRs; @SerializedName("privateendport") private int privateEndPort; @SerializedName("publicendport") @@ -156,7 +156,7 @@ public class PortForwardingRule implements Comparable { this.virtualMachineDisplayName = virtualMachineDisplayName; this.virtualMachineId = virtualMachineId; this.virtualMachineName = virtualMachineName; - this.CIDRs = Joiner.on(' ').join(CIDRs); + this.CIDRs = CIDRs; this.privateEndPort = privateEndPort; this.publicEndPort = publicEndPort; } @@ -240,7 +240,7 @@ public class PortForwardingRule implements Comparable { * @return the cidr list to forward traffic from */ public Set getCIDRs() { - return CIDRs != null ? ImmutableSet.copyOf(Splitter.on(' ').split(CIDRs)) : ImmutableSet. of(); + return CIDRs; } /** 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 6a0b607fe5..85b41e656c 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 @@ -20,8 +20,12 @@ package org.jclouds.cloudstack.parse; import java.util.Set; +import com.google.inject.Guice; +import com.google.inject.Injector; +import org.jclouds.cloudstack.config.CloudStackParserModule; import org.jclouds.cloudstack.domain.PortForwardingRule; import org.jclouds.json.BaseSetParserTest; +import org.jclouds.json.config.GsonModule; import org.jclouds.rest.annotations.SelectJson; import org.testng.annotations.Test; @@ -34,6 +38,19 @@ import com.google.common.collect.ImmutableSet; @Test(groups = "unit") public class ListPortForwardingRulesResponseTest extends BaseSetParserTest { + @Override + protected Injector injector() { + return Guice.createInjector(new CloudStackParserModule(), new GsonModule() { + + @Override + protected void configure() { + bind(DateAdapter.class).to(Iso8601DateAdapter.class); + super.configure(); + } + + }); + } + @Override public String resource() { return "/listportforwardingrulesresponse.json"; @@ -42,11 +59,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("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()); + 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").CIDRs(cidrs).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()); } } diff --git a/apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json b/apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json index 0f1373f089..c385f7ee78 100644 --- a/apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json +++ b/apis/cloudstack/src/test/resources/listportforwardingrulesresponse.json @@ -1 +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","cidrlist":""} ] } } \ No newline at end of file +{ "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","cidrlist":"0.0.0.0/1,128.0.0.0/1"} ] } } \ No newline at end of file