Issue 815: example usage of ServerClient using keystone authentication

This commit is contained in:
Adrian Cole 2012-01-18 22:47:28 -08:00
parent 1e19db93fd
commit b78dc18e3a
4 changed files with 83 additions and 54 deletions

View File

@ -50,7 +50,7 @@ public interface ServiceAsyncClient {
* @see ServiceClient#authenticateTenantWithCredentials(String,PasswordCredentials) * @see ServiceClient#authenticateTenantWithCredentials(String,PasswordCredentials)
*/ */
@POST @POST
@SelectJson("auth") @SelectJson("access")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Path("/tokens") @Path("/tokens")
@MapBinder(BindAuthToJsonPayload.class) @MapBinder(BindAuthToJsonPayload.class)
@ -61,7 +61,7 @@ public interface ServiceAsyncClient {
* @see ServiceClient#authenticateTenantWithCredentials(String,ApiAccessKeyCredentials) * @see ServiceClient#authenticateTenantWithCredentials(String,ApiAccessKeyCredentials)
*/ */
@POST @POST
@SelectJson("auth") @SelectJson("access")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Path("/tokens") @Path("/tokens")
@MapBinder(BindAuthToJsonPayload.class) @MapBinder(BindAuthToJsonPayload.class)

View File

@ -0,0 +1,71 @@
/**
* 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.internal;
import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.keystone.v2_0.config.KeyStoneAuthenticationModule;
import org.jclouds.rest.BaseRestClientExpectTest;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.net.HttpHeaders;
/**
* Base class for writing KeyStone Rest Client Expect tests
*
* @author Adrian Cole
*/
public class BaseKeyStoneRestClientExpectTest<S> extends BaseRestClientExpectTest<S> {
public BaseKeyStoneRestClientExpectTest() {
// username:tenantId
identity = "user@jclouds.org:12346637803162";
credential = "Password1234";
}
protected HttpRequest initialAuth = HttpRequest
.builder()
.method("POST")
.endpoint(URI.create("http://localhost:5000/v2.0/tokens"))
.headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json"))
.payload(
payloadFromStringWithContentType(
"{\"tenantId\":\"12346637803162\",\"auth\":{\"passwordCredentials\":{\"username\":\"user@jclouds.org\",\"password\":\"Password1234\"}}}",
"application/json")).build();
protected String authToken = "Auth_4f173437e4b013bee56d1007";
protected HttpResponse responseWithAccess = HttpResponse.builder().statusCode(200).message("HTTP/1.1 200").payload(
payloadFromResourceWithContentType("/keystoneAuthResponse.json", "application/json")).build();
/**
* in case you need to override anything
*/
public static class TestKeyStoneAuthenticationModule extends KeyStoneAuthenticationModule {
@Override
protected void configure() {
super.configure();
}
}
}

View File

@ -26,11 +26,9 @@ public class ServerClientExpectTest extends BaseNovaRestClientExpectTest {
provider = "openstack-nova"; provider = "openstack-nova";
} }
//TODO:
@Test(enabled=false)
public void testListServersWhenResponseIs2xx() throws Exception { public void testListServersWhenResponseIs2xx() throws Exception {
HttpRequest listServers = HttpRequest.builder().method("GET") HttpRequest listServers = HttpRequest.builder().method("GET")
.endpoint(URI.create("http://localhost:8774/v1.1/identity/servers")) .endpoint(URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers"))
.headers(ImmutableMultimap.<String, String> builder() .headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "application/json") .put("Accept", "application/json")
.put("X-Auth-Token", authToken).build()).build(); .put("X-Auth-Token", authToken).build()).build();
@ -38,34 +36,31 @@ public class ServerClientExpectTest extends BaseNovaRestClientExpectTest {
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, responseWithUrls, listServers, ServerClient clientWhenServersExist = requestsSendResponses(initialAuth, responseWithAccess, listServers,
listServersResponse).getServerClient(); listServersResponse).getServerClient();
assertEquals(clientWhenServersExist.listServers().toString(), new ParseServerListTest().expected().toString()); assertEquals(clientWhenServersExist.listServers().toString(), new ParseServerListTest().expected().toString());
} }
//TODO:
@Test(enabled=false)
public void testListServersWhenReponseIs404IsEmpty() throws Exception { public void testListServersWhenReponseIs404IsEmpty() throws Exception {
HttpRequest listServers = HttpRequest.builder().method("GET") HttpRequest listServers = HttpRequest.builder().method("GET")
.endpoint(URI.create("http://localhost:8774/v1.1/identity/servers")) .endpoint(URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers"))
.headers(ImmutableMultimap.<String, String> builder() .headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "application/json") .put("Accept", "application/json")
.put("X-Auth-Token", 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, responseWithUrls, listServers, ServerClient clientWhenServersExist = requestsSendResponses(initialAuth, responseWithAccess, listServers,
listServersResponse).getServerClient(); listServersResponse).getServerClient();
assertTrue(clientWhenServersExist.listServers().isEmpty()); assertTrue(clientWhenServersExist.listServers().isEmpty());
} }
//TODO: gson deserializer for Multimap //TODO: gson deserializer for Multimap
//TODO:
@Test(enabled=false)
public void testGetServerWhenResponseIs2xx() throws Exception { public void testGetServerWhenResponseIs2xx() throws Exception {
HttpRequest listServers = HttpRequest.builder().method("GET") HttpRequest listServers = HttpRequest.builder().method("GET")
.endpoint(URI.create("http://localhost:8774/v1.1/identity/servers/foo")) .endpoint(URI.create("http://compute-1.jclouds.org:8774/v1.1/40806637803162/servers/foo"))
.headers(ImmutableMultimap.<String, String> builder() .headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "application/json") .put("Accept", "application/json")
.put("X-Auth-Token", authToken).build()).build(); .put("X-Auth-Token", authToken).build()).build();
@ -73,7 +68,7 @@ public class ServerClientExpectTest extends BaseNovaRestClientExpectTest {
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, responseWithUrls, listServers, ServerClient clientWhenServersExist = requestsSendResponses(initialAuth, responseWithAccess, listServers,
listServersResponse).getServerClient(); listServersResponse).getServerClient();
assertEquals(clientWhenServersExist.getServer("foo").toString(), new ParseServerTest().expected().toString()); assertEquals(clientWhenServersExist.getServer("foo").toString(), new ParseServerTest().expected().toString());

View File

@ -18,60 +18,23 @@
*/ */
package org.jclouds.openstack.nova.v1_1.internal; package org.jclouds.openstack.nova.v1_1.internal;
import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.RequiresHttp; import org.jclouds.http.RequiresHttp;
import org.jclouds.openstack.keystone.v2_0.config.KeyStoneAuthenticationModule; import org.jclouds.openstack.keystone.v2_0.internal.BaseKeyStoneRestClientExpectTest;
import org.jclouds.openstack.nova.v1_1.NovaClient; import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.config.NovaRestClientModule; import org.jclouds.openstack.nova.v1_1.config.NovaRestClientModule;
import org.jclouds.rest.BaseRestClientExpectTest;
import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.ConfiguresRestClient;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.net.HttpHeaders;
import com.google.inject.Module; import com.google.inject.Module;
/** /**
* Base class for writing Nova Rest Client Expect tests * Base class for writing KeyStone Rest Client Expect tests
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class BaseNovaRestClientExpectTest extends BaseRestClientExpectTest<NovaClient> { public class BaseNovaRestClientExpectTest extends BaseKeyStoneRestClientExpectTest<NovaClient> {
public BaseNovaRestClientExpectTest() { public BaseNovaRestClientExpectTest() {
provider = "openstack-nova"; provider = "openstack-nova";
// username:tenantId
identity = "user@jclouds.org:12346637803162";
credential = "Password1234";
}
protected HttpRequest initialAuth = HttpRequest
.builder()
.method("POST")
.endpoint(URI.create("http://localhost:5000/v2.0/tokens"))
.headers(ImmutableMultimap.of(HttpHeaders.ACCEPT, "application/json"))
.payload(
payloadFromStringWithContentType(
"{\"tenantId\":\"12346637803162\",\"auth\":{\"passwordCredentials\":{\"username\":\"user@jclouds.org\",\"password\":\"Password1234\"}}}",
"application/json")).build();
protected String authToken = "d6245d35-22a0-47c0-9770-2c5097da25fc";
protected HttpResponse responseWithUrls = HttpResponse.builder().statusCode(200).message("HTTP/1.1 200").payload(
payloadFromResourceWithContentType("/keystoneAuthResponse.json", "application/json")).build();
/**
* in case you need to override anything
*/
static class TestKeyStoneAuthenticationModule extends KeyStoneAuthenticationModule {
@Override
protected void configure() {
super.configure();
}
} }
@Override @Override