From a4b6c0c46ebae8ae9dd4de31d38e367c8e63061d Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 11 Mar 2012 10:51:32 -0700 Subject: [PATCH] create/update media is a media operation, not a vdc one; copied methods over to media client --- .../v1_5/features/MediaAsyncClient.java | 25 ++++++ .../director/v1_5/features/MediaClient.java | 17 ++++ .../v1_5/features/MediaClientExpectTest.java | 12 +-- .../v1_5/features/MediaClientLiveTest.java | 23 +++-- .../v1_5/features/VdcClientExpectTest.java | 85 +++++++++++++++---- 5 files changed, 131 insertions(+), 31 deletions(-) diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaAsyncClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaAsyncClient.java index 9f19cc5375..3d6b9ef570 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaAsyncClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaAsyncClient.java @@ -23,6 +23,7 @@ import java.net.URI; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -35,6 +36,7 @@ import org.jclouds.rest.annotations.JAXBResponseParser; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.binders.BindToXMLPayload; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; +import org.jclouds.vcloud.director.v1_5.domain.CloneMediaParams; import org.jclouds.vcloud.director.v1_5.domain.Media; import org.jclouds.vcloud.director.v1_5.domain.Metadata; import org.jclouds.vcloud.director.v1_5.domain.Owner; @@ -60,6 +62,29 @@ public interface MediaAsyncClient { @ExceptionParser(ThrowVCloudErrorOn4xx.class) ListenableFuture getMedia(@EndpointParam URI uri); + /** + * @see MediaClient#createMedia(URI, Media) + */ + @POST + @Consumes(VCloudDirectorMediaType.MEDIA) + @Produces(VCloudDirectorMediaType.MEDIA) + @JAXBResponseParser + @ExceptionParser(ThrowVCloudErrorOn4xx.class) + ListenableFuture createMedia(@EndpointParam URI link, + @BinderParam(BindToXMLPayload.class) Media media); + + + /** + * @see MediaClient#cloneMedia(URI, CloneMediaParams) + */ + @POST + @Consumes(VCloudDirectorMediaType.MEDIA) + @Produces(VCloudDirectorMediaType.CLONE_MEDIA_PARAMS) + @JAXBResponseParser + @ExceptionParser(ThrowVCloudErrorOn4xx.class) + ListenableFuture cloneMedia(@EndpointParam URI cloneLink, + @BinderParam(BindToXMLPayload.class) CloneMediaParams params); + /** * @see MediaClient#updateMedia(URI, Media)) */ diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaClient.java index c779ac046f..b5445757f3 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/MediaClient.java @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; import org.jclouds.rest.annotations.Delegate; +import org.jclouds.vcloud.director.v1_5.domain.CloneMediaParams; import org.jclouds.vcloud.director.v1_5.domain.Media; import org.jclouds.vcloud.director.v1_5.domain.Metadata; import org.jclouds.vcloud.director.v1_5.domain.Owner; @@ -46,6 +47,22 @@ public interface MediaClient { */ Media getMedia(URI mediaUri); + /** + * Creates a media (and present upload link for the floppy/iso file). + * + * @return The response will return a link to transfer site to be able to continue with uploading the media. + */ + Media createMedia(URI uploadLink, Media media); + + /** + * Clones a media into new one. + * The status of the returned media is UNRESOLVED(0) until the task for cloning finish. + * + * @return a Media resource which will contain a task. + * The user should monitor the contained task status in order to check when it is completed. + */ + Media cloneMedia(URI cloneLink, CloneMediaParams params); + /** * Updates the name/description of a media. * diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java index 00d2f18ee4..6340a5733f 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java @@ -41,7 +41,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes @Test public void testCreateMedia() { - URI vdcUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f"); + URI uploadLink = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/media"); VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, new VcloudHttpRequestPrimer() @@ -62,12 +62,12 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes .build(); Media expected = createMedia(); - assertEquals(client.getVdcClient().createMedia(vdcUri, source), expected); + assertEquals(client.getMediaClient().createMedia(uploadLink, source), expected); } @Test public void testCloneMedia() { - URI vdcUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f"); + URI cloneUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/action/cloneMedia"); VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, new VcloudHttpRequestPrimer() @@ -92,7 +92,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes .build(); Media expected = cloneMedia(); - assertEquals(client.getVdcClient().cloneMedia(vdcUri, params), expected); + assertEquals(client.getMediaClient().cloneMedia(cloneUri, params), expected); } @Test @@ -351,7 +351,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes assertEquals(client.getMediaClient().getOwner(mediaUri), expected); } - private static Media createMedia() { + static Media createMedia() { return Media.builder() .size(0) .imageType("iso") @@ -385,7 +385,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes .build(); } - private static Media cloneMedia() { + static Media cloneMedia() { return Media.builder() .size(175163392) .imageType("iso") diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java index 732917d92f..5e87f56aaf 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java @@ -19,6 +19,10 @@ package org.jclouds.vcloud.director.v1_5.features; import static com.google.common.base.Objects.equal; +import static com.google.common.base.Predicates.and; +import static com.google.common.collect.Iterables.find; +import static com.google.common.collect.Iterables.getFirst; +import static com.google.common.collect.Iterables.isEmpty; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.GETTER_RETURNS_SAME_OBJ; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_DEL; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_DEL; @@ -31,6 +35,8 @@ import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.O import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.TASK_COMPLETE_TIMELY; +import static org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates.relEquals; +import static org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates.typeEquals; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; @@ -55,6 +61,7 @@ import org.jclouds.vcloud.director.v1_5.domain.MetadataValue; import org.jclouds.vcloud.director.v1_5.domain.Owner; import org.jclouds.vcloud.director.v1_5.domain.Reference; import org.jclouds.vcloud.director.v1_5.domain.Task; +import org.jclouds.vcloud.director.v1_5.domain.Vdc; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -98,7 +105,9 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { @Test(testName = "POST /vdc/{id}/media", enabled = false) public void testCreateMedia() throws URISyntaxException { assertNotNull(vdcURI, String.format(REF_REQ_LIVE, VDC)); - assertNotNull(vdcClient.getVdc(vdcURI), String.format(OBJ_REQ_LIVE, VDC)); + Vdc vdc = vdcClient.getVdc(vdcURI); + assertNotNull(vdc, String.format(OBJ_REQ_LIVE, VDC)); + Link addMedia = find(vdc.getLinks(), and(relEquals("add"), typeEquals(VCloudDirectorMediaType.MEDIA))); java.io.File sourceFile = new java.io.File(getClass().getResource("/media/test.iso").toURI()); @@ -109,14 +118,14 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { .imageType(Media.ImageType.ISO) .description("Test media generated by testCreateMedia()") .build(); - media = vdcClient.createMedia(vdcURI, sourceMedia); + media = mediaClient.createMedia(addMedia.getHref(), sourceMedia); Checks.checkMediaFor(MEDIA, media); assertNotNull(media.getFiles(), String.format(OBJ_FIELD_REQ, MEDIA, "files")); assertTrue(media.getFiles().getFiles().size() == 1, String.format(OBJ_FIELD_EQ, MEDIA, "files.size()", "1", media.getFiles().getFiles().size())); - File uploadFile = Iterables.getFirst(media.getFiles().getFiles(), null); + File uploadFile = getFirst(media.getFiles().getFiles(), null); assertNotNull(uploadFile, String.format(OBJ_FIELD_REQ, MEDIA, "files.first")); assertTrue(equal(uploadFile.getSize(), sourceMedia.getSize()), String.format(OBJ_FIELD_EQ, MEDIA, "uploadFile.size()", sourceMedia.getSize(), uploadFile.getSize())); @@ -125,7 +134,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { assertNotNull(links, String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links")); assertTrue(links.size() == 1, String.format(OBJ_FIELD_EQ, MEDIA, "uploadfile.links.size()", "1", links.size())); - Link uploadLink = Iterables.getFirst(links, null); + Link uploadLink = getFirst(links, null); assertTrue(equal(uploadLink.getRel(), Link.Rel.UPLOAD_DEFAULT), String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links.first")); fail(); //TODO upload file and assert it succeeds @@ -172,7 +181,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { Checks.checkMediaFor(VDC, media); if (media.getTasksInProgress() != null) { - Task copyTask = Iterables.getFirst(media.getTasksInProgress().getTasks(), null); + Task copyTask = getFirst(media.getTasksInProgress().getTasks(), null); if (copyTask != null) { Checks.checkTask(copyTask); assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask")); @@ -196,7 +205,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { Checks.checkMediaFor(VDC, media); if (media.getTasksInProgress() != null) { - Task copyTask = Iterables.getFirst(media.getTasksInProgress().getTasks(), null); + Task copyTask = getFirst(media.getTasksInProgress().getTasks(), null); if (copyTask != null) { Checks.checkTask(copyTask); assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask")); @@ -244,7 +253,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { public void testGetMetadata() { metadata = mediaClient.getMetadataClient().getMetadata(media.getHref()); // required for testing - assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()), + assertFalse(isEmpty(metadata.getMetadataEntries()), String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "metadata.entries")); Checks.checkMetadataFor(MEDIA, metadata); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java index 5dee93c46e..7421e8f9cd 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java @@ -26,8 +26,28 @@ import java.net.URI; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; -import org.jclouds.vcloud.director.v1_5.domain.*; +import org.jclouds.vcloud.director.v1_5.domain.AvailableNetworks; +import org.jclouds.vcloud.director.v1_5.domain.Capabilities; +import org.jclouds.vcloud.director.v1_5.domain.CapacityWithUsage; +import org.jclouds.vcloud.director.v1_5.domain.CaptureVAppParams; +import org.jclouds.vcloud.director.v1_5.domain.CloneMediaParams; +import org.jclouds.vcloud.director.v1_5.domain.CloneVAppParams; +import org.jclouds.vcloud.director.v1_5.domain.CloneVAppTemplateParams; +import org.jclouds.vcloud.director.v1_5.domain.ComposeVAppParams; +import org.jclouds.vcloud.director.v1_5.domain.ComputeCapacity; import org.jclouds.vcloud.director.v1_5.domain.Error; +import org.jclouds.vcloud.director.v1_5.domain.InstantiateVAppTemplateParams; +import org.jclouds.vcloud.director.v1_5.domain.Link; +import org.jclouds.vcloud.director.v1_5.domain.Media; +import org.jclouds.vcloud.director.v1_5.domain.Metadata; +import org.jclouds.vcloud.director.v1_5.domain.MetadataValue; +import org.jclouds.vcloud.director.v1_5.domain.Reference; +import org.jclouds.vcloud.director.v1_5.domain.ResourceEntities; +import org.jclouds.vcloud.director.v1_5.domain.SupportedHardwareVersions; +import org.jclouds.vcloud.director.v1_5.domain.UploadVAppTemplateParams; +import org.jclouds.vcloud.director.v1_5.domain.VApp; +import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate; +import org.jclouds.vcloud.director.v1_5.domain.Vdc; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorRestClientExpectTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -274,28 +294,62 @@ public class VdcClientExpectTest extends BaseVCloudDirectorRestClientExpectTest assertEquals(client.getVdcClient().uploadVAppTemplate(vdcURI, params), expected); } - @Test(enabled = false) + @Test public void testCreateMedia() { + URI vdcUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f"); + VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, new VcloudHttpRequestPrimer() - .apiCommand("POST", "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/action/media") - .xmlFilePayload("/vdc/params/createMedia.xml", VCloudDirectorMediaType.MEDIA) - .acceptAnyMedia() + .apiCommand("POST", "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/media") + .acceptMedia(VCloudDirectorMediaType.MEDIA) + .xmlFilePayload("/media/createMediaSource.xml", VCloudDirectorMediaType.MEDIA) .httpRequestBuilder().build(), new VcloudHttpResponsePrimer() - .xmlFilePayload("/vdc/createMedia.xml", VCloudDirectorMediaType.MEDIA) - .httpResponseBuilder().build()); + .xmlFilePayload("/media/createMedia.xml", VCloudDirectorMediaType.MEDIA) + .httpResponseBuilder().statusCode(201).build()); - Media expected = createMedia(); - - // TODO: configure params - Media createMedia = Media.builder() - + Media source = Media.builder() + .size(0) + .imageType("iso") + .name("Test media 1") + .type("application/vnd.vmware.vcloud.media+xml") + .description("Test media generated by testCreateMedia()") .build(); + Media expected = MediaClientExpectTest.createMedia(); - assertEquals(client.getVdcClient().createMedia(vdcURI, createMedia), expected); + assertEquals(client.getVdcClient().createMedia(vdcUri, source), expected); } + + @Test + public void testCloneMedia() { + URI vdcUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f"); + VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, + new VcloudHttpRequestPrimer() + .apiCommand("POST", "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/action/cloneMedia") + .acceptMedia(VCloudDirectorMediaType.MEDIA) + .xmlFilePayload("/media/cloneMediaParams.xml", VCloudDirectorMediaType.CLONE_MEDIA_PARAMS) + .httpRequestBuilder().build(), + new VcloudHttpResponsePrimer() + .xmlFilePayload("/media/cloneMedia.xml", VCloudDirectorMediaType.MEDIA) + .httpResponseBuilder().statusCode(201).build()); + + CloneMediaParams params = CloneMediaParams.builder() + .name("moved test media") + .description("moved by testCloneMedia()") + .source(Reference.builder() + .type("application/vnd.vmware.vcloud.media+xml") + .name("copied test media") + .id("urn:vcloud:media:da8361af-cccd-4103-a71c-493513c49094") + .href(URI.create("https://mycloud.greenhousedata.com/api/media/da8361af-cccd-4103-a71c-493513c49094")) + .build()) + .isSourceDelete(false) + .build(); + Media expected = MediaClientExpectTest.cloneMedia(); + + assertEquals(client.getVdcClient().cloneMedia(vdcUri, params), expected); + } + @Test(enabled = false) public void testGetMetadata() { URI vdcUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f"); @@ -492,11 +546,6 @@ public class VdcClientExpectTest extends BaseVCloudDirectorRestClientExpectTest return null; } - private Media createMedia() { - // TODO Auto-generated method stub - return null; - } - private Metadata metadata() { // TODO Auto-generated method stub return null;