Skip to content

Commit 1e6670e

Browse files
larkeeleahecole
andauthored
feat(spanner): add sample for create instance [(#4230)](GoogleCloudPlatform/python-docs-samples#4230)
Co-authored-by: larkee <[email protected]> Co-authored-by: Leah E. Cole <[email protected]>
1 parent a54e210 commit 1e6670e

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

samples/samples/snippets.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@
2828
from google.cloud.spanner_v1 import param_types
2929

3030

31+
# [START spanner_create_instance]
32+
def create_instance(instance_id):
33+
"""Creates an instance."""
34+
spanner_client = spanner.Client()
35+
36+
config_name = "{}/instanceConfigs/regional-us-central1".format(
37+
spanner_client.project_name
38+
)
39+
40+
instance = spanner_client.instance(
41+
instance_id,
42+
configuration_name=config_name,
43+
display_name="This is a display name.",
44+
node_count=1,
45+
)
46+
47+
operation = instance.create()
48+
49+
print('Waiting for operation to complete...')
50+
operation.result(120)
51+
52+
print('Created instance {}'.format(instance_id))
53+
# [END spanner_create_instance]
54+
55+
3156
# [START spanner_create_database]
3257
def create_database(instance_id, database_id):
3358
"""Creates a database and tables for sample data."""
@@ -1455,6 +1480,7 @@ def create_client_with_query_options(instance_id, database_id):
14551480
default='example_db')
14561481

14571482
subparsers = parser.add_subparsers(dest='command')
1483+
subparsers.add_parser('create_instance', help=create_instance.__doc__)
14581484
subparsers.add_parser('create_database', help=create_database.__doc__)
14591485
subparsers.add_parser('insert_data', help=insert_data.__doc__)
14601486
subparsers.add_parser('delete_data', help=delete_data.__doc__)
@@ -1567,7 +1593,9 @@ def create_client_with_query_options(instance_id, database_id):
15671593

15681594
args = parser.parse_args()
15691595

1570-
if args.command == 'create_database':
1596+
if args.command == 'create_instance':
1597+
create_instance(args.instance_id)
1598+
elif args.command == 'create_database':
15711599
create_database(args.instance_id, args.database_id)
15721600
elif args.command == 'insert_data':
15731601
insert_data(args.instance_id, args.database_id)

samples/samples/snippets_test.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import time
1716
import uuid
1817

@@ -22,19 +21,27 @@
2221
import snippets
2322

2423

24+
def unique_instance_id():
25+
""" Creates a unique id for the database. """
26+
return f'test-instance-{uuid.uuid4().hex[:10]}'
27+
28+
2529
def unique_database_id():
2630
""" Creates a unique id for the database. """
2731
return f'test-db-{uuid.uuid4().hex[:10]}'
2832

2933

30-
INSTANCE_ID = os.environ['SPANNER_INSTANCE']
34+
INSTANCE_ID = unique_instance_id()
3135
DATABASE_ID = unique_database_id()
3236

3337

3438
@pytest.fixture(scope='module')
3539
def spanner_instance():
40+
snippets.create_instance(INSTANCE_ID)
3641
spanner_client = spanner.Client()
37-
return spanner_client.instance(INSTANCE_ID)
42+
instance = spanner_client.instance(INSTANCE_ID)
43+
yield instance
44+
instance.delete()
3845

3946

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

4855

56+
def test_create_instance(spanner_instance):
57+
# Reload will only succeed if the instance exists.
58+
spanner_instance.reload()
59+
60+
4961
def test_create_database(database):
5062
# Reload will only succeed if the database exists.
5163
database.reload()

0 commit comments

Comments
 (0)