From d2eb9866c591e3e8ddf1e91ce7c243439aca4c12 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Sun, 11 Dec 2011 16:14:12 +0200 Subject: [PATCH] Implemented create / deleteAccount global admin APIs --- .../jclouds/cloudstack/domain/Account.java | 2 +- .../features/GlobalAccountAsyncClient.java | 39 ++++++++- .../features/GlobalAccountClient.java | 29 ++++++- .../cloudstack/features/GlobalUserClient.java | 1 - .../options/CreateAccountOptions.java | 87 +++++++++++++++++++ .../options/CreateNetworkOptions.java | 4 +- .../features/GlobalAccountClientLiveTest.java | 27 +++++- 7 files changed, 180 insertions(+), 9 deletions(-) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateAccountOptions.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Account.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Account.java index 7b1d7d76c4..81a2ecdff1 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Account.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Account.java @@ -273,7 +273,7 @@ public class Account extends ForwardingSet implements Comparable @Override public String toString() { - return name(); + return "" + code; } public static Type fromValue(String type) { diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountAsyncClient.java index 83dba8fa12..87485a93c7 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountAsyncClient.java @@ -18,15 +18,27 @@ */ package org.jclouds.cloudstack.features; +import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.options.CreateAccountOptions; +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; +import javax.ws.rs.GET; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import java.util.Set; /** * Provides asynchronous access to CloudStack Account features available to Global * Admin users. - * - * @author Adrian Cole + * + * @author Adrian Cole, Andrei Savu * @see @@ -34,5 +46,26 @@ import org.jclouds.rest.annotations.RequestFilters; @RequestFilters(QuerySigner.class) @QueryParams(keys = "response", values = "json") public interface GlobalAccountAsyncClient extends DomainAccountAsyncClient { - + + /** + * @see GlobalAccountClient#createAccount + */ + @GET + @QueryParams(keys = "command", values = "createAccount") + @SelectJson("account") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture createAccount(@QueryParam("username") String userName, + @QueryParam("accounttype") Account.Type accountType, @QueryParam("email") String email, + @QueryParam("firstname") String firstName, @QueryParam("lastname") String lastName, + @QueryParam("password") String hashedPassword, CreateAccountOptions... options); + + /** + * @see GlobalAccountClient#deleteAccount + */ + @GET + @QueryParams(keys = "command", values = "deleteAccount") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture deleteAccount(@QueryParam("id") long id); } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountClient.java index 503cdea24b..d7c09c8d5f 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalAccountClient.java @@ -20,13 +20,15 @@ package org.jclouds.cloudstack.features; import java.util.concurrent.TimeUnit; +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.cloudstack.options.CreateAccountOptions; import org.jclouds.concurrent.Timeout; /** * Provides synchronous access to CloudStack Account features available to Global * Admin users. * - * @author Adrian Cole + * @author Adrian Cole, Andrei Savu * @see @@ -34,5 +36,30 @@ import org.jclouds.concurrent.Timeout; @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) public interface GlobalAccountClient extends DomainAccountClient { + /** + * Create a new Cloudstack account + * + * @param userName unique username. + * @param accountType type of account + * @param email + * @param firstName + * @param lastName + * @param hashedPassword + * Hashed password (Default is MD5). If you wish to use any other + * hashing algorithm, you would need to write a custom authentication adapter See Docs section. + * @param options + * optional parameters + * @return + */ + Account createAccount(String userName, Account.Type accountType, String email, + String firstName, String lastName, String hashedPassword, CreateAccountOptions... options); + + /** + * Delete an account with the specified ID + * + * @param accountId + * @return + */ + Void deleteAccount(long accountId); } 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 21afb1ac83..a420fd3381 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 @@ -34,5 +34,4 @@ import org.jclouds.concurrent.Timeout; @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) public interface GlobalUserClient extends DomainUserClient { - } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateAccountOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateAccountOptions.java new file mode 100644 index 0000000000..10d05941d4 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateAccountOptions.java @@ -0,0 +1,87 @@ +/** + * 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 account creation + * + * @author Adrian Cole + * @see + */ +public class CreateAccountOptions extends BaseHttpRequestOptions { + + public static final CreateAccountOptions NONE = new CreateAccountOptions(); + + /** + * @param networkDomain network domain + */ + public CreateAccountOptions networkDomain(String networkDomain) { + this.queryParameters.replaceValues("networkdomain", ImmutableSet.of(networkDomain)); + return this; + } + + /** + * @param account an optional account for the resource + */ + public CreateAccountOptions account(String account) { + this.queryParameters.replaceValues("account", ImmutableSet.of(account)); + return this; + } + + /** + * @param domainId The domain for the resource + */ + public CreateAccountOptions domainId(long domainId) { + this.queryParameters.replaceValues("domainid", ImmutableSet.of(domainId + "")); + return this; + + } + + public static class Builder { + + /** + * @see CreateAccountOptions#networkDomain + */ + public static CreateAccountOptions networkDomain(String networkDomain) { + CreateAccountOptions options = new CreateAccountOptions(); + return options.networkDomain(networkDomain); + } + + /** + * @see CreateAccountOptions#account + */ + public static CreateAccountOptions account(String account) { + CreateAccountOptions options = new CreateAccountOptions(); + return options.account(account); + } + + /** + * @see CreateAccountOptions#domainId + */ + public static CreateAccountOptions domainId(long domainId) { + CreateAccountOptions options = new CreateAccountOptions(); + return options.domainId(domainId); + } + } +} diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateNetworkOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateNetworkOptions.java index e35d8400f1..72ad4fd4d8 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateNetworkOptions.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateNetworkOptions.java @@ -21,10 +21,10 @@ package org.jclouds.cloudstack.options; import com.google.common.collect.ImmutableSet; /** - * Options used to control what networks information is returned + * Optional fields for network creation * * @see * @author Adrian Cole */ diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalAccountClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalAccountClientLiveTest.java index f220b2bcd1..bfa2d00c5d 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalAccountClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalAccountClientLiveTest.java @@ -18,14 +18,39 @@ */ package org.jclouds.cloudstack.features; +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.logging.Logger; import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + /** * Tests behavior of {@code GlobalAccountClient} * - * @author Adrian Cole + * @author Adrian Cole, Andrei Savu */ @Test(groups = "live", singleThreaded = true, testName = "GlobalAccountClientLiveTest") public class GlobalAccountClientLiveTest extends BaseCloudStackClientLiveTest { + @Test + public void testCreateAndRemoveAccount() { + Account account = null; + try { + account = globalAdminClient.getAccountClient().createAccount( + prefix + "-account", Account.Type.USER, "dummy@example.com", + "First", "Last", "hashed-password"); + + assertNotNull(account); + assertEquals(account.getName(), prefix + "-account"); + assertEquals(account.getType(), Account.Type.USER); + + } finally { + if (account != null) { + globalAdminClient.getAccountClient().deleteAccount(account.getId()); + } + } + + } + }