Skip to content

feat(spanner): add sample for create instance #4230

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion spanner/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@
from google.cloud.spanner_v1 import param_types


# [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
)

instance = spanner_client.instance(
instance_id,
configuration_name=config_name,
display_name="This is a display name.",
node_count=1,
)

operation = instance.create()

print('Waiting for operation to complete...')
operation.result(120)

print('Created instance {}'.format(instance_id))
# [END spanner_create_instance]


# [START spanner_create_database]
def create_database(instance_id, database_id):
"""Creates a database and tables for sample data."""
Expand Down Expand Up @@ -1455,6 +1480,7 @@ def create_client_with_query_options(instance_id, database_id):
default='example_db')

subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser('create_instance', help=create_instance.__doc__)
subparsers.add_parser('create_database', help=create_database.__doc__)
subparsers.add_parser('insert_data', help=insert_data.__doc__)
subparsers.add_parser('delete_data', help=delete_data.__doc__)
Expand Down Expand Up @@ -1567,7 +1593,9 @@ def create_client_with_query_options(instance_id, database_id):

args = parser.parse_args()

if args.command == 'create_database':
if args.command == 'create_instance':
create_instance(args.instance_id)
elif args.command == 'create_database':
create_database(args.instance_id, args.database_id)
elif args.command == 'insert_data':
insert_data(args.instance_id, args.database_id)
Expand Down
18 changes: 15 additions & 3 deletions spanner/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import time
import uuid

Expand All @@ -22,19 +21,27 @@
import snippets


def unique_instance_id():
""" Creates a unique id for the database. """
return f'test-instance-{uuid.uuid4().hex[:10]}'


def unique_database_id():
""" Creates a unique id for the database. """
return f'test-db-{uuid.uuid4().hex[:10]}'


INSTANCE_ID = os.environ['SPANNER_INSTANCE']
INSTANCE_ID = unique_instance_id()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it have a case that I want to use an existing instance for other tests (not create_instance test)?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, is there a way to keep SPANNER_INSTANCE but also be able to run create_instance test?

DATABASE_ID = unique_database_id()


@pytest.fixture(scope='module')
def spanner_instance():
snippets.create_instance(INSTANCE_ID)
spanner_client = spanner.Client()
return spanner_client.instance(INSTANCE_ID)
instance = spanner_client.instance(INSTANCE_ID)
yield instance
instance.delete()


@pytest.fixture(scope='module')
Expand All @@ -46,6 +53,11 @@ def database(spanner_instance):
db.drop()


def test_create_instance(spanner_instance):
# Reload will only succeed if the instance exists.
spanner_instance.reload()


def test_create_database(database):
# Reload will only succeed if the database exists.
database.reload()
Expand Down