Live test disable / enableUser API

This commit is contained in:
andreisavu 2011-12-12 22:15:05 +02:00
parent 7b6ebefbd4
commit 616485364b
7 changed files with 91 additions and 17 deletions

View File

@ -30,6 +30,25 @@ import com.google.gson.annotations.SerializedName;
*/ */
public class User implements Comparable<User> { public class User implements Comparable<User> {
public static enum State {
ENABLED,
DISABLED,
UNKNOWN;
public static State fromValue(String value) {
try {
return valueOf(value.toUpperCase());
} catch(IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return name().toLowerCase();
}
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -41,7 +60,7 @@ public class User implements Comparable<User> {
private String lastName; private String lastName;
private String email; private String email;
private Date created; private Date created;
private String state; private State state;
private String account; private String account;
private Account.Type accountType; private Account.Type accountType;
private String domain; private String domain;
@ -80,7 +99,7 @@ public class User implements Comparable<User> {
return this; return this;
} }
public Builder state(String state) { public Builder state(State state) {
this.state = state; this.state = state;
return this; return this;
} }
@ -143,7 +162,7 @@ public class User implements Comparable<User> {
private String lastName; private String lastName;
private String email; private String email;
private Date created; private Date created;
private String state; private State state;
private String account; private String account;
@SerializedName("accounttype") @SerializedName("accounttype")
private Account.Type accountType; private Account.Type accountType;
@ -157,7 +176,7 @@ public class User implements Comparable<User> {
@SerializedName("secretkey") @SerializedName("secretkey")
private String secretKey; private String secretKey;
public User(long id, String name, String firstname, String lastname, String email, Date created, String state, public User(long id, String name, String firstname, String lastname, String email, Date created, State state,
String account, Type accountType, String domain, long domainId, String timeZone, String apiKey, String account, Type accountType, String domain, long domainId, String timeZone, String apiKey,
String secretKey) { String secretKey) {
this.id = id; this.id = id;
@ -228,7 +247,7 @@ public class User implements Comparable<User> {
* *
* @return the user state * @return the user state
*/ */
public String getState() { public State getState() {
return state; return state;
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.ListUsersOptions; import org.jclouds.cloudstack.options.ListUsersOptions;
@ -26,6 +27,7 @@ import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.Unwrap;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
@ -73,9 +75,9 @@ public interface DomainUserAsyncClient {
*/ */
@GET @GET
@QueryParams(keys = "command", values = "disableUser") @QueryParams(keys = "command", values = "disableUser")
@SelectJson("user") @Unwrap
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class) @ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<User> disableUser(@QueryParam("id") long userId); ListenableFuture<AsyncCreateResponse> disableUser(@QueryParam("id") long userId);
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.options.ListUsersOptions; import org.jclouds.cloudstack.options.ListUsersOptions;
import org.jclouds.concurrent.Timeout; import org.jclouds.concurrent.Timeout;
@ -57,6 +58,6 @@ public interface DomainUserClient {
/** /**
* Disable a user with a specific ID * Disable a user with a specific ID
*/ */
User disableUser(long userId); AsyncCreateResponse disableUser(long userId);
} }

View File

@ -39,6 +39,7 @@ import org.jclouds.cloudstack.domain.PublicIPAddress;
import org.jclouds.cloudstack.domain.SecurityGroup; import org.jclouds.cloudstack.domain.SecurityGroup;
import org.jclouds.cloudstack.domain.Template; import org.jclouds.cloudstack.domain.Template;
import org.jclouds.cloudstack.domain.TemplateExtraction; import org.jclouds.cloudstack.domain.TemplateExtraction;
import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.domain.VirtualMachine; import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.domain.JsonBall; import org.jclouds.domain.JsonBall;
import org.jclouds.json.Json; import org.jclouds.json.Json;
@ -62,6 +63,7 @@ public class ParseTypedAsyncJob implements Function<AsyncJob<Map<String, JsonBal
@VisibleForTesting @VisibleForTesting
@Named("jclouds.cloudstack.jobresult-type-map") @Named("jclouds.cloudstack.jobresult-type-map")
Map<String, Class<?>> typeMap = ImmutableMap.<String, Class<?>>builder() Map<String, Class<?>> typeMap = ImmutableMap.<String, Class<?>>builder()
.put("user", User.class)
.put("securitygroup", SecurityGroup.class) .put("securitygroup", SecurityGroup.class)
.put("portforwardingrule", PortForwardingRule.class) .put("portforwardingrule", PortForwardingRule.class)
.put("ipforwardingrule", IPForwardingRule.class) .put("ipforwardingrule", IPForwardingRule.class)

View File

@ -18,26 +18,37 @@
*/ */
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.AsyncJob;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.logging.Logger;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.Set; import java.util.Set;
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestAccount;
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
/** /**
* Tests behavior of {@code DomainUserClient} * Tests behavior of {@code DomainUserClient}
*
*/ */
@Test(groups = "live", singleThreaded = true, testName = "DomainUserClientLiveTest") @Test(groups = "live", singleThreaded = true, testName = "DomainUserClientLiveTest")
public class DomainUserClientLiveTest extends BaseCloudStackClientLiveTest { public class DomainUserClientLiveTest extends BaseCloudStackClientLiveTest {
@Test @Test
public void testListUsers() { public void testListUsers() {
assert domainAdminEnabled;
Set<User> users = domainAdminClient.getUserClient().listUsers(); Set<User> users = domainAdminClient.getUserClient().listUsers();
assert users.size() > 0; assert users.size() > 0;
assert users.contains(user); // contains the current user assert users.contains(user); // contains the current user
for(User user : users) { for (User user : users) {
checkUser(user); checkUser(user);
} }
} }
@ -47,4 +58,36 @@ public class DomainUserClientLiveTest extends BaseCloudStackClientLiveTest {
assert user.getAccount() != null; assert user.getAccount() != null;
assert user.getDomain() != null; assert user.getDomain() != null;
} }
@Test
public void testEnableDisableUser() {
assert globalAdminEnabled && domainAdminEnabled;
Account testAccount = null;
User testUser = null;
try {
testAccount = createTestAccount(globalAdminClient, prefix);
testUser = createTestUser(globalAdminClient, testAccount, prefix);
AsyncCreateResponse response = domainAdminClient.getUserClient().disableUser(testUser.getId());
assertNotNull(response);
assertTrue(jobComplete.apply(response.getJobId()));
AsyncJob<User> job = domainAdminClient.getAsyncJobClient().getAsyncJob(response.getJobId());
assertNotNull(job);
assertEquals(job.getResult().getState(), User.State.DISABLED);
User updated = domainAdminClient.getUserClient().enableUser(testUser.getId());
assertNotNull(updated);
assertEquals(updated.getState(), User.State.ENABLED);
} finally {
if (testUser != null) {
globalAdminClient.getUserClient().deleteUser(testUser.getId());
}
if (testAccount != null) {
globalAdminClient.getAccountClient().deleteAccount(testAccount.getId());
}
}
}
} }

View File

@ -23,6 +23,7 @@ import com.google.common.collect.Iterables;
import com.google.inject.Module; import com.google.inject.Module;
import org.jclouds.cloudstack.CloudStackClient; import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.CloudStackContext; import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.CloudStackGlobalClient;
import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.ApiKeyPair; import org.jclouds.cloudstack.domain.ApiKeyPair;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
@ -46,20 +47,25 @@ import static org.testng.Assert.assertNotNull;
@Test(groups = "live", singleThreaded = true, testName = "GlobalUserClientLiveTest") @Test(groups = "live", singleThreaded = true, testName = "GlobalUserClientLiveTest")
public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest { public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest {
private Account createTestAccount() { public static Account createTestAccount(CloudStackGlobalClient client, String prefix) {
return globalAdminClient.getAccountClient().createAccount( return client.getAccountClient().createAccount(
prefix + "-account", Account.Type.USER, "dummy@example.com", prefix + "-account", Account.Type.USER, "dummy@example.com",
"First", "Last", "hashed-password"); "First", "Last", "hashed-password");
}
public static User createTestUser(CloudStackGlobalClient client, Account account, String prefix) {
return client.getUserClient().createUser(prefix + "-user",
account.getName(), "dummy2@example.com", "md5-password", "First", "Last");
} }
@Test @Test
public void testCreateUser() { public void testCreateUser() {
Account testAccount = createTestAccount(); assert globalAdminEnabled;
Account testAccount = createTestAccount(globalAdminClient, prefix);
User testUser = null; User testUser = null;
try { try {
testUser = globalAdminClient.getUserClient().createUser(prefix + "-user", testUser = createTestUser(globalAdminClient, testAccount, prefix);
testAccount.getName(), "dummy2@example.com", "md5-password", "First", "Last");
assertNotNull(testUser); assertNotNull(testUser);
assertEquals(testUser.getName(), prefix + "-user"); assertEquals(testUser.getName(), prefix + "-user");
@ -71,13 +77,14 @@ public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest {
assertNotNull(updatedUser); assertNotNull(updatedUser);
assertEquals(updatedUser.getName(), prefix + "-user-2"); assertEquals(updatedUser.getName(), prefix + "-user-2");
/*
ApiKeyPair apiKeys = globalAdminClient.getUserClient() ApiKeyPair apiKeys = globalAdminClient.getUserClient()
.registerUserKeys(updatedUser.getId()); .registerUserKeys(updatedUser.getId());
assertNotNull(apiKeys.getApiKey()); assertNotNull(apiKeys.getApiKey());
assertNotNull(apiKeys.getSecretKey()); assertNotNull(apiKeys.getSecretKey());
checkAuthAsUser(apiKeys); checkAuthAsUser(apiKeys); */
} finally { } finally {
if (testUser != null) { if (testUser != null) {

View File

@ -96,7 +96,7 @@ public class ListAccountsResponseTest extends BaseSetParserTest<Account> {
ImmutableSet.of(User.builder().id(505).name("jclouds").firstName("Adrian").lastName("Cole") ImmutableSet.of(User.builder().id(505).name("jclouds").firstName("Adrian").lastName("Cole")
.email("adrian@jclouds.org") .email("adrian@jclouds.org")
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-04-19T01:57:24+0000")) .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-04-19T01:57:24+0000"))
.state("enabled").account("jclouds").accountType(Type.USER).domainId(457) .state(User.State.ENABLED).account("jclouds").accountType(Type.USER).domainId(457)
.domain("AA000062-jclouds-dev").apiKey("APIKEY").secretKey("SECRETKEY").build())).build()); .domain("AA000062-jclouds-dev").apiKey("APIKEY").secretKey("SECRETKEY").build())).build());
} }