diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/RebuildServerOptions.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/RebuildServerOptions.java index 111ab9b75d..e1f0d08c75 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/RebuildServerOptions.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/RebuildServerOptions.java @@ -31,21 +31,33 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; /** - * - * * @author Adrian Cole - * + * @author Inbar Stolberg */ public class RebuildServerOptions implements MapBinder { @Inject private BindToJsonPayload jsonBinder; String imageRef; + String name; + String adminPass; + String accessIPv4; + String accessIPv6; @Override public R bindToRequest(R request, Map postParams) { Map image = Maps.newHashMap(); if (imageRef != null) image.put("imageRef", imageRef); + if (name != null) + image.put("name", name); + if (adminPass != null) + image.put("adminPass", adminPass); + if (accessIPv4 != null) + image.put("accessIPv4", accessIPv4); + if (accessIPv6 != null) + image.put("accessIPv6", accessIPv6); + + return jsonBinder.bindToRequest(request, ImmutableMap.of("rebuild", image)); } @@ -65,6 +77,26 @@ public class RebuildServerOptions implements MapBinder { return this; } + public RebuildServerOptions name(String name) { + this.name = name; + return this; + } + + public RebuildServerOptions adminPass(String adminPass) { + this.adminPass = adminPass; + return this; + } + + public RebuildServerOptions ipv4Address(String ipv4Address) { + this.accessIPv4 = ipv4Address; + return this; + } + + public RebuildServerOptions ipv6Address(String iPv6Address) { + this.accessIPv6 = iPv6Address; + return this; + } + public static class Builder { /** @@ -74,5 +106,37 @@ public class RebuildServerOptions implements MapBinder { RebuildServerOptions options = new RebuildServerOptions(); return options.withImage(ref); } + + /** + * @see RebuildServerOptions#name(String) + */ + public static RebuildServerOptions name(String name) { + RebuildServerOptions options = new RebuildServerOptions(); + return options.name(name); + } + + /** + * @see RebuildServerOptions#adminPass(String) + */ + public static RebuildServerOptions adminPass(String adminPass) { + RebuildServerOptions options = new RebuildServerOptions(); + return options.adminPass(adminPass); + } + + /** + * @see RebuildServerOptions#ipv4Address(String) + */ + public static RebuildServerOptions ipv4Address(String ipv4Address) { + RebuildServerOptions options = new RebuildServerOptions(); + return options.ipv4Address(ipv4Address); + } + + /** + * @see RebuildServerOptions#ipv6Address(String) + */ + public static RebuildServerOptions ipv6Address(String ipv6Address) { + RebuildServerOptions options = new RebuildServerOptions(); + return options.ipv6Address(ipv6Address); + } } } diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java index 6778fa4134..8b2b0d1450 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java @@ -26,6 +26,7 @@ import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.openstack.nova.v2_0.domain.Server; import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiExpectTest; import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions; +import org.jclouds.openstack.nova.v2_0.options.RebuildServerOptions; import org.jclouds.openstack.nova.v2_0.parse.ParseCreatedServerTest; import org.jclouds.openstack.nova.v2_0.parse.ParseMetadataListTest; import org.jclouds.openstack.nova.v2_0.parse.ParseMetadataUpdateTest; @@ -229,6 +230,28 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { new ParseCreatedServerTest().expectedWithDiskConfig(Server.DISK_CONFIG_MANUAL).toString()); } + public void testRebuildServerWhenResponseIs202() throws Exception { + String serverId = "52415800-8b69-11e0-9b19-734f565bc83b"; + HttpRequest rebuildServer = HttpRequest + .builder() + .method("POST") + .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId +"/action") + .addHeader("Accept", "*/*") + .addHeader("X-Auth-Token", authToken) + .payload(payloadFromStringWithContentType( + "{\"rebuild\":{\"adminPass\":\"password\",\"imageRef\":\"1234\",\"name\":\"newName\",\"accessIPv4\":\"1.1.1.1\",\"accessIPv6\":\"fe80::100\"}}","application/json")) + .build(); + + HttpResponse rebuildServerResponse = HttpResponse.builder().statusCode(202).build(); + + NovaApi apiRebuildServer = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, + responseWithKeystoneAccess, rebuildServer, rebuildServerResponse); + + RebuildServerOptions options = new RebuildServerOptions().withImage("1234").name("newName").adminPass("password").ipv4Address("1.1.1.1").ipv6Address("fe80::100"); + + apiRebuildServer.getServerApiForZone("az-1.region-a.geo-1").rebuild(serverId, options); + } + public void testCreateImageWhenResponseIs2xx() throws Exception { String serverId = "123"; String imageId = "456"; diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiLiveTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiLiveTest.java index 492c98af34..7f8ea9c1c7 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiLiveTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiLiveTest.java @@ -25,6 +25,7 @@ import org.jclouds.openstack.nova.v2_0.domain.Server; import org.jclouds.openstack.nova.v2_0.domain.ServerCreated; import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiLiveTest; import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions; +import org.jclouds.openstack.nova.v2_0.options.RebuildServerOptions; import org.jclouds.openstack.v2_0.domain.Link.Relation; import org.jclouds.openstack.v2_0.domain.Resource; import org.jclouds.openstack.v2_0.predicates.LinkPredicates; @@ -105,6 +106,51 @@ public class ServerApiLiveTest extends BaseNovaApiLiveTest { } } + @Test + public void testRebuildServer() { + + String serverId = null; + + for (String zoneId : zones) { + ServerApi serverApi = api.getServerApiForZone(zoneId); + try { + serverId = createServer(zoneId, Server.Status.ACTIVE).getId(); + + Server server = serverApi.get(serverId); + + assertEquals(server.getStatus(), Server.Status.ACTIVE); + + RebuildServerOptions options = new RebuildServerOptions(). + withImage(server.getImage().getId()). + name("newName"). + adminPass("password"). + ipv4Address("1.1.1.1"). + ipv6Address("fe80::100"); + + serverApi.rebuild(serverId, options); + + Server rebuiltServer = serverApi.get(serverId); + + assertEquals("newName", rebuiltServer.getName()); + assertEquals("1.1.1.1", rebuiltServer.getAccessIPv4()); + assertEquals("fe80::100", rebuiltServer.getAccessIPv6()); + + } finally { + serverApi.delete(serverId); + } + } + } + + private Server createServer(String regionId, Server.Status serverStatus) { + ServerApi serverApi = api.getServerApiForZone(regionId); + CreateServerOptions options = new CreateServerOptions(); + ServerCreated server = serverApi.create(hostName, imageIdForZone(regionId), flavorRefForZone(regionId), options); + + blockUntilServerInState(server.getId(), serverApi, serverStatus); + + return serverApi.get(server.getId()); + } + private Server createServer(String regionId, String availabilityZoneId, Server.Status serverStatus) { ServerApi serverApi = api.getServerApiForZone(regionId); CreateServerOptions options = new CreateServerOptions();