refactored Metadata to implement Immutable Map<String, String>; addGroupToOrg GroupApi, MediaApi to use urn

This commit is contained in:
Adrian Cole 2012-08-19 21:48:04 -07:00
parent e016367280
commit aff96befbe
26 changed files with 397 additions and 381 deletions

View File

@ -42,6 +42,7 @@
<test.vcloud-director.template></test.vcloud-director.template>
<!-- URN format: ex. urn:vcloud:catalog:7212e451-76e1-4631-b2de-ba1dfd8080e4 -->
<test.vcloud-director.catalog-id></test.vcloud-director.catalog-id>
<!-- URN format: ex. urn:vcloud:media:7212e451-76e1-4631-b2de-ba1dfd8080e4 -->
<test.vcloud-director.media-id></test.vcloud-director.media-id>
<!-- URN format: ex. urn:vcloud:vapptemplate:7212e451-76e1-4631-b2de-ba1dfd8080e4 -->
<test.vcloud-director.vapptemplate-id></test.vcloud-director.vapptemplate-id>

View File

@ -21,7 +21,8 @@ package org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collections;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.annotation.XmlElement;
@ -31,20 +32,22 @@ import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/**
* Represents a set of metadata
* <p/>
*
* <pre>
* &lt;xs:complexType name="Metadata"&gt;
* </pre>
*
*
* @author danikov
*/
@XmlRootElement(name = "Metadata")
public class Metadata extends Resource {
public class Metadata extends Resource implements Map<String, String> {
public static final String MEDIA_TYPE = VCloudDirectorMediaType.METADATA;
@ -59,7 +62,7 @@ public class Metadata extends Resource {
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
}
public static abstract class Builder<B extends Builder<B>> extends Resource.Builder<B> {
private Set<MetadataEntry> metadataEntries = Sets.newLinkedHashSet();
@ -99,12 +102,19 @@ public class Metadata extends Resource {
this.metadataEntries = ImmutableSet.copyOf(builder.metadataEntries);
}
@XmlElement(name = "MetadataEntry")
private Set<MetadataEntry> metadataEntries = Sets.newLinkedHashSet();
public Set<MetadataEntry> getMetadataEntries() {
return Collections.unmodifiableSet(metadataEntries);
return ImmutableSet.copyOf(metadataEntries);
}
protected Map<String, String> toMap() {
ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String> builder();
for (MetadataEntry entry : metadataEntries) {
builder.put(entry.getKey(), entry.getValue());
}
return builder.build();
}
@Override
@ -127,4 +137,64 @@ public class Metadata extends Resource {
return super.string().add("metadataEntries", metadataEntries);
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean containsKey(Object arg0) {
return toMap().containsKey(arg0);
}
@Override
public boolean containsValue(Object arg0) {
return toMap().containsValue(arg0);
}
@Override
public Set<Map.Entry<String, String>> entrySet() {
return toMap().entrySet();
}
@Override
public String get(Object arg0) {
return toMap().get(arg0);
}
@Override
public boolean isEmpty() {
return getMetadataEntries().size() == 0;
}
@Override
public Set<String> keySet() {
return toMap().keySet();
}
@Override
public String put(String arg0, String arg1) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map<? extends String, ? extends String> arg0) {
throw new UnsupportedOperationException();
}
@Override
public String remove(Object arg0) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return getMetadataEntries().size();
}
@Override
public Collection<String> values() {
return toMap().values();
}
}

View File

@ -137,7 +137,7 @@ public class MetadataEntry extends Resource {
}
private MetadataEntry() {
MetadataEntry() {
// for JAXB
}

View File

@ -131,7 +131,7 @@ public class IpScope {
}
}
private IpScope() {
IpScope() {
// For JAXB
}

View File

@ -140,7 +140,7 @@ public class NatRule {
this.vmRule = vmRule;
}
private NatRule() {
NatRule() {
// for JAXB
}

View File

@ -227,7 +227,7 @@ public class NetworkConnection {
this.needsCustomization = needsCustomization;
}
private NetworkConnection() {
NetworkConnection() {
// for JAXB
}

View File

@ -76,7 +76,7 @@ public class NetworkFeatures {
}
}
private NetworkFeatures() {
NetworkFeatures() {
// for JAXB
}

View File

@ -84,7 +84,7 @@ public abstract class NetworkServiceType<T extends NetworkServiceType<T>> {
return true;
if (o == null || getClass() != o.getClass())
return false;
NetworkServiceType that = NetworkServiceType.class.cast(o);
NetworkServiceType<?> that = NetworkServiceType.class.cast(o);
return equal(isEnabled, that.isEnabled);
}

View File

@ -147,7 +147,7 @@ public class SourcedCompositionItemParam {
this.sourceDelete = sourceDelete;
}
private SourcedCompositionItemParam() {
SourcedCompositionItemParam() {
// for JAXB
}

View File

@ -61,7 +61,7 @@ public class QueryList extends ContainerType {
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
}
private QueryList() {
QueryList() {
// for JAXB
}

View File

@ -168,7 +168,7 @@ public class QueryResultNetworkRecord extends QueryResultRecordType {
isBusy = builder.isBusy;
}
private QueryResultNetworkRecord() {
QueryResultNetworkRecord() {
// for JAXB
}

View File

@ -35,7 +35,7 @@ import org.jclouds.vcloud.director.v1_5.functions.href.MediaURNToHref;
* Provides synchronous access to {@link Media}.
*
* @see MediaAsyncApi
* @author danikov
* @author danikov, Adrian Cole
*/
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface MediaApi {
@ -45,44 +45,55 @@ public interface MediaApi {
*
* @return the media or null if not found
*/
Media getMedia(URI mediaUri);
Media get(String mediaUrn);
Media get(URI mediaHref);
/**
* 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.
* @return The response will return a link to transfer site to be able to continue with uploading
* the media.
*/
Media addMedia(URI uploadLink, Media media);
Media add(URI uploadHref, Media media);
/**
* Clones a media into new one.
* The status of the returned media is UNRESOLVED(0) until the task for cloning finish.
* 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.
* @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);
Media clone(String mediaUrn, CloneMediaParams params);
Media clone(URI mediaHref, CloneMediaParams params);
/**
* Updates the name/description of a media.
*
* @return a task. This operation is asynchronous and the user should monitor the returned
* task status in order to check when it is completed.
* @return a task. This operation is asynchronous and the user should monitor the returned task
* status in order to check when it is completed.
*/
Task editMedia(URI mediaUri, Media media);
Task edit(String mediaUrn, Media media);
Task edit(URI mediaHref, Media media);
/**
* Deletes a media.
*/
Task removeMedia(URI mediaUri);
Task remove(String mediaUrn);
Task remove(URI mediaHref);
/**
* Retrieves an owner.
*
* @return the owner or null if not found
*/
Owner getOwner(URI mediaUri);
Owner getOwner(String mediaUrn);
Owner getOwner(URI mediaHref);
/**
* @return synchronous access to {@link Metadata.Writeable} features
*/
@ -90,6 +101,6 @@ public interface MediaApi {
MetadataApi.Writeable getMetadataApi(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn);
@Delegate
MetadataApi.Writeable getMetadataApi(@EndpointParam URI mediaUri);
MetadataApi.Writeable getMetadataApi(@EndpointParam URI mediaHref);
}

View File

@ -49,59 +49,105 @@ import com.google.common.util.concurrent.ListenableFuture;
/**
* @see MediaApi
* @author danikov
* @author danikov, Adrian Cole
*/
@RequestFilters(AddVCloudAuthorizationAndCookieToRequest.class)
public interface MediaAsyncApi {
/**
* @see MediaApi#getMedia(URI)
* @see MediaApi#get(String)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Media> getMedia(@EndpointParam URI uri);
ListenableFuture<Media> get(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn);
/**
* @see MediaApi#addMedia(URI, Media)
* @see MediaApi#add(URI, Media)
*/
@POST
@Consumes(VCloudDirectorMediaType.MEDIA)
@Produces(VCloudDirectorMediaType.MEDIA)
@JAXBResponseParser
ListenableFuture<Media> addMedia(@EndpointParam URI link,
@BinderParam(BindToXMLPayload.class) Media media);
ListenableFuture<Media> add(@EndpointParam URI updateHref, @BinderParam(BindToXMLPayload.class) Media media);
/**
* @see MediaApi#cloneMedia(URI, CloneMediaParams)
* @see MediaApi#clone(String, CloneMediaParams)
*/
@POST
@Path("/action/cloneMedia")
@Consumes(VCloudDirectorMediaType.MEDIA)
@Produces(VCloudDirectorMediaType.CLONE_MEDIA_PARAMS)
@JAXBResponseParser
ListenableFuture<Media> cloneMedia(@EndpointParam URI vdcRef,
@BinderParam(BindToXMLPayload.class) CloneMediaParams params);
ListenableFuture<Media> clone(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn,
@BinderParam(BindToXMLPayload.class) CloneMediaParams params);
/**
* @see MediaApi#editMedia(URI, Media))
* @see MediaApi#editMedia(String, Media)
*/
@PUT
@Consumes(VCloudDirectorMediaType.TASK)
@Produces(VCloudDirectorMediaType.MEDIA)
@JAXBResponseParser
ListenableFuture<Task> editMedia(@EndpointParam URI uri, @BinderParam(BindToXMLPayload.class) Media media);
ListenableFuture<Task> edit(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn,
@BinderParam(BindToXMLPayload.class) Media media);
/**
* @see MediaApi#removeMedia(URI))
*/
* @see MediaApi#removeMedia(String)
*/
@DELETE
@Consumes(VCloudDirectorMediaType.TASK)
@JAXBResponseParser
ListenableFuture<Task> removeMedia(@EndpointParam URI uri);
ListenableFuture<Task> remove(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn);
/**
* @see MediaApi#getOwner(String)
*/
@GET
@Path("/owner")
@Consumes
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Owner> getOwner(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn);
/**
* @see MediaApi#get(URI)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Media> get(@EndpointParam URI mediaHref);
/**
* @see MediaApi#clone(URI, CloneMediaParams)
*/
@POST
@Path("/action/cloneMedia")
@Consumes(VCloudDirectorMediaType.MEDIA)
@Produces(VCloudDirectorMediaType.CLONE_MEDIA_PARAMS)
@JAXBResponseParser
ListenableFuture<Media> clone(@EndpointParam URI mediaHref,
@BinderParam(BindToXMLPayload.class) CloneMediaParams params);
/**
* @see MediaApi#editMedia(URI, Media)
*/
@PUT
@Consumes(VCloudDirectorMediaType.TASK)
@Produces(VCloudDirectorMediaType.MEDIA)
@JAXBResponseParser
ListenableFuture<Task> edit(@EndpointParam URI mediaHref, @BinderParam(BindToXMLPayload.class) Media media);
/**
* @see MediaApi#removeMedia(URI)
*/
@DELETE
@Consumes(VCloudDirectorMediaType.TASK)
@JAXBResponseParser
ListenableFuture<Task> remove(@EndpointParam URI mediaHref);
/**
* @see MediaApi#getOwner(URI)
*/
@ -110,14 +156,14 @@ public interface MediaAsyncApi {
@Consumes
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Owner> getOwner(@EndpointParam URI uri);
ListenableFuture<Owner> getOwner(@EndpointParam URI mediaHref);
/**
* @return asynchronous access to {@link Metadata.Writeable} features
*/
* @return asynchronous access to {@link Metadata.Writeable} features
*/
@Delegate
MetadataAsyncApi.Writeable getMetadataApi(@EndpointParam(parser = MediaURNToHref.class) String mediaUrn);
@Delegate
MetadataAsyncApi.Writeable getMetadataApi(@EndpointParam URI uri);
MetadataAsyncApi.Writeable getMetadataApi(@EndpointParam URI mediaHref);
}

View File

@ -28,7 +28,7 @@ import org.jclouds.vcloud.director.v1_5.domain.Group;
* Provides synchronous access to {@link Group} objects.
*
* @see GroupAsyncApi
* @author danikov
* @author danikov, Adrian Cole
*/
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface GroupApi {
@ -40,11 +40,13 @@ public interface GroupApi {
* POST /admin/org/{id}/groups
* </pre>
*
* @param orgUri the admin org to add the group in
* @param orgUrn the admin org to add the group in
* @return the addd group
*/
Group addGroup(URI adminOrgUri, Group group);
Group addGroupToOrg(Group group, String orgUrn);
Group addGroupToOrg(Group group, URI orgHref);
/**
* Retrieves a group.
*
@ -52,10 +54,12 @@ public interface GroupApi {
* GET /admin/group/{id}
* </pre>
*
* @param groupURI the reference for the group
* @param groupString the reference for the group
* @return a group
*/
Group getGroup(URI groupUri);
Group get(String groupUrn);
Group get(URI groupHref);
/**
* Modifies a group.
@ -66,7 +70,9 @@ public interface GroupApi {
*
* @return the edited group
*/
Group editGroup(URI groupRef, Group group);
Group edit(String groupUrn, Group group);
Group edit(URI groupHref, Group group);
/**
* Deletes a group.
@ -75,5 +81,8 @@ public interface GroupApi {
* DELETE /admin/group/{id}
* </pre>
*/
void removeGroup(URI groupRef);
void remove(String groupUrn);
void remove(URI groupHref);
}

View File

@ -38,48 +38,89 @@ import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Group;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationAndCookieToRequest;
import org.jclouds.vcloud.director.v1_5.functions.href.GroupURNToHref;
import org.jclouds.vcloud.director.v1_5.functions.href.OrgURNToAdminHref;
import com.google.common.util.concurrent.ListenableFuture;
/**
* @see GroupApi
* @author danikov
* @author danikov, Adrian Cole
*/
@RequestFilters(AddVCloudAuthorizationAndCookieToRequest.class)
public interface GroupAsyncApi {
/**
* @see GroupApi#addGroupToOrg(Group, String)
*/
@POST
@Path("/groups")
@Consumes(VCloudDirectorMediaType.GROUP)
@Produces(VCloudDirectorMediaType.GROUP)
@JAXBResponseParser
ListenableFuture<Group> addGroup(@EndpointParam URI adminOrgUri,
@BinderParam(BindToXMLPayload.class) Group group);
ListenableFuture<Group> addGroupToOrg(@BinderParam(BindToXMLPayload.class) Group group,
@EndpointParam(parser = OrgURNToAdminHref.class) String adminUrn);
/**
* @see GroupApi#getGroup(URI)
* @see GroupApi#get(String)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Group> getGroup(@EndpointParam URI groupUri);
ListenableFuture<Group> get(@EndpointParam(parser = GroupURNToHref.class) String groupUri);
/**
* @see GroupApi#editGroup(URI, Group)
* @see GroupApi#edit(String, Group)
*/
@PUT
@Consumes(VCloudDirectorMediaType.GROUP)
@Produces(VCloudDirectorMediaType.GROUP)
@JAXBResponseParser
ListenableFuture<Group> editGroup(@EndpointParam URI groupRef,
@BinderParam(BindToXMLPayload.class) Group group);
ListenableFuture<Group> edit(@EndpointParam(parser = GroupURNToHref.class) String groupUrn,
@BinderParam(BindToXMLPayload.class) Group group);
/**
* @see GroupApi#removeGroup(URI)
* @see GroupApi#remove(String)
*/
@DELETE
@Consumes
@JAXBResponseParser
ListenableFuture<Void> removeGroup(@EndpointParam URI groupRef);
ListenableFuture<Void> remove(@EndpointParam(parser = GroupURNToHref.class) String groupUrn);
/**
* @see GroupApi#addGroupToOrg(Group, URI)
*/
@POST
@Path("/groups")
@Consumes(VCloudDirectorMediaType.GROUP)
@Produces(VCloudDirectorMediaType.GROUP)
@JAXBResponseParser
ListenableFuture<Group> addGroupToOrg(@BinderParam(BindToXMLPayload.class) Group group, @EndpointParam URI adminUrn);
/**
* @see GroupApi#get(URI)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Group> get(@EndpointParam URI groupUri);
/**
* @see GroupApi#edit(URI, Group)
*/
@PUT
@Consumes(VCloudDirectorMediaType.GROUP)
@Produces(VCloudDirectorMediaType.GROUP)
@JAXBResponseParser
ListenableFuture<Group> edit(@EndpointParam URI groupUrn, @BinderParam(BindToXMLPayload.class) Group group);
/**
* @see GroupApi#remove(URI)
*/
@DELETE
@Consumes
@JAXBResponseParser
ListenableFuture<Void> remove(@EndpointParam URI groupUrn);
}

View File

@ -0,0 +1,43 @@
/*
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.vcloud.director.v1_5.functions.href;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Entity;
import org.jclouds.vcloud.director.v1_5.functions.URNToHref;
import com.google.common.cache.LoadingCache;
@Singleton
public class GroupURNToHref extends URNToHref {
@Inject
public GroupURNToHref(LoadingCache<String, Entity> resolveEntityCache) {
super(resolveEntityCache);
}
@Override
protected String type() {
return VCloudDirectorMediaType.GROUP;
}
}

View File

@ -114,7 +114,7 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest {
}
if (media != null) {
try {
Task remove = context.getApi().getMediaApi().removeMedia(media.getHref());
Task remove = context.getApi().getMediaApi().remove(media.getId());
taskDoneEventually(remove);
} catch (Exception e) {
logger.warn(e, "Error when deleting media '%s'", media.getName());
@ -153,7 +153,7 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest {
Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name("Test media 1").size(iso.length)
.imageType(Media.ImageType.ISO).description("Test media generated by testAddMedia()").build();
media = context.getApi().getMediaApi().addMedia(addMedia.getHref(), sourceMedia);
media = context.getApi().getMediaApi().add(addMedia.getHref(), sourceMedia);
Checks.checkMediaFor(VCloudDirectorMediaType.MEDIA, media);

View File

@ -75,7 +75,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
.build();
Media expected = addMedia();
assertEquals(api.getMediaApi().addMedia(uploadLink, source), expected);
assertEquals(api.getMediaApi().add(uploadLink, source), expected);
}
@Test
@ -104,7 +104,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
.build();
Media expected = cloneMedia();
assertEquals(api.getMediaApi().cloneMedia(vdcURI, params), expected);
assertEquals(api.getMediaApi().clone(vdcURI, params), expected);
}
@Test
@ -121,7 +121,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
.httpResponseBuilder().build());
Media expected = getMedia();
assertEquals(api.getMediaApi().getMedia(mediaUri), expected);
assertEquals(api.getMediaApi().get(mediaUri), expected);
}
@Test
@ -144,7 +144,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
.build();
try {
api.getMediaApi().getMedia(mediaUri);
api.getMediaApi().get(mediaUri);
fail("Should give HTTP 400 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
@ -166,7 +166,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
.xmlFilePayload("/media/error403-catalog.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
assertNull(api.getMediaApi().getMedia(mediaUri));
assertNull(api.getMediaApi().get(mediaUri));
}
@Test
@ -182,7 +182,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
.xmlFilePayload("/media/error403-fake.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
assertNull(api.getMediaApi().getMedia(mediaUri));
assertNull(api.getMediaApi().get(mediaUri));
}
@Test
@ -202,7 +202,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
Media edit = editMedia();
Task expected = editMediaTask();
assertEquals(api.getMediaApi().editMedia(mediaUri, edit), expected);
assertEquals(api.getMediaApi().edit(mediaUri, edit), expected);
}
@Test
@ -220,7 +220,7 @@ public class MediaApiExpectTest extends VCloudDirectorAdminApiExpectTest {
Task expected = removeMediaTask();
assertEquals(api.getMediaApi().removeMedia(mediaUri), expected);
assertEquals(api.getMediaApi().remove(mediaUri), expected);
}
@Test

View File

@ -109,7 +109,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
protected void tidyUp() {
if (media != null) {
try {
Task remove = mediaApi.removeMedia(media.getHref());
Task remove = mediaApi.remove(media.getId());
taskDoneEventually(remove);
} catch (Exception e) {
logger.warn(e, "Error when deleting media '%s': %s", media.getName());
@ -117,7 +117,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
}
if (oldMedia != null) {
try {
Task remove = mediaApi.removeMedia(oldMedia.getHref());
Task remove = mediaApi.remove(oldMedia.getId());
taskDoneEventually(remove);
} catch (Exception e) {
logger.warn(e, "Error when deleting media '%s': %s", oldMedia.getName());
@ -136,7 +136,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name("Test media " + random.nextInt())
.size(iso.length).imageType(Media.ImageType.ISO).description("Test media generated by testAddMedia()")
.build();
media = mediaApi.addMedia(addMedia.getHref(), sourceMedia);
media = mediaApi.add(addMedia.getHref(), sourceMedia);
Checks.checkMediaFor(MEDIA, media);
@ -162,19 +162,19 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
Link uploadLink = Iterables.find(links, LinkPredicates.relEquals(Link.Rel.UPLOAD_DEFAULT));
context.getApi().getUploadApi().upload(uploadLink.getHref(), Payloads.newByteArrayPayload(iso));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
if (media.getTasks().size() == 1) {
Task uploadTask = Iterables.getOnlyElement(media.getTasks());
Checks.checkTask(uploadTask);
assertEquals(uploadTask.getStatus(), Task.Status.RUNNING);
assertTrue(retryTaskSuccess.apply(uploadTask), String.format(TASK_COMPLETE_TIMELY, "uploadTask"));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
}
}
@Test(description = "GET /media/{id}", dependsOnMethods = { "testAddMedia" })
public void testGetMedia() {
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
assertNotNull(media, String.format(OBJ_REQ_LIVE, MEDIA));
owner = media.getOwner();
@ -186,7 +186,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
@Test(description = "GET /media/{id}/owner", dependsOnMethods = { "testGetMedia" })
public void testGetMediaOwner() {
Owner directOwner = mediaApi.getOwner(media.getHref());
Owner directOwner = mediaApi.getOwner(media.getId());
assertEquals(owner.toBuilder().user(owner.getUser()).build(),
directOwner.toBuilder().links(Collections.<Link> emptySet()).build(), String.format(
GETTER_RETURNS_SAME_OBJ, "getOwner()", "owner", "media.getOwner()", owner.toString(),
@ -215,7 +215,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
if (copyTask != null) {
Checks.checkTask(copyTask);
assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask"));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
}
}
@ -236,7 +236,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
if (copyTask != null) {
Checks.checkTask(copyTask);
assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask"));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
}
}
@ -253,10 +253,10 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
String newDescription = "new " + oldDescription;
media = media.toBuilder().name(newName).description(newDescription).build();
Task editMedia = mediaApi.editMedia(media.getHref(), media);
Task editMedia = mediaApi.edit(media.getId(), media);
Checks.checkTask(editMedia);
assertTrue(retryTaskSuccess.apply(editMedia), String.format(TASK_COMPLETE_TIMELY, "editMedia"));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
assertTrue(equal(media.getName(), newName), String.format(OBJ_FIELD_UPDATABLE, MEDIA, "name"));
assertTrue(equal(media.getDescription(), newDescription),
@ -268,10 +268,10 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
media = media.toBuilder().name(oldName).description(oldDescription).build();
editMedia = mediaApi.editMedia(media.getHref(), media);
editMedia = mediaApi.edit(media.getId(), media);
Checks.checkTask(editMedia);
assertTrue(retryTaskSuccess.apply(editMedia), String.format(TASK_COMPLETE_TIMELY, "editMedia"));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
}
@Test(description = "GET /media/{id}/metadata", dependsOnMethods = { "testSetMetadataValue" })
@ -296,7 +296,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
Checks.checkMetadataFor(MEDIA, metadata);
checkMetadataContainsEntries(metadata, inputEntries);
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
Checks.checkMediaFor(MEDIA, media);
// test edit
@ -310,7 +310,7 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
Checks.checkMetadataFor(MEDIA, metadata);
checkMetadataContainsEntries(metadata, inputEntries);
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
Checks.checkMediaFor(MEDIA, media);
}
@ -364,20 +364,20 @@ public class MediaApiLiveTest extends BaseVCloudDirectorApiLiveTest {
metadataValue = mediaApi.getMetadataApi(media.getId()).getValue("key");
Checks.checkMetadataValueFor(MEDIA, metadataValue);
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
Checks.checkMediaFor(MEDIA, media);
}
@Test(description = "DELETE /media/{id}", dependsOnMethods = { "testRemoveMetadata" })
public void testRemoveMedia() {
Task removeMedia = mediaApi.removeMedia(media.getHref());
Task removeMedia = mediaApi.remove(media.getId());
Checks.checkTask(removeMedia);
assertTrue(retryTaskSuccess.apply(removeMedia), String.format(TASK_COMPLETE_TIMELY, "removeMedia"));
media = mediaApi.getMedia(media.getHref());
media = mediaApi.get(media.getId());
assertNull(media, String.format(OBJ_DEL, MEDIA, media != null ? media.toString() : ""));
removeMedia = mediaApi.removeMedia(oldMedia.getHref());
removeMedia = mediaApi.remove(oldMedia.getId());
Checks.checkTask(removeMedia);
assertTrue(retryTaskSuccess.apply(removeMedia), String.format(TASK_COMPLETE_TIMELY, "removeMedia"));
oldMedia = null;

View File

@ -18,10 +18,6 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import static com.google.common.base.Predicates.and;
import static com.google.common.collect.Iterables.contains;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.getFirst;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.CONDITION_FMT;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.CORRECT_VALUE_OBJECT_FMT;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.ENTITY_EQUAL;
@ -43,8 +39,6 @@ import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkOwner;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkProductSectionList;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkStartupSection;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkVApp;
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.assertNull;
@ -60,13 +54,9 @@ import org.jclouds.dmtf.ovf.MsgType;
import org.jclouds.dmtf.ovf.NetworkSection;
import org.jclouds.dmtf.ovf.ProductSection;
import org.jclouds.dmtf.ovf.StartupSection;
import org.jclouds.io.Payloads;
import org.jclouds.vcloud.director.v1_5.AbstractVAppApiLiveTest;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.AccessSetting;
import org.jclouds.vcloud.director.v1_5.domain.Checks;
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.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.MetadataValue;
@ -118,43 +108,11 @@ public class VAppApiLiveTest extends AbstractVAppApiLiveTest {
private MetadataValue metadataValue;
private String key;
private boolean mediaCreated = false;
private boolean testUserCreated = false;
private User user;
@BeforeClass(alwaysRun = true)
protected void setupRequiredEntities() {
Set<Link> links = lazyGetVdc().getLinks();
if (mediaURI == null) {
Predicate<Link> addMediaLink = and(relEquals(Link.Rel.ADD), typeEquals(VCloudDirectorMediaType.MEDIA));
if (contains(links, addMediaLink)) {
Link addMedia = find(links, addMediaLink);
byte[] iso = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name(name("media"))
.size(iso.length).imageType(Media.ImageType.ISO)
.description("Test media generated by VAppApiLiveTest").build();
Media media = context.getApi().getMediaApi().addMedia(addMedia.getHref(), sourceMedia);
Link uploadLink = getFirst(getFirst(media.getFiles(), null).getLinks(), null);
context.getApi().getUploadApi().upload(uploadLink.getHref(), Payloads.newByteArrayPayload(iso));
media = context.getApi().getMediaApi().getMedia(media.getHref());
if (media.getTasks().size() == 1) {
Task uploadTask = Iterables.getOnlyElement(media.getTasks());
Checks.checkTask(uploadTask);
assertEquals(uploadTask.getStatus(), Task.Status.RUNNING);
assertTrue(retryTaskSuccess.apply(uploadTask), String.format(TASK_COMPLETE_TIMELY, "uploadTask"));
media = context.getApi().getMediaApi().getMedia(media.getHref());
}
mediaURI = media.getHref();
mediaCreated = true;
}
}
if (adminContext != null) {
userUrn = adminContext.getApi().getUserApi().addUserToOrg(randomTestUser("VAppAccessTest"), org.getId())
@ -165,14 +123,6 @@ public class VAppApiLiveTest extends AbstractVAppApiLiveTest {
@AfterClass(alwaysRun = true, dependsOnMethods = { "cleanUpEnvironment" })
public void cleanUp() {
if (adminContext != null && mediaCreated && mediaURI != null) {
try {
Task remove = context.getApi().getMediaApi().removeMedia(mediaURI);
taskDoneEventually(remove);
} catch (Exception e) {
logger.warn(e, "Error when deleting media");
}
}
if (adminContext != null && testUserCreated && userUrn != null) {
try {
adminContext.getApi().getUserApi().remove(userUrn);

View File

@ -18,10 +18,6 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import static com.google.common.base.Predicates.and;
import static com.google.common.collect.Iterables.contains;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.getFirst;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.CORRECT_VALUE_OBJECT_FMT;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.ENTITY_EQUAL;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.NOT_EMPTY_OBJECT_FMT;
@ -44,8 +40,6 @@ import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkScreenTicket;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkVirtualHardwareSection;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkVm;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkVmPendingQuestion;
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;
@ -62,12 +56,8 @@ import org.jclouds.dmtf.cim.OSType;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData;
import org.jclouds.dmtf.ovf.MsgType;
import org.jclouds.dmtf.ovf.ProductSection;
import org.jclouds.io.Payloads;
import org.jclouds.vcloud.director.v1_5.AbstractVAppApiLiveTest;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Checks;
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.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.MetadataValue;
@ -116,42 +106,10 @@ public class VmApiLiveTest extends AbstractVAppApiLiveTest {
private MetadataValue metadataValue;
private String key;
private boolean mediaCreated = false;
private boolean testUserCreated = false;
@BeforeClass(alwaysRun = true)
protected void setupRequiredEntities() {
Set<Link> links = vdcApi.get(vdcUrn).getLinks();
if (mediaURI == null) {
Predicate<Link> addMediaLink = and(relEquals(Link.Rel.ADD), typeEquals(VCloudDirectorMediaType.MEDIA));
if (contains(links, addMediaLink)) {
Link addMedia = find(links, addMediaLink);
byte[] iso = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name(name("media"))
.size(iso.length).imageType(Media.ImageType.ISO)
.description("Test media generated by VmApiLiveTest").build();
Media media = context.getApi().getMediaApi().addMedia(addMedia.getHref(), sourceMedia);
Link uploadLink = getFirst(getFirst(media.getFiles(), null).getLinks(), null);
context.getApi().getUploadApi().upload(uploadLink.getHref(), Payloads.newByteArrayPayload(iso));
media = context.getApi().getMediaApi().getMedia(media.getHref());
if (media.getTasks().size() == 1) {
Task uploadTask = Iterables.getOnlyElement(media.getTasks());
Checks.checkTask(uploadTask);
assertEquals(uploadTask.getStatus(), Task.Status.RUNNING);
assertTrue(retryTaskSuccess.apply(uploadTask), String.format(TASK_COMPLETE_TIMELY, "uploadTask"));
media = context.getApi().getMediaApi().getMedia(media.getHref());
}
mediaURI = media.getHref();
mediaCreated = true;
}
}
if (adminContext != null) {
userUrn = adminContext.getApi().getUserApi().addUserToOrg(randomTestUser("VAppAccessTest"), org.getId())
@ -161,9 +119,9 @@ public class VmApiLiveTest extends AbstractVAppApiLiveTest {
@AfterClass(alwaysRun = true, dependsOnMethods = { "cleanUpEnvironment" })
public void cleanUp() {
if (adminContext != null && mediaCreated && mediaURI != null) {
if (adminContext != null && mediaUrn != null) {
try {
Task remove = context.getApi().getMediaApi().removeMedia(mediaURI);
Task remove = context.getApi().getMediaApi().remove(mediaUrn);
taskDoneEventually(remove);
} catch (Exception e) {
logger.warn("Error when deleting media: %s", e.getMessage());
@ -462,7 +420,7 @@ public class VmApiLiveTest extends AbstractVAppApiLiveTest {
public void testInsertMedia() {
// Setup media params from configured media id
MediaInsertOrEjectParams params = MediaInsertOrEjectParams.builder()
.media(Reference.builder().href(mediaURI).type(MEDIA).build()).build();
.media(Reference.builder().href(lazyGetMedia().getHref()).type(MEDIA).build()).build();
// The method under test
Task insertMedia = vmApi.insertMedia(vmUrn, params);
@ -473,7 +431,7 @@ public class VmApiLiveTest extends AbstractVAppApiLiveTest {
public void testEjectMedia() {
// Setup media params from configured media id
MediaInsertOrEjectParams params = MediaInsertOrEjectParams.builder()
.media(Reference.builder().href(mediaURI).type(MEDIA).build()).build();
.media(Reference.builder().href(lazyGetMedia().getHref()).type(MEDIA).build()).build();
// The method under test
Task ejectMedia = vmApi.ejectMedia(vmUrn, params);
@ -864,8 +822,7 @@ public class VmApiLiveTest extends AbstractVAppApiLiveTest {
RasdItemsList newSection = oldSection.toBuilder().build();
// Method under test
Task editVirtualHardwareSectionNetworkCards = vmApi.editVirtualHardwareSectionNetworkCards(vmUrn,
newSection);
Task editVirtualHardwareSectionNetworkCards = vmApi.editVirtualHardwareSectionNetworkCards(vmUrn, newSection);
assertTrue(retryTaskSuccess.apply(editVirtualHardwareSectionNetworkCards),
String.format(TASK_COMPLETE_TIMELY, "editVirtualHardwareSectionNetworkCards"));
@ -896,8 +853,7 @@ public class VmApiLiveTest extends AbstractVAppApiLiveTest {
RasdItemsList newSection = oldSection.toBuilder().build();
// Method under test
Task editVirtualHardwareSectionSerialPorts = vmApi
.editVirtualHardwareSectionSerialPorts(vmUrn, newSection);
Task editVirtualHardwareSectionSerialPorts = vmApi.editVirtualHardwareSectionSerialPorts(vmUrn, newSection);
assertTrue(retryTaskSuccess.apply(editVirtualHardwareSectionSerialPorts),
String.format(TASK_COMPLETE_TIMELY, "editVirtualHardwareSectionSerialPorts"));

View File

@ -56,7 +56,7 @@ public class GroupApiExpectTest extends VCloudDirectorAdminApiExpectTest {
Group expected = group();
assertEquals(api.getGroupApi().getGroup(groupRef.getHref()), expected);
assertEquals(api.getGroupApi().get(groupRef.getHref()), expected);
}
public static final Group group() {
@ -79,7 +79,7 @@ public class GroupApiExpectTest extends VCloudDirectorAdminApiExpectTest {
Group expected = editGroup();
assertEquals(api.getGroupApi().editGroup(groupRef.getHref(), expected), expected);
assertEquals(api.getGroupApi().edit(groupRef.getHref(), expected), expected);
}
public static Group editGroup() {
@ -96,6 +96,6 @@ public class GroupApiExpectTest extends VCloudDirectorAdminApiExpectTest {
new VcloudHttpResponsePrimer()
.httpResponseBuilder().statusCode(204).build());
api.getGroupApi().removeGroup(groupRef.getHref());
api.getGroupApi().remove(groupRef.getHref());
}
}

View File

@ -50,7 +50,6 @@ public class GroupApiLiveTest extends BaseVCloudDirectorApiLiveTest {
/*
* Shared state between dependant tests.
*/
private Reference groupRef;
private Group group;
private OrgLdapSettings oldLdapSettings, newLdapSettings;
@ -61,7 +60,7 @@ public class GroupApiLiveTest extends BaseVCloudDirectorApiLiveTest {
Reference orgRef = null;
// TODO: requisite LDAP settings
// oldLdapSettings = adminContext.getApi().getAdminOrgApi().getLdapSettings(orgRef.getHref());
// oldLdapSettings = adminContext.getApi().getAdminOrgApi().getLdapSettings(orgRef.getId());
// OrgLdapSettings newLdapSettings = oldLdapSettings.toBuilder()
// .ldapMode(OrgLdapSettings.LdapMode.SYSTEM)
// .build();
@ -79,7 +78,7 @@ public class GroupApiLiveTest extends BaseVCloudDirectorApiLiveTest {
@Test(description = "GET /admin/group/{id}", dependsOnMethods = { "testAddGroup" })
public void testGetGroup() {
group = groupApi.getGroup(groupRef.getHref());
group = groupApi.get(group.getId());
Checks.checkGroup(group);
}
@ -98,7 +97,7 @@ public class GroupApiLiveTest extends BaseVCloudDirectorApiLiveTest {
.description(newDescription)
.build();
group = groupApi.editGroup(group.getHref(), group);
group = groupApi.edit(group.getId(), group);
assertTrue(equal(group.getName(), newName), String.format(OBJ_FIELD_UPDATABLE, GROUP, "name"));
assertTrue(equal(group.getDescription(), newDescription),
@ -113,13 +112,13 @@ public class GroupApiLiveTest extends BaseVCloudDirectorApiLiveTest {
.description(oldDescription)
.build();
group = groupApi.editGroup(group.getHref(), group);
group = groupApi.edit(group.getId(), group);
}
}
@Test(description = "DELETE /admin/group/{id}", dependsOnMethods = { "testEditGroup" } )
public void testRemoveGroup() {
groupApi.removeGroup(groupRef.getHref());
groupApi.remove(group.getId());
// TODO stronger assertion of error expected
// Error expected = Error.builder()
@ -129,7 +128,7 @@ public class GroupApiLiveTest extends BaseVCloudDirectorApiLiveTest {
// .build();
try {
group = groupApi.getGroup(groupRef.getHref());
group = groupApi.get(group.getId());
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
// success

View File

@ -18,12 +18,13 @@
*/
package org.jclouds.vcloud.director.v1_5.internal;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.contains;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.getFirst;
import static com.google.common.collect.Iterables.tryFind;
import static com.google.common.io.Closeables.closeQuietly;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.ENTITY_NON_NULL;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.TASK_COMPLETE_TIMELY;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.URN_REQ_LIVE;
@ -34,6 +35,9 @@ import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.USER;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.VAPP;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.VAPP_TEMPLATE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.VDC;
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.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@ -42,7 +46,6 @@ import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
@ -51,17 +54,21 @@ import javax.inject.Inject;
import org.jclouds.apis.BaseContextLiveTest;
import org.jclouds.date.DateService;
import org.jclouds.io.Payloads;
import org.jclouds.logging.Logger;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.rest.RestContext;
import org.jclouds.vcloud.director.testng.FormatApiResultsListener;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorApiMetadata;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminApi;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncApi;
import org.jclouds.vcloud.director.v1_5.domain.Catalog;
import org.jclouds.vcloud.director.v1_5.domain.Checks;
import org.jclouds.vcloud.director.v1_5.domain.Link;
import org.jclouds.vcloud.director.v1_5.domain.Link.Rel;
import org.jclouds.vcloud.director.v1_5.domain.Media;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.ResourceEntity.Status;
import org.jclouds.vcloud.director.v1_5.domain.Role.DefaultRoles;
@ -89,8 +96,6 @@ import org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates;
import org.jclouds.vcloud.director.v1_5.predicates.TaskStatusEquals;
import org.jclouds.vcloud.director.v1_5.predicates.TaskSuccess;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorApi;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncApi;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@ -131,8 +136,7 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
public Predicate<Task> retryTaskSuccessLong;
protected RestContext<VCloudDirectorAdminApi, VCloudDirectorAdminAsyncApi> adminContext;
protected RestContext<VCloudDirectorApi, VCloudDirectorAsyncApi> context; // FIXME: rename to
// userContext?
protected Session adminSession;
protected Session session;
@ -142,7 +146,8 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
private Catalog catalog;
protected String vAppTemplateUrn;
private VAppTemplate vAppTemplate;
protected URI mediaURI;
protected String mediaUrn;
private Media media;
protected String networkUrn;
private Network network;
protected String vdcUrn;
@ -159,8 +164,6 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
protected DateService dateService;
protected VCloudDirectorTestSession testSession;
protected static String testStamp;
@BeforeClass(alwaysRun = true)
@ -182,24 +185,13 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
retryTaskSuccessLong = new RetryablePredicate<Task>(taskSuccess, LONG_TASK_TIMEOUT_SECONDS * 1000L);
}
@AfterClass(alwaysRun = true)
protected void tearDownTestSession() {
closeQuietly(testSession);
}
@BeforeClass(groups = { "integration", "live" })
public void setupContext() {
super.setupContext();
adminContext = context.getAdminContext();
@Override
protected void initializeContext() {
Properties overrides = setupProperties();
testSession = VCloudDirectorTestSession.builder().provider(provider).identity(identity).credential(credential)
.endpoint(endpoint).overrides(overrides).build();
context = testSession.getUserContext();
adminContext = testSession.getAdminContext();
if (adminContext != null) {
adminSession = adminContext.getApi().getCurrentSession();
adminContext.utils().injector().injectMembers(this);
}
adminSession = adminContext.getApi().getCurrentSession();
adminContext.utils().injector().injectMembers(this);
session = context.getApi().getCurrentSession();
context.utils().injector().injectMembers(this);
@ -246,9 +238,7 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
vdcUrn = emptyToNull(System.getProperty("test." + provider + ".vdc-id"));
String mediaId = emptyToNull(System.getProperty("test." + provider + ".media-id"));
if (mediaId != null)
mediaURI = URI.create(endpoint + "/media/" + mediaId);
mediaUrn = emptyToNull(System.getProperty("test." + provider + ".media-id"));
networkUrn = emptyToNull(System.getProperty("test." + provider + ".network-id"));
@ -344,7 +334,7 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
}
};
public Optional<Network> tryFindBridgedNetworkInOrg() {
FluentIterable<Network> networks = FluentIterable.from(org.getLinks())
.filter(ReferencePredicates.<Link> typeEquals(ORG_NETWORK)).transform(new Function<Link, Network>() {
@ -412,6 +402,37 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest<
return user;
}
protected Media lazyGetMedia(){
if (media == null || mediaUrn == null) {
Predicate<Link> addMediaLink = and(relEquals(Link.Rel.ADD), typeEquals(VCloudDirectorMediaType.MEDIA));
if (contains(lazyGetVdc().getLinks(), addMediaLink)) {
Link addMedia = find(lazyGetVdc().getLinks(), addMediaLink);
byte[] iso = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name(name("media"))
.size(iso.length).imageType(Media.ImageType.ISO)
.description("Test media generated by VmApiLiveTest").build();
media = context.getApi().getMediaApi().add(addMedia.getHref(), sourceMedia);
Link uploadLink = getFirst(getFirst(media.getFiles(), null).getLinks(), null);
context.getApi().getUploadApi().upload(uploadLink.getHref(), Payloads.newByteArrayPayload(iso));
media = context.getApi().getMediaApi().get(media.getId());
if (media.getTasks().size() == 1) {
Task uploadTask = Iterables.getOnlyElement(media.getTasks());
Checks.checkTask(uploadTask);
assertEquals(uploadTask.getStatus(), Task.Status.RUNNING);
assertTrue(retryTaskSuccess.apply(uploadTask), String.format(TASK_COMPLETE_TIMELY, "uploadTask"));
media = context.getApi().getMediaApi().get(media.getId());
}
mediaUrn = media.getId();
}
}
return media;
}
protected VAppTemplate lazyGetVAppTemplate() {
if (vAppTemplate == null) {
assertNotNull(vAppTemplateUrn, String.format(URN_REQ_LIVE, VAPP_TEMPLATE));

View File

@ -30,7 +30,6 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.internal.BaseRestApiExpectTest;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.testng.annotations.BeforeGroups;
import com.google.common.base.Throwables;
@ -249,15 +248,6 @@ public abstract class BaseVCloudDirectorExpectTest<T> extends BaseRestApiExpectT
}
}
public URI toAdminUri(Reference ref) {
return toAdminUri(ref.getHref());
}
public URI toAdminUri(URI uri) {
return Reference.builder().href(uri).build().toAdminReference(endpoint).getHref();
}
protected static XMLBuilder createXMLBuilder(String root){
try {
return XMLBuilder.create(root);

View File

@ -1,121 +0,0 @@
package org.jclouds.vcloud.director.v1_5.internal;
import static org.testng.Assert.assertEquals;
import java.io.Closeable;
import java.util.Properties;
import org.jclouds.ContextBuilder;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.rest.RestContext;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminApi;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncApi;
import org.jclouds.vcloud.director.v1_5.domain.Link;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Role.DefaultRoles;
import org.jclouds.vcloud.director.v1_5.domain.User;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.Closeables;
import com.google.inject.Module;
public class VCloudDirectorTestSession implements Closeable {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String provider;
private String identity;
private String credential;
private Properties overrides;
private String endpoint;
public Builder provider(String provider) {
this.provider = provider;
return this;
}
public Builder identity(String identity) {
this.identity = identity;
return this;
}
public Builder credential(String credential) {
this.credential = credential;
return this;
}
public Builder overrides(Properties overrides) {
this.overrides = overrides;
return this;
}
public Builder endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
public VCloudDirectorTestSession build() {
return new VCloudDirectorTestSession(provider, identity, credential, overrides, endpoint);
}
}
private VCloudDirectorContext userContext;
private RestContext<VCloudDirectorAdminApi, VCloudDirectorAdminAsyncApi> adminContext;
private VCloudDirectorTestSession(String provider, String identity, String credential, Properties overrides, String endpoint) {
ContextBuilder builder = ContextBuilder.newBuilder(provider)
.credentials(identity, credential)
.endpoint(endpoint)
.modules(ImmutableSet.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule()))
.overrides(overrides);
userContext = VCloudDirectorContext.class.cast(builder.build());
// Look for the admin link in the current session
Link admin = Iterables.tryFind(
userContext.getApi().getCurrentSession().getLinks(),
Predicates.and(LinkPredicates.relEquals(Link.Rel.DOWN), ReferencePredicates.typeEquals(VCloudDirectorMediaType.ADMIN)))
.orNull();
// Get the admin context if the link exists
if (admin != null) {
adminContext = userContext.getAdminContext();
// Lookup the user details
Reference orgRef = Iterables.getFirst(userContext.getApi().getOrgApi().list(), null)
.toAdminReference(endpoint);
Reference userRef = Iterables.find(
adminContext.getApi().getOrgApi().get(orgRef.getHref()).getUsers(),
ReferencePredicates.nameEquals(adminContext.getApi().getCurrentSession().getUser()));
User user = adminContext.getApi().getUserApi().get(userRef.getHref());
// Check that the user has the org admin role
Reference userRole = user.getRole();
assertEquals(userRole.getName(), DefaultRoles.ORG_ADMIN.value());
}
}
@Override
public void close() {
// NOTE we only need to close the user context to log out
Closeables.closeQuietly(userContext);
}
public VCloudDirectorContext getUserContext() {
return userContext;
}
public RestContext<VCloudDirectorAdminApi, VCloudDirectorAdminAsyncApi> getAdminContext() {
return adminContext;
}
}