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 Printing Bucket ACLs and Printing Bucket ACL…
… for a specific user. (#1236) Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com>
- Loading branch information
1 parent
6b08589
commit d82333b
Showing
8 changed files
with
247 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
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
52 changes: 52 additions & 0 deletions
52
samples/snippets/src/main/java/com/example/storage/bucket/PrintBucketAcl.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,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 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 = "someuser@domain.com" | ||
|
||
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 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 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 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"); | ||
} | ||
} |