mirror of https://github.com/apache/jclouds.git
Use a custom parser for the CIDR list
This commit is contained in:
parent
ef8438d59a
commit
b5bba81d05
|
@ -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;
|
||||
|
@ -47,11 +48,12 @@ 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);
|
||||
|
@ -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> {
|
||||
|
||||
|
@ -240,7 +288,8 @@ public class CloudStackParserModule extends AbstractModule {
|
|||
}).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()
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue