Use a custom parser for the CIDR list

This commit is contained in:
andreisavu 2012-01-10 02:47:42 +02:00
parent ef8438d59a
commit b5bba81d05
6 changed files with 91 additions and 38 deletions

View File

@ -28,6 +28,7 @@ import javax.inject.Singleton;
import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.Account.State; import org.jclouds.cloudstack.domain.Account.State;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.LoadBalancerRule; import org.jclouds.cloudstack.domain.LoadBalancerRule;
import org.jclouds.cloudstack.domain.PortForwardingRule; import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
@ -47,11 +48,12 @@ import com.google.inject.TypeLiteral;
/** /**
* Configures the cloudstack parsers. * Configures the cloudstack parsers.
* *
* @author Adrian Cole * @author Adrian Cole, Andrei Savu
*/ */
public class CloudStackParserModule extends AbstractModule { public class CloudStackParserModule extends AbstractModule {
public static class PortForwardingRuleAdaptor implements JsonSerializer<PortForwardingRule>, JsonDeserializer<PortForwardingRule> { @Singleton
public static class PortForwardingRuleAdapter implements JsonSerializer<PortForwardingRule>, JsonDeserializer<PortForwardingRule> {
public JsonElement serialize(PortForwardingRule src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(PortForwardingRule src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src); return context.serialize(src);
@ -104,6 +106,52 @@ public class CloudStackParserModule extends AbstractModule {
} }
} }
@Singleton
public static class FirewallRuleAdapter implements JsonSerializer<FirewallRule>, JsonDeserializer<FirewallRule> {
public JsonElement serialize(FirewallRule src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src);
}
public FirewallRule deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return apply(context.<FirewallRuleInternal>deserialize(json, FirewallRuleInternal.class));
}
public FirewallRule apply(FirewallRuleInternal in) {
Set<String> cidrSet;
if (in.CIDRs != null) {
String[] elements = in.CIDRs.split(",");
cidrSet = Sets.newTreeSet(Arrays.asList(elements));
} else {
cidrSet = Collections.emptySet();
}
return FirewallRule.builder().id(in.id).CIDRs(cidrSet).startPort(in.startPort).endPort(in.endPort)
.icmpCode(in.icmpCode).icmpType(in.icmpType).ipAddress(in.ipAddress).ipAddressId(in.ipAddressId)
.protocol(in.protocol).state(in.state).build();
}
static final class FirewallRuleInternal {
private long id;
@SerializedName("cidrlist")
private String CIDRs;
@SerializedName("startport")
private int startPort;
@SerializedName("endport")
private int endPort;
@SerializedName("icmpcode")
private String icmpCode;
@SerializedName("icmptype")
private String icmpType;
@SerializedName("ipaddress")
private String ipAddress;
@SerializedName("ipaddressid")
private long ipAddressId;
private FirewallRule.Protocol protocol;
private String state;
}
}
@Singleton @Singleton
public static class LoadBalancerRuleAdapter implements JsonSerializer<LoadBalancerRule>, JsonDeserializer<LoadBalancerRule> { public static class LoadBalancerRuleAdapter implements JsonSerializer<LoadBalancerRule>, JsonDeserializer<LoadBalancerRule> {
@ -240,7 +288,8 @@ public class CloudStackParserModule extends AbstractModule {
}).toInstance(ImmutableMap.<Type, Object>of( }).toInstance(ImmutableMap.<Type, Object>of(
Account.class, new BreakGenericSetAdapter(), Account.class, new BreakGenericSetAdapter(),
LoadBalancerRule.class, new LoadBalancerRuleAdapter(), LoadBalancerRule.class, new LoadBalancerRuleAdapter(),
PortForwardingRule.class, new PortForwardingRuleAdaptor() PortForwardingRule.class, new PortForwardingRuleAdapter(),
FirewallRule.class, new FirewallRuleAdapter()
)); ));
} }

View File

@ -18,8 +18,11 @@
*/ */
package org.jclouds.cloudstack.domain; package org.jclouds.cloudstack.domain;
import com.google.common.collect.ImmutableSet;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.Set;
/** /**
* @author Andrei Savu * @author Andrei Savu
*/ */
@ -51,7 +54,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
public static class Builder { public static class Builder {
private long id; private long id;
private String CIDRs; private Set<String> CIDRs;
private int startPort; private int startPort;
private int endPort; private int endPort;
@ -70,8 +73,8 @@ public class FirewallRule implements Comparable<FirewallRule> {
return this; return this;
} }
public Builder CIDRs(String CIDRs) { public Builder CIDRs(Set<String> CIDRs) {
this.CIDRs = CIDRs; this.CIDRs = ImmutableSet.copyOf(CIDRs);
return this; return this;
} }
@ -123,7 +126,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
private long id; private long id;
@SerializedName("cidrlist") @SerializedName("cidrlist")
private String CIDRs; private Set<String> CIDRs;
@SerializedName("startport") @SerializedName("startport")
private int startPort; private int startPort;
@SerializedName("endport") @SerializedName("endport")
@ -139,11 +142,11 @@ public class FirewallRule implements Comparable<FirewallRule> {
private Protocol protocol; private Protocol protocol;
private String state; private String state;
public FirewallRule(long id, 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, String state) {
this.id = id; this.id = id;
this.CIDRs = CIDRs; this.CIDRs = ImmutableSet.copyOf(CIDRs);
this.startPort = startPort; this.startPort = startPort;
this.endPort = endPort; this.endPort = endPort;
this.icmpCode = icmpCode; this.icmpCode = icmpCode;
@ -163,7 +166,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
return id; return id;
} }
public String getCIDRs() { public Set<String> getCIDRs() {
return CIDRs; return CIDRs;
} }

View File

@ -20,8 +20,6 @@ package org.jclouds.cloudstack.domain;
import java.util.Set; import java.util.Set;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;

View File

@ -41,7 +41,7 @@ public class CreateFirewallRuleOptions extends BaseHttpRequestOptions {
* the list of CIDRs to forward traffic from * the list of CIDRs to forward traffic from
*/ */
public CreateFirewallRuleOptions CIDRs(Set<String> CIDRs) { public CreateFirewallRuleOptions CIDRs(Set<String> CIDRs) {
this.queryParameters.replaceValues("id", ImmutableSet.of(Joiner.on(",").join(CIDRs))); this.queryParameters.replaceValues("cidrlist", ImmutableSet.of(Joiner.on(",").join(CIDRs)));
return this; return this;
} }

View File

@ -62,14 +62,15 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build()) .build())
.getFirewallClient(); .getFirewallClient();
Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0");
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("0.0.0.0/0").build(), .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").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("0.0.0.0/0").build(), .endPort(22).ipAddressId(2).ipAddress("10.27.27.51").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("0.0.0.0/0").build() .endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state("Active").CIDRs(CIDRs).build()
)); ));
} }
@ -113,7 +114,8 @@ 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").CIDRs("0.0.0.0/0").build() .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").state("Active")
.CIDRs(ImmutableSet.of("0.0.0.0/0")).build()
); );
} }

View File

@ -58,13 +58,14 @@ public class ListFirewallRulesResponseTest extends BaseSetParserTest<FirewallRul
@Override @Override
@SelectJson("firewallrule") @SelectJson("firewallrule")
public Set<FirewallRule> expected() { public Set<FirewallRule> expected() {
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("0.0.0.0/0").build(), .endPort(35).ipAddressId(2).ipAddress("10.27.27.51").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("0.0.0.0/0").build(), .endPort(22).ipAddressId(2).ipAddress("10.27.27.51").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("0.0.0.0/0").build() .endPort(22).ipAddressId(8).ipAddress("10.27.27.57").state("Active").CIDRs(CIDRs).build()
); );
} }