From 94404841ca8ca3f8afb842288a29a1f6d90bbe8a Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 20 May 2012 14:39:32 -0700 Subject: [PATCH] allow use of tenantId when specified numeric --- ...eyAndTenantIdAuthenticationExpectTest.java | 76 +++++++++++++++++++ ...yAndSecretKeyAuthenticationExpectTest.java | 75 ++++++++++++++++++ .../v1_1/internal/BaseNovaExpectTest.java | 5 ++ .../v2_0/AuthenticationAsyncClient.java | 11 +++ .../keystone/v2_0/AuthenticationClient.java | 7 ++ .../v2_0/binders/BindAuthToJsonPayload.java | 2 + .../AuthenticateApiAccessKeyCredentials.java | 8 +- .../v2_0/internal/KeystoneFixture.java | 19 ++++- 8 files changed, 201 insertions(+), 2 deletions(-) create mode 100755 apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest.java create mode 100755 apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAuthenticationExpectTest.java diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest.java new file mode 100755 index 0000000000..007274bd7a --- /dev/null +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest.java @@ -0,0 +1,76 @@ +/** + * 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; + +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.keystone.v2_0.config.KeystoneProperties; +import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest; +import org.jclouds.openstack.nova.v1_1.parse.ParseServerListTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; + +/** + * + * @see KeystoneProperties#CREDENTIAL_TYPE + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest") +public class AccessKeyAndSecretKeyAndTenantIdAuthenticationExpectTest extends BaseNovaClientExpectTest { + + /** + * 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", "authenticateApiAccessKeyCredentials"); + return contextProperties; + } + + public void testListServersWhenResponseIs2xx() throws Exception { + HttpRequest listServers = HttpRequest + .builder() + .method("GET") + .endpoint(URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers")) + .headers( + ImmutableMultimap. builder().put("Accept", "application/json") + .put("X-Auth-Token", authToken).build()).build(); + + HttpResponse listServersResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/server_list.json")).build(); + + NovaClient clientWhenServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKeyAndTenantId, + responseWithKeystoneAccess, listServers, listServersResponse); + + assertEquals(clientWhenServersExist.getConfiguredZones(), ImmutableSet.of("az-1.region-a.geo-1")); + + assertEquals(clientWhenServersExist.getServerClientForZone("az-1.region-a.geo-1").listServers().toString(), + new ParseServerListTest().expected().toString()); + } + +} + diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAuthenticationExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAuthenticationExpectTest.java new file mode 100755 index 0000000000..5a1e530494 --- /dev/null +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/AccessKeyAndSecretKeyAuthenticationExpectTest.java @@ -0,0 +1,75 @@ +/** + * 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; + +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.keystone.v2_0.config.KeystoneProperties; +import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest; +import org.jclouds.openstack.nova.v1_1.parse.ParseServerListTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; + +/** + * + * @see KeystoneProperties#CREDENTIAL_TYPE + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "AccessKeyAndSecretKeyAuthenticationExpectTest") +public class AccessKeyAndSecretKeyAuthenticationExpectTest extends BaseNovaClientExpectTest { + + /** + * 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", "authenticateApiAccessKeyCredentials"); + return contextProperties; + } + + public void testListServersWhenResponseIs2xx() throws Exception { + HttpRequest listServers = HttpRequest + .builder() + .method("GET") + .endpoint(URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers")) + .headers( + ImmutableMultimap. builder().put("Accept", "application/json") + .put("X-Auth-Token", authToken).build()).build(); + + HttpResponse listServersResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/server_list.json")).build(); + + NovaClient clientWhenServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey, + responseWithKeystoneAccess, listServers, listServersResponse); + + assertEquals(clientWhenServersExist.getConfiguredZones(), ImmutableSet.of("az-1.region-a.geo-1")); + + assertEquals(clientWhenServersExist.getServerClientForZone("az-1.region-a.geo-1").listServers().toString(), + new ParseServerListTest().expected().toString()); + } + +} diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/internal/BaseNovaExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/internal/BaseNovaExpectTest.java index dda5a43348..338574d6b6 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/internal/BaseNovaExpectTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/internal/BaseNovaExpectTest.java @@ -43,6 +43,8 @@ public class BaseNovaExpectTest extends BaseRestClientExpectTest { protected HttpRequest extensionsOfNovaRequest; protected HttpResponse extensionsOfNovaResponse; protected HttpResponse unmatchedExtensionsOfNovaResponse; + protected HttpRequest keystoneAuthWithAccessKeyAndSecretKeyAndTenantId; + protected String identityWithTenantId; public BaseNovaExpectTest() { provider = "openstack-nova"; @@ -50,10 +52,13 @@ public class BaseNovaExpectTest extends BaseRestClientExpectTest { credential); keystoneAuthWithAccessKeyAndSecretKey = KeystoneFixture.INSTANCE.initialAuthWithAccessKeyAndSecretKey(identity, credential); + keystoneAuthWithAccessKeyAndSecretKeyAndTenantId = KeystoneFixture.INSTANCE.initialAuthWithAccessKeyAndSecretKeyAndTenantId(identity, + credential); authToken = KeystoneFixture.INSTANCE.getAuthToken(); responseWithKeystoneAccess = KeystoneFixture.INSTANCE.responseWithAccess(); // now, createContext arg will need tenant prefix + identityWithTenantId = KeystoneFixture.INSTANCE.getTenantId() + ":" + identity; identity = KeystoneFixture.INSTANCE.getTenantName() + ":" + identity; extensionsOfNovaRequest = HttpRequest diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationAsyncClient.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationAsyncClient.java index 2f160ed365..aaa1c6fc89 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationAsyncClient.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationAsyncClient.java @@ -68,4 +68,15 @@ public interface AuthenticationAsyncClient { // still use tenantName ListenableFuture authenticateTenantWithCredentials(@PayloadParam("tenantName") String tenantId, ApiAccessKeyCredentials apiAccessKeyCredentials); + + /** + * @see AuthenticationClient#authenticateTenantWithCredentials(String,ApiAccessKeyCredentials) + */ + @POST + @SelectJson("access") + @Consumes(MediaType.APPLICATION_JSON) + @Path("/tokens") + @MapBinder(BindAuthToJsonPayload.class) + ListenableFuture authenticateTenantWithTenantIdAndCredentials(@PayloadParam("tenantId") String tenantId, + ApiAccessKeyCredentials apiAccessKeyCredentials); } diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationClient.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationClient.java index 695b55f527..2d8c0429fd 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationClient.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/AuthenticationClient.java @@ -50,4 +50,11 @@ public interface AuthenticationClient { * @return access with token */ Access authenticateTenantWithCredentials(String tenantId, ApiAccessKeyCredentials passwordCredentials); + + /** + * Authenticate to generate a token. + * + * @return access with token + */ + Access authenticateTenantWithTenantIdAndCredentials(String tenantId, ApiAccessKeyCredentials passwordCredentials); } \ No newline at end of file diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java index 6990aecfb4..87f583db2c 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java @@ -79,6 +79,8 @@ public class BindAuthToJsonPayload extends BindToJsonPayload implements MapBinde // still use tenantName if (!Strings.isNullOrEmpty((String) postParams.get("tenantName"))) builder.put("tenantName", postParams.get("tenantName")); + else if (!Strings.isNullOrEmpty((String) postParams.get("tenantId"))) + builder.put("tenantId", postParams.get("tenantId")); return super.bindToRequest(request, ImmutableMap.of("auth", builder.build())); } diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/AuthenticateApiAccessKeyCredentials.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/AuthenticateApiAccessKeyCredentials.java index 68844857de..035be75ca3 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/AuthenticateApiAccessKeyCredentials.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/functions/AuthenticateApiAccessKeyCredentials.java @@ -51,7 +51,13 @@ public class AuthenticateApiAccessKeyCredentials implements Function