mirror of https://github.com/apache/jclouds.git
JCLOUDS-311. work around mixed versions in hpcloud-objectstorage regions
This commit is contained in:
parent
f1fc63ab76
commit
6eda031e27
|
@ -92,6 +92,11 @@
|
|||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.mockwebserver</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
|
|
|
@ -16,22 +16,28 @@
|
|||
*/
|
||||
package org.jclouds.hpcloud.objectstorage;
|
||||
|
||||
import static org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties.SERVICE_TYPE;
|
||||
import static org.jclouds.rest.config.BinderUtils.bindSyncToAsyncHttpApi;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.inject.Named;
|
||||
|
||||
import org.jclouds.blobstore.BlobRequestSigner;
|
||||
import org.jclouds.hpcloud.objectstorage.blobstore.HPCloudObjectStorageBlobRequestSigner;
|
||||
import org.jclouds.hpcloud.objectstorage.blobstore.config.HPCloudObjectStorageBlobStoreContextModule;
|
||||
import org.jclouds.hpcloud.objectstorage.config.HPCloudObjectStorageRestClientModule;
|
||||
import org.jclouds.openstack.keystone.v2_0.config.MappedAuthenticationApiModule;
|
||||
import org.jclouds.location.suppliers.RegionIdToURISupplier;
|
||||
import org.jclouds.openstack.keystone.v2_0.config.KeystoneAuthenticationModule.RegionModule;
|
||||
import org.jclouds.openstack.keystone.v2_0.config.MappedAuthenticationApiModule;
|
||||
import org.jclouds.openstack.keystone.v2_0.suppliers.RegionIdToAdminURISupplier;
|
||||
import org.jclouds.openstack.swift.SwiftKeystoneApiMetadata;
|
||||
import org.jclouds.openstack.swift.blobstore.config.TemporaryUrlExtensionModule;
|
||||
import org.jclouds.openstack.swift.config.SwiftRestClientModule.KeystoneStorageEndpointModule;
|
||||
import org.jclouds.openstack.swift.extensions.KeystoneTemporaryUrlKeyAsyncApi;
|
||||
import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi;
|
||||
import org.jclouds.rest.annotations.ApiVersion;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
|
@ -84,7 +90,7 @@ public class HPCloudObjectStorageApiMetadata extends SwiftKeystoneApiMetadata {
|
|||
.defaultModules(ImmutableSet.<Class<? extends Module>>builder()
|
||||
.add(MappedAuthenticationApiModule.class)
|
||||
.add(KeystoneStorageEndpointModule.class)
|
||||
.add(RegionModule.class)
|
||||
.add(IgnoreRegionVersionsModule.class)
|
||||
.add(HPCloudObjectStorageRestClientModule.class)
|
||||
.add(HPCloudObjectStorageBlobStoreContextModule.class)
|
||||
.add(HPCloudObjectStorageTemporaryUrlExtensionModule.class).build());
|
||||
|
@ -115,4 +121,25 @@ public class HPCloudObjectStorageApiMetadata extends SwiftKeystoneApiMetadata {
|
|||
bindSyncToAsyncHttpApi(binder(), TemporaryUrlKeyApi.class, KeystoneTemporaryUrlKeyAsyncApi.class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this when the keystone configuration incorrectly mismatches api
|
||||
* versions across regions.
|
||||
*/
|
||||
public static class IgnoreRegionVersionsModule extends RegionModule {
|
||||
|
||||
@Override
|
||||
protected RegionIdToURISupplier provideRegionIdToURISupplierForApiVersion(
|
||||
@Named(SERVICE_TYPE) String serviceType, @ApiVersion String apiVersion,
|
||||
RegionIdToURISupplier.Factory factory) {
|
||||
return factory.createForApiTypeAndVersion(serviceType, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RegionIdToAdminURISupplier provideRegionIdToAdminURISupplierForApiVersion(
|
||||
@Named(SERVICE_TYPE) String serviceType, @ApiVersion String apiVersion,
|
||||
RegionIdToAdminURISupplier.Factory factory) {
|
||||
return factory.createForApiTypeAndVersion(serviceType, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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.hpcloud.objectstorage;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import org.jclouds.hpcloud.objectstorage.internal.BaseHPCloudObjectStorageMockTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.mockwebserver.MockResponse;
|
||||
import com.google.mockwebserver.MockWebServer;
|
||||
import com.google.mockwebserver.RecordedRequest;
|
||||
|
||||
@Test
|
||||
public class HPCloudObjectStorageApiMockTest extends BaseHPCloudObjectStorageMockTest {
|
||||
|
||||
String mixedSwiftVersions = "" //
|
||||
+ "{\"access\": {\n" //
|
||||
+ " \"token\": {\n" //
|
||||
+ " \"expires\": \"2013-10-01T09:16:20.177Z\",\n" //
|
||||
+ " \"id\": \"HPAuth10_1234567890\",\n" //
|
||||
+ " \"tenant\": {\n" //
|
||||
+ " \"id\": \"12345678\",\n" //
|
||||
+ " \"name\": \"jclouds-project\"\n" //
|
||||
+ " }\n" //
|
||||
+ " },\n" //
|
||||
+ " \"user\": {\n" //
|
||||
+ " \"id\": \"12345678\",\n" //
|
||||
+ " \"name\": \"jclouds\"\n" //
|
||||
+ " },\n" //
|
||||
+ " \"serviceCatalog\": [\n" //
|
||||
+ " {\n" //
|
||||
+ " \"name\": \"Object Storage\",\n" //
|
||||
+ " \"type\": \"object-store\",\n" //
|
||||
+ " \"endpoints\": [\n" //
|
||||
+ " {\n" //
|
||||
+ " \"tenantId\": \"12345678\",\n" //
|
||||
+ " \"publicURL\": \"URL\\/v1\\/12345678\",\n" //
|
||||
+ " \"region\": \"region-a.geo-1\",\n" //
|
||||
+ " \"versionId\": \"1.0\",\n" //
|
||||
+ " \"versionInfo\": \"URL\\/v1.0\\/\",\n" //
|
||||
+ " \"versionList\": \"URL\"\n" //
|
||||
+ " },\n" //
|
||||
+ " {\n" //
|
||||
+ " \"tenantId\": \"12345678\",\n" //
|
||||
+ " \"publicURL\": \"URL\\/v1\\/12345678\",\n" //
|
||||
+ " \"region\": \"region-b.geo-1\",\n" //
|
||||
+ " \"versionId\": \"1\",\n" //
|
||||
+ " \"versionInfo\": \"URL\\/v1\\/\",\n" //
|
||||
+ " \"versionList\": \"URL\"\n" //
|
||||
+ " }]\n" //
|
||||
+ " }\n" //
|
||||
+ " ]\n" //
|
||||
+ "}}";
|
||||
|
||||
public void mixedVersionsInRegions() throws Exception {
|
||||
MockWebServer server = mockWebServer();
|
||||
server.enqueue(new MockResponse().setBody(mixedSwiftVersions));
|
||||
try {
|
||||
|
||||
HPCloudObjectStorageApi api = api(server.getUrl("/").toString());
|
||||
|
||||
assertEquals(api.getConfiguredRegions(), ImmutableSet.of("region-a.geo-1", "region-b.geo-1"));
|
||||
|
||||
assertEquals(server.getRequestCount(), 1);
|
||||
RecordedRequest authRequest = server.takeRequest();
|
||||
assertEquals(authRequest.getRequestLine(), "POST /tokens HTTP/1.1");
|
||||
} finally {
|
||||
server.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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.hpcloud.objectstorage.internal;
|
||||
|
||||
import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
|
||||
import static org.jclouds.Constants.PROPERTY_MAX_RETRIES;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.jclouds.ContextBuilder;
|
||||
import org.jclouds.concurrent.config.ExecutorServiceModule;
|
||||
import org.jclouds.hpcloud.objectstorage.HPCloudObjectStorageApi;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.Module;
|
||||
import com.google.mockwebserver.MockResponse;
|
||||
import com.google.mockwebserver.MockWebServer;
|
||||
import com.google.mockwebserver.QueueDispatcher;
|
||||
import com.google.mockwebserver.RecordedRequest;
|
||||
|
||||
public class BaseHPCloudObjectStorageMockTest {
|
||||
|
||||
public static HPCloudObjectStorageApi api(String uri) {
|
||||
Properties overrides = new Properties();
|
||||
overrides.setProperty(PROPERTY_MAX_RETRIES, "1");
|
||||
|
||||
return ContextBuilder.newBuilder("hpcloud-objectstorage") //
|
||||
.credentials("jclouds:joe", "letmein") //
|
||||
.endpoint(uri) //
|
||||
.overrides(overrides) //
|
||||
.modules(ImmutableSet.<Module> of(new ExecutorServiceModule(sameThreadExecutor(), sameThreadExecutor()))) //
|
||||
.buildApi(HPCloudObjectStorageApi.class);
|
||||
}
|
||||
|
||||
public static MockWebServer mockWebServer() throws IOException {
|
||||
MockWebServer server = new MockWebServer();
|
||||
server.play();
|
||||
URL url = server.getUrl("");
|
||||
server.setDispatcher(getURLReplacingQueueDispatcher(url));
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* there's no built-in way to defer evaluation of a response header, hence
|
||||
* this method, which allows us to send back links to the mock server.
|
||||
*/
|
||||
public static QueueDispatcher getURLReplacingQueueDispatcher(final URL url) {
|
||||
final QueueDispatcher dispatcher = new QueueDispatcher() {
|
||||
protected final BlockingQueue<MockResponse> responseQueue = new LinkedBlockingQueue<MockResponse>();
|
||||
|
||||
@Override
|
||||
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
|
||||
MockResponse response = responseQueue.take();
|
||||
if (response.getBody() != null) {
|
||||
String newBody = new String(response.getBody()).replace(":\"URL", ":\"" + url.toString());
|
||||
response = response.setBody(newBody);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueueResponse(MockResponse response) {
|
||||
responseQueue.add(response);
|
||||
}
|
||||
};
|
||||
return dispatcher;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue