diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/ISOAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/ISOAsyncClient.java index 940e0fe789..84a7bc526f 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/ISOAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/ISOAsyncClient.java @@ -127,7 +127,8 @@ public interface ISOAsyncClient { @GET @Consumes(MediaType.APPLICATION_JSON) @QueryParams(keys = "command", values = "registerIso") - @Unwrap + @SelectJson("iso") + @OnlyElement ListenableFuture registerISO(@QueryParam("name") String name, @QueryParam("displaytext") String displayText, @QueryParam("url") String url, @QueryParam("zoneid") String zoneId, RegisterISOOptions... options); /** diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOAsyncClientTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOAsyncClientTest.java index 11b3899380..014f95d207 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOAsyncClientTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOAsyncClientTest.java @@ -29,6 +29,7 @@ import org.jclouds.cloudstack.options.ExtractISOOptions; import org.jclouds.cloudstack.options.RegisterISOOptions; import org.jclouds.cloudstack.options.UpdateISOOptions; import org.jclouds.cloudstack.options.UpdateISOPermissionsOptions; +import org.jclouds.functions.IdentityFunction; import org.jclouds.http.HttpRequest; import org.jclouds.http.functions.ParseFirstJsonValueNamed; import org.jclouds.http.functions.ReleasePayloadAndReturn; @@ -37,6 +38,7 @@ import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.testng.annotations.Test; +import com.google.common.base.Functions; import com.google.common.collect.ImmutableSet; import com.google.inject.TypeLiteral; /** @@ -82,38 +84,6 @@ public class ISOAsyncClientTest extends BaseCloudStackAsyncClientTest { assertNull(client.getISO("018e0928-8205-4d8e-9329-f731a9ccd488")); } + HttpRequest registerIso = HttpRequest.builder().method("GET") + .endpoint("http://localhost:8080/client/api") + .addQueryParam("response", "json") + .addQueryParam("command", "registerIso") + .addQueryParam("name", "ubuntu10.10") + .addQueryParam("url", "http://ubuntu/ubuntu-10.10.iso") + .addQueryParam("displaytext", "ubuntu 10.10 (32 bit)") + .addQueryParam("zoneid", "1e0335d9-b6cc-4805-bddf-0828e66a0d01") + .addQueryParam("account", "root") + .addQueryParam("domainid", "99f4159b-c698-4bd9-b8c5-5ac462f101eb") + .addQueryParam("bootable", "true") + .addQueryParam("isextractable", "true") + .addQueryParam("isfeatured", "true") + .addQueryParam("ispublic", "true") + .addQueryParam("ostypeid", "1234-abcd") + .addQueryParam("apiKey", "identity") + .addQueryParam("signature", "YpFMYUUu0daLgwxNFubVfkV0Nw8%3D") + .addHeader("Accept", "application/json") + .build(); + + RegisterISOOptions registerISOOptions = RegisterISOOptions.Builder + .accountInDomain("root", "99f4159b-c698-4bd9-b8c5-5ac462f101eb") + .bootable(true).isExtractable(true).isFeatured(true).isPublic(true).osTypeId("1234-abcd"); + + @Test + public void testRegisterISOsWhenResponseIs2xx() { + ISOClient client = requestSendsResponse( + registerIso, + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/registerisoresponse.json")) + .build()); + + assertEquals(client.registerISO("ubuntu10.10", "ubuntu 10.10 (32 bit)", "http://ubuntu/ubuntu-10.10.iso", "1e0335d9-b6cc-4805-bddf-0828e66a0d01", + registerISOOptions), + ISO.builder().id("b52c509d-c6e2-452c-b6ec-aa00720ed6cd").name("ubuntu10.10").displayText("ubuntu 10.10 (32 bit)").isPublic(true) + .isReady(false).bootable(true).isFeatured(false).crossZones(false).osTypeId("0e0335d9-b6cc-4808-bddf-0828e66a0d03") + .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-08-21T15:45:01+0530")) + .osTypeName("Ubuntu 10.10 (32-bit)").account("root").domain("ROOT").domainid("99f4159b-c698-4bd9-b8c5-5ac462f101eb").status("") + .account("admin").zoneId("6f9a2921-b22a-4149-8b71-6ffc275a2177").zoneName("Basic1") + .isExtractable(false).build()); + } + + @Test(expectedExceptions=ResourceNotFoundException.class) + public void testRegisterISOWhenResponseIs404() { + ISOClient client = requestSendsResponse(registerIso, + HttpResponse.builder() + .statusCode(404) + .build()); + + assertNull(client.registerISO("ubuntu10.10", "ubuntu 10.10 (32 bit)", "http://ubuntu/ubuntu-10.10.iso", "1e0335d9-b6cc-4805-bddf-0828e66a0d01", + registerISOOptions)); + } @Override protected ISOClient clientFrom(CloudStackContext context) { diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOClientLiveTest.java index 46f1b5c55d..f603b16f2b 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/ISOClientLiveTest.java @@ -18,6 +18,7 @@ */ package org.jclouds.cloudstack.features; +import static org.jclouds.cloudstack.options.ListZonesOptions.Builder.available; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; @@ -27,10 +28,20 @@ import java.util.Set; import org.jclouds.cloudstack.domain.ISO; import org.jclouds.cloudstack.domain.ISOPermissions; +import org.jclouds.cloudstack.domain.OSType; +import org.jclouds.cloudstack.domain.Zone; import org.jclouds.cloudstack.internal.BaseCloudStackClientLiveTest; +import org.jclouds.cloudstack.options.DeleteISOOptions; import org.jclouds.cloudstack.options.ListISOsOptions; +import org.jclouds.cloudstack.options.RegisterISOOptions; +import org.testng.SkipException; +import org.testng.annotations.AfterClass; import org.testng.annotations.Test; +import com.google.common.base.Optional; +import com.google.common.base.Predicates; +import com.google.common.collect.Iterables; + /** * Tests behavior of {@link ISOClient} and {@link ISOAsyncClient} * @@ -38,6 +49,10 @@ import org.testng.annotations.Test; */ @Test(groups = "live", singleThreaded = true, testName = "ISOClientLiveTest") public class ISOClientLiveTest extends BaseCloudStackClientLiveTest { + + private static final String isoName = "jcloudsTestISO"; + private static final String url = System.getProperty("test.cloudstack.iso-url", "http://archive.ubuntu.com/ubuntu/dists/maverick/main/installer-i386/current/images/netboot/mini.iso"); + public void testListPublicISOs() throws Exception { Set response = client.getISOClient().listISOs(ListISOsOptions.Builder.isPublic()); assertNotNull(response); @@ -63,4 +78,35 @@ public class ISOClientLiveTest extends BaseCloudStackClientLiveTest { assertNotNull(perms); } } + + public void testRegisterISO() throws Exception { + Optional guestOSTypeOptional = Iterables.tryFind(client.getGuestOSClient().listOSTypes(), Predicates.notNull()); + Optional zoneOptional = Iterables.tryFind(client.getZoneClient().listZones(available(true)), Predicates.notNull()); + if(guestOSTypeOptional.isPresent() && zoneOptional.isPresent()) { + String osTypeId = guestOSTypeOptional.get().getId(); + String zoneId = zoneOptional.get().getId(); + ISO iso = client.getISOClient().registerISO(isoName, "", url, zoneId, RegisterISOOptions.Builder.isPublic(true).osTypeId(osTypeId)); + assertNotNull(iso); + assertNotNull(iso.getId()); + assertEquals(iso.getName(), isoName); + } else { + String skipMessage = String.format("Cannot register the iso with url: %s", url); + if(zoneOptional.isPresent()) + skipMessage += " without a valid zone"; + else + skipMessage += " without a valid guest OS type"; + throw new SkipException(skipMessage); + } + } + + @AfterClass + @Override + protected void tearDownContext() { + Set isos = client.getISOClient().listISOs(ListISOsOptions.Builder.name(isoName)); + for (ISO iso : isos) { + client.getISOClient().deleteISO(iso.getId(), DeleteISOOptions.NONE); + } + super.tearDownContext(); + } + } diff --git a/apis/cloudstack/src/test/resources/registerisoresponse.json b/apis/cloudstack/src/test/resources/registerisoresponse.json new file mode 100644 index 0000000000..c0be454b07 --- /dev/null +++ b/apis/cloudstack/src/test/resources/registerisoresponse.json @@ -0,0 +1,3 @@ +{ "registerisoresponse" : { "count":1 ,"iso" : [ +{"id":"b52c509d-c6e2-452c-b6ec-aa00720ed6cd","name":"ubuntu10.10","displaytext":"ubuntu 10.10 (32 bit)","ispublic":true,"created":"2012-08-21T15:45:01+0530","isready":false,"bootable":true,"isfeatured":false,"crossZones":false,"ostypeid":"0e0335d9-b6cc-4808-bddf-0828e66a0d03","ostypename":"Ubuntu 10.10 (32-bit)","account":"admin","zoneid":"6f9a2921-b22a-4149-8b71-6ffc275a2177","zonename":"Basic1","status":"","domain":"ROOT","domainid":"99f4159b-c698-4bd9-b8c5-5ac462f101eb","isextractable":false} ] +}}