mirror of https://github.com/apache/jclouds.git
set default credential type to apiAccessKeyCredentials and a property to use passwordCredentials
This commit is contained in:
parent
4ba1da5d20
commit
6f8c633ff2
|
@ -0,0 +1,46 @@
|
||||||
|
/**
|
||||||
|
* 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.openstack.keystone.v2_0.config;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
import com.google.common.base.CaseFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration properties and constants used in Keystone connections.
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public enum CredentialType {
|
||||||
|
|
||||||
|
API_ACCESS_KEY_CREDENTIALS,
|
||||||
|
|
||||||
|
PASSWORD_CREDENTIALS;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CredentialType fromValue(String credentialType) {
|
||||||
|
return valueOf(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(credentialType,
|
||||||
|
"credentialType")));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,7 +24,6 @@ import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
@ -38,21 +37,19 @@ import org.jclouds.location.Provider;
|
||||||
import org.jclouds.openstack.Authentication;
|
import org.jclouds.openstack.Authentication;
|
||||||
import org.jclouds.openstack.keystone.v2_0.ServiceAsyncClient;
|
import org.jclouds.openstack.keystone.v2_0.ServiceAsyncClient;
|
||||||
import org.jclouds.openstack.keystone.v2_0.domain.Access;
|
import org.jclouds.openstack.keystone.v2_0.domain.Access;
|
||||||
import org.jclouds.openstack.keystone.v2_0.domain.PasswordCredentials;
|
import org.jclouds.openstack.keystone.v2_0.functions.AuthenticateApiAccessKeyCredentials;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.functions.AuthenticatePasswordCredentials;
|
||||||
import org.jclouds.openstack.keystone.v2_0.handlers.RetryOnRenew;
|
import org.jclouds.openstack.keystone.v2_0.handlers.RetryOnRenew;
|
||||||
import org.jclouds.rest.AsyncClientFactory;
|
import org.jclouds.rest.AsyncClientFactory;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Splitter;
|
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.base.Throwables;
|
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.cache.CacheLoader;
|
import com.google.common.cache.CacheLoader;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
import com.google.inject.TypeLiteral;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -63,9 +60,8 @@ public class KeyStoneAuthenticationModule extends AbstractModule {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
bind(new TypeLiteral<Function<Credentials, Access>>() {
|
|
||||||
}).to(GetAccess.class);
|
|
||||||
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(RetryOnRenew.class);
|
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(RetryOnRenew.class);
|
||||||
|
bind(CredentialType.class).toProvider(CredentialTypeFromPropertyOrDefault.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,45 +85,48 @@ public class KeyStoneAuthenticationModule extends AbstractModule {
|
||||||
return factory.create(ServiceAsyncClient.class);
|
return factory.create(ServiceAsyncClient.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
static class CredentialTypeFromPropertyOrDefault implements javax.inject.Provider<CredentialType> {
|
||||||
|
/**
|
||||||
|
* use optional injection to supply a default value for credential type. so that we don't have
|
||||||
|
* to set a default property.
|
||||||
|
*/
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(KeystoneProperties.CREDENTIAL_TYPE)
|
||||||
|
String credentialType = CredentialType.API_ACCESS_KEY_CREDENTIALS.toString();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CredentialType get() {
|
||||||
|
return CredentialType.fromValue(credentialType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
protected Function<Credentials, Access> authenticationMethodForCredentialType(CredentialType credentialType,
|
||||||
|
AuthenticatePasswordCredentials authenticatePasswordCredentials,
|
||||||
|
AuthenticateApiAccessKeyCredentials authenticateApiAccessKeyCredentials) {
|
||||||
|
Function<Credentials, Access> authMethod;
|
||||||
|
switch (credentialType) {
|
||||||
|
case PASSWORD_CREDENTIALS:
|
||||||
|
authMethod = authenticatePasswordCredentials;
|
||||||
|
break;
|
||||||
|
case API_ACCESS_KEY_CREDENTIALS:
|
||||||
|
authMethod = authenticateApiAccessKeyCredentials;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("credential type not supported: " + credentialType);
|
||||||
|
}
|
||||||
|
// regardless of how we authenticate, we should retry if there is a timeout exception logging
|
||||||
|
// in.
|
||||||
|
return new RetryOnTimeOutExceptionFunction<Credentials, Access>(authMethod);
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Provider
|
@Provider
|
||||||
protected Credentials provideAuthenticationCredentials(@Named(Constants.PROPERTY_IDENTITY) String userOrApiKey,
|
protected Credentials provideAuthenticationCredentials(@Named(Constants.PROPERTY_IDENTITY) String userOrAccessKey,
|
||||||
@Named(Constants.PROPERTY_CREDENTIAL) String keyOrSecretKey) {
|
@Named(Constants.PROPERTY_CREDENTIAL) String keyOrSecretKey) {
|
||||||
return new Credentials(userOrApiKey, keyOrSecretKey);
|
return new Credentials(userOrAccessKey, keyOrSecretKey);
|
||||||
}
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public static class GetAccess extends RetryOnTimeOutExceptionFunction<Credentials, Access> {
|
|
||||||
|
|
||||||
// passing factory here to avoid a circular dependency on
|
|
||||||
// OpenStackAuthAsyncClient resolving ServiceAsyncClient
|
|
||||||
@Inject
|
|
||||||
public GetAccess(final AsyncClientFactory factory) {
|
|
||||||
super(new Function<Credentials, Access>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Access apply(Credentials input) {
|
|
||||||
// TODO: nice error messages, etc.
|
|
||||||
Iterable<String> tenantIdUsername = Splitter.on(':').split(input.identity);
|
|
||||||
String tenantId = Iterables.get(tenantIdUsername, 0);
|
|
||||||
String username = Iterables.get(tenantIdUsername, 1);
|
|
||||||
PasswordCredentials passwordCredentials = PasswordCredentials.createWithUsernameAndPassword(username,
|
|
||||||
input.credential);
|
|
||||||
try {
|
|
||||||
return factory.create(ServiceAsyncClient.class).authenticateTenantWithCredentials(tenantId,
|
|
||||||
passwordCredentials).get();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw Throwables.propagate(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "authenticate()";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: what is the timeout of the session token? modify default accordingly
|
// TODO: what is the timeout of the session token? modify default accordingly
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/**
|
||||||
|
* 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.openstack.keystone.v2_0.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration properties and constants used in Keystone connections.
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public interface KeystoneProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of credentials used to log into the auth service.
|
||||||
|
*
|
||||||
|
* <h3>valid values</h3>
|
||||||
|
* <ul>
|
||||||
|
* <li>apiAccessKeyCredentials</li>
|
||||||
|
* <li>passwordCredentials</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @see CredentialType
|
||||||
|
* @see <a href="http://docs.openstack.org/api/openstack-identity-service/2.0/content/POST_authenticate_v2.0_tokens_Service_API_Client_Operations.html"
|
||||||
|
* />
|
||||||
|
*/
|
||||||
|
public static final String CREDENTIAL_TYPE = "jclouds.keystone.credential-type";
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
/**
|
||||||
|
* 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.openstack.keystone.v2_0.functions;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Credentials;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.ServiceAsyncClient;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.domain.Access;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.domain.ApiAccessKeyCredentials;
|
||||||
|
import org.jclouds.rest.AsyncClientFactory;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
|
public class AuthenticateApiAccessKeyCredentials implements Function<Credentials, Access> {
|
||||||
|
private final AsyncClientFactory factory;
|
||||||
|
|
||||||
|
// passing factory here to avoid a circular dependency on
|
||||||
|
// OpenStackAuthAsyncClient resolving ServiceAsyncClient
|
||||||
|
@Inject
|
||||||
|
public AuthenticateApiAccessKeyCredentials(AsyncClientFactory factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Access apply(Credentials input) {
|
||||||
|
// TODO: tenantID may not be present
|
||||||
|
Iterable<String> tenantIdUsernameOrAccessKey = Splitter.on(':').split(input.identity);
|
||||||
|
String tenantId = Iterables.get(tenantIdUsernameOrAccessKey, 0);
|
||||||
|
String usernameOrAccessKey = Iterables.get(tenantIdUsernameOrAccessKey, 1);
|
||||||
|
String passwordOrSecretKey = input.credential;
|
||||||
|
|
||||||
|
ServiceAsyncClient client = factory.create(ServiceAsyncClient.class);
|
||||||
|
try {
|
||||||
|
ApiAccessKeyCredentials apiAccessKeyCredentials = ApiAccessKeyCredentials.createWithAccessKeyAndSecretKey(
|
||||||
|
usernameOrAccessKey, passwordOrSecretKey);
|
||||||
|
return client.authenticateTenantWithCredentials(tenantId, apiAccessKeyCredentials).get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw Throwables.propagate(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "authenticateApiAccessKeyCredentials()";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
/**
|
||||||
|
* 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.openstack.keystone.v2_0.functions;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Credentials;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.ServiceAsyncClient;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.domain.Access;
|
||||||
|
import org.jclouds.openstack.keystone.v2_0.domain.PasswordCredentials;
|
||||||
|
import org.jclouds.rest.AsyncClientFactory;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
|
public class AuthenticatePasswordCredentials implements Function<Credentials, Access> {
|
||||||
|
private final AsyncClientFactory factory;
|
||||||
|
// passing factory here to avoid a circular dependency on
|
||||||
|
// OpenStackAuthAsyncClient resolving ServiceAsyncClient
|
||||||
|
@Inject
|
||||||
|
public AuthenticatePasswordCredentials(AsyncClientFactory factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Access apply(Credentials input) {
|
||||||
|
// TODO: tenantID may not be present
|
||||||
|
Iterable<String> tenantIdUsernameOrAccessKey = Splitter.on(':').split(input.identity);
|
||||||
|
String tenantId = Iterables.get(tenantIdUsernameOrAccessKey, 0);
|
||||||
|
String usernameOrAccessKey = Iterables.get(tenantIdUsernameOrAccessKey, 1);
|
||||||
|
String passwordOrSecretKey = input.credential;
|
||||||
|
|
||||||
|
ServiceAsyncClient client = factory.create(ServiceAsyncClient.class);
|
||||||
|
try {
|
||||||
|
PasswordCredentials passwordCredentials = PasswordCredentials.createWithUsernameAndPassword(
|
||||||
|
usernameOrAccessKey, passwordOrSecretKey);
|
||||||
|
return client.authenticateTenantWithCredentials(tenantId, passwordCredentials).get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw Throwables.propagate(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "authenticatePasswordCredentials()";
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.openstack.keystone.v2_0.internal;
|
package org.jclouds.openstack.keystone.v2_0.internal;
|
||||||
|
|
||||||
|
import static java.lang.String.format;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import org.jclouds.http.HttpRequest;
|
import org.jclouds.http.HttpRequest;
|
||||||
|
@ -35,21 +36,39 @@ import com.google.common.net.HttpHeaders;
|
||||||
*/
|
*/
|
||||||
public class BaseKeyStoneRestClientExpectTest<S> extends BaseRestClientExpectTest<S> {
|
public class BaseKeyStoneRestClientExpectTest<S> extends BaseRestClientExpectTest<S> {
|
||||||
|
|
||||||
public BaseKeyStoneRestClientExpectTest() {
|
protected static final String tenantId = "12346637803162";
|
||||||
// tenantId:username
|
|
||||||
identity = "12346637803162:user@jclouds.org";
|
|
||||||
credential = "Password1234";
|
|
||||||
}
|
|
||||||
|
|
||||||
protected HttpRequest initialAuth = HttpRequest
|
protected static final String username = "user@jclouds.org";
|
||||||
|
protected static final String password = "Password1234";
|
||||||
|
protected static final HttpRequest initialAuthWithPasswordCredentials = HttpRequest
|
||||||
.builder()
|
.builder()
|
||||||
.method("POST")
|
.method("POST")
|
||||||
.endpoint(URI.create("http://localhost:5000/v2.0/tokens"))
|
.endpoint(URI.create("http://localhost:5000/v2.0/tokens"))
|
||||||
.headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json"))
|
.headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json"))
|
||||||
.payload(
|
.payload(
|
||||||
payloadFromStringWithContentType(
|
payloadFromStringWithContentType(
|
||||||
"{\"auth\":{\"passwordCredentials\":{\"username\":\"user@jclouds.org\",\"password\":\"Password1234\"},\"tenantId\":\"12346637803162\"}}",
|
format(
|
||||||
"application/json")).build();
|
"{\"auth\":{\"passwordCredentials\":{\"username\":\"%s\",\"password\":\"%s\"},\"tenantId\":\"%s\"}}",
|
||||||
|
username, password, tenantId), "application/json")).build();
|
||||||
|
|
||||||
|
protected static final String accessKey = "FH6FU8GMZFLKP5BUR2X1";
|
||||||
|
protected static final String secretKey = "G4QWed0lh5SH7kBrcvOM1cHygKWk81EBt+Hr1dsl";
|
||||||
|
protected static final HttpRequest initialAuthWithApiAccessKeyCredentials = HttpRequest
|
||||||
|
.builder()
|
||||||
|
.method("POST")
|
||||||
|
.endpoint(URI.create("http://localhost:5000/v2.0/tokens"))
|
||||||
|
.headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json"))
|
||||||
|
.payload(
|
||||||
|
payloadFromStringWithContentType(
|
||||||
|
format(
|
||||||
|
"{\"auth\":{\"apiAccessKeyCredentials\":{\"accessKey\":\"%s\",\"secretKey\":\"%s\"},\"tenantId\":\"%s\"}}",
|
||||||
|
accessKey, secretKey, tenantId), "application/json")).build();
|
||||||
|
|
||||||
|
public BaseKeyStoneRestClientExpectTest() {
|
||||||
|
identity = tenantId + ":" + accessKey;
|
||||||
|
credential = secretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String authToken = "Auth_4f173437e4b013bee56d1007";
|
protected String authToken = "Auth_4f173437e4b013bee56d1007";
|
||||||
|
|
||||||
|
|
|
@ -153,18 +153,19 @@ public abstract class BaseRestClientExpectTest<S> {
|
||||||
|
|
||||||
public Payload payloadFromResourceWithContentType(String resource, String contentType) {
|
public Payload payloadFromResourceWithContentType(String resource, String contentType) {
|
||||||
try {
|
try {
|
||||||
return payloadFromStringWithContentType(Strings2.toStringAndClose(getClass().getResourceAsStream(resource)), contentType);
|
return payloadFromStringWithContentType(Strings2.toStringAndClose(getClass().getResourceAsStream(resource)),
|
||||||
|
contentType);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw Throwables.propagate(e);
|
throw Throwables.propagate(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Payload payloadFromString(String payload) {
|
public static Payload payloadFromString(String payload) {
|
||||||
return Payloads.newStringPayload(payload);
|
return Payloads.newStringPayload(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Payload payloadFromStringWithContentType(String payload, String contentType) {
|
public static Payload payloadFromStringWithContentType(String payload, String contentType) {
|
||||||
Payload p = Payloads.newStringPayload(payload);
|
Payload p = Payloads.newStringPayload(payload);
|
||||||
p.getContentMetadata().setContentType(contentType);
|
p.getContentMetadata().setContentType(contentType);
|
||||||
return p;
|
return p;
|
||||||
|
@ -347,9 +348,22 @@ public abstract class BaseRestClientExpectTest<S> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpResponse apply(HttpRequest input) {
|
public HttpResponse apply(HttpRequest input) {
|
||||||
if (!(requestToResponse.containsKey(input)))
|
if (!(requestToResponse.containsKey(input))) {
|
||||||
|
|
||||||
|
StringBuilder payload = new StringBuilder("\n");
|
||||||
|
payload.append("the following request is not configured:\n");
|
||||||
|
payload.append("----------------------------------------\n");
|
||||||
|
payload.append(renderRequest(input));
|
||||||
|
payload.append("----------------------------------------\n");
|
||||||
|
payload.append("configured requests:\n");
|
||||||
|
for (HttpRequest request : requestToResponse.keySet()) {
|
||||||
|
payload.append("----------------------------------------\n");
|
||||||
|
payload.append(renderRequest(request));
|
||||||
|
}
|
||||||
return HttpResponse.builder().statusCode(500).message("no response configured for request").payload(
|
return HttpResponse.builder().statusCode(500).message("no response configured for request").payload(
|
||||||
Payloads.newStringPayload(renderRequest(input))).build();
|
Payloads.newStringPayload(payload.toString())).build();
|
||||||
|
|
||||||
|
}
|
||||||
HttpResponse response = requestToResponse.get(input);
|
HttpResponse response = requestToResponse.get(input);
|
||||||
// in case hashCode/equals doesn't do a full content check
|
// in case hashCode/equals doesn't do a full content check
|
||||||
assertEquals(renderRequest(input), renderRequest(bimap.inverse().get(response)));
|
assertEquals(renderRequest(input), renderRequest(bimap.inverse().get(response)));
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/**
|
||||||
|
* 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.openstack.nova.v1_1.features;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.jclouds.http.HttpRequest;
|
||||||
|
import org.jclouds.http.HttpResponse;
|
||||||
|
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaRestClientExpectTest;
|
||||||
|
import org.jclouds.openstack.nova.v1_1.parse.ParseServerListTest;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @see KeystoneProperties#CREDENTIAL_TYPE
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit", testName = "PasswordAuthenticationExpectTest")
|
||||||
|
public class PasswordAuthenticationExpectTest extends BaseNovaRestClientExpectTest {
|
||||||
|
|
||||||
|
public PasswordAuthenticationExpectTest() {
|
||||||
|
provider = "openstack-nova";
|
||||||
|
identity = tenantId + ":" + username;
|
||||||
|
credential = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this reflects the properties that a user would pass to createContext
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Properties setupProperties() {
|
||||||
|
Properties contextProperties = super.setupProperties();
|
||||||
|
contextProperties.setProperty("jclouds.keystone.credential-type", "passwordCredentials");
|
||||||
|
return contextProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testListServersWhenResponseIs2xx() throws Exception {
|
||||||
|
HttpRequest listServers = HttpRequest.builder().method("GET").endpoint(
|
||||||
|
URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers")).headers(
|
||||||
|
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||||
|
authToken).build()).build();
|
||||||
|
|
||||||
|
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200).payload(
|
||||||
|
payloadFromResource("/server_list.json")).build();
|
||||||
|
|
||||||
|
ServerClient clientWhenServersExist = requestsSendResponses(initialAuthWithPasswordCredentials,
|
||||||
|
responseWithAccess, listServers, listServersResponse).getServerClient();
|
||||||
|
|
||||||
|
assertEquals(clientWhenServersExist.listServers().toString(), new ParseServerListTest().expected().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -27,49 +27,46 @@ public class ServerClientExpectTest extends BaseNovaRestClientExpectTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testListServersWhenResponseIs2xx() throws Exception {
|
public void testListServersWhenResponseIs2xx() throws Exception {
|
||||||
HttpRequest listServers = HttpRequest.builder().method("GET")
|
HttpRequest listServers = HttpRequest.builder().method("GET").endpoint(
|
||||||
.endpoint(URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers"))
|
URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers")).headers(
|
||||||
.headers(ImmutableMultimap.<String, String> builder()
|
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||||
.put("Accept", "application/json")
|
authToken).build()).build();
|
||||||
.put("X-Auth-Token", authToken).build()).build();
|
|
||||||
|
|
||||||
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200).payload(
|
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200).payload(
|
||||||
payloadFromResource("/server_list.json")).build();
|
payloadFromResource("/server_list.json")).build();
|
||||||
|
|
||||||
ServerClient clientWhenServersExist = requestsSendResponses(initialAuth, responseWithAccess, listServers,
|
ServerClient clientWhenServersExist = requestsSendResponses(initialAuthWithApiAccessKeyCredentials,
|
||||||
listServersResponse).getServerClient();
|
responseWithAccess, listServers, listServersResponse).getServerClient();
|
||||||
|
|
||||||
assertEquals(clientWhenServersExist.listServers().toString(), new ParseServerListTest().expected().toString());
|
assertEquals(clientWhenServersExist.listServers().toString(), new ParseServerListTest().expected().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testListServersWhenReponseIs404IsEmpty() throws Exception {
|
public void testListServersWhenReponseIs404IsEmpty() throws Exception {
|
||||||
HttpRequest listServers = HttpRequest.builder().method("GET")
|
HttpRequest listServers = HttpRequest.builder().method("GET").endpoint(
|
||||||
.endpoint(URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers"))
|
URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers")).headers(
|
||||||
.headers(ImmutableMultimap.<String, String> builder()
|
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||||
.put("Accept", "application/json")
|
authToken).build()).build();
|
||||||
.put("X-Auth-Token", authToken).build()).build();
|
|
||||||
|
|
||||||
HttpResponse listServersResponse = HttpResponse.builder().statusCode(404).build();
|
HttpResponse listServersResponse = HttpResponse.builder().statusCode(404).build();
|
||||||
|
|
||||||
ServerClient clientWhenServersExist = requestsSendResponses(initialAuth, responseWithAccess, listServers,
|
ServerClient clientWhenServersExist = requestsSendResponses(initialAuthWithApiAccessKeyCredentials,
|
||||||
listServersResponse).getServerClient();
|
responseWithAccess, listServers, listServersResponse).getServerClient();
|
||||||
|
|
||||||
assertTrue(clientWhenServersExist.listServers().isEmpty());
|
assertTrue(clientWhenServersExist.listServers().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: gson deserializer for Multimap
|
// TODO: gson deserializer for Multimap
|
||||||
public void testGetServerWhenResponseIs2xx() throws Exception {
|
public void testGetServerWhenResponseIs2xx() throws Exception {
|
||||||
HttpRequest listServers = HttpRequest.builder().method("GET")
|
HttpRequest listServers = HttpRequest.builder().method("GET").endpoint(
|
||||||
.endpoint(URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers/foo"))
|
URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers/foo")).headers(
|
||||||
.headers(ImmutableMultimap.<String, String> builder()
|
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
|
||||||
.put("Accept", "application/json")
|
authToken).build()).build();
|
||||||
.put("X-Auth-Token", authToken).build()).build();
|
|
||||||
|
|
||||||
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200).payload(
|
HttpResponse listServersResponse = HttpResponse.builder().statusCode(200).payload(
|
||||||
payloadFromResource("/server_details.json")).build();
|
payloadFromResource("/server_details.json")).build();
|
||||||
|
|
||||||
ServerClient clientWhenServersExist = requestsSendResponses(initialAuth, responseWithAccess, listServers,
|
ServerClient clientWhenServersExist = requestsSendResponses(initialAuthWithApiAccessKeyCredentials,
|
||||||
listServersResponse).getServerClient();
|
responseWithAccess, listServers, listServersResponse).getServerClient();
|
||||||
|
|
||||||
assertEquals(clientWhenServersExist.getServer("foo").toString(), new ParseServerTest().expected().toString());
|
assertEquals(clientWhenServersExist.getServer("foo").toString(), new ParseServerTest().expected().toString());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue