Skip to content

Commit a0d7585

Browse files
eapl-gemugamiiennaegemini-code-assist[bot]
authored
refactor(storagecontrol): update sample managed_folder_list (#13532)
* refactor(storagecontrol): update sample `managed_folder_list` based on RPC Workshop * refactor(storagecontrol): fix failing test due to printing the wrong variable * refactor(storagecontrol): add docstring to list_managed_folders * fix: decrease spacing Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fix: use ubla_enabled_bucket fixture since this is managed folders Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: Jennifer Davis <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent ef29ddd commit a0d7585

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

storagecontrol/managed_folder_list.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,50 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import sys
15+
import argparse
1616

1717
# [START storage_control_managed_folder_list]
1818
from google.cloud import storage_control_v2
1919

2020

21-
def list_managed_folders(bucket_name: str) -> None:
22-
# The ID of your GCS bucket
23-
# bucket_name = "your-unique-bucket-name"
21+
def list_managed_folders(bucket_name: str = "your-bucket-name") -> None:
22+
"""Lists all managed folders in a Google Cloud Storage bucket.
2423
25-
storage_control_client = storage_control_v2.StorageControlClient()
26-
# The storage bucket path uses the global access pattern, in which the "_"
27-
# denotes this bucket exists in the global namespace.
28-
project_path = storage_control_client.common_project_path("_")
29-
bucket_path = f"{project_path}/buckets/{bucket_name}"
24+
Args:
25+
bucket_name: The name of the Google Cloud Storage bucket.
3026
31-
request = storage_control_v2.ListManagedFoldersRequest(
32-
parent=bucket_path,
33-
)
27+
Returns:
28+
None. The function prints the name of each managed folder to the
29+
console.
3430
35-
page_result = storage_control_client.list_managed_folders(request=request)
36-
for managed_folder in page_result:
37-
print(managed_folder)
31+
Example:
32+
>>> list_managed_folders(bucket_name="my-test-bucket")
33+
Managed folders in bucket projects/_/buckets/my-test-bucket:
34+
projects/_/buckets/my-test-bucket/managedFolders/folder-one
35+
projects/_/buckets/my-test-bucket/managedFolders/folder-two
36+
"""
37+
client = storage_control_v2.StorageControlClient()
3838

39-
print(f"Listed managed folders in bucket {bucket_name}")
39+
# The storage bucket path uses the global access pattern,
40+
# in which the "_" denotes this bucket exists in the global namespace.
41+
GLOBAL_NAMESPACE_PATTERN = "_"
42+
bucket_resource_name = f"projects/{GLOBAL_NAMESPACE_PATTERN}/buckets/{bucket_name}"
4043

44+
managed_folders = client.list_managed_folders(parent=bucket_resource_name)
4145

46+
print(f"Managed folders in bucket {bucket_resource_name}:")
47+
for managed_folder in managed_folders:
48+
print(f"\t{managed_folder.name}")
4249
# [END storage_control_managed_folder_list]
4350

4451

4552
if __name__ == "__main__":
46-
list_managed_folders(bucket_name=sys.argv[1])
53+
parser = argparse.ArgumentParser()
54+
parser.add_argument(
55+
"bucket_name",
56+
help="The name of the GCS bucket.",
57+
)
58+
59+
args = parser.parse_args()
60+
61+
list_managed_folders(args.bucket_name)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import storage
16+
17+
import pytest
18+
19+
import managed_folder_list
20+
21+
22+
def test_storage_control_managed_folder_list(
23+
capsys: pytest.LogCaptureFixture, ubla_enabled_bucket: storage.Bucket
24+
) -> None:
25+
bucket_name = ubla_enabled_bucket.name
26+
managed_folder_list.list_managed_folders(bucket_name=bucket_name)
27+
28+
out, _ = capsys.readouterr()
29+
bucket_resource_name = f"projects/_/buckets/{bucket_name}"
30+
assert bucket_resource_name in out

0 commit comments

Comments
 (0)