Skip to content

Commit ec184fd

Browse files
cojencodandhlee
andauthored
feat: add storagecontrol quickstart sample (GoogleCloudPlatform#11391)
* feat: add storagecontrol quickstart sample * update codeowners and blunderbuss * Update storagecontrol/conftest.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update storagecontrol/noxfile_config.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * update to import module --------- Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
1 parent 2b8c938 commit ec184fd

File tree

8 files changed

+163
-0
lines changed

8 files changed

+163
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
/firestore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers
8282
# ---* Cloud Storage
8383
/storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
84+
/storagecontrol/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
8485
/storagetransfer/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
8586
# ---* Infra DB
8687
/cloud-sql/**/* @GoogleCloudPlatform/infra-db-sdk @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ assign_issues_by:
9090
- GoogleCloudPlatform/api-spanner-python
9191
- labels:
9292
- "api: storage"
93+
- "api: storagecontrol"
94+
- "api: storagetransfer"
9395
to:
9496
- GoogleCloudPlatform/cloud-storage-dpes
9597
- labels:

storagecontrol/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 os
16+
import uuid
17+
18+
from google.cloud import storage
19+
20+
import pytest
21+
22+
23+
@pytest.fixture(scope="module")
24+
def project_id() -> str:
25+
yield os.environ.get("BUILD_SPECIFIC_GCLOUD_PROJECT")
26+
27+
28+
@pytest.fixture(scope="function")
29+
def bucket_name() -> str:
30+
yield f"storagecontrol-samples-{uuid.uuid4()}"
31+
32+
33+
@pytest.fixture(scope="function")
34+
def gcs_bucket(project_id: str, bucket_name: str) -> storage.Bucket:
35+
"""
36+
Yields and auto-cleans up a GCS bucket for use in Storage Control quickstart
37+
"""
38+
39+
storage_client = storage.Client(project=project_id)
40+
bucket = storage_client.create_bucket(bucket_name)
41+
42+
yield bucket
43+
44+
bucket.delete(force=True)

storagecontrol/noxfile_config.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.9", "3.10", "3.11"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
34+
# If you need to use a specific version of pip,
35+
# change pip_version_override to the string representation
36+
# of the version number, for example, "20.2.4"
37+
"pip_version_override": None,
38+
# A dictionary you want to inject into your test. Don't put any
39+
# secrets here. These values will override predefined values.
40+
"envs": {},
41+
}

storagecontrol/quickstart.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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_quickstart_sample]
18+
from google.cloud import storage_control_v2
19+
20+
21+
def storage_control_quickstart(bucket_name: str) -> None:
22+
# The ID of your GCS bucket
23+
# bucket_name = "your-bucket-name"
24+
25+
storage_control_client = storage_control_v2.StorageControlClient()
26+
# The storage layout path uses the global access pattern, in which
27+
# the “_” denotes this bucket exists in the global namespace.
28+
layout_path = storage_control_v2.StorageControlClient.storage_layout_path(
29+
project="_", bucket=bucket_name
30+
)
31+
request = storage_control_v2.GetStorageLayoutRequest(
32+
name=layout_path,
33+
)
34+
response = storage_control_client.get_storage_layout(request=request)
35+
36+
print(f"Performed get_storage_layout request for {response.name}")
37+
38+
# [END storage_control_quickstart_sample]
39+
40+
41+
if __name__ == "__main__":
42+
storage_control_quickstart(bucket_name=sys.argv[1])

storagecontrol/quickstart_test.py

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 quickstart
20+
21+
22+
def test_storage_control_quickstart(
23+
capsys: pytest.LogCaptureFixture, gcs_bucket: storage.Bucket
24+
) -> None:
25+
bucket_name = gcs_bucket.name
26+
quickstart.storage_control_quickstart(bucket_name=bucket_name)
27+
28+
out, _ = capsys.readouterr()
29+
layout_name = f"projects/_/buckets/{bucket_name}/storageLayout"
30+
assert layout_name in out
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest==6.2.4
2+
google-cloud-storage==2.16.0

storagecontrol/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-storage-control==0.1.0

0 commit comments

Comments
 (0)