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.State;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.LoadBalancerRule;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.domain.User;
@ -46,20 +47,21 @@ import com.google.inject.TypeLiteral;
/**
* Configures the cloudstack parsers.
*
* @author Adrian Cole
*
* @author Adrian Cole, Andrei Savu
*/
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) {
return context.serialize(src);
}
public PortForwardingRule deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return apply(context.<PortForwardingRuleInternal> deserialize(json, PortForwardingRuleInternal.class));
throws JsonParseException {
return apply(context.<PortForwardingRuleInternal>deserialize(json, PortForwardingRuleInternal.class));
}
public PortForwardingRule apply(PortForwardingRuleInternal in) {
@ -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
public static class LoadBalancerRuleAdapter implements JsonSerializer<LoadBalancerRule>, JsonDeserializer<LoadBalancerRule> {
@ -112,8 +160,8 @@ public class CloudStackParserModule extends AbstractModule {
}
public LoadBalancerRule deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return apply(context.<LoadBalancerRuleInternal> deserialize(json, LoadBalancerRuleInternal.class));
throws JsonParseException {
return apply(context.<LoadBalancerRuleInternal>deserialize(json, LoadBalancerRuleInternal.class));
}
public LoadBalancerRule apply(LoadBalancerRuleInternal in) {
@ -157,22 +205,22 @@ public class CloudStackParserModule extends AbstractModule {
}
public Account deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return apply(context.<AccountInternal> deserialize(json, AccountInternal.class));
throws JsonParseException {
return apply(context.<AccountInternal>deserialize(json, AccountInternal.class));
}
public Account apply(AccountInternal in) {
return Account.builder().id(in.id).type(in.type).domain(in.domain).domainId(in.domainId)
.IPsAvailable(nullIfUnlimited(in.IPsAvailable)).IPLimit(nullIfUnlimited(in.IPLimit)).IPs(in.IPs)
.cleanupRequired(in.cleanupRequired).name(in.name).receivedBytes(in.receivedBytes)
.sentBytes(in.sentBytes).snapshotsAvailable(nullIfUnlimited(in.snapshotsAvailable))
.snapshotLimit(nullIfUnlimited(in.snapshotLimit)).snapshots(in.snapshots).state(in.state)
.templatesAvailable(nullIfUnlimited(in.templatesAvailable))
.templateLimit(nullIfUnlimited(in.templateLimit)).templates(in.templates)
.VMsAvailable(nullIfUnlimited(in.VMsAvailable)).VMLimit(nullIfUnlimited(in.VMLimit))
.VMsRunning(in.VMsRunning).VMsStopped(in.VMsStopped).VMs(in.VMs)
.volumesAvailable(nullIfUnlimited(in.volumesAvailable)).volumeLimit(nullIfUnlimited(in.volumeLimit))
.volumes(in.volumes).users(in.users).build();
.IPsAvailable(nullIfUnlimited(in.IPsAvailable)).IPLimit(nullIfUnlimited(in.IPLimit)).IPs(in.IPs)
.cleanupRequired(in.cleanupRequired).name(in.name).receivedBytes(in.receivedBytes)
.sentBytes(in.sentBytes).snapshotsAvailable(nullIfUnlimited(in.snapshotsAvailable))
.snapshotLimit(nullIfUnlimited(in.snapshotLimit)).snapshots(in.snapshots).state(in.state)
.templatesAvailable(nullIfUnlimited(in.templatesAvailable))
.templateLimit(nullIfUnlimited(in.templateLimit)).templates(in.templates)
.VMsAvailable(nullIfUnlimited(in.VMsAvailable)).VMLimit(nullIfUnlimited(in.VMLimit))
.VMsRunning(in.VMsRunning).VMsStopped(in.VMsStopped).VMs(in.VMs)
.volumesAvailable(nullIfUnlimited(in.volumesAvailable)).volumeLimit(nullIfUnlimited(in.volumeLimit))
.volumes(in.volumes).users(in.users).build();
}
static final class AccountInternal {
@ -237,10 +285,11 @@ public class CloudStackParserModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<Map<Type, Object>>() {
}).toInstance(ImmutableMap.<Type, Object> of(
}).toInstance(ImmutableMap.<Type, Object>of(
Account.class, new BreakGenericSetAdapter(),
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;
import com.google.common.collect.ImmutableSet;
import com.google.gson.annotations.SerializedName;
import java.util.Set;
/**
* @author Andrei Savu
*/
@ -51,7 +54,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
public static class Builder {
private long id;
private String CIDRs;
private Set<String> CIDRs;
private int startPort;
private int endPort;
@ -70,8 +73,8 @@ public class FirewallRule implements Comparable<FirewallRule> {
return this;
}
public Builder CIDRs(String CIDRs) {
this.CIDRs = CIDRs;
public Builder CIDRs(Set<String> CIDRs) {
this.CIDRs = ImmutableSet.copyOf(CIDRs);
return this;
}
@ -123,7 +126,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
private long id;
@SerializedName("cidrlist")
private String CIDRs;
private Set<String> CIDRs;
@SerializedName("startport")
private int startPort;
@SerializedName("endport")
@ -139,11 +142,11 @@ public class FirewallRule implements Comparable<FirewallRule> {
private Protocol protocol;
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,
Protocol protocol, String state) {
this.id = id;
this.CIDRs = CIDRs;
this.CIDRs = ImmutableSet.copyOf(CIDRs);
this.startPort = startPort;
this.endPort = endPort;
this.icmpCode = icmpCode;
@ -163,7 +166,7 @@ public class FirewallRule implements Comparable<FirewallRule> {
return id;
}
public String getCIDRs() {
public Set<String> getCIDRs() {
return CIDRs;
}

View File

@ -20,8 +20,6 @@ package org.jclouds.cloudstack.domain;
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.gson.annotations.SerializedName;

View File

@ -41,7 +41,7 @@ public class CreateFirewallRuleOptions extends BaseHttpRequestOptions {
* the list of CIDRs to forward traffic from
*/
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;
}

View File

@ -62,14 +62,15 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build())
.getFirewallClient();
Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0");
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("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)
.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)
.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),
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
@SelectJson("firewallrule")
public Set<FirewallRule> expected() {
Set<String> 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("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)
.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)
.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()
);
}