* 'issue-669' of https://github.com/mattiasholmqvist/jclouds:
  Read "smp:cores" instead of "smp"
  Intermediate fix that fixes vnc:ip and nic:0:dhcp:ip for new API version of ElasticStack. Still some issues left to fix due to hanging tests.
This commit is contained in:
Adrian Cole 2011-09-08 16:25:50 +02:00
commit 952e58e74d
11 changed files with 181 additions and 38 deletions

View File

@ -23,29 +23,43 @@ import java.util.Map;
import javax.inject.Singleton;
import com.google.inject.Inject;
import org.jclouds.elasticstack.domain.Model;
import org.jclouds.elasticstack.domain.NIC;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import org.jclouds.rest.annotations.ApiVersion;
/**
*
* @author Adrian Cole
*/
@Singleton
public class MapToNICs implements Function<Map<String, String>, List<NIC>> {
private String apiVersion;
@Inject
public MapToNICs(@ApiVersion String apiVersion) {
this.apiVersion = apiVersion;
}
@Override
public List<NIC> apply(Map<String, String> from) {
ImmutableList.Builder<NIC> nics = ImmutableList.builder();
NIC: for (int id : new int[] { 0, 1 }) {
NIC:
for (int id : new int[]{0, 1}) {
String key = String.format("nic:%d", id);
if (!from.containsKey(key + ":model"))
break NIC;
NIC.Builder nicBuilder = new NIC.Builder();
nicBuilder.dhcp(from.get(key + ":dhcp"));
if (apiVersion.equals("2.0")) {
nicBuilder.dhcp(from.get(key + ":dhcp:ip"));
} else {
nicBuilder.dhcp(from.get(key + ":dhcp"));
}
nicBuilder.model(Model.fromValue(from.get(key + ":model")));
nicBuilder.vlan(from.get(key + ":vlan"));
nicBuilder.mac(from.get(key + ":mac"));

View File

@ -36,20 +36,22 @@ import org.jclouds.elasticstack.domain.VNC;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import org.jclouds.rest.annotations.ApiVersion;
/**
*
* @author Adrian Cole
*/
@Singleton
public class MapToServerInfo implements Function<Map<String, String>, ServerInfo> {
private String apiVersion;
private final Function<Map<String, String>, Map<String, ? extends Device>> mapToDevices;
private final Function<Map<String, String>, ServerMetrics> mapToMetrics;
private final Function<Map<String, String>, List<NIC>> mapToNICs;
@Inject
public MapToServerInfo(Function<Map<String, String>, Map<String, ? extends Device>> mapToDevices,
Function<Map<String, String>, ServerMetrics> mapToMetrics, Function<Map<String, String>, List<NIC>> mapToNICs) {
Function<Map<String, String>, ServerMetrics> mapToMetrics, Function<Map<String, String>, List<NIC>> mapToNICs) {
this.apiVersion = apiVersion;
this.mapToDevices = mapToDevices;
this.mapToMetrics = mapToMetrics;
this.mapToNICs = mapToNICs;
@ -66,19 +68,28 @@ public class MapToServerInfo implements Function<Map<String, String>, ServerInfo
builder.tags(Splitter.on(' ').split(from.get("tags")));
if (from.containsKey("status"))
builder.status(ServerStatus.fromValue(from.get("status")));
if (from.containsKey("smp") && !"auto".equals(from.get("smp")))
builder.smp(new Integer(from.get("smp")));
if (from.containsKey("smp:cores")) {
builder.smp(new Integer(from.get("smp:cores")));
} else if (from.containsKey("smp") && !"auto".equals(from.get("smp"))) {
builder.smp(new Integer(from.get("smp")));
}
builder.cpu(Integer.parseInt(from.get("cpu")));
builder.mem(Integer.parseInt(from.get("mem")));
builder.user(from.get("user"));
if (from.containsKey("started"))
builder.started(new Date(new Long(from.get("started"))));
builder.uuid(from.get("server"));
builder.vnc(new VNC(from.get("vnc:ip"), from.get("vnc:password"), from.containsKey("vnc:tls")
&& Boolean.valueOf(from.get("vnc:tls"))));
if (from.containsKey("boot"))
builder.bootDeviceIds(Splitter.on(' ').split(from.get("boot")));
builder.vnc(new VNC(from.get("vnc:ip") == null ? "auto" : from.get("vnc:ip"), from.get("vnc:password"), from.containsKey("vnc:tls")
&& Boolean.valueOf(from.get("vnc:tls"))));
Map<String, String> metadata = Maps.newLinkedHashMap();
for (Entry<String, String> entry : from.entrySet()) {
if (entry.getKey().startsWith("user:"))

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.elasticstack.domain.Device;
@ -32,13 +33,22 @@ import org.jclouds.elasticstack.domain.Server;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import org.jclouds.rest.annotations.ApiVersion;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ServerToMap implements Function<Server, Map<String, String>> {
@ApiVersion
private final String apiVersion;
@Inject
public ServerToMap(@ApiVersion String apiVersion) {
this.apiVersion = apiVersion;
}
@Override
public Map<String, String> apply(Server from) {
checkNotNull(from, "server");
@ -68,7 +78,14 @@ public class ServerToMap implements Function<Server, Map<String, String>> {
builder.put("nic:" + nicId + ":mac", nic.getMac());
nicId++;
}
builder.put("vnc:ip", from.getVnc().getIp() == null ? "auto" : from.getVnc().getIp());
String vncIp = from.getVnc().getIp();
if (apiVersion.equals("2.0")) {
builder.put("vnc", "auto");
} else {
builder.put("vnc:ip", vncIp == null ? "auto" : vncIp);
}
if (from.getVnc().getPassword() != null)
builder.put("vnc:password", from.getVnc().getPassword());
if (from.getVnc().isTls())

View File

@ -18,30 +18,26 @@
*/
package org.jclouds.elasticstack.binders;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import org.jclouds.elasticstack.domain.IDEDevice;
import org.jclouds.elasticstack.domain.Model;
import org.jclouds.elasticstack.domain.NIC;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.VNC;
import org.jclouds.elasticstack.functions.ServerToMap;
import org.jclouds.http.HttpRequest;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import org.jclouds.elasticstack.domain.*;
import org.jclouds.elasticstack.functions.ServerToMap;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.annotations.ApiVersion;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import static org.testng.Assert.assertEquals;
/**
*
@ -69,19 +65,20 @@ public class BindServerToPlainTextStringTest {
.nics(ImmutableSet.of(new NIC.Builder().model(Model.E1000).
build())).vnc(new VNC(null, "XXXXXXXX", false)).build();
private static final BindServerToPlainTextString FN = Guice.createInjector(new AbstractModule() {
private Injector i = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(ApiVersion.class).to("1.0");
bind(new TypeLiteral<Function<Server, Map<String, String>>>() {
}).to(ServerToMap.class);
}
}).getInstance(BindServerToPlainTextString.class);
});
public void testSimple() throws IOException {
HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create"));
FN.bindToRequest(request, SERVER);
i.getInstance(BindServerToPlainTextString.class).bindToRequest(request, SERVER);
assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN);
assertEquals(request.getPayload().getRawContent(), CREATED_SERVER);
}

View File

@ -30,6 +30,7 @@ import org.jclouds.elasticstack.domain.ServerMetrics;
import org.jclouds.elasticstack.functions.MapToDevices.DeviceToId;
import org.jclouds.http.HttpResponse;
import org.jclouds.io.Payloads;
import org.jclouds.rest.annotations.ApiVersion;
import org.testng.annotations.Test;
import com.google.common.base.Function;
@ -48,6 +49,7 @@ public class KeyValuesDelimitedByBlankLinesToServerInfoTest {
@Override
protected void configure() {
bindConstant().annotatedWith(ApiVersion.class).to("1.0");
bind(new TypeLiteral<Function<Map<String, String>, List<NIC>>>() {
}).to(MapToNICs.class);
bind(new TypeLiteral<Function<Map<String, String>, Map<String, ? extends Device>>>() {

View File

@ -31,6 +31,7 @@ import org.jclouds.elasticstack.domain.ServerMetrics;
import org.jclouds.elasticstack.functions.MapToDevices.DeviceToId;
import org.jclouds.http.HttpResponse;
import org.jclouds.io.Payloads;
import org.jclouds.rest.annotations.ApiVersion;
import org.testng.annotations.Test;
import com.google.common.base.Function;
@ -51,6 +52,7 @@ public class ListOfKeyValuesDelimitedByBlankLinesToServerInfoSetTest {
@Override
protected void configure() {
bindConstant().annotatedWith(ApiVersion.class).to("1.0");
bind(new TypeLiteral<Function<Map<String, String>, List<NIC>>>() {
}).to(MapToNICs.class);
bind(new TypeLiteral<Function<Map<String, String>, Map<String, ? extends Device>>>() {

View File

@ -42,7 +42,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
*
*
* @author Adrian Cole
*/
@Test(groups = { "unit" })
@ -102,7 +102,7 @@ public class MapToServerInfoTest {
.build()).build();
private static final MapToServerInfo MAP_TO_DRIVE = new MapToServerInfo(new MapToDevices(new DeviceToId()),
new MapToServerMetrics(new MapToDriveMetrics()), new MapToNICs());
new MapToServerMetrics(new MapToDriveMetrics()), new MapToNICs("1.0"));
public void testEmptyMapReturnsNull() {
assertEquals(MAP_TO_DRIVE.apply(ImmutableMap.<String, String> of()), null);
@ -158,4 +158,14 @@ public class MapToServerInfoTest {
assertEquals(MAP_TO_DRIVE.apply(input), NEW);
}
public void testNew2() throws IOException {
Map<String, String> input = new ListOfKeyValuesDelimitedByBlankLinesToListOfMaps().apply(
Strings2.toStringAndClose(MapToServerInfoTest.class.getResourceAsStream("/new_server2.txt"))).get(0);
assertEquals(MAP_TO_DRIVE.apply(input), NEW);
}
}

View File

@ -31,15 +31,16 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
*
*
* @author Adrian Cole
*/
@Test(groups = { "unit" })
public class ServerToMapTest {
private static final ServerToMap SERVER_TO_MAP = new ServerToMap();
private static final ServerToMap SERVER_TO_MAP = new ServerToMap("1.0");
private static final ServerToMap SERVER_TO_MAP_V2 = new ServerToMap("2.0");
public void testBasics() {
public void testBasics() {
assertEquals(
SERVER_TO_MAP.apply(new Server.Builder()
.name("TestServer")
@ -62,4 +63,27 @@ public class ServerToMapTest {
"vnc:password", "XXXXXXXX")).build());
}
public void testBasicsV2() {
assertEquals(
SERVER_TO_MAP_V2.apply(new Server.Builder()
.name("TestServer")
.cpu(2000)
.mem(1024)
.devices(
ImmutableMap.of("ide:0:0",
new IDEDevice.Builder(0, 0).uuid("08c92dd5-70a0-4f51-83d2-835919d254df").build()))
.bootDeviceIds(ImmutableSet.of("ide:0:0")).nics(ImmutableSet.of(new NIC.Builder().model(Model.E1000).
build())).vnc(new VNC(null, "XXXXXXXX", false)).build()),
ImmutableMap
.builder()
.putAll(ImmutableMap.of("name", "TestServer", "cpu", "2000", "smp", "auto", "mem", "1024"))
.putAll(
ImmutableMap.of("persistent", "false", "boot", "ide:0:0", "ide:0:0",
"08c92dd5-70a0-4f51-83d2-835919d254df"))
.putAll(
ImmutableMap.of("ide:0:0:media", "disk", "nic:0:model", "e1000", "vnc", "auto",
"vnc:password", "XXXXXXXX")).build());
}
}

View File

@ -0,0 +1,25 @@
ide:0:0:write:requests 0
boot ide:0:0
vnc:password XXXXXXXX
ide:0:0 403c9a86-0aab-4e47-aa95-e9768021c4c1
ide:0:0:read:requests 0
ide:0:0:read:bytes 0
vnc auto
vnc:ip 83.222.249.221
tx:packets 0
tx 0
rx 0
smp 1
mem 512
nic:0:model e1000
status active
started 1292695612
rx:packets 0
user 2f6244eb-50bc-4403-847e-f03cc3706a1f
ide:0:0:media disk
name adriancole.test
persistent true
nic:0:block tcp/43594 tcp/5902 udp/5060 tcp/5900 tcp/5901 tcp/21 tcp/22 tcp/23 tcp/25 tcp/110 tcp/143 tcp/43595
server bd98615a-6f74-4d63-ad1e-b13338b9356a
ide:0:0:write:bytes 0
cpu 1000

View File

@ -0,0 +1,41 @@
ide:0:0:write:requests 3698
boot ide:0:0
vnc:password HfHzVmLT
ide:0:0 4af85ed3-0caa-4736-8a26-a33d7de0a122
ide:0:0:read:requests 11154
ide:0:0:read:bytes 45686784
vnc:ip 46.20.114.124
tx:packets 31
tx 2550
rx 455530
smp 1
mem 1024
nic:0:model e1000
status active
started 1291493868
rx:packets 7583
user 2f6244eb-50bc-4403-847e-f03cc3706a1f
name jo
persistent true
nic:0:block tcp/43594 tcp/5902 udp/5060 tcp/5900 tcp/5901 tcp/21 tcp/22 tcp/23 tcp/25 tcp/110 tcp/143 tcp/43595
server f8bee9cd-8e4b-4a05-8593-1314e3bfe49b
nic:0:dhcp auto
nic:0:dhcp:ip 46.20.114.124
ide:0:0:write:bytes 15147008
cpu 2000
status stopped
name Demo
mem 1024
boot ide:0:0
vnc:password HWbjvrg2
persistent true
server 0f962616-2071-4173-be79-7dd084271edf
smp auto
nic:0:dhcp auto
user 2f6244eb-50bc-4403-847e-f03cc3706a1f
nic:0:model e1000
vnc:ip auto
ide:0:0 853bb98a-4fff-4c2f-a265-97c363f19ea5
cpu 2000
ide:0:0:media cdrom

View File

@ -37,7 +37,7 @@ public class ElasticHostsBlueSquareLondonPropertiesBuilder extends ElasticStackP
Properties properties = super.defaultProperties();
properties.setProperty(PROPERTY_ISO3166_CODES, "GB-LND");
properties.setProperty(PROPERTY_ENDPOINT, "https://api.lon-b.elastichosts.com");
properties.setProperty(PROPERTY_API_VERSION, "1.0");
properties.setProperty(PROPERTY_API_VERSION, "2.0");
return properties;
}