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: diff --git a/storagecontrol/conftest.py b/storagecontrol/conftest.py new file mode 100644 index 00000000000..8b0951fe38a --- /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 GCS 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..290c1f35f78 --- /dev/null +++ b/storagecontrol/noxfile_config.py @@ -0,0 +1,41 @@ +# 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. + +# 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..ec122ae95ee --- /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 import storage + +import pytest + +import quickstart + + +def test_storage_control_quickstart( + capsys: pytest.LogCaptureFixture, gcs_bucket: storage.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