Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Adding Samples for Add/Remove Bucket Owner (#1272)
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d5def96
commit 9d25fa9
Showing
6 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
samples/snippets/src/main/java/com/example/storage/bucket/AddBucketOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_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 AddBucketOwner { | ||
|
||
public static void addBucketOwner(String bucketName, String userEmail) { | ||
// The ID to give your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
// Email of the user you wish to add as an owner | ||
// String userEmail = "someuser@domain.com" | ||
|
||
Storage storage = StorageOptions.newBuilder().build().getService(); | ||
Bucket bucket = storage.get(bucketName); | ||
Acl newOwner = Acl.of(new User(userEmail), Role.OWNER); | ||
|
||
bucket.createAcl(newOwner); | ||
System.out.println("Added user " + userEmail + " as an owner on " + bucketName); | ||
} | ||
} | ||
|
||
// [END storage_add_bucket_owner] |
48 changes: 48 additions & 0 deletions
48
samples/snippets/src/main/java/com/example/storage/bucket/RemoveBucketOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_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 RemoveBucketOwner { | ||
|
||
public static void removeBucketOwner(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 an owner | ||
// String userEmail = "someuser@domain.com" | ||
|
||
Storage storage = StorageOptions.newBuilder().build().getService(); | ||
Bucket bucket = storage.get(bucketName); | ||
User ownerToRemove = new User(userEmail); | ||
|
||
boolean success = bucket.deleteAcl(ownerToRemove); | ||
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_owner] |
39 changes: 39 additions & 0 deletions
39
samples/snippets/src/test/java/com/example/storage/bucket/AddBucketOwnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 AddBucketOwnerTest extends TestBase { | ||
|
||
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); | ||
|
||
@Test | ||
public void testAddBucketOwner() { | ||
// Check for user email before the actual test. | ||
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
|
||
AddBucketOwner.addBucketOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(bucket.getAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNotNull(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
samples/snippets/src/test/java/com/example/storage/bucket/RemoveBucketOwnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 RemoveBucketOwnerTest extends TestBase { | ||
|
||
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); | ||
|
||
@Test | ||
public void testRemoveBucketOwner() { | ||
// Check for user email before the actual test. | ||
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
// Add User as Owner | ||
Acl newOwner = Acl.of(new User(IT_SERVICE_ACCOUNT_EMAIL), Role.OWNER); | ||
bucket.createAcl(newOwner); | ||
|
||
// Remove User as owner | ||
RemoveBucketOwner.removeBucketOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Removed user"); | ||
assertThat(bucket.getAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNull(); | ||
} | ||
|
||
@Test | ||
public void testUserNotFound() { | ||
// Remove User without Owner Permissions | ||
RemoveBucketOwner.removeBucketOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("was not found"); | ||
} | ||
} |