-
Notifications
You must be signed in to change notification settings - Fork 89
docs: Adding Samples for Printing Bucket ACLs and Printing Bucket ACL for a specific user. #1236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2f13222
feat: Adding Samples for Printing Bucket ACLs and Printing Bucket ACL…
sydney-munro 9ec1f9c
Adding Copyright to PrintBucketAclFilterByUser
sydney-munro 341d147
🦉 Updates from OwlBot
gcf-owl-bot[bot] 33d1d27
Adding Copyright to tests
sydney-munro 756a1b6
Addressing PR comments and Adding TestBase
sydney-munro 766b7d0
Updating owlbot to ignore template changes
sydney-munro e714ed4
Fixing Broken kokoro build
sydney-munro 6a02f30
🦉 Updates from OwlBot
gcf-owl-bot[bot] 19a7ff6
Update .kokoro/presubmit/samples.cfg
sydney-munro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.java
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * 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_print_bucket_acl] | ||
|
|
||
| import com.google.cloud.storage.Acl; | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.util.List; | ||
|
|
||
| public class PrintBucketAcl { | ||
|
|
||
| public static void printBucketAcl(String bucketName) { | ||
|
|
||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
| List<Acl> bucketAcls = bucket.getAcl(); | ||
|
|
||
| for (Acl acl : bucketAcls) { | ||
|
|
||
| // This will give you the role. | ||
| // See https://cloud.google.com/storage/docs/access-control/lists#permissions | ||
| String role = acl.getRole().name(); | ||
|
|
||
| // This will give you the Entity type (i.e. User, Group, Project etc.) | ||
| // See https://cloud.google.com/storage/docs/access-control/lists#scopes | ||
| String entityType = acl.getEntity().getType().name(); | ||
|
|
||
| System.out.printf("%s: %s \n", role, entityType); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_print_bucket_acl] |
46 changes: 46 additions & 0 deletions
46
samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAclFilterByUser.java
This file contains hidden or 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_print_bucket_acl_for_user] | ||
|
|
||
| import com.google.cloud.storage.Acl; | ||
| 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 PrintBucketAclFilterByUser { | ||
|
|
||
| public static void printBucketAclFilterByUser(String bucketName, String userEmail) { | ||
|
|
||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The email of the user whose acl is being retrieved. | ||
| // String userEmail = "[email protected]" | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| Acl userAcl = bucket.getAcl(new User(userEmail)); | ||
| String userRole = userAcl.getRole().name(); | ||
| System.out.println("User " + userEmail + " has role " + userRole); | ||
| } | ||
| } | ||
|
|
||
| // [END storage_print_bucket_acl_for_user] |
53 changes: 53 additions & 0 deletions
53
samples/snippets/src/test/java/com/example/storage/TestBase.java
This file contains hidden or 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,53 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.google.cloud.storage.Blob; | ||
| import com.google.cloud.storage.BlobInfo; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import com.google.cloud.storage.testing.RemoteStorageHelper; | ||
| import com.google.cloud.testing.junit4.StdOutCaptureRule; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
|
|
||
| public abstract class TestBase { | ||
|
|
||
| @Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule(); | ||
|
|
||
| protected String bucketName; | ||
| protected Storage storage; | ||
| protected String blobName; | ||
|
|
||
| protected Blob blob; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| blobName = "blob"; | ||
| bucketName = RemoteStorageHelper.generateBucketName(); | ||
| storage = StorageOptions.getDefaultInstance().getService(); | ||
| storage.create(BucketInfo.of(bucketName)); | ||
| blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| RemoteStorageHelper.forceDelete(storage, bucketName); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
...les/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclFilterByUserTest.java
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * 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.Entity; | ||
| import com.google.cloud.storage.Acl.Role; | ||
| import com.google.cloud.storage.Acl.User; | ||
| import org.junit.Test; | ||
|
|
||
| public class PrintBucketAclFilterByUserTest extends TestBase { | ||
|
|
||
| public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); | ||
|
|
||
| @Test | ||
| public void testPrintBucketAclByUser() { | ||
| // Check for user email before the actual test. | ||
| assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
|
|
||
| Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL); | ||
| storage.createAcl(bucketName, Acl.of(testUser, Role.READER)); | ||
| PrintBucketAclFilterByUser.printBucketAclFilterByUser(bucketName, IT_SERVICE_ACCOUNT_EMAIL); | ||
| assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL); | ||
| assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(Role.READER.name()); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
samples/snippets/src/test/java/com/example/storage/bucket/PrintBucketAclTest.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * 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.Entity; | ||
| import com.google.cloud.storage.Acl.Role; | ||
| import com.google.cloud.storage.Acl.User; | ||
| import org.junit.Test; | ||
|
|
||
| public class PrintBucketAclTest extends TestBase { | ||
|
|
||
| public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL"); | ||
|
|
||
| @Test | ||
| public void testPrintBucketAcls() { | ||
| // Check for user email before the actual test. | ||
| assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL); | ||
|
|
||
| Entity testUser = new User(IT_SERVICE_ACCOUNT_EMAIL); | ||
| storage.createAcl(bucketName, Acl.of(testUser, Role.READER)); | ||
| PrintBucketAcl.printBucketAcl(bucketName); | ||
| assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("READER: USER"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.