JCLOUDS-334. Return correct status while creating containers in Swift.

BlobStore.createContainerInLocation is supposed to return True if the
container was newly created and False if the container already
existed. This commit makes that happen for Swift blobstores.
This commit is contained in:
Shri Javadekar 2013-10-07 14:04:38 -07:00 committed by Andrew Gaul
parent 46b8fd0eee
commit af245d67fe
3 changed files with 72 additions and 0 deletions

View File

@ -41,6 +41,7 @@ import org.jclouds.blobstore.BlobStoreFallbacks.NullOnKeyNotFound;
import org.jclouds.blobstore.binders.BindMapToHeadersWithPrefix; import org.jclouds.blobstore.binders.BindMapToHeadersWithPrefix;
import org.jclouds.blobstore.domain.PageSet; import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.http.functions.ParseETagHeader; import org.jclouds.http.functions.ParseETagHeader;
import org.jclouds.http.functions.ReturnTrueIf201;
import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.GetOptions;
import org.jclouds.openstack.swift.SwiftFallbacks.TrueOn404FalseOn409; import org.jclouds.openstack.swift.SwiftFallbacks.TrueOn404FalseOn409;
import org.jclouds.openstack.swift.binders.BindIterableToHeadersWithContainerDeleteMetadataPrefix; import org.jclouds.openstack.swift.binders.BindIterableToHeadersWithContainerDeleteMetadataPrefix;
@ -142,6 +143,7 @@ public interface CommonSwiftAsyncClient extends Closeable {
*/ */
@Named("CreateContainer") @Named("CreateContainer")
@PUT @PUT
@ResponseParser(ReturnTrueIf201.class)
@Path("/{container}") @Path("/{container}")
ListenableFuture<Boolean> createContainer(@PathParam("container") String container, ListenableFuture<Boolean> createContainer(@PathParam("container") String container,
CreateContainerOptions... options); CreateContainerOptions... options);
@ -161,6 +163,7 @@ public interface CommonSwiftAsyncClient extends Closeable {
*/ */
@Named("CreateContainer") @Named("CreateContainer")
@PUT @PUT
@ResponseParser(ReturnTrueIf201.class)
@Path("/{container}") @Path("/{container}")
ListenableFuture<Boolean> createContainer(@PathParam("container") String container); ListenableFuture<Boolean> createContainer(@PathParam("container") String container);

View File

@ -54,6 +54,37 @@ public class SwiftClientExpectTest extends BaseSwiftExpectTest<SwiftClient> {
assertTrue(clientWhenContainerExists.containerExists("foo")); assertTrue(clientWhenContainerExists.containerExists("foo"));
} }
@Test
public void testCreateContainerReturnStatus() {
String containerName = "foo";
HttpRequest createContainerRequest = HttpRequest.builder()
.method("PUT")
.endpoint(swiftEndpointWithHostReplaced + "/" + containerName)
.addHeader("X-Auth-Token", authToken).build();
HttpResponse createContainerResponse = HttpResponse.builder()
.statusCode(201)
.build();
SwiftClient clientWhenNonExistingContainer = requestsSendResponses(
authRequest, authResponse, createContainerRequest,
createContainerResponse);
assertTrue(clientWhenNonExistingContainer.createContainer(
containerName));
// Try creating the same container again. This should return a status
// code of 202 as per the following:
// http://docs.openstack.org/api/openstack-object-storage/1.0/content/create-container.html
// http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_Container-d1e1694.html
createContainerResponse = HttpResponse.builder().statusCode(202)
.build();
SwiftClient clientWhenExistingContainer = requestsSendResponses(
authRequest, authResponse, createContainerRequest,
createContainerResponse);
assertFalse(clientWhenExistingContainer.createContainer(containerName));
}
@Test @Test
public void testContainerExistsWhenResponseIs404ReturnsFalse() { public void testContainerExistsWhenResponseIs404ReturnsFalse() {
HttpRequest headContainer = HttpRequest.builder() HttpRequest headContainer = HttpRequest.builder()

View File

@ -0,0 +1,38 @@
/*
* 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.http.functions;
import static org.jclouds.http.HttpUtils.releasePayload;
import javax.inject.Singleton;
import org.jclouds.http.HttpResponse;
import com.google.common.base.Function;
/**
* Parses the status code of an HTTPResponse and returns True if it is 201.
* False otherwise.
*/
@Singleton
public class ReturnTrueIf201 implements Function<HttpResponse, Boolean> {
@Override
public Boolean apply(HttpResponse from) {
releasePayload(from);
return from.getStatusCode() == 201;
}
}