You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ga...@apache.org on 2013/10/08 06:49:10 UTC

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

Updated Branches:
  refs/heads/master 46b8fd0ee -> af245d67f


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.


Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-jclouds/commit/af245d67
Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds/tree/af245d67
Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds/diff/af245d67

Branch: refs/heads/master
Commit: af245d67feb82f9099d5d225ef24149ae1811fee
Parents: 46b8fd0
Author: Shri Javadekar <sh...@maginatics.com>
Authored: Mon Oct 7 14:04:38 2013 -0700
Committer: Andrew Gaul <ga...@apache.org>
Committed: Mon Oct 7 21:48:57 2013 -0700

----------------------------------------------------------------------
 .../openstack/swift/CommonSwiftAsyncClient.java |  3 ++
 .../openstack/swift/SwiftClientExpectTest.java  | 31 ++++++++++++++++
 .../jclouds/http/functions/ReturnTrueIf201.java | 38 ++++++++++++++++++++
 3 files changed, 72 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-jclouds/blob/af245d67/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java
----------------------------------------------------------------------
diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java
index 9ac73c7..988b953 100644
--- a/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java
+++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java
@@ -41,6 +41,7 @@ import org.jclouds.blobstore.BlobStoreFallbacks.NullOnKeyNotFound;
 import org.jclouds.blobstore.binders.BindMapToHeadersWithPrefix;
 import org.jclouds.blobstore.domain.PageSet;
 import org.jclouds.http.functions.ParseETagHeader;
+import org.jclouds.http.functions.ReturnTrueIf201;
 import org.jclouds.http.options.GetOptions;
 import org.jclouds.openstack.swift.SwiftFallbacks.TrueOn404FalseOn409;
 import org.jclouds.openstack.swift.binders.BindIterableToHeadersWithContainerDeleteMetadataPrefix;
@@ -142,6 +143,7 @@ public interface CommonSwiftAsyncClient extends Closeable {
     */
    @Named("CreateContainer")
    @PUT
+   @ResponseParser(ReturnTrueIf201.class)
    @Path("/{container}")
    ListenableFuture<Boolean> createContainer(@PathParam("container") String container,
                                              CreateContainerOptions... options);
@@ -161,6 +163,7 @@ public interface CommonSwiftAsyncClient extends Closeable {
     */
    @Named("CreateContainer")
    @PUT
+   @ResponseParser(ReturnTrueIf201.class)
    @Path("/{container}")
    ListenableFuture<Boolean> createContainer(@PathParam("container") String container);
 

http://git-wip-us.apache.org/repos/asf/incubator-jclouds/blob/af245d67/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java
----------------------------------------------------------------------
diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java
index 61159bd..e217445 100644
--- a/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java
+++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java
@@ -55,6 +55,37 @@ public class SwiftClientExpectTest extends BaseSwiftExpectTest<SwiftClient> {
    }
 
    @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
    public void testContainerExistsWhenResponseIs404ReturnsFalse() {
       HttpRequest headContainer = HttpRequest.builder()
             .method("HEAD")

http://git-wip-us.apache.org/repos/asf/incubator-jclouds/blob/af245d67/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java b/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java
new file mode 100644
index 0000000..e44e98d
--- /dev/null
+++ b/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java
@@ -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;
+   }
+}