Skip to content

Commit ac57071

Browse files
committed
Adding Samples for Add/Remove Bucket Default Owner
1 parent d82333b commit ac57071

File tree

5 files changed

+199
-3
lines changed

5 files changed

+199
-3
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.bucket;
18+
19+
// [START storage_add_bucket_default_owner]
20+
21+
import com.google.cloud.storage.Acl;
22+
import com.google.cloud.storage.Acl.Role;
23+
import com.google.cloud.storage.Acl.User;
24+
import com.google.cloud.storage.Bucket;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageOptions;
27+
28+
public class AddBucketDefaultOwner {
29+
30+
public static void addBucketDefaultOwner(String bucketName, String userEmail) {
31+
32+
// The ID to give your GCS bucket
33+
// String bucketName = "your-unique-bucket-name";
34+
35+
// Email of the user you wish to remove as a default owner
36+
// String userEmail = "[email protected]"
37+
38+
Storage storage = StorageOptions.newBuilder().build().getService();
39+
Bucket bucket = storage.get(bucketName);
40+
Acl newDefaultOwner = Acl.of(new User(userEmail), Role.OWNER);
41+
42+
bucket.createDefaultAcl(newDefaultOwner);
43+
System.out.println("Added user " + userEmail + " as an owner on " + bucketName);
44+
}
45+
46+
}
47+
// [END storage_add_bucket_default_owner]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.bucket;
18+
19+
// [START storage_remove_bucket_default_owner]
20+
21+
import com.google.cloud.storage.Acl.User;
22+
import com.google.cloud.storage.Bucket;
23+
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.StorageOptions;
25+
26+
public class RemoveBucketDefaultOwner {
27+
28+
public static void removeBucketDefaultOwner(String bucketName, String userEmail) {
29+
30+
// The ID to give your GCS bucket
31+
// String bucketName = "your-unique-bucket-name";
32+
33+
// Email of the user you wish to remove as a default owner
34+
// String userEmail = "[email protected]"
35+
36+
Storage storage = StorageOptions.newBuilder().build().getService();
37+
Bucket bucket = storage.get(bucketName);
38+
User userToRemove = new User(userEmail);
39+
40+
boolean success = bucket.deleteDefaultAcl(userToRemove);
41+
if (success) {
42+
System.out.println("Removed user " + userEmail + " as an owner on " + bucketName);
43+
} else {
44+
System.out.println("User " + userEmail + " was not found");
45+
}
46+
}
47+
48+
}
49+
// [END storage_remove_bucket_default_owner]

samples/snippets/src/test/java/com/example/storage/TestBase.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.storage.Blob;
2020
import com.google.cloud.storage.BlobInfo;
21+
import com.google.cloud.storage.Bucket;
2122
import com.google.cloud.storage.BucketInfo;
2223
import com.google.cloud.storage.Storage;
2324
import com.google.cloud.storage.StorageOptions;
@@ -29,20 +30,21 @@
2930

3031
public abstract class TestBase {
3132

32-
@Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule();
33+
@Rule
34+
public StdOutCaptureRule stdOut = new StdOutCaptureRule();
3335

3436
protected String bucketName;
3537
protected Storage storage;
3638
protected String blobName;
37-
39+
protected Bucket bucket;
3840
protected Blob blob;
3941

4042
@Before
4143
public void setUp() {
4244
blobName = "blob";
4345
bucketName = RemoteStorageHelper.generateBucketName();
4446
storage = StorageOptions.getDefaultInstance().getService();
45-
storage.create(BucketInfo.of(bucketName));
47+
bucket = storage.create(BucketInfo.of(bucketName));
4648
blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build());
4749
}
4850

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.bucket;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import com.example.storage.TestBase;
23+
import com.google.cloud.storage.Acl;
24+
import com.google.cloud.storage.Acl.Entity;
25+
import com.google.cloud.storage.Acl.Role;
26+
import com.google.cloud.storage.Acl.User;
27+
import org.junit.Test;
28+
29+
public class AddBucketDefaultOwnerTest extends TestBase {
30+
31+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
32+
33+
@Test
34+
public void testAddBucketDefaultOwner() {
35+
// Check for user email before the actual test.
36+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
37+
38+
AddBucketDefaultOwner.addBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
39+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
40+
assertThat(bucket.getDefaultAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNotNull();
41+
}
42+
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.bucket;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
22+
23+
import com.example.storage.TestBase;
24+
import com.google.cloud.storage.Acl;
25+
import com.google.cloud.storage.Acl.Role;
26+
import com.google.cloud.storage.Acl.User;
27+
import org.junit.Test;
28+
29+
public class RemoveBucketDefaultOwnerTest extends TestBase {
30+
31+
public static final String IT_SERVICE_ACCOUNT_EMAIL = System.getenv("IT_SERVICE_ACCOUNT_EMAIL");
32+
33+
@Test
34+
public void testRemoveBucketDefaultOwner() {
35+
// Check for user email before the actual test.
36+
assertNotNull("Unable to determine user email", IT_SERVICE_ACCOUNT_EMAIL);
37+
// Add User as Default Owner
38+
Acl newDefaultOwner = Acl.of(new User(IT_SERVICE_ACCOUNT_EMAIL), Role.OWNER);
39+
bucket.createDefaultAcl(newDefaultOwner);
40+
41+
// Remove User as Default owner
42+
RemoveBucketDefaultOwner.removeBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
43+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
44+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Removed user");
45+
assertThat(bucket.getDefaultAcl(new User(IT_SERVICE_ACCOUNT_EMAIL))).isNull();
46+
}
47+
48+
@Test
49+
public void testUserNotFound() {
50+
// Remove User without Default Owner Permissions
51+
RemoveBucketDefaultOwner.removeBucketDefaultOwner(bucketName, IT_SERVICE_ACCOUNT_EMAIL);
52+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(IT_SERVICE_ACCOUNT_EMAIL);
53+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("was not found");
54+
}
55+
}

0 commit comments

Comments
 (0)