Merge pull request #280 from richardcloudsoft/cloudstack-lb

PortForwardingRule CIDR list elements
This commit is contained in:
Adrian Cole 2011-12-22 03:20:23 -08:00
commit 4a22d1aaf1
4 changed files with 87 additions and 9 deletions

View File

@ -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<PortForwardingRule>, JsonDeserializer<PortForwardingRule> {
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.<PortForwardingRuleInternal> deserialize(json, PortForwardingRuleInternal.class));
}
public PortForwardingRule apply(PortForwardingRuleInternal in) {
Set<String> 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<LoadBalancerRule>, JsonDeserializer<LoadBalancerRule> {
@ -181,7 +237,11 @@ public class CloudStackParserModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<Map<Type, Object>>() {
}).toInstance(ImmutableMap.<Type, Object> of(Account.class, new BreakGenericSetAdapter(), LoadBalancerRule.class, new LoadBalancerRuleAdapter()));
}).toInstance(ImmutableMap.<Type, Object> of(
Account.class, new BreakGenericSetAdapter(),
LoadBalancerRule.class, new LoadBalancerRuleAdapter(),
PortForwardingRule.class, new PortForwardingRuleAdaptor()
));
}
}

View File

@ -137,7 +137,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
@SerializedName("virtualmachinename")
private String virtualMachineName;
@SerializedName("cidrlist")
private String CIDRs;
private Set<String> CIDRs;
@SerializedName("privateendport")
private int privateEndPort;
@SerializedName("publicendport")
@ -156,7 +156,7 @@ public class PortForwardingRule implements Comparable<PortForwardingRule> {
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<PortForwardingRule> {
* @return the cidr list to forward traffic from
*/
public Set<String> getCIDRs() {
return CIDRs != null ? ImmutableSet.copyOf(Splitter.on(' ').split(CIDRs)) : ImmutableSet.<String> of();
return CIDRs;
}
/**

View File

@ -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<PortForwardingRule> {
@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<PortF
@Override
@SelectJson("portforwardingrule")
public Set<PortForwardingRule> expected() {
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1");
return ImmutableSet.<PortForwardingRule> 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());
}
}

View File

@ -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":""} ] } }
{ "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"} ] } }