diff --git a/README.md b/README.md index f0df79ebbe..e21da0a582 100644 --- a/README.md +++ b/README.md @@ -229,8 +229,10 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | --------------------------- | --------------------------------- | ------ | | Configure Retries | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | | Quickstart Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | +| Add Bucket Default Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/AddBucketDefaultOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/AddBucketDefaultOwner.java) | | Print Bucket Acl | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java) | | Print Bucket Acl Filter By User | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java) | +| Remove Bucket Default Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java) | diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/AddBucketDefaultOwner.java b/samples/snippets/src/main/java/com/example/storage/bucket/AddBucketDefaultOwner.java new file mode 100644 index 0000000000..662c40cca7 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/AddBucketDefaultOwner.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed 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 com.example.storage.bucket; + +// [START storage_add_bucket_default_owner] + +import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Acl.Role; +import com.google.cloud.storage.Acl.User; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +public class AddBucketDefaultOwner { + + public static void addBucketDefaultOwner(String bucketName, String userEmail) { + + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // Email of the user you wish to remove as a default owner + // String userEmail = "someuser@domain.com" + + Storage storage = StorageOptions.newBuilder().build().getService(); + Bucket bucket = storage.get(bucketName); + Acl newDefaultOwner = Acl.of(new User(userEmail), Role.OWNER); + + bucket.createDefaultAcl(newDefaultOwner); + System.out.println("Added user " + userEmail + " as an owner on " + bucketName); + } +} +// [END storage_add_bucket_default_owner] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java b/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java new file mode 100644 index 0000000000..bc3289c009 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketDefaultOwner.java @@ -0,0 +1,48 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed 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 com.example.storage.bucket; + +// [START storage_remove_bucket_default_owner] + +import com.google.cloud.storage.Acl.User; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +public class RemoveBucketDefaultOwner { + + public static void removeBucketDefaultOwner(String bucketName, String userEmail) { + + // The ID to give your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // Email of the user you wish to remove as a default owner + // String userEmail = "someuser@domain.com" + + Storage storage = StorageOptions.newBuilder().build().getService(); + Bucket bucket = storage.get(bucketName); + User userToRemove = new User(userEmail); + + boolean success = bucket.deleteDefaultAcl(userToRemove); + if (success) { + System.out.println("Removed user " + userEmail + " as an owner on " + bucketName); + } else { + System.out.println("User " + userEmail + " was not found"); + } + } +} +// [END storage_remove_bucket_default_owner] diff --git a/samples/snippets/src/test/java/com/example/storage/TestBase.java b/samples/snippets/src/test/java/com/example/storage/TestBase.java index 3adaddabdc..a13edb4ba1 100644 --- a/samples/snippets/src/test/java/com/example/storage/TestBase.java +++ b/samples/snippets/src/test/java/com/example/storage/TestBase.java @@ -18,6 +18,7 @@ import com.google.cloud.storage.Blob; import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Bucket; import com.google.cloud.storage.BucketInfo; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; @@ -34,7 +35,7 @@ public abstract class TestBase { protected String bucketName; protected Storage storage; protected String blobName; - + protected Bucket bucket; protected Blob blob; @Before @@ -42,7 +43,7 @@ public void setUp() { blobName = "blob"; bucketName = RemoteStorageHelper.generateBucketName(); storage = StorageOptions.getDefaultInstance().getService(); - storage.create(BucketInfo.of(bucketName)); + bucket = storage.create(BucketInfo.of(bucketName)); blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build()); } diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/AddBucketDefaultOwnerTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/AddBucketDefaultOwnerTest.java new file mode 100644 index 0000000000..8dbb1786a2 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/AddBucketDefaultOwnerTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed 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 com.example.storage.bucket; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.example.storage.TestBase; +import com.google.cloud.storage.Acl.User; +import org.junit.Test; + +public class AddBucketDefaultOwnerTest extends TestBase { + + public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); + + @Test + public void testAddBucketDefaultOwner() { + // Check for user email before the actual test. + assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); + + AddBucketDefaultOwner.addBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); + assertThat(bucket.getDefaultAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNotNull(); + } +} diff --git a/samples/snippets/src/test/java/com/example/storage/bucket/RemoveBucketDefaultOwnerTest.java b/samples/snippets/src/test/java/com/example/storage/bucket/RemoveBucketDefaultOwnerTest.java new file mode 100644 index 0000000000..8af2f1ef9c --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/bucket/RemoveBucketDefaultOwnerTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed 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 com.example.storage.bucket; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.example.storage.TestBase; +import com.google.cloud.storage.Acl; +import com.google.cloud.storage.Acl.Role; +import com.google.cloud.storage.Acl.User; +import org.junit.Test; + +public class RemoveBucketDefaultOwnerTest extends TestBase { + + public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); + + @Test + public void testRemoveBucketDefaultOwner() { + // Check for user email before the actual test. + assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); + // Add User as Default Owner + Acl newDefaultOwner = Acl.of(new User(IT_SERVICE_ACCOUNT_EMAIL), Role.OWNER); + bucket.createDefaultAcl(newDefaultOwner); + + // Remove User as Default owner + RemoveBucketDefaultOwner.removeBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Removed user"); + assertThat(bucket.getDefaultAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNull(); + } + + @Test + public void testUserNotFound() { + // Remove User without Default Owner Permissions + RemoveBucketDefaultOwner.removeBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("was not found"); + } +}