create/update media is a media operation, not a vdc one; copied methods over to media client

This commit is contained in:
Adrian Cole 2012-03-11 10:51:32 -07:00
parent b917055930
commit a4b6c0c46e
5 changed files with 131 additions and 31 deletions

View File

@ -23,6 +23,7 @@ import java.net.URI;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; 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.annotations.RequestFilters;
import org.jclouds.rest.binders.BindToXMLPayload; import org.jclouds.rest.binders.BindToXMLPayload;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; 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.Media;
import org.jclouds.vcloud.director.v1_5.domain.Metadata; import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.Owner; import org.jclouds.vcloud.director.v1_5.domain.Owner;
@ -60,6 +62,29 @@ public interface MediaAsyncClient {
@ExceptionParser(ThrowVCloudErrorOn4xx.class) @ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Media> getMedia(@EndpointParam URI uri); ListenableFuture<Media> getMedia(@EndpointParam URI uri);
/**
* @see MediaClient#createMedia(URI, Media)
*/
@POST
@Consumes(VCloudDirectorMediaType.MEDIA)
@Produces(VCloudDirectorMediaType.MEDIA)
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Media> 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<Media> cloneMedia(@EndpointParam URI cloneLink,
@BinderParam(BindToXMLPayload.class) CloneMediaParams params);
/** /**
* @see MediaClient#updateMedia(URI, Media)) * @see MediaClient#updateMedia(URI, Media))
*/ */

View File

@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout; import org.jclouds.concurrent.Timeout;
import org.jclouds.rest.annotations.Delegate; 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.Media;
import org.jclouds.vcloud.director.v1_5.domain.Metadata; import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.Owner; import org.jclouds.vcloud.director.v1_5.domain.Owner;
@ -46,6 +47,22 @@ public interface MediaClient {
*/ */
Media getMedia(URI mediaUri); 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. * Updates the name/description of a media.
* *

View File

@ -41,7 +41,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
@Test @Test
public void testCreateMedia() { 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, VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
new VcloudHttpRequestPrimer() new VcloudHttpRequestPrimer()
@ -62,12 +62,12 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
.build(); .build();
Media expected = createMedia(); Media expected = createMedia();
assertEquals(client.getVdcClient().createMedia(vdcUri, source), expected); assertEquals(client.getMediaClient().createMedia(uploadLink, source), expected);
} }
@Test @Test
public void testCloneMedia() { 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, VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
new VcloudHttpRequestPrimer() new VcloudHttpRequestPrimer()
@ -92,7 +92,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
.build(); .build();
Media expected = cloneMedia(); Media expected = cloneMedia();
assertEquals(client.getVdcClient().cloneMedia(vdcUri, params), expected); assertEquals(client.getMediaClient().cloneMedia(cloneUri, params), expected);
} }
@Test @Test
@ -351,7 +351,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
assertEquals(client.getMediaClient().getOwner(mediaUri), expected); assertEquals(client.getMediaClient().getOwner(mediaUri), expected);
} }
private static Media createMedia() { static Media createMedia() {
return Media.builder() return Media.builder()
.size(0) .size(0)
.imageType("iso") .imageType("iso")
@ -385,7 +385,7 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
.build(); .build();
} }
private static Media cloneMedia() { static Media cloneMedia() {
return Media.builder() return Media.builder()
.size(175163392) .size(175163392)
.imageType("iso") .imageType("iso")

View File

@ -19,6 +19,10 @@
package org.jclouds.vcloud.director.v1_5.features; package org.jclouds.vcloud.director.v1_5.features;
import static com.google.common.base.Objects.equal; 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.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_DEL;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_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.OBJ_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_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.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.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; 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.Owner;
import org.jclouds.vcloud.director.v1_5.domain.Reference; 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.Task;
import org.jclouds.vcloud.director.v1_5.domain.Vdc;
import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -98,7 +105,9 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
@Test(testName = "POST /vdc/{id}/media", enabled = false) @Test(testName = "POST /vdc/{id}/media", enabled = false)
public void testCreateMedia() throws URISyntaxException { public void testCreateMedia() throws URISyntaxException {
assertNotNull(vdcURI, String.format(REF_REQ_LIVE, VDC)); 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()); 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) .imageType(Media.ImageType.ISO)
.description("Test media generated by testCreateMedia()") .description("Test media generated by testCreateMedia()")
.build(); .build();
media = vdcClient.createMedia(vdcURI, sourceMedia); media = mediaClient.createMedia(addMedia.getHref(), sourceMedia);
Checks.checkMediaFor(MEDIA, media); Checks.checkMediaFor(MEDIA, media);
assertNotNull(media.getFiles(), String.format(OBJ_FIELD_REQ, MEDIA, "files")); 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", assertTrue(media.getFiles().getFiles().size() == 1, String.format(OBJ_FIELD_EQ, MEDIA, "files.size()", "1",
media.getFiles().getFiles().size())); 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")); assertNotNull(uploadFile, String.format(OBJ_FIELD_REQ, MEDIA, "files.first"));
assertTrue(equal(uploadFile.getSize(), sourceMedia.getSize()), String.format(OBJ_FIELD_EQ, MEDIA, "uploadFile.size()", assertTrue(equal(uploadFile.getSize(), sourceMedia.getSize()), String.format(OBJ_FIELD_EQ, MEDIA, "uploadFile.size()",
sourceMedia.getSize(), uploadFile.getSize())); sourceMedia.getSize(), uploadFile.getSize()));
@ -125,7 +134,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
assertNotNull(links, String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links")); assertNotNull(links, String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links"));
assertTrue(links.size() == 1, String.format(OBJ_FIELD_EQ, MEDIA, "uploadfile.links.size()", "1", assertTrue(links.size() == 1, String.format(OBJ_FIELD_EQ, MEDIA, "uploadfile.links.size()", "1",
links.size())); 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")); 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 fail(); //TODO upload file and assert it succeeds
@ -172,7 +181,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
Checks.checkMediaFor(VDC, media); Checks.checkMediaFor(VDC, media);
if (media.getTasksInProgress() != null) { if (media.getTasksInProgress() != null) {
Task copyTask = Iterables.getFirst(media.getTasksInProgress().getTasks(), null); Task copyTask = getFirst(media.getTasksInProgress().getTasks(), null);
if (copyTask != null) { if (copyTask != null) {
Checks.checkTask(copyTask); Checks.checkTask(copyTask);
assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask")); assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask"));
@ -196,7 +205,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
Checks.checkMediaFor(VDC, media); Checks.checkMediaFor(VDC, media);
if (media.getTasksInProgress() != null) { if (media.getTasksInProgress() != null) {
Task copyTask = Iterables.getFirst(media.getTasksInProgress().getTasks(), null); Task copyTask = getFirst(media.getTasksInProgress().getTasks(), null);
if (copyTask != null) { if (copyTask != null) {
Checks.checkTask(copyTask); Checks.checkTask(copyTask);
assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask")); assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask"));
@ -244,7 +253,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
public void testGetMetadata() { public void testGetMetadata() {
metadata = mediaClient.getMetadataClient().getMetadata(media.getHref()); metadata = mediaClient.getMetadataClient().getMetadata(media.getHref());
// required for testing // required for testing
assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()), assertFalse(isEmpty(metadata.getMetadataEntries()),
String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "metadata.entries")); String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "metadata.entries"));
Checks.checkMetadataFor(MEDIA, metadata); Checks.checkMetadataFor(MEDIA, metadata);

View File

@ -26,8 +26,28 @@ import java.net.URI;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; 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.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.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorRestClientExpectTest;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -274,26 +294,60 @@ public class VdcClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
assertEquals(client.getVdcClient().uploadVAppTemplate(vdcURI, params), expected); assertEquals(client.getVdcClient().uploadVAppTemplate(vdcURI, params), expected);
} }
@Test(enabled = false) @Test
public void testCreateMedia() { public void testCreateMedia() {
URI vdcUri = URI.create(endpoint + "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f");
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
new VcloudHttpRequestPrimer() new VcloudHttpRequestPrimer()
.apiCommand("POST", "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/action/media") .apiCommand("POST", "/vdc/e9cd3387-ac57-4d27-a481-9bee75e0690f/media")
.xmlFilePayload("/vdc/params/createMedia.xml", VCloudDirectorMediaType.MEDIA) .acceptMedia(VCloudDirectorMediaType.MEDIA)
.acceptAnyMedia() .xmlFilePayload("/media/createMediaSource.xml", VCloudDirectorMediaType.MEDIA)
.httpRequestBuilder().build(), .httpRequestBuilder().build(),
new VcloudHttpResponsePrimer() new VcloudHttpResponsePrimer()
.xmlFilePayload("/vdc/createMedia.xml", VCloudDirectorMediaType.MEDIA) .xmlFilePayload("/media/createMedia.xml", VCloudDirectorMediaType.MEDIA)
.httpResponseBuilder().build()); .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(); .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) @Test(enabled = false)
@ -492,11 +546,6 @@ public class VdcClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
return null; return null;
} }
private Media createMedia() {
// TODO Auto-generated method stub
return null;
}
private Metadata metadata() { private Metadata metadata() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;