Fixed NPE when Server has no Image

This commit is contained in:
Everett Toews 2014-06-24 15:30:37 -05:00
parent a68eb38901
commit 1db5d38a09
3 changed files with 43 additions and 13 deletions

View File

@ -104,7 +104,11 @@ public class ServerInZoneToNodeMetadata implements Function<ServerInZone, NodeMe
.description(from.getHostId()).parent(zone).build() : zone);
builder.group(groupFromMapOrName(from.getMetadata(), from.getName(), nodeNamingConvention));
addMetadataAndParseTagsFromCommaDelimitedValue(builder, from.getMetadata());
builder.imageId(ZoneAndId.fromZoneAndId(serverInZone.getZone(), from.getImage().getId()).slashEncode());
if (from.getImage() != null) {
builder.imageId(ZoneAndId.fromZoneAndId(serverInZone.getZone(), from.getImage().getId()).slashEncode());
}
builder.operatingSystem(findOperatingSystemForServerOrNull(serverInZone));
builder.hardware(findHardwareForServerOrNull(serverInZone));
builder.status(toPortableNodeStatus.get(from.getStatus()));
@ -174,9 +178,15 @@ public class ServerInZoneToNodeMetadata implements Function<ServerInZone, NodeMe
}
protected OperatingSystem findOperatingSystemForServerOrNull(ServerInZone serverInZone) {
Image image = findObjectOfTypeForServerOrNull(images.get(), "image", serverInZone.getServer().getImage().getId(),
serverInZone);
return (image != null) ? image.getOperatingSystem() : null;
if (serverInZone.getServer().getImage() != null) {
Image image = findObjectOfTypeForServerOrNull(
images.get(), "image", serverInZone.getServer().getImage().getId(), serverInZone);
return (image != null) ? image.getOperatingSystem() : null;
} else {
return null;
}
}
public <T extends ComputeMetadata> T findObjectOfTypeForServerOrNull(Set<? extends T> supply, String type,

View File

@ -122,13 +122,12 @@ public class NovaParserModule extends AbstractModule {
@Override
public Server deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
Server serverBase = null;
Server serverBase;
// Servers can be created without an image so test if an image object is returned
if (jsonElement.getAsJsonObject().get("image").isJsonObject()) {
serverBase = apply((ServerInternal) context.deserialize(jsonElement, ServerInternal.class));
}
else {
} else {
serverBase = apply((ServerInternalWithoutImage) context.deserialize(jsonElement, ServerInternalWithoutImage.class));
}
@ -144,7 +143,7 @@ public class NovaParserModule extends AbstractModule {
return result.build();
}
public Server apply(ServerInternal in) {
public Server apply(Server in) {
return in.toBuilder().build();
}
@ -161,10 +160,6 @@ public class NovaParserModule extends AbstractModule {
}
}
public Server apply(ServerInternalWithoutImage in) {
return in.toBuilder().build();
}
private static class ServerInternalWithoutImage extends Server {
@ConstructorProperties({
"id", "name", "links", "uuid", "tenant_id", "user_id", "updated", "created", "hostId", "accessIPv4", "accessIPv6", "status", "flavor", "key_name", "config_drive", "addresses", "metadata", "extendedStatus", "extendedAttributes", "OS-DCF:diskConfig"

View File

@ -18,6 +18,7 @@ package org.jclouds.openstack.nova.v2_0.compute.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import java.net.URI;
import java.util.Map;
@ -39,6 +40,7 @@ import org.jclouds.openstack.nova.v2_0.compute.config.NovaComputeServiceContextM
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.ServerInZone;
import org.jclouds.openstack.nova.v2_0.parse.ParseServerTest;
import org.jclouds.openstack.nova.v2_0.parse.ParseServerWithoutImageTest;
import org.jclouds.openstack.v2_0.domain.Link;
import org.jclouds.openstack.v2_0.domain.Resource;
import org.testng.annotations.Test;
@ -201,7 +203,30 @@ public class ServerInZoneToNodeMetadataTest {
}
}
// TODO: clean up this syntax
@Test
public void testServerWithoutImage() {
Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
.providerId("52415800-8b69-11e0-9b19-734f216543fd").location(zone).build();
Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
.operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
.providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
.location(zone).build();
Server serverToConvert = new ParseServerWithoutImageTest().expected();
ServerInZone serverInZoneToConvert = new ServerInZone(serverToConvert, "az-1.region-a.geo-1");
ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata(
NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex,
Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(existingImage)),
Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet.of(existingHardware)),
namingConvention);
NodeMetadata convertedNodeMetadata = converter.apply(serverInZoneToConvert);
assertNull(convertedNodeMetadata.getImageId());
}
// TODO: clean up this syntax
private void checkHardwareAndImageStatus(Hardware expectedHardware, Hardware existingHardware,
String expectedImageId, OperatingSystem expectedOs, Image existingImage) {