From 1db5d38a09f577a0edd3481810b8fb0c01e72a19 Mon Sep 17 00:00:00 2001 From: Everett Toews Date: Tue, 24 Jun 2014 15:30:37 -0500 Subject: [PATCH] Fixed NPE when Server has no Image --- .../functions/ServerInZoneToNodeMetadata.java | 18 ++++++++++--- .../nova/v2_0/config/NovaParserModule.java | 11 +++----- .../ServerInZoneToNodeMetadataTest.java | 27 ++++++++++++++++++- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java index 4029fd0929..57d66c270d 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadata.java @@ -104,7 +104,11 @@ public class ServerInZoneToNodeMetadata implements Function T findObjectOfTypeForServerOrNull(Set supply, String type, diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/config/NovaParserModule.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/config/NovaParserModule.java index 9cc5aef86f..f35dba88e3 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/config/NovaParserModule.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/config/NovaParserModule.java @@ -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" diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java index d02f179457..d0cedfe8f6 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInZoneToNodeMetadataTest.java @@ -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.> ofInstance(ImmutableSet.of(existingImage)), + Suppliers.> 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) {