diff --git a/samples/snippets/src/main/java/com/example/storage/managedfolders/CreateManagedFolder.java b/samples/snippets/src/main/java/com/example/storage/managedfolders/CreateManagedFolder.java new file mode 100644 index 0000000000..2f0ea2827a --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/managedfolders/CreateManagedFolder.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 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.managedfolders; + +// [START storage_control_managed_folder_create] + +import com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.CreateManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolder; +import com.google.storage.control.v2.StorageControlClient; + +public class CreateManagedFolder { + public static void managedFolderCreate(String bucketName, String managedFolderId) + throws Exception { + + // Instantiates a client in a try-with-resource to automatically cleanup underlying resources + try (StorageControlClient storageControlClient = StorageControlClient.create()) { + CreateManagedFolderRequest request = + CreateManagedFolderRequest.newBuilder() + // Set project to "_" to signify global bucket + .setParent(BucketName.format("_", bucketName)) + .setManagedFolder(ManagedFolder.newBuilder().build()) + .setManagedFolderId(managedFolderId).build(); + String response = storageControlClient.createManagedFolder(request).getName(); + System.out.printf("Performed createManagedFolder request for %s%n", response); + } + } +} +// [END storage_control_managed_folder_create] diff --git a/samples/snippets/src/main/java/com/example/storage/managedfolders/DeleteManagedFolder.java b/samples/snippets/src/main/java/com/example/storage/managedfolders/DeleteManagedFolder.java new file mode 100644 index 0000000000..777053c157 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/managedfolders/DeleteManagedFolder.java @@ -0,0 +1,48 @@ +/* + * Copyright 2024 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.managedfolders; + +// [START storage_control_managed_folder_delete] +import com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.DeleteManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolderName; +import com.google.storage.control.v2.StorageControlClient; + +class DeleteManagedFolder { + public static void managedFolderDelete(String bucketName, String managedFolderId) + throws Exception { + // Instantiates a client in a try-with-resource to automatically cleanup underlying resources + try (StorageControlClient storageControlClient = StorageControlClient.create()) { + // Set project to "_" to signify global bucket + BucketName resourceBucketName = BucketName.of("_", bucketName); + DeleteManagedFolderRequest deleteManagedFolderRequest = + DeleteManagedFolderRequest.newBuilder() + .setName( + ManagedFolderName.format( + resourceBucketName.getProject(), + resourceBucketName.getBucket(), + managedFolderId) + ) + .build(); + storageControlClient.deleteManagedFolder(deleteManagedFolderRequest); + System.out.printf("Deleted Managed Folder %s%n", managedFolderId); + } + } + +} + +// [END storage_control_managed_folder_delete] diff --git a/samples/snippets/src/main/java/com/example/storage/managedfolders/GetManagedFolder.java b/samples/snippets/src/main/java/com/example/storage/managedfolders/GetManagedFolder.java new file mode 100644 index 0000000000..5e5adf7961 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/managedfolders/GetManagedFolder.java @@ -0,0 +1,49 @@ +/* + * Copyright 2024 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.managedfolders; + +// [START storage_control_managed_folder_get] + +import com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.GetManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolder; +import com.google.storage.control.v2.ManagedFolderName; +import com.google.storage.control.v2.StorageControlClient; + +class GetManagedFolder { + + public static void managedFolderGet(String bucketName, String managedFolderId) throws Exception { + // Instantiates a client in a try-with-resource to automatically cleanup underlying resources + try (StorageControlClient storageControlClient = StorageControlClient.create()) { + // Set project to "_" to signify global bucket + BucketName resourceBucketName = BucketName.of("_", bucketName); + GetManagedFolderRequest getManagedFolderRequest = + GetManagedFolderRequest.newBuilder() + .setName( + ManagedFolderName.format( + resourceBucketName.getProject(), + resourceBucketName.getBucket(), + managedFolderId)) + .build(); + ManagedFolder managedFolder = storageControlClient.getManagedFolder(getManagedFolderRequest); + System.out.printf("Got Managed Folder %s%n", managedFolder.getName()); + } + } + +} + +// [END storage_control_managed_folder_get] diff --git a/samples/snippets/src/main/java/com/example/storage/managedfolders/ListManagedFolders.java b/samples/snippets/src/main/java/com/example/storage/managedfolders/ListManagedFolders.java new file mode 100644 index 0000000000..bb7ce977c3 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/managedfolders/ListManagedFolders.java @@ -0,0 +1,45 @@ +/* + * Copyright 2024 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.managedfolders; + +// [START storage_control_managed_folder_list] + +import com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.ListManagedFoldersRequest; +import com.google.storage.control.v2.ManagedFolder; +import com.google.storage.control.v2.StorageControlClient; + +class ListManagedFolders { + + public static void managedFolderList(String bucketName) throws Exception { + // Instantiates a client in a try-with-resource to automatically cleanup underlying resources + try (StorageControlClient storageControlClient = StorageControlClient.create()) { + ListManagedFoldersRequest listManagedFoldersRequest = + ListManagedFoldersRequest.newBuilder() + // Set project to "_" to signify global bucket + .setParent(BucketName.format("_", bucketName)) + .build(); + Iterable managedFolders = + storageControlClient.listManagedFolders(listManagedFoldersRequest).iterateAll(); + for (ManagedFolder folder : managedFolders) { + System.out.printf("%s bucket has managed folder %s%n", bucketName, folder.getName()); + } + } + } +} + +// [END storage_control_managed_folder_list] diff --git a/samples/snippets/src/test/java/com/example/storage/managedfolders/CreateManagedFolderTest.java b/samples/snippets/src/test/java/com/example/storage/managedfolders/CreateManagedFolderTest.java new file mode 100644 index 0000000000..a6183e5e34 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/managedfolders/CreateManagedFolderTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2024 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.managedfolders; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.BucketInfo.IamConfiguration; +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 com.google.storage.control.v2.DeleteManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolderName; +import com.google.storage.control.v2.StorageControlClient; +import java.io.IOException; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class CreateManagedFolderTest { + + @Rule + public StdOutCaptureRule stdOut = new StdOutCaptureRule(); + + protected String bucketName; + protected Storage storage; + protected Bucket bucket; + protected String managedFolderId; + protected StorageControlClient storageControl; + + @Before + public void setUp() throws IOException { + bucketName = RemoteStorageHelper.generateBucketName(); + storageControl = StorageControlClient.create(); + storage = StorageOptions.getDefaultInstance().getService(); + managedFolderId = "new-managed-folder-" + UUID.randomUUID(); + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) + .setIamConfiguration( + IamConfiguration + .newBuilder() + .setIsUniformBucketLevelAccessEnabled(true) + .build()).build(); + bucket = storage.create(bucketInfo); + } + + @After + public void tearDown() { + storageControl.deleteManagedFolder( + DeleteManagedFolderRequest.newBuilder().setName( + ManagedFolderName.format("_", bucketName, managedFolderId) + ).build()); + storage.delete(bucketName); + storageControl.shutdown(); + } + + @Test + public void testCreateManagedFolder() throws Exception { + CreateManagedFolder.managedFolderCreate(bucketName, managedFolderId); + String got = stdOut.getCapturedOutputAsUtf8String(); + assertThat(got).contains(String.format(managedFolderId)); + } +} + diff --git a/samples/snippets/src/test/java/com/example/storage/managedfolders/DeleteManagedFolderTest.java b/samples/snippets/src/test/java/com/example/storage/managedfolders/DeleteManagedFolderTest.java new file mode 100644 index 0000000000..79f318764b --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/managedfolders/DeleteManagedFolderTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2024 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.managedfolders; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.BucketInfo.IamConfiguration; +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 com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.CreateManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolder; +import com.google.storage.control.v2.StorageControlClient; +import java.io.IOException; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class DeleteManagedFolderTest { + + @Rule + public StdOutCaptureRule stdOut = new StdOutCaptureRule(); + + protected String bucketName; + protected Storage storage; + protected Bucket bucket; + protected String managedFolderId; + protected StorageControlClient storageControl; + + @Before + public void setUp() throws IOException { + bucketName = RemoteStorageHelper.generateBucketName(); + storageControl = StorageControlClient.create(); + storage = StorageOptions.getDefaultInstance().getService(); + managedFolderId = "new-managed-folder-" + UUID.randomUUID(); + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) + .setIamConfiguration( + IamConfiguration + .newBuilder() + .setIsUniformBucketLevelAccessEnabled(true) + .build()).build(); + bucket = storage.create(bucketInfo); + storageControl.createManagedFolder( + CreateManagedFolderRequest.newBuilder() + // Set project to "_" to signify global bucket + .setParent(BucketName.format("_", bucketName)) + .setManagedFolder(ManagedFolder.newBuilder().build()) + .setManagedFolderId(managedFolderId).build()); + } + + @After + public void tearDown() { + storage.delete(bucketName); + storageControl.shutdown(); + } + + @Test + public void testDeleteManagedFolder() throws Exception { + DeleteManagedFolder.managedFolderDelete(bucketName, managedFolderId); + String got = stdOut.getCapturedOutputAsUtf8String(); + assertThat(got).contains(String.format(managedFolderId)); + } +} \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/storage/managedfolders/GetManagedFolderTest.java b/samples/snippets/src/test/java/com/example/storage/managedfolders/GetManagedFolderTest.java new file mode 100644 index 0000000000..34da570131 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/managedfolders/GetManagedFolderTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2024 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.managedfolders; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.BucketInfo.IamConfiguration; +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 com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.CreateManagedFolderRequest; +import com.google.storage.control.v2.DeleteManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolder; +import com.google.storage.control.v2.ManagedFolderName; +import com.google.storage.control.v2.StorageControlClient; +import java.io.IOException; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class GetManagedFolderTest { + + @Rule + public StdOutCaptureRule stdOut = new StdOutCaptureRule(); + + protected String bucketName; + protected Storage storage; + protected Bucket bucket; + protected String managedFolderId; + protected StorageControlClient storageControl; + + @Before + public void setUp() throws IOException { + bucketName = RemoteStorageHelper.generateBucketName(); + storageControl = StorageControlClient.create(); + storage = StorageOptions.getDefaultInstance().getService(); + managedFolderId = "new-managed-folder-" + UUID.randomUUID(); + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) + .setIamConfiguration( + IamConfiguration + .newBuilder() + .setIsUniformBucketLevelAccessEnabled(true) + .build()).build(); + bucket = storage.create(bucketInfo); + storageControl.createManagedFolder( + CreateManagedFolderRequest.newBuilder() + // Set project to "_" to signify global bucket + .setParent(BucketName.format("_", bucketName)) + .setManagedFolder(ManagedFolder.newBuilder().build()) + .setManagedFolderId(managedFolderId).build()); + } + + @After + public void tearDown() { + storageControl.deleteManagedFolder( + DeleteManagedFolderRequest.newBuilder().setName( + ManagedFolderName.format("_", bucketName, managedFolderId) + ).build()); + storage.delete(bucketName); + storageControl.shutdown(); + } + + @Test + public void testGetManagedFolder() throws Exception { + GetManagedFolder.managedFolderGet(bucketName, managedFolderId); + String got = stdOut.getCapturedOutputAsUtf8String(); + assertThat(got).contains(String.format(managedFolderId)); + } +} + diff --git a/samples/snippets/src/test/java/com/example/storage/managedfolders/ListManagedFolderTest.java b/samples/snippets/src/test/java/com/example/storage/managedfolders/ListManagedFolderTest.java new file mode 100644 index 0000000000..7397f0ff65 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/managedfolders/ListManagedFolderTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2024 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.managedfolders; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.BucketInfo.IamConfiguration; +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 com.google.storage.control.v2.BucketName; +import com.google.storage.control.v2.CreateManagedFolderRequest; +import com.google.storage.control.v2.DeleteManagedFolderRequest; +import com.google.storage.control.v2.ManagedFolder; +import com.google.storage.control.v2.ManagedFolderName; +import com.google.storage.control.v2.StorageControlClient; +import java.io.IOException; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class ListManagedFolderTest { + + @Rule + public StdOutCaptureRule stdOut = new StdOutCaptureRule(); + + protected String bucketName; + protected Storage storage; + protected Bucket bucket; + protected String managedFolderId; + protected StorageControlClient storageControl; + + @Before + public void setUp() throws IOException { + bucketName = RemoteStorageHelper.generateBucketName(); + storageControl = StorageControlClient.create(); + storage = StorageOptions.getDefaultInstance().getService(); + managedFolderId = "new-managed-folder-" + UUID.randomUUID(); + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName) + .setIamConfiguration( + IamConfiguration + .newBuilder() + .setIsUniformBucketLevelAccessEnabled(true) + .build()).build(); + bucket = storage.create(bucketInfo); + storageControl.createManagedFolder( + CreateManagedFolderRequest.newBuilder() + // Set project to "_" to signify global bucket + .setParent(BucketName.format("_", bucketName)) + .setManagedFolder(ManagedFolder.newBuilder().build()) + .setManagedFolderId(managedFolderId).build()); + } + + @After + public void tearDown() { + storageControl.deleteManagedFolder( + DeleteManagedFolderRequest.newBuilder().setName( + ManagedFolderName.format("_", bucketName, managedFolderId) + ).build()); + storage.delete(bucketName); + storageControl.shutdown(); + } + + @Test + public void testListManagedFolder() throws Exception { + ListManagedFolders.managedFolderList(bucketName); + String got = stdOut.getCapturedOutputAsUtf8String(); + assertThat(got).contains(String.format(managedFolderId)); + } +}