Tentative on implementing the registerUserKeys API

It doesn't work yet because it requires a different authentication
strategy.
This commit is contained in:
andreisavu 2011-12-12 15:10:32 +02:00
parent 9c32c06c25
commit 59c2b2c8bc
4 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,112 @@
/**
* 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.domain;
import com.google.gson.annotations.SerializedName;
/**
* Representation of the API keypair response
*
* @author Andrei Savu
*/
public class ApiKeyPair implements Comparable<ApiKeyPair> {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String apiKey;
private String secretKey;
public Builder apiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}
public Builder secretKey(String secretKey) {
this.secretKey = secretKey;
return this;
}
public ApiKeyPair build() {
return new ApiKeyPair(apiKey, secretKey);
}
}
// for deserialization
ApiKeyPair() {
}
@SerializedName("apikey")
private String apiKey;
@SerializedName("secretkey")
private String secretKey;
public ApiKeyPair(String apiKey, String secretKey) {
this.apiKey = apiKey;
this.secretKey = secretKey;
}
public String getSecretKey() {
return secretKey;
}
public String getApiKey() {
return apiKey;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ApiKeyPair that = (ApiKeyPair) o;
if (apiKey != null ? !apiKey.equals(that.apiKey) : that.apiKey != null)
return false;
if (secretKey != null ? !secretKey.equals(that.secretKey) : that.secretKey != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = apiKey != null ? apiKey.hashCode() : 0;
result = 31 * result + (secretKey != null ? secretKey.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ApiKeyPair{" +
"apiKey='" + apiKey + '\'' +
", secretKey='" + secretKey + '\'' +
'}';
}
@Override
public int compareTo(ApiKeyPair arg0) {
return apiKey.compareTo(arg0.getApiKey());
}
}

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.ApiKeyPair;
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.CreateUserOptions; import org.jclouds.cloudstack.options.CreateUserOptions;
@ -59,6 +60,14 @@ public interface GlobalUserAsyncClient extends DomainUserAsyncClient {
@QueryParam("email") String email, @QueryParam("password") String hashedPassword, @QueryParam("email") String email, @QueryParam("password") String hashedPassword,
@QueryParam("firstname") String firstName, @QueryParam("lastname") String lastName, CreateUserOptions... options); @QueryParam("firstname") String firstName, @QueryParam("lastname") String lastName, CreateUserOptions... options);
/**
* @see GlobalUserClient#registerUserKeys
*/
@GET
@QueryParams(keys = "comand", values = "registerUserKeys")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<ApiKeyPair> registerUserKeys(@QueryParam("id") long userId);
/** /**
* @see GlobalUserClient#updateUser * @see GlobalUserClient#updateUser

View File

@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.domain.ApiKeyPair;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.options.CreateUserOptions; import org.jclouds.cloudstack.options.CreateUserOptions;
import org.jclouds.cloudstack.options.UpdateUserOptions; import org.jclouds.cloudstack.options.UpdateUserOptions;
@ -57,6 +58,15 @@ public interface GlobalUserClient extends DomainUserClient {
String firstName, String lastName, CreateUserOptions... options); String firstName, String lastName, CreateUserOptions... options);
/**
* This command allows a user to register for the developer API, returning a
* secret key and an API key
*
* @param userId the ID of the user
* @return
*/
ApiKeyPair registerUserKeys(long userId);
/** /**
* Update an user * Update an user
* *

View File

@ -18,10 +18,24 @@
*/ */
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.ApiKeyPair;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.Properties;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.cloudstack.options.UpdateUserOptions.Builder.userName; import static org.jclouds.cloudstack.options.UpdateUserOptions.Builder.userName;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
@ -57,6 +71,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()
.registerUserKeys(updatedUser.getId());
assertNotNull(apiKeys.getApiKey());
assertNotNull(apiKeys.getSecretKey());
checkAuthAsUser(apiKeys);
} finally { } finally {
if (testUser != null) { if (testUser != null) {
globalAdminClient.getUserClient().deleteUser(testUser.getId()); globalAdminClient.getUserClient().deleteUser(testUser.getId());
@ -66,4 +88,24 @@ public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest {
} }
private void checkAuthAsUser(ApiKeyPair keyPair) {
ComputeServiceContext context = new ComputeServiceContextFactory(setupRestProperties()).
createContext(provider, ImmutableSet.<Module>of(
new Log4JLoggingModule(), new SshjSshClientModule()), credentialsAsProperties(keyPair));
CloudStackClient client = CloudStackClient.class.cast(context.getProviderSpecificContext().getApi());
Set<Account> accounts = client.getAccountClient().listAccounts();
assert accounts.size() > 0;
context.close();
}
private Properties credentialsAsProperties(ApiKeyPair keyPair) {
Properties overrides = new Properties();
overrides.put(provider + ".identity", checkNotNull(keyPair.getApiKey()));
overrides.put(provider + ".credential", checkNotNull(keyPair.getSecretKey()));
return overrides;
}
} }