From 9b6a14111192b7eb0c63cfd3691a00fc50bfc447 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Thu, 22 Dec 2011 11:06:52 +0000 Subject: [PATCH] Make PortForwardingRule natively use Set for CIDR lists, and have the CloudStackParserModule deal with converting from a comma-separated list --- .../config/CloudStackParserModule.java | 62 ++++++++++++++++++- .../cloudstack/domain/PortForwardingRule.java | 6 +- .../ListPortForwardingRulesResponseTest.java | 17 +++++ 3 files changed, 81 insertions(+), 4 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 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 3e24a49d80..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 960c6f5617..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";