create/get/delete user

This commit is contained in:
danikov 2012-03-12 08:24:20 +00:00
parent 0ee77f99ec
commit bdc09ee053
9 changed files with 345 additions and 23 deletions

View File

@ -153,6 +153,10 @@ public class VCloudDirectorMediaType {
public static final String ADMIN_ORG_NETWORK = "application/vnd.vmware.admin.orgNetwork+xml";
public static final String USER = "application/vnd.vmware.admin.user+xml";
public static final String ROLE = "application/vnd.vmware.admin.role+xml";
/**
* All acceptable media types.
*
@ -173,6 +177,6 @@ public class VCloudDirectorMediaType {
PUBLISH_CATALOG_PARAMS, GROUP, ORG_VAPP_TEMPLATE_LEASE_SETTINGS,
ORG_LEASE_SETTINGS, ORG_PASSWORD_POLICY_SETTINGS, ORG_LDAP_SETTINGS,
ORG_GENERAL_SETTINGS, ORG_EMAIL_SETTINGS, ORG_SETTINGS, ADMIN_NETWORK,
ADMIN_ORG_NETWORK
ADMIN_ORG_NETWORK, USER, ROLE
);
}

View File

@ -30,6 +30,8 @@ import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.testng.collections.Lists;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
@ -125,7 +127,7 @@ public class User
private Integer deployedVmQuota;
private Reference role;
private String password;
private List<Reference> groups;
private List<Reference> groups = Lists.newArrayList();
/**
* @see User#getFullName()
@ -267,7 +269,7 @@ public class User
* @see User#getGroups()
*/
public Builder groups(List<Reference> groups) {
this.groups = ImmutableList.copyOf(groups);
this.groups = groups == null ? null : ImmutableList.copyOf(groups);
return this;
}
@ -287,6 +289,14 @@ public class User
role, password, groups);
}
/**
* @see EntityType#getName()
*/
@Override
public Builder name(String name) {
this.name = name;
return this;
}
/**
* @see EntityType#getId()
@ -616,7 +626,7 @@ public class User
* {@link GroupsListType }
*/
public List<Reference> getGroups() {
return groups;
return groups == null ? Lists.<Reference>newArrayList() : groups;
}
@Override
@ -690,5 +700,4 @@ public class User
.add("password", password)
.add("groups", groups).toString();
}
}

View File

@ -18,8 +18,27 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
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.Path;
import javax.ws.rs.Produces;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.annotations.ExceptionParser;
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.User;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx;
import com.google.common.util.concurrent.ListenableFuture;
/**
* @see GroupClient
@ -27,13 +46,37 @@ import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest;
*/
@RequestFilters(AddVCloudAuthorizationToRequest.class)
public interface UserAsyncClient {
// POST /admin/org/{id}/users
/**
* @see UserClient#createUser(URI, User)
*/
@POST
@Path("/users")
@Consumes(VCloudDirectorMediaType.USER)
@Produces(VCloudDirectorMediaType.USER)
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<User> createUser(@EndpointParam URI orgRef,
@BinderParam(BindToXMLPayload.class) User user);
// GET /admin/user/{id}
/**
* @see UserClient#getUser(URI)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<User> getUser(@EndpointParam URI userRef);
// PUT /admin/user/{id}
// DELETE /admin/user/{id}
/**
* @see UserClient#deleteUser(URI)
*/
@DELETE
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Void> deleteUser(@EndpointParam URI userRef);
// POST /admin/user/{id}/action/unlock
}

View File

@ -18,10 +18,12 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.vcloud.director.v1_5.domain.Group;
import org.jclouds.vcloud.director.v1_5.domain.User;
/**
* Provides synchronous access to {@link Group} objects.
@ -31,13 +33,40 @@ import org.jclouds.vcloud.director.v1_5.domain.Group;
*/
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface UserClient {
// POST /admin/org/{id}/users
/**
* Creates or imports a user in an organization. The user could be enabled or disabled.
*
* <pre>
* POST /admin/org/{id}/users
* </pre>
*
* @param orgRef the reference for the org
* @return the created user
*/
User createUser(URI orgRef, User user);
// GET /admin/user/{id}
/**
* Retrieves a user. This entity could be enabled or disabled.
*
* <pre>
* GET /admin/user/{id}
* </pre>
*
* @param userRef the reference for the user
* @return a user
*/
User getUser(URI userRef);
// PUT /admin/user/{id}
// DELETE /admin/user/{id}
/**
* Deletes a user. Enabled and disabled users could be deleted.
*
* <pre>
* DELETE /admin/catalog/{id}
* </pre>
*/
void deleteUser(URI userRef);
// POST /admin/user/{id}/action/unlock
}

View File

@ -18,9 +18,16 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import java.net.URI;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Collections;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
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.User;
import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorRestClientExpectTest;
import org.testng.annotations.Test;
@ -32,17 +39,113 @@ import org.testng.annotations.Test;
@Test(groups = { "unit", "admin", "adminUser"}, singleThreaded = true, testName = "UserClientExpectTest")
public class UserClientExpectTest extends BaseVCloudDirectorRestClientExpectTest {
private Reference userRef = Reference.builder()
.href(URI.create(endpoint + "/admin/user/???"))
private Reference orgRef = Reference.builder()
.href(URI.create(endpoint + "/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build();
// POST /admin/org/{id}/users
private Reference userRef = Reference.builder()
.href(URI.create(endpoint + "/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
.build();
// GET /admin/user/{id}
@Test
public void testCreateUser() {
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
new VcloudHttpRequestPrimer()
.apiCommand("POST", "/admin/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/catalogs")
.xmlFilePayload("/user/createUserSource.xml", VCloudDirectorMediaType.USER)
.acceptMedia(VCloudDirectorMediaType.USER)
.httpRequestBuilder().build(),
new VcloudHttpResponsePrimer()
.xmlFilePayload("/user/createUser.xml", VCloudDirectorMediaType.USER)
.httpResponseBuilder().build());
User source = createUserSource();
User expected = createUser();
assertEquals(client.getUserClient().createUser(orgRef.getHref(), source), expected);
}
public static final User createUserSource() {
return User.builder()
.name("test")
.fullName("testFullName")
.emailAddress("test@test.com")
.telephone("555-1234")
.isEnabled(false)
.im("testIM")
.isAlertEnabled(false)
.alertEmailPrefix("testPrefix")
.alertEmail("testAlert@test.com")
.isExternal(false)
.isGroupRole(false)
.role(Reference.builder()
.type("application/vnd.vmware.admin.role+xml")
.name("vApp User")
.href(URI.create("https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"))
.build())
.password("password")
.groups(Collections.<Reference>emptyList())
.build();
}
public static final User createUser() {
return createUserSource().toBuilder()
.id("urn:vcloud:user:b37223f3-8792-477a-820f-334998f61cd6")
.type("application/vnd.vmware.admin.user+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
.link(Link.builder()
.rel("edit")
.type("application/vnd.vmware.admin.user+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
.build())
.link(Link.builder()
.rel("remove")
.href(URI.create("https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"))
.build())
.isLocked(false)
.isDefaultCached(false)
.storedVmQuota(0)
.deployedVmQuota(0)
.password(null)
.build();
}
@Test
public void testGetUser() {
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
new VcloudHttpRequestPrimer()
.apiCommand("GET", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
.acceptAnyMedia()
.httpRequestBuilder().build(),
new VcloudHttpResponsePrimer()
.xmlFilePayload("/user/user.xml", VCloudDirectorMediaType.USER)
.httpResponseBuilder().build());
User expected = user();
assertEquals(client.getUserClient().getUser(userRef.getHref()), expected);
}
public static final User user() {
return createUser().toBuilder()
.nameInSource("test")
.build();
}
// PUT /admin/user/{id}
// DELETE /admin/user/{id}
// POST /admin/user/{id}/action/unlock
@Test
public void testDeleteUser() {
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
new VcloudHttpRequestPrimer()
.apiCommand("DELETE", "/admin/user/b37223f3-8792-477a-820f-334998f61cd6")
.acceptAnyMedia()
.httpRequestBuilder().build(),
new VcloudHttpResponsePrimer()
.httpResponseBuilder().statusCode(204).build());
client.getUserClient().deleteUser(userRef.getHref());
}
}

View File

@ -18,10 +18,25 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_DEL;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.fail;
import java.net.URI;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.domain.Checks;
import org.jclouds.vcloud.director.v1_5.domain.Error;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.domain.User;
import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
* Tests live behavior of {@link AdminGroupClient}.
*
@ -30,28 +45,88 @@ import org.testng.annotations.Test;
@Test(groups = { "live", "admin", "adminUser" }, singleThreaded = true, testName = "UserClientLiveTest")
public class UserClientLiveTest extends BaseVCloudDirectorClientLiveTest {
public static final String GROUP = "admin group";
public static final String USER = "admin user";
/*
* Convenience references to API clients.
*/
UserClient userClient;
/*
* Shared state between dependant tests.
*/
private ReferenceType<?> orgRef;
private User user;
@Override
@BeforeClass(inheritGroups = true)
public void setupRequiredClients() {
userClient = context.getApi().getUserClient();
orgRef = Iterables.getFirst(context.getApi().getOrgClient().getOrgList().getOrgs(), null).toAdminReference(endpoint);
}
// POST /admin/org/{id}/users
@Test(testName = "POST /admin/org/{id}/users")
public void testCreateUser() {
User newUser = User.builder()
.name("test")
.fullName("testFullName")
.emailAddress("test@test.com")
.telephone("555-1234")
.isEnabled(false)
.im("testIM")
.isAlertEnabled(false)
.alertEmailPrefix("testPrefix")
.alertEmail("testAlert@test.com")
.isExternal(false)
.isGroupRole(false)
.role(Reference.builder() // FIXME: auto-fetch a role? or inject
.name("vApp User")
.href(URI.create("https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"))
.build())
.password("password")
// .group()
.build();
user = userClient.createUser(orgRef.getHref(), newUser);
// GET /admin/user/{id}
Checks.checkUser(user);
}
@Test(testName = "GET /admin/user/{id}",
dependsOnMethods = { "testCreateUser" })
public void testGetUser() {
user = userClient.getUser(user.getHref());
Checks.checkUser(user);
}
// PUT /admin/user/{id}
// DELETE /admin/user/{id}
// POST /admin/user/{id}/action/unlock
@Test(testName = "DELETE /admin/user/{id}",
dependsOnMethods = { "testGetUser" } )
public void testDeleteUser() {
userClient.deleteUser(user.getHref());
Error expected = Error.builder()
.message("No access to entity \"(com.vmware.vcloud.entity.user:"+
user.getId().substring("urn:vcloud:user:".length())+")\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
try {
user = userClient.getUser(user.getHref());
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
user = null;
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
}
if (user != null) { // guard against NPE on the .toStrings
assertNull(user, String.format(OBJ_DEL, USER, user.toString()));
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<User xmlns="http://www.vmware.com/vcloud/v1.5" name="test" id="urn:vcloud:user:b37223f3-8792-477a-820f-334998f61cd6" type="application/vnd.vmware.admin.user+xml" href="https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://vcloudbeta.bluelock.com/api/v1.5/schema/master.xsd">
<Link rel="edit" type="application/vnd.vmware.admin.user+xml" href="https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"/>
<Link rel="remove" href="https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"/>
<FullName>testFullName</FullName>
<EmailAddress>test@test.com</EmailAddress>
<Telephone>555-1234</Telephone>
<IsEnabled>false</IsEnabled>
<IsLocked>false</IsLocked>
<IM>testIM</IM>
<IsAlertEnabled>false</IsAlertEnabled>
<AlertEmailPrefix>testPrefix</AlertEmailPrefix>
<AlertEmail>testAlert@test.com</AlertEmail>
<IsExternal>false</IsExternal>
<IsDefaultCached>false</IsDefaultCached>
<IsGroupRole>false</IsGroupRole>
<StoredVmQuota>0</StoredVmQuota>
<DeployedVmQuota>0</DeployedVmQuota>
<Role type="application/vnd.vmware.admin.role+xml" name="vApp User" href="https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"/>
<GroupReferences/>
</User>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<User name="test" xmlns="http://www.vmware.com/vcloud/v1.5">
<FullName>testFullName</FullName>
<EmailAddress>test@test.com</EmailAddress>
<Telephone>555-1234</Telephone>
<IsEnabled>false</IsEnabled>
<IM>testIM</IM>
<IsAlertEnabled>false</IsAlertEnabled>
<AlertEmailPrefix>testPrefix</AlertEmailPrefix>
<AlertEmail>testAlert@test.com</AlertEmail>
<IsExternal>false</IsExternal>
<IsGroupRole>false</IsGroupRole>
<Role type="application/vnd.vmware.admin.role+xml" name="vApp User" href="https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"/>
<Password>password</Password>
<GroupReferences/>
</User>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<User xmlns="http://www.vmware.com/vcloud/v1.5" name="test" id="urn:vcloud:user:b37223f3-8792-477a-820f-334998f61cd6" type="application/vnd.vmware.admin.user+xml" href="https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://vcloudbeta.bluelock.com/api/v1.5/schema/master.xsd">
<Link rel="edit" type="application/vnd.vmware.admin.user+xml" href="https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"/>
<Link rel="remove" href="https://vcloudbeta.bluelock.com/api/admin/user/b37223f3-8792-477a-820f-334998f61cd6"/>
<FullName>testFullName</FullName>
<EmailAddress>test@test.com</EmailAddress>
<Telephone>555-1234</Telephone>
<IsEnabled>false</IsEnabled>
<IsLocked>false</IsLocked>
<IM>testIM</IM>
<NameInSource>test</NameInSource>
<IsAlertEnabled>false</IsAlertEnabled>
<AlertEmailPrefix>testPrefix</AlertEmailPrefix>
<AlertEmail>testAlert@test.com</AlertEmail>
<IsExternal>false</IsExternal>
<IsDefaultCached>false</IsDefaultCached>
<IsGroupRole>false</IsGroupRole>
<StoredVmQuota>0</StoredVmQuota>
<DeployedVmQuota>0</DeployedVmQuota>
<Role type="application/vnd.vmware.admin.role+xml" name="vApp User" href="https://vcloudbeta.bluelock.com/api/admin/role/ff1e0c91-1288-3664-82b7-a6fa303af4d1"/>
<GroupReferences/>
</User>