Merge pull request #267 from richardcloudsoft/cloudstack-lb

CloudStack LoadBalancer fixes
This commit is contained in:
Adrian Cole 2011-12-20 08:35:59 -08:00
commit 39a5af1364
6 changed files with 129 additions and 5 deletions

View File

@ -19,13 +19,17 @@
package org.jclouds.cloudstack.config; package org.jclouds.cloudstack.config;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.inject.Singleton; import javax.inject.Singleton;
import com.google.common.collect.Sets;
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.LoadBalancerRule;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -46,6 +50,51 @@ import com.google.inject.TypeLiteral;
*/ */
public class CloudStackParserModule extends AbstractModule { public class CloudStackParserModule extends AbstractModule {
@Singleton
public static class LoadBalancerRuleAdapter implements JsonSerializer<LoadBalancerRule>, JsonDeserializer<LoadBalancerRule> {
public JsonElement serialize(LoadBalancerRule src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src);
}
public LoadBalancerRule deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return apply(context.<LoadBalancerRuleInternal> deserialize(json, LoadBalancerRuleInternal.class));
}
public LoadBalancerRule apply(LoadBalancerRuleInternal in) {
Set<String> cidrSet = Sets.newHashSet(in.CIDRs.split(","));
return LoadBalancerRule.builder().id(in.id).account(in.account).algorithm(in.algorithm)
.description(in.description).domain(in.domain).domainId(in.domainId).name(in.name)
.privatePort(in.privatePort).publicIP(in.publicIP).publicIPId(in.publicIPId)
.publicPort(in.publicPort).state(in.state).CIDRs(cidrSet).zoneId(in.zoneId).build();
}
static final class LoadBalancerRuleInternal {
private long id;
private String account;
private LoadBalancerRule.Algorithm algorithm;
private String description;
private String domain;
@SerializedName("domainid")
private long domainId;
private String name;
@SerializedName("privateport")
private int privatePort;
@SerializedName("publicip")
private String publicIP;
@SerializedName("publicipid")
private long publicIPId;
@SerializedName("publicport")
private int publicPort;
private LoadBalancerRule.State state;
@SerializedName("cidrlist")
private String CIDRs;
@SerializedName("zoneId")
private long zoneId;
}
}
@Singleton @Singleton
public static class BreakGenericSetAdapter implements JsonSerializer<Account>, JsonDeserializer<Account> { public static class BreakGenericSetAdapter implements JsonSerializer<Account>, JsonDeserializer<Account> {
@ -134,7 +183,7 @@ public class CloudStackParserModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
bind(new TypeLiteral<Map<Type, Object>>() { bind(new TypeLiteral<Map<Type, Object>>() {
}).toInstance(ImmutableMap.<Type, Object> of(Account.class, new BreakGenericSetAdapter())); }).toInstance(ImmutableMap.<Type, Object> of(Account.class, new BreakGenericSetAdapter(), LoadBalancerRule.class, new LoadBalancerRuleAdapter()));
} }
} }

View File

@ -207,7 +207,7 @@ public class LoadBalancerRule implements Comparable<LoadBalancerRule> {
this.publicPort = publicPort; this.publicPort = publicPort;
this.state = state; this.state = state;
this.zoneId = zoneId; this.zoneId = zoneId;
this.CIDRs = Sets.newHashSet(CIDRs); this.CIDRs = ImmutableSet.copyOf(CIDRs);
} }
@ -299,7 +299,7 @@ public class LoadBalancerRule implements Comparable<LoadBalancerRule> {
* @return the cidr list to forward traffic from * @return the cidr list to forward traffic from
*/ */
public Set<String> getCIDRs() { public Set<String> getCIDRs() {
return Collections.unmodifiableSet(CIDRs); return CIDRs;
} }
/** /**

View File

@ -26,7 +26,7 @@ import com.google.common.collect.ImmutableSet;
* @see <a href= * @see <a href=
* "http://download.cloud.com/releases/2.2.0/api/user/listLoadBalancerRules.html" * "http://download.cloud.com/releases/2.2.0/api/user/listLoadBalancerRules.html"
* /> * />
* @author Adrian Cole * @author Adrian Cole, Andrei Savu
*/ */
public class ListLoadBalancerRulesOptions extends AccountInDomainOptions { public class ListLoadBalancerRulesOptions extends AccountInDomainOptions {

View File

@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features;
import static com.google.common.collect.Iterables.find; import static com.google.common.collect.Iterables.find;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
@ -87,7 +88,8 @@ public class LoadBalancerClientLiveTest extends BaseCloudStackClientLiveTest {
public void testCreateLoadBalancerRule() throws Exception { public void testCreateLoadBalancerRule() throws Exception {
if (networksDisabled) if (networksDisabled)
return; return;
while (rule == null) { int attempts = 0;
while (rule == null && attempts < 50) {
ip = reuseOrAssociate.apply(network); ip = reuseOrAssociate.apply(network);
try { try {
rule = client.getLoadBalancerClient().createLoadBalancerRuleForPublicIP(ip.getId(), Algorithm.LEASTCONN, rule = client.getLoadBalancerClient().createLoadBalancerRuleForPublicIP(ip.getId(), Algorithm.LEASTCONN,
@ -96,6 +98,7 @@ public class LoadBalancerClientLiveTest extends BaseCloudStackClientLiveTest {
// very likely an ip conflict, so retry; // very likely an ip conflict, so retry;
} }
} }
assertNotNull(rule, "Failed to get a load balancer rule after "+attempts+" attempts");
assert (rule.getPublicIPId() == ip.getId()) : rule; assert (rule.getPublicIPId() == ip.getId()) : rule;
assertEquals(rule.getPublicPort(), 22); assertEquals(rule.getPublicPort(), 22);
assertEquals(rule.getPrivatePort(), 22); assertEquals(rule.getPrivatePort(), 22);

View File

@ -0,0 +1,70 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.parse;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.cloudstack.config.CloudStackParserModule;
import org.jclouds.cloudstack.domain.LoadBalancerRule;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import org.testng.collections.Sets;
import java.util.Set;
/**
*
* @author Andrei Savu
*/
@Test(groups = "unit")
public class ListLoadBalancerRulesResponseTest extends BaseSetParserTest<LoadBalancerRule> {
@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 "/listloadbalancerrulesresponse.json";
}
@Override
@SelectJson("loadbalancerrule")
public Set<LoadBalancerRule> expected() {
return ImmutableSet.<LoadBalancerRule> of(LoadBalancerRule.builder()
.id(93).account("admin").algorithm(LoadBalancerRule.Algorithm.ROUNDROBIN)
.description("null").domain("ROOT").domainId(1).name("Ranny").privatePort(80)
.publicIP("10.27.27.59").publicIPId(10).publicPort(80).state(LoadBalancerRule.State.ADD)
.CIDRs(Sets.<String>newHashSet()).zoneId(0)
.build());
}
}

View File

@ -0,0 +1,2 @@
{ "listloadbalancerrulesresponse" : { "count":1 ,"loadbalancerrule" : [
{"id":93,"name":"Ranny","publicipid":10,"publicip":"10.27.27.59","publicport":"80","privateport":"80","algorithm":"roundrobin","cidrlist":"","account":"admin","domainid":1,"domain":"ROOT","state":"Add","zoneid":1} ] } }