From cb61ace4172491ace0a53683e57f6d4df0075abe Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Mon, 1 Apr 2024 15:46:30 -0700 Subject: [PATCH 1/5] feat: add storagecontrol quickstart sample --- storagecontrol/conftest.py | 44 ++++++++++++++++++++++++++++ storagecontrol/noxfile_config.py | 41 ++++++++++++++++++++++++++ storagecontrol/quickstart.py | 42 ++++++++++++++++++++++++++ storagecontrol/quickstart_test.py | 30 +++++++++++++++++++ storagecontrol/requirements-test.txt | 2 ++ storagecontrol/requirements.txt | 1 + 6 files changed, 160 insertions(+) create mode 100644 storagecontrol/conftest.py create mode 100644 storagecontrol/noxfile_config.py create mode 100644 storagecontrol/quickstart.py create mode 100644 storagecontrol/quickstart_test.py create mode 100644 storagecontrol/requirements-test.txt create mode 100644 storagecontrol/requirements.txt diff --git a/storagecontrol/conftest.py b/storagecontrol/conftest.py new file mode 100644 index 00000000000..ca3e7e00603 --- /dev/null +++ b/storagecontrol/conftest.py @@ -0,0 +1,44 @@ +# 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 +# +# https://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. + +import os +import uuid + +from google.cloud import storage + +import pytest + + +@pytest.fixture(scope="module") +def project_id() -> str: + yield os.environ.get("BUILD_SPECIFIC_GCLOUD_PROJECT") + + +@pytest.fixture(scope="function") +def bucket_name() -> str: + yield f"storagecontrol-samples-{uuid.uuid4()}" + + +@pytest.fixture(scope="function") +def gcs_bucket(project_id: str, bucket_name: str) -> storage.Bucket: + """ + Yields and auto-cleans up a CGS bucket for use in Storage Control quickstart + """ + + storage_client = storage.Client(project=project_id) + bucket = storage_client.create_bucket(bucket_name) + + yield bucket + + bucket.delete(force=True) diff --git a/storagecontrol/noxfile_config.py b/storagecontrol/noxfile_config.py new file mode 100644 index 00000000000..22c1ab1ed90 --- /dev/null +++ b/storagecontrol/noxfile_config.py @@ -0,0 +1,41 @@ +# Copyright 2021 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be imported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.7", "3.9", "3.10", "3.11"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT", + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/storagecontrol/quickstart.py b/storagecontrol/quickstart.py new file mode 100644 index 00000000000..7b9c4d7393f --- /dev/null +++ b/storagecontrol/quickstart.py @@ -0,0 +1,42 @@ +# 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 +# +# https://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. + +import sys + +# [START storage_control_quickstart_sample] +from google.cloud import storage_control_v2 + + +def storage_control_quickstart(bucket_name: str) -> None: + # The ID of your GCS bucket + # bucket_name = "your-bucket-name" + + storage_control_client = storage_control_v2.StorageControlClient() + # The storage layout path uses the global access pattern, in which + # the “_” denotes this bucket exists in the global namespace. + layout_path = storage_control_v2.StorageControlClient.storage_layout_path( + project="_", bucket=bucket_name + ) + request = storage_control_v2.GetStorageLayoutRequest( + name=layout_path, + ) + response = storage_control_client.get_storage_layout(request=request) + + print(f"Performed get_storage_layout request for {response.name}") + + # [END storage_control_quickstart_sample] + + +if __name__ == "__main__": + storage_control_quickstart(bucket_name=sys.argv[1]) diff --git a/storagecontrol/quickstart_test.py b/storagecontrol/quickstart_test.py new file mode 100644 index 00000000000..787bd4b4271 --- /dev/null +++ b/storagecontrol/quickstart_test.py @@ -0,0 +1,30 @@ +# 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 +# +# https://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. + +from google.cloud.storage import Bucket + +import pytest + +import quickstart + + +def test_storage_control_quickstart( + capsys: pytest.LogCaptureFixture, gcs_bucket: Bucket +) -> None: + bucket_name = gcs_bucket.name + quickstart.storage_control_quickstart(bucket_name=bucket_name) + + out, _ = capsys.readouterr() + layout_name = f"projects/_/buckets/{bucket_name}/storageLayout" + assert layout_name in out diff --git a/storagecontrol/requirements-test.txt b/storagecontrol/requirements-test.txt new file mode 100644 index 00000000000..16b9845140d --- /dev/null +++ b/storagecontrol/requirements-test.txt @@ -0,0 +1,2 @@ +pytest==6.2.4 +google-cloud-storage==2.16.0 \ No newline at end of file diff --git a/storagecontrol/requirements.txt b/storagecontrol/requirements.txt new file mode 100644 index 00000000000..684087afa61 --- /dev/null +++ b/storagecontrol/requirements.txt @@ -0,0 +1 @@ +google-cloud-storage-control==0.1.0 \ No newline at end of file From 8af824009ed69cdeff6f71a618c676f8a6aebab6 Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Tue, 2 Apr 2024 15:05:01 -0700 Subject: [PATCH 2/5] update codeowners and blunderbuss --- .github/CODEOWNERS | 1 + .github/blunderbuss.yml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 959e9d8a772..9768e46b85f 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -81,6 +81,7 @@ /firestore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers # ---* Cloud Storage /storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers +/storagecontrol/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers /storagetransfer/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers # ---* Infra DB /cloud-sql/**/* @GoogleCloudPlatform/infra-db-sdk @GoogleCloudPlatform/python-samples-reviewers diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index e9e9c2f2688..4c0f4ce1a12 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -90,6 +90,8 @@ assign_issues_by: - GoogleCloudPlatform/api-spanner-python - labels: - "api: storage" + - "api: storagecontrol" + - "api: storagetransfer" to: - GoogleCloudPlatform/cloud-storage-dpes - labels: From 961ca9b9f13a6f9d85dd366164719d7aad64a26e Mon Sep 17 00:00:00 2001 From: cojenco Date: Thu, 4 Apr 2024 11:33:52 -0700 Subject: [PATCH 3/5] Update storagecontrol/conftest.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- storagecontrol/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storagecontrol/conftest.py b/storagecontrol/conftest.py index ca3e7e00603..8b0951fe38a 100644 --- a/storagecontrol/conftest.py +++ b/storagecontrol/conftest.py @@ -33,7 +33,7 @@ def bucket_name() -> str: @pytest.fixture(scope="function") def gcs_bucket(project_id: str, bucket_name: str) -> storage.Bucket: """ - Yields and auto-cleans up a CGS bucket for use in Storage Control quickstart + Yields and auto-cleans up a GCS bucket for use in Storage Control quickstart """ storage_client = storage.Client(project=project_id) From 4257cf7d04ec7f83be625e7ef37b864effb61b8a Mon Sep 17 00:00:00 2001 From: cojenco Date: Thu, 4 Apr 2024 11:34:41 -0700 Subject: [PATCH 4/5] Update storagecontrol/noxfile_config.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- storagecontrol/noxfile_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storagecontrol/noxfile_config.py b/storagecontrol/noxfile_config.py index 22c1ab1ed90..290c1f35f78 100644 --- a/storagecontrol/noxfile_config.py +++ b/storagecontrol/noxfile_config.py @@ -1,4 +1,4 @@ -# Copyright 2021 Google LLC +# 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. From 04e6053861bbdb3190cfc80f59221e48784506c1 Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Thu, 4 Apr 2024 11:45:05 -0700 Subject: [PATCH 5/5] update to import module --- storagecontrol/quickstart_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storagecontrol/quickstart_test.py b/storagecontrol/quickstart_test.py index 787bd4b4271..ec122ae95ee 100644 --- a/storagecontrol/quickstart_test.py +++ b/storagecontrol/quickstart_test.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from google.cloud.storage import Bucket +from google.cloud import storage import pytest @@ -20,7 +20,7 @@ def test_storage_control_quickstart( - capsys: pytest.LogCaptureFixture, gcs_bucket: Bucket + capsys: pytest.LogCaptureFixture, gcs_bucket: storage.Bucket ) -> None: bucket_name = gcs_bucket.name quickstart.storage_control_quickstart(bucket_name=bucket_name)