From 8591bdb1117a1a9827dcbe0d885cef74b3a336f4 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Sun, 11 Dec 2011 16:54:58 +0200 Subject: [PATCH] Implemented createUser global admin API and live test --- .../features/GlobalUserAsyncClient.java | 14 ++++ .../cloudstack/features/GlobalUserClient.java | 10 +++ .../cloudstack/options/CreateUserOptions.java | 72 +++++++++++++++++++ .../features/GlobalUserClientLiveTest.java | 34 ++++++++- 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateUserOptions.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserAsyncClient.java index ea8736dcaa..9e86f2ccfb 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserAsyncClient.java @@ -19,10 +19,13 @@ package org.jclouds.cloudstack.features; import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.options.CreateUserOptions; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import javax.ws.rs.Consumes; @@ -43,6 +46,17 @@ import javax.ws.rs.core.MediaType; @QueryParams(keys = "response", values = "json") public interface GlobalUserAsyncClient extends DomainUserAsyncClient { + /** + * @see GlobalUserClient#createUser + */ + @GET + @QueryParams(keys = "command", values = "createUser") + @SelectJson("user") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuturecreateUser(@QueryParam("username") String userName, @QueryParam("account") String accountName, + @QueryParam("email") String email, @QueryParam("password") String hashedPassword, + @QueryParam("firstname") String firstName, @QueryParam("lastname") String lastName, CreateUserOptions... options); /** * @see GlobalUserClient#deleteUser diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserClient.java index 693b214d11..d1cce118c6 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUserClient.java @@ -21,6 +21,7 @@ package org.jclouds.cloudstack.features; import java.util.concurrent.TimeUnit; import org.jclouds.cloudstack.domain.User; +import org.jclouds.cloudstack.options.CreateUserOptions; import org.jclouds.concurrent.Timeout; /** @@ -36,6 +37,15 @@ import org.jclouds.concurrent.Timeout; public interface GlobalUserClient extends DomainUserClient { + /** + * Create an user for an account that already exists + * + * @return + */ + User createUser(String userName, String accountName, String email, String hashedPassword, + String firstName, String lastName, CreateUserOptions... options); + + /** * Delete an user with the specified ID * diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateUserOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateUserOptions.java new file mode 100644 index 0000000000..d407ebcbfa --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateUserOptions.java @@ -0,0 +1,72 @@ +/** + * 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.cloudstack.options; + +import com.google.common.collect.ImmutableSet; +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Optional fields for user creation + * + * @author Andrei Savu + * @see + */ +public class CreateUserOptions extends BaseHttpRequestOptions { + + public static final CreateUserOptions NONE = new CreateUserOptions(); + + /** + * @param domainId The domain for the resource + */ + public CreateUserOptions domainId(long domainId) { + this.queryParameters.replaceValues("domainid", ImmutableSet.of(domainId + "")); + return this; + + } + + /** + * @param timezone Specifies a timezone for this command. For more information + * on the timezone parameter, see Time Zone Format. + */ + public CreateUserOptions timezone(String timezone) { + this.queryParameters.replaceValues("timezone", ImmutableSet.of(timezone)); + return this; + } + + public static class Builder { + + /** + * @see CreateUserOptions#domainId + */ + public static CreateUserOptions domainId(long domainId) { + CreateUserOptions options = new CreateUserOptions(); + return options.domainId(domainId); + } + + /** + * @see CreateUserOptions#timezone + */ + public static CreateUserOptions timezone(String timezone) { + CreateUserOptions options = new CreateUserOptions(); + return options.timezone(timezone); + } + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUserClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUserClientLiveTest.java index 45e6de5229..e4b26a9880 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUserClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUserClientLiveTest.java @@ -18,13 +18,45 @@ */ package org.jclouds.cloudstack.features; +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.cloudstack.domain.User; import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + /** * Tests behavior of {@code GlobaUserClient} - * */ @Test(groups = "live", singleThreaded = true, testName = "GlobalUserClientLiveTest") public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest { + private Account createTestAccount() { + return globalAdminClient.getAccountClient().createAccount( + prefix + "-account", Account.Type.USER, "dummy@example.com", + "First", "Last", "hashed-password"); + + } + + @Test + public void testCreateUser() { + Account testAccount = createTestAccount(); + User testUser = null; + try { + testUser = globalAdminClient.getUserClient().createUser(prefix + "-user", + testAccount.getName(), "dummy2@example.com", "md5-password", "First", "Last"); + + assertNotNull(testUser); + assertEquals(testUser.getName(), prefix + "-user"); + assertEquals(testUser.getAccount(), prefix + "-account"); + + } finally { + if (testUser != null) { + globalAdminClient.getUserClient().deleteUser(testUser.getId()); + } + globalAdminClient.getAccountClient().deleteAccount(testAccount.getId()); + } + + } + }