Skip to content

Commit b2e117b

Browse files
authored
feat(storagecontrol): add folder samples (GoogleCloudPlatform#11843)
* feat(storagecontrol): add folder samples * update versions and dependencies
1 parent 8cf862f commit b2e117b

File tree

9 files changed

+333
-2
lines changed

9 files changed

+333
-2
lines changed

storagecontrol/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def project_id() -> str:
2525
yield os.environ.get("BUILD_SPECIFIC_GCLOUD_PROJECT")
2626

2727

28+
@pytest.fixture(scope="function")
29+
def uuid_name(prefix: str = "storagecontrol") -> str:
30+
yield f"{prefix}-{uuid.uuid4().hex[:5]}"
31+
32+
2833
@pytest.fixture(scope="function")
2934
def bucket_name() -> str:
3035
yield f"storagecontrol-samples-{uuid.uuid4()}"
@@ -42,3 +47,20 @@ def gcs_bucket(project_id: str, bucket_name: str) -> storage.Bucket:
4247
yield bucket
4348

4449
bucket.delete(force=True)
50+
51+
52+
@pytest.fixture(scope="function")
53+
def hns_enabled_bucket(project_id: str, bucket_name: str) -> storage.Bucket:
54+
"""
55+
Yields and auto-cleans up an HNS enabled bucket.
56+
"""
57+
58+
storage_client = storage.Client(project=project_id)
59+
bucket = storage_client.bucket(bucket_name)
60+
bucket.iam_configuration.uniform_bucket_level_access_enabled = True
61+
bucket.hierarchical_namespace_enabled = True
62+
bucket = storage_client.create_bucket(bucket)
63+
64+
yield bucket
65+
66+
bucket.delete(force=True)

storagecontrol/create_folder.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import sys
16+
17+
# [START storage_control_create_folder]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def create_folder(bucket_name: str, folder_name: str) -> None:
22+
# The ID of your GCS bucket
23+
# bucket_name = "your-unique-bucket-name"
24+
25+
# The name of the folder to be created
26+
# folder_name = "folder-name"
27+
28+
storage_control_client = storage_control_v2.StorageControlClient()
29+
# The storage bucket path uses the global access pattern, in which the "_"
30+
# denotes this bucket exists in the global namespace.
31+
project_path = storage_control_client.common_project_path("_")
32+
bucket_path = f"{project_path}/buckets/{bucket_name}"
33+
34+
request = storage_control_v2.CreateFolderRequest(
35+
parent=bucket_path,
36+
folder_id=folder_name,
37+
)
38+
response = storage_control_client.create_folder(request=request)
39+
40+
print(f"Created folder: {response.name}")
41+
42+
43+
# [END storage_control_create_folder]
44+
45+
46+
if __name__ == "__main__":
47+
create_folder(bucket_name=sys.argv[1], folder_name=sys.argv[2])

storagecontrol/delete_folder.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import sys
16+
17+
# [START storage_control_delete_folder]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def delete_folder(bucket_name: str, folder_name: str) -> None:
22+
# The ID of your GCS bucket
23+
# bucket_name = "your-unique-bucket-name"
24+
25+
# The name of the folder to be deleted
26+
# folder_name = "folder-name"
27+
28+
storage_control_client = storage_control_v2.StorageControlClient()
29+
# The storage bucket path uses the global access pattern, in which the "_"
30+
# denotes this bucket exists in the global namespace.
31+
folder_path = storage_control_client.folder_path(
32+
project="_", bucket=bucket_name, folder=folder_name
33+
)
34+
35+
request = storage_control_v2.DeleteFolderRequest(
36+
name=folder_path,
37+
)
38+
storage_control_client.delete_folder(request=request)
39+
40+
print(f"Deleted folder {folder_name}")
41+
42+
43+
# [END storage_control_delete_folder]
44+
45+
46+
if __name__ == "__main__":
47+
delete_folder(bucket_name=sys.argv[1], folder_name=sys.argv[2])

storagecontrol/get_folder.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import sys
16+
17+
# [START storage_control_get_folder]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def get_folder(bucket_name: str, folder_name: str) -> None:
22+
# The ID of your GCS bucket
23+
# bucket_name = "your-unique-bucket-name"
24+
25+
# The name of the folder
26+
# folder_name = "folder-name"
27+
28+
storage_control_client = storage_control_v2.StorageControlClient()
29+
# The storage bucket path uses the global access pattern, in which the "_"
30+
# denotes this bucket exists in the global namespace.
31+
folder_path = storage_control_client.folder_path(
32+
project="_", bucket=bucket_name, folder=folder_name
33+
)
34+
35+
request = storage_control_v2.GetFolderRequest(
36+
name=folder_path,
37+
)
38+
response = storage_control_client.get_folder(request=request)
39+
40+
print(f"Got folder: {response.name}")
41+
42+
43+
# [END storage_control_get_folder]
44+
45+
46+
if __name__ == "__main__":
47+
get_folder(bucket_name=sys.argv[1], folder_name=sys.argv[2])

storagecontrol/list_folders.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
import sys
16+
17+
# [START storage_control_list_folders]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def list_folders(bucket_name: str) -> None:
22+
# The ID of your GCS bucket
23+
# bucket_name = "your-unique-bucket-name"
24+
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}"
30+
31+
request = storage_control_v2.ListFoldersRequest(
32+
parent=bucket_path,
33+
)
34+
35+
page_result = storage_control_client.list_folders(request=request)
36+
for folder in page_result:
37+
print(folder)
38+
39+
print(f"Listed folders in bucket {bucket_name}")
40+
41+
42+
# [END storage_control_list_folders]
43+
44+
45+
if __name__ == "__main__":
46+
list_folders(bucket_name=sys.argv[1])

storagecontrol/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.9", "3.10", "3.11"],
25+
"ignored_versions": ["2.7", "3.7", "3.9", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

storagecontrol/rename_folder.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import sys
16+
17+
# [START storage_control_rename_folder]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def rename_folder(
22+
bucket_name: str, source_folder_name: str, destination_folder_name: str
23+
) -> None:
24+
# The ID of your GCS bucket
25+
# bucket_name = "your-unique-bucket-name"
26+
#
27+
# The source folder ID
28+
# source_folder_name = "current-folder-name"
29+
#
30+
# The destination folder ID
31+
# destination_folder_name = "new-folder-name"
32+
33+
storage_control_client = storage_control_v2.StorageControlClient()
34+
# The storage bucket path uses the global access pattern, in which the "_"
35+
# denotes this bucket exists in the global namespace.
36+
source_folder_path = storage_control_client.folder_path(
37+
project="_", bucket=bucket_name, folder=source_folder_name
38+
)
39+
40+
request = storage_control_v2.RenameFolderRequest(
41+
name=source_folder_path,
42+
destination_folder_id=destination_folder_name,
43+
)
44+
45+
operation = storage_control_client.rename_folder(request=request)
46+
operation.result(60)
47+
48+
print(f"Renamed folder {source_folder_name} to {destination_folder_name}")
49+
50+
51+
# [END storage_control_rename_folder]
52+
53+
54+
if __name__ == "__main__":
55+
rename_folder(
56+
bucket_name=sys.argv[1],
57+
source_folder_name=sys.argv[2],
58+
destination_folder_name=sys.argv[3],
59+
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pytest==8.2.0
2-
google-cloud-storage==2.16.0
2+
google-cloud-storage==2.17.0

storagecontrol/snippets_test.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 create_folder
20+
import delete_folder
21+
import get_folder
22+
import list_folders
23+
import rename_folder
24+
25+
26+
# === Folders === #
27+
28+
29+
def test_folder_create_get_list_rename_delete(
30+
capsys: pytest.LogCaptureFixture, hns_enabled_bucket: storage.Bucket, uuid_name: str
31+
) -> None:
32+
bucket_name = hns_enabled_bucket.name
33+
folder_name = uuid_name
34+
35+
# Test create folder
36+
create_folder.create_folder(bucket_name=bucket_name, folder_name=folder_name)
37+
out, _ = capsys.readouterr()
38+
assert folder_name in out
39+
40+
# Test get folder
41+
get_folder.get_folder(bucket_name=bucket_name, folder_name=folder_name)
42+
out, _ = capsys.readouterr()
43+
assert folder_name in out
44+
45+
# Test list folders
46+
list_folders.list_folders(bucket_name=bucket_name)
47+
out, _ = capsys.readouterr()
48+
assert folder_name in out
49+
50+
# Test rename folder
51+
new_name = f"new-name-{uuid_name}"
52+
rename_folder.rename_folder(
53+
bucket_name=bucket_name,
54+
source_folder_name=folder_name,
55+
destination_folder_name=new_name,
56+
)
57+
out, _ = capsys.readouterr()
58+
assert folder_name in out
59+
60+
# Test delete folder
61+
delete_folder.delete_folder(bucket_name=bucket_name, folder_name=new_name)
62+
out, _ = capsys.readouterr()
63+
assert new_name in out

0 commit comments

Comments
 (0)