Fix for JSON parse error on createNodesInGroup (JCLOUDS-558)

This commit is contained in:
Everett Toews 2014-06-19 15:19:18 -05:00
parent 3d4d5aded8
commit b33825441f
4 changed files with 202 additions and 3 deletions

View File

@ -126,7 +126,16 @@ public class NovaParserModule extends AbstractModule {
@Override
public Server deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
Server serverBase = apply((ServerInternal) context.deserialize(jsonElement, ServerInternal.class));
Server serverBase = null;
// 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 {
serverBase = apply((ServerInternalWithoutImage) context.deserialize(jsonElement, ServerInternalWithoutImage.class));
}
Server.Builder<?> result = Server.builder().fromServer(serverBase);
ServerExtendedStatus extendedStatus = context.deserialize(jsonElement, ServerExtendedStatus.class);
if (!Objects.equal(extendedStatus, ServerExtendedStatus.builder().build())) {
@ -155,5 +164,22 @@ public class NovaParserModule extends AbstractModule {
super(id, name, links, uuid, tenantId, userId, updated, created, hostId, accessIPv4, accessIPv6, status, image, flavor, keyName, configDrive, addresses, metadata, extendedStatus, extendedAttributes, diskConfig);
}
}
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"
})
protected ServerInternalWithoutImage(String id, @Nullable String name, java.util.Set<Link> links, @Nullable String uuid, String tenantId,
String userId, Date updated, Date created, @Nullable String hostId, @Nullable String accessIPv4,
@Nullable String accessIPv6, Server.Status status, Resource flavor, @Nullable String keyName,
@Nullable String configDrive, Multimap<String, Address> addresses, Map<String, String> metadata,
@Nullable ServerExtendedStatus extendedStatus, @Nullable ServerExtendedAttributes extendedAttributes, @Nullable String diskConfig) {
super(id, name, links, uuid, tenantId, userId, updated, created, hostId, accessIPv4, accessIPv6, status, null, flavor, keyName, configDrive, addresses, metadata, extendedStatus, extendedAttributes, diskConfig);
}
}
}
}

View File

@ -319,7 +319,7 @@ public class Server extends Resource {
})
protected Server(String id, @Nullable String name, java.util.Set<Link> links, @Nullable String uuid, String tenantId,
String userId, @Nullable Date updated, Date created, @Nullable String hostId, @Nullable String accessIPv4,
@Nullable String accessIPv6, Server.Status status, Resource image, Resource flavor, @Nullable String keyName,
@Nullable String accessIPv6, Server.Status status, @Nullable Resource image, Resource flavor, @Nullable String keyName,
@Nullable String configDrive, Multimap<String, Address> addresses, Map<String, String> metadata,
@Nullable ServerExtendedStatus extendedStatus, @Nullable ServerExtendedAttributes extendedAttributes,
@Nullable String diskConfig) {
@ -333,7 +333,7 @@ public class Server extends Resource {
this.accessIPv4 = Strings.emptyToNull(accessIPv4);
this.accessIPv6 = Strings.emptyToNull(accessIPv6);
this.status = checkNotNull(status, "status");
this.image = checkNotNull(image, "image");
this.image = image;
this.flavor = checkNotNull(flavor, "flavor");
this.keyName = Strings.emptyToNull(keyName);
this.configDrive = Strings.emptyToNull(configDrive);

View File

@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.openstack.nova.v2_0.parse;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.json.BaseItemParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.openstack.nova.v2_0.config.NovaParserModule;
import org.jclouds.openstack.nova.v2_0.domain.Address;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.domain.Server.Status;
import org.jclouds.openstack.v2_0.domain.Link;
import org.jclouds.openstack.v2_0.domain.Link.Relation;
import org.jclouds.openstack.v2_0.domain.Resource;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import java.net.URI;
import static org.jclouds.openstack.nova.v2_0.domain.Address.createV4;
import static org.jclouds.openstack.nova.v2_0.domain.Address.createV6;
@Test(groups = "unit", testName = "ParseServerWithoutImageTest")
public class ParseServerWithoutImageTest extends BaseItemParserTest<Server> {
@Override
public String resource() {
return "/server_details_without_image.json";
}
@Override
@SelectJson("server")
@Consumes(MediaType.APPLICATION_JSON)
public Server expected() {
return Server
.builder()
.id("52415800-8b69-11e0-9b19-734f000004d2")
.tenantId("1234")
.userId("5678")
.name("sample-f352")
.updated(new SimpleDateFormatDateService().iso8601SecondsDateParse("2010-10-10T12:00:00Z"))
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2010-08-10T12:00:00Z"))
.hostId("e4d909c290d0fb1ca068ffaddf22cbd0")
.accessIPv4("67.23.10.132")
.accessIPv6("::babe:67.23.10.132")
.status(Status.BUILD)
.diskConfig(Server.DISK_CONFIG_AUTO)
.flavor(
Resource
.builder()
.id("52415800-8b69-11e0-9b19-734f216543fd")
.name("null")
.links(
Link.create(
Relation.SELF,
URI.create("http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd")),
Link.create(
Relation.BOOKMARK,
URI.create("http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd")))
.build())
.metadata(
new ImmutableMap.Builder<String, String>().put("Server Label", "Web Head 1")
.put("Image Version", "2.1").build())
.addresses(ImmutableMultimap.<String, Address>builder()
.putAll("public", createV4("67.23.10.132"), createV6("::babe:67.23.10.132"), createV4("67.23.10.131"), createV6("::babe:4317:0A83"))
.putAll("private", createV4("10.176.42.16"), createV6("::babe:10.176.42.16"))
.build())
.links(Link.create(
Relation.SELF, URI.create("http://servers.api.openstack.org/v1.1/1234/servers/52415800-8b69-11e0-9b19-734f6f006e54")),
Link.create(
Relation.BOOKMARK,
URI.create("http://servers.api.openstack.org/1234/servers/52415800-8b69-11e0-9b19-734f6f006e54")))
.build();
}
protected Injector injector() {
return Guice.createInjector(new NovaParserModule(), new GsonModule());
}
}

View File

@ -0,0 +1,74 @@
{
"server": {
"id": "52415800-8b69-11e0-9b19-734f000004d2",
"tenant_id": "1234",
"user_id": "5678",
"name": "sample-f352",
"updated": "2010-10-10T12:00:00Z",
"created": "2010-08-10T12:00:00Z",
"hostId": "e4d909c290d0fb1ca068ffaddf22cbd0",
"accessIPv4" : "67.23.10.132",
"accessIPv6" : "::babe:67.23.10.132",
"status": "BUILD(scheduling)",
"progress": 60,
"OS-DCF:diskConfig": "AUTO",
"image" : "",
"flavor" : {
"id": "52415800-8b69-11e0-9b19-734f216543fd",
"links": [
{
"rel": "self",
"href": "http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd"
},
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd"
}
]
},
"addresses": {
"public" : [
{
"version": 4,
"addr": "67.23.10.132"
},
{
"version": 6,
"addr": "::babe:67.23.10.132"
},
{
"version": 4,
"addr": "67.23.10.131"
},
{
"version": 6,
"addr": "::babe:4317:0A83"
}
],
"private" : [
{
"version": 4,
"addr": "10.176.42.16"
},
{
"version": 6,
"addr": "::babe:10.176.42.16"
}
]
},
"metadata": {
"Server Label": "Web Head 1",
"Image Version": "2.1"
},
"links": [
{
"rel": "self",
"href": "http://servers.api.openstack.org/v1.1/1234/servers/52415800-8b69-11e0-9b19-734f6f006e54"
},
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/servers/52415800-8b69-11e0-9b19-734f6f006e54"
}
]
}
}