diff --git a/core/src/test/java/org/jclouds/apis/BaseContextLiveTest.java b/core/src/test/java/org/jclouds/apis/BaseContextLiveTest.java index f71ac1e121..deb4c08ce2 100644 --- a/core/src/test/java/org/jclouds/apis/BaseContextLiveTest.java +++ b/core/src/test/java/org/jclouds/apis/BaseContextLiveTest.java @@ -82,8 +82,7 @@ public abstract class BaseContextLiveTest { } protected void initializeContext() { - if (context != null) - Closeables.closeQuietly(context); + Closeables.closeQuietly(context); context = createContext(setupProperties(), setupModules()); } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorMediaType.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorMediaType.java index 68d93a1b09..d12ed4fdb1 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorMediaType.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorMediaType.java @@ -196,6 +196,8 @@ public class VCloudDirectorMediaType { public static final String NETWORK_POOL = "application/vnd.vmware.admin.networkPool+xml"; public static final String ENTITY = "application/vnd.vmware.vcloud.entity+xml"; + + public static final String ADMIN = "application/vnd.vmware.admin.vcloud+xml"; /** * All acceptable media types. @@ -220,7 +222,7 @@ public class VCloudDirectorMediaType { ADMIN_ORG_NETWORK, USER, ROLE, DEPLOY_VAPP_PARAMS, RECOMPOSE_VAPP_PARAMS, RELOCATE_VM_PARAMS, UNDEPLOY_VAPP_PARAMS, ADMIN_VDC, MEDIA_PARAMS, RUNTIME_INFO_SECTION, SCREEN_TICKET, VAPP_NETWORK, - TEXT_XML, ADMIN_VDC, NETWORK_POOL, ADMIN_ORG, ENTITY + TEXT_XML, ADMIN_VDC, NETWORK_POOL, ADMIN_ORG, ENTITY, ADMIN ); // NOTE These lists must be updated whenever a new media type constant is added. diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java index 12b093af8e..44cbe2b2b3 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java @@ -179,8 +179,6 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe .overrides(overrides) .build(); - System.err.println("*** " + endpoint + " ***"); - context = testSession.getUserContext(); adminContext = testSession.getAdminContext(); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java index 858e98d7db..ed8bc0a351 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java @@ -2,6 +2,7 @@ package org.jclouds.vcloud.director.v1_5.internal; import static com.google.common.base.Objects.equal; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; @@ -14,16 +15,19 @@ import org.jclouds.logging.log4j.config.Log4JLoggingModule; 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.VCloudDirectorAdminAsyncClient; import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminClient; 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 org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient; import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient; +import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.io.Closeables; @@ -73,11 +77,8 @@ public class VCloudDirectorTestSession implements Closeable { } } + private VCloudDirectorContext userContext; private RestContext adminContext; - private RestContext userContext; - - private User createdAdminUser; - private User createdUser; private VCloudDirectorTestSession(String provider, String identity, String credential, Properties overrides, String endpoint) { ContextBuilder builder = ContextBuilder.newBuilder(provider) @@ -85,74 +86,36 @@ public class VCloudDirectorTestSession implements Closeable { .endpoint(endpoint) .modules(ImmutableSet. of(new Log4JLoggingModule(), new SshjSshClientModule())) .overrides(overrides); - VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast(builder.build()); + userContext = VCloudDirectorContext.class.cast(builder.build()); - if (rootContext.getApi().getCurrentSession().getLinks().contains(Link.builder() - .rel("down") - .type("application/vnd.vmware.admin.vcloud+xml") - .href(URI.create(endpoint+"/admin/")) - .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(); - adminContext = rootContext.getAdminContext(); + // Get the admin context if the link exists + if (admin != null) { + adminContext = userContext.getAdminContext(); - Reference orgRef = Iterables.getFirst(rootContext.getApi().getOrgClient().getOrgList().getOrgs(), null) + // Lookup the user details + Reference orgRef = Iterables.getFirst(userContext.getApi().getOrgClient().getOrgList().getOrgs(), null) .toAdminReference(endpoint); - assertNotNull(orgRef, String.format(REF_REQ_LIVE, "admin org")); - - Reference userRef = Iterables.find(adminContext.getApi().getOrgClient().getOrg(orgRef.getHref()).getUsers(), + Reference userRef = Iterables.find( + adminContext.getApi().getOrgClient().getOrg(orgRef.getHref()).getUsers(), ReferencePredicates.nameEquals(adminContext.getApi().getCurrentSession().getUser())); - User user = adminContext.getApi().getUserClient().getUser(userRef.getHref()); - Reference orgAdmin = user.getRole(); - assertTrue(equal(orgAdmin.getName(), DefaultRoles.ORG_ADMIN.value()), "must give org admin or user-only credentials"); - String adminIdentity = "testAdmin"+BaseVCloudDirectorClientLiveTest.getTestDateTimeStamp(); - String adminCredential = "testAdminPassword"; - - createdAdminUser = rootContext.getAdminContext().getApi().getUserClient().createUser(orgRef.getHref(), User.builder() - .name(adminIdentity) - .password(adminCredential) - .description("test user with user-level privileges") - .role(orgAdmin) - .deployedVmQuota(BaseVCloudDirectorClientLiveTest.REQUIRED_ADMIN_VM_QUOTA) - .isEnabled(true) - .build()); - - Closeables.closeQuietly(rootContext); - - builder.credentials(adminIdentity, adminCredential); - adminContext = VCloudDirectorContext.class.cast(builder.build()).getAdminContext(); - - String userIdentity = "test"+BaseVCloudDirectorClientLiveTest.getTestDateTimeStamp(); - String userCredential = "testPassword"; - - createdUser = adminContext.getApi().getUserClient().createUser(orgRef.getHref(), User.builder() - .name(userIdentity) - .password(userCredential) - .description("test user with user-level privileges") - .role(BaseVCloudDirectorClientLiveTest.getRoleReferenceFor(DefaultRoles.USER.value(), adminContext)) - .deployedVmQuota(BaseVCloudDirectorClientLiveTest.REQUIRED_USER_VM_QUOTA) - .isEnabled(true) - .build()); - - builder.credentials(userIdentity, userCredential); - userContext = VCloudDirectorContext.class.cast(builder.build()); - } else { - userContext = rootContext; + // Check that the user has the org admin role + Reference userRole = user.getRole(); + assertEquals(userRole.getName(), DefaultRoles.ORG_ADMIN.value()); } } @Override public void close() { - if (createdUser != null) { - adminContext.getApi().getUserClient().deleteUser(createdUser.getHref()); - } - if (userContext != null) userContext.close(); - if (createdAdminUser != null) { - // TODO: may have to preserve root context if we can't delete the user for it's own context here - adminContext.getApi().getUserClient().deleteUser(createdAdminUser.getHref()); - } - if (adminContext != null) adminContext.close(); + Closeables.closeQuietly(userContext); + Closeables.closeQuietly(adminContext); } public RestContext getUserContext() {