-
Notifications
You must be signed in to change notification settings - Fork 99
docs: samples and tests for auto-generated createDatabase and createInstance APIs. #1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+248
−0
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
02a48d7
docs: samples and tests for auto-generated createDatabase and createI…
rahul2393 e64bbc7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 97d2d63
fix lint
rahul2393 19d596a
incorporate suggestions
rahul2393 b54849c
rename tests
rahul2393 ec79381
fix lint
rahul2393 2cba753
fix failures
rahul2393 d486b13
chore(spanner): fix formatting
harshachinta 77fbaa3
Merge branch 'main' into auto-gen-admin-client
harshachinta af66951
incorporate suggesitons
rahul2393 577f901
Merge branch 'main' into auto-gen-admin-client
rahul2393 0f4db87
Merge branch 'main' into auto-gen-admin-client
rahul2393 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2024 Google, Inc. | ||
| # | ||
| # 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. | ||
|
|
||
| """This application demonstrates how to do basic operations using Cloud | ||
| Spanner. | ||
| For more information, see the README.rst under /spanner. | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| from google.cloud import spanner | ||
| from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin | ||
| from google.cloud.spanner_admin_database_v1.types import spanner_database_admin | ||
|
|
||
| OPERATION_TIMEOUT_SECONDS = 240 | ||
|
|
||
|
|
||
| # [START spanner_create_instance] | ||
| def create_instance(instance_id): | ||
| """Creates an instance.""" | ||
| spanner_client = spanner.Client() | ||
|
|
||
| config_name = "{}/instanceConfigs/regional-us-central1".format( | ||
| spanner_client.project_name | ||
| ) | ||
|
|
||
| operation = spanner_client.instance_admin_api.create_instance( | ||
| parent="projects/{}".format(spanner_client.project), | ||
| instance_id=instance_id, | ||
| instance=spanner_instance_admin.Instance( | ||
| config=config_name, | ||
| display_name="This is a display name.", | ||
| node_count=1, | ||
| labels={ | ||
| "cloud_spanner_samples": "true", | ||
| "sample_name": "snippets-create_instance-explicit", | ||
| "created": str(int(time.time())), | ||
| }, | ||
| ), | ||
| ) | ||
|
|
||
| print("Waiting for operation to complete...") | ||
| operation.result(OPERATION_TIMEOUT_SECONDS) | ||
|
|
||
| print("Created instance {}".format(instance_id)) | ||
|
|
||
|
|
||
| # [END spanner_create_instance] | ||
|
|
||
|
|
||
| # [START spanner_create_database_with_default_leader] | ||
| def create_database_with_default_leader(instance_id, database_id, default_leader): | ||
| """Creates a database with tables with a default leader.""" | ||
| spanner_client = spanner.Client() | ||
| operation = spanner_client.database_admin_api.create_database( | ||
| request=spanner_database_admin.CreateDatabaseRequest( | ||
| parent="projects/{}/instances/{}".format( | ||
| spanner_client.project, instance_id | ||
| ), | ||
| create_statement="CREATE DATABASE {}".format(database_id), | ||
| extra_statements=[ | ||
| """CREATE TABLE Singers ( | ||
| SingerId INT64 NOT NULL, | ||
| FirstName STRING(1024), | ||
| LastName STRING(1024), | ||
| SingerInfo BYTES(MAX) | ||
| ) PRIMARY KEY (SingerId)""", | ||
| """CREATE TABLE Albums ( | ||
| SingerId INT64 NOT NULL, | ||
| AlbumId INT64 NOT NULL, | ||
| AlbumTitle STRING(MAX) | ||
| ) PRIMARY KEY (SingerId, AlbumId), | ||
| INTERLEAVE IN PARENT Singers ON DELETE CASCADE""", | ||
| "ALTER DATABASE {}" | ||
| " SET OPTIONS (default_leader = '{}')".format( | ||
| database_id, default_leader | ||
| ), | ||
| ], | ||
| ) | ||
| ) | ||
|
|
||
| print("Waiting for operation to complete...") | ||
| database = operation.result(OPERATION_TIMEOUT_SECONDS) | ||
|
|
||
| print( | ||
| "Database {} created with default leader {}".format( | ||
| database.name, database.default_leader | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| # [END spanner_create_database_with_default_leader] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2024 Google, Inc. | ||
| # | ||
| # 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. | ||
|
|
||
| """This application demonstrates how to do basic operations using Cloud | ||
| Spanner. | ||
| For more information, see the README.rst under /spanner. | ||
| """ | ||
|
|
||
| import uuid | ||
|
|
||
| from google.api_core import exceptions | ||
| from google.cloud.spanner_admin_database_v1.types.common import DatabaseDialect | ||
| import pytest | ||
| from test_utils.retry import RetryErrors | ||
|
|
||
| import samples | ||
|
|
||
| CREATE_TABLE_SINGERS = """\ | ||
| CREATE TABLE Singers ( | ||
| SingerId INT64 NOT NULL, | ||
| FirstName STRING(1024), | ||
| LastName STRING(1024), | ||
| SingerInfo BYTES(MAX), | ||
| FullName STRING(2048) AS ( | ||
| ARRAY_TO_STRING([FirstName, LastName], " ") | ||
| ) STORED | ||
| ) PRIMARY KEY (SingerId) | ||
| """ | ||
|
|
||
| CREATE_TABLE_ALBUMS = """\ | ||
| CREATE TABLE Albums ( | ||
| SingerId INT64 NOT NULL, | ||
| AlbumId INT64 NOT NULL, | ||
| AlbumTitle STRING(MAX) | ||
| ) PRIMARY KEY (SingerId, AlbumId), | ||
| INTERLEAVE IN PARENT Singers ON DELETE CASCADE | ||
| """ | ||
|
|
||
| retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def sample_name(): | ||
| return "snippets" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def database_dialect(): | ||
| """Spanner dialect to be used for this sample. | ||
|
|
||
| The dialect is used to initialize the dialect for the database. | ||
| It can either be GoogleStandardSql or PostgreSql. | ||
| """ | ||
| return DatabaseDialect.GOOGLE_STANDARD_SQL | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def create_instance_id(): | ||
| """Id for the low-cost instance.""" | ||
| return f"create-instance-{uuid.uuid4().hex[:10]}" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def lci_instance_id(): | ||
| """Id for the low-cost instance.""" | ||
| return f"lci-instance-{uuid.uuid4().hex[:10]}" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def database_id(): | ||
| return f"test-db-{uuid.uuid4().hex[:10]}" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def create_database_id(): | ||
| return f"create-db-{uuid.uuid4().hex[:10]}" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def cmek_database_id(): | ||
| return f"cmek-db-{uuid.uuid4().hex[:10]}" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def default_leader_database_id(): | ||
| return f"leader_db_{uuid.uuid4().hex[:10]}" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def database_ddl(): | ||
| """Sequence of DDL statements used to set up the database. | ||
|
|
||
| Sample testcase modules can override as needed. | ||
| """ | ||
| return [CREATE_TABLE_SINGERS, CREATE_TABLE_ALBUMS] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def default_leader(): | ||
| """Default leader for multi-region instances.""" | ||
| return "us-east4" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def base_instance_config_id(spanner_client): | ||
| return "{}/instanceConfigs/{}".format(spanner_client.project_name, "nam7") | ||
|
|
||
|
|
||
| def test_create_instance_explicit(spanner_client, create_instance_id): | ||
| # Rather than re-use 'sample_isntance', we create a new instance, to | ||
| # ensure that the 'create_instance' snippet is tested. | ||
| retry_429(samples.create_instance)(create_instance_id) | ||
| instance = spanner_client.instance(create_instance_id) | ||
| retry_429(instance.delete)() | ||
|
|
||
|
|
||
| def test_create_database_with_default_leader( | ||
| capsys, | ||
| multi_region_instance, | ||
| multi_region_instance_id, | ||
| default_leader_database_id, | ||
| default_leader, | ||
| ): | ||
| retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15) | ||
| retry_429(samples.create_database_with_default_leader)( | ||
| multi_region_instance_id, default_leader_database_id, default_leader | ||
| ) | ||
| out, _ = capsys.readouterr() | ||
| assert default_leader_database_id in out | ||
| assert default_leader in out |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This python file should be run as
mainprogram since this will get referred in the Getting started with Python guide.Can we add it similar to existing one in snippets.py?
python-spanner/samples/samples/snippets.py
Line 2799 in f3b23b2
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is planned in another PR when we switch the tags later to new samples.