Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Prev Previous commit
Next Next commit
add more samples
  • Loading branch information
rahul2393 committed Feb 21, 2024
commit b18c55e64b4b29455c31f7c1037d47d7e79f6900
131 changes: 131 additions & 0 deletions samples/samples/admin/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,45 @@ def create_instance(instance_id):
# [END spanner_create_instance]


# [START spanner_create_instance_with_processing_units]
def create_instance_with_processing_units(instance_id, processing_units):
"""Creates an instance."""
spanner_client = spanner.Client()

config_name = "{}/instanceConfigs/regional-us-central1".format(
spanner_client.project_name
)

request = spanner_instance_admin.CreateInstanceRequest(
parent="projects/{}".format(spanner_client.project),
Comment thread
rahul2393 marked this conversation as resolved.
Outdated
instance_id=instance_id,
instance=spanner_instance_admin.Instance(
config=config_name,
Comment thread
harshachinta marked this conversation as resolved.
display_name="This is a display name.",
processing_units=processing_units,
labels={
"cloud_spanner_samples": "true",
"sample_name": "snippets-create_instance_with_processing_units",
"created": str(int(time.time())),
},
),
)

operation = spanner_client.instance_admin_api.create_instance(request=request)

print("Waiting for operation to complete...")
instance = operation.result(OPERATION_TIMEOUT_SECONDS)

print(
"Created instance {} with {} processing units".format(
instance_id, instance.processing_units
)
)


# [END spanner_create_instance_with_processing_units]


# [START spanner_create_database]
def create_database(instance_id, database_id):
"""Creates a database and tables for sample data."""
Expand Down Expand Up @@ -144,6 +183,49 @@ def create_database_with_default_leader(instance_id, database_id, default_leader
# [END spanner_create_database_with_default_leader]


# [START spanner_create_database_with_encryption_key]
def create_database_with_encryption_key(instance_id, database_id, kms_key_name):
"""Creates a database with tables using a Customer Managed Encryption Key (CMEK)."""
from google.cloud.spanner_admin_database_v1 import EncryptionConfig

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)

request = spanner_database_admin.CreateDatabaseRequest(
parent=instance.name,
create_statement=f"CREATE DATABASE `{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""",
],
encryption_config=EncryptionConfig(kms_key_name=kms_key_name),
)

operation = spanner_client.database_admin_api.create_database(request=request)

print("Waiting for operation to complete...")
database = operation.result(OPERATION_TIMEOUT_SECONDS)

print(
"Database {} created with encryption key {}".format(
database.name, database.encryption_config.kms_key_name
)
)


# [END spanner_create_database_with_encryption_key]


def add_and_drop_database_roles(instance_id, database_id):
"""Showcases how to manage a user defined database role."""
# [START spanner_add_and_drop_database_role]
Expand Down Expand Up @@ -572,3 +654,52 @@ def add_timestamp_column(instance_id, database_id):


# [END spanner_add_timestamp_column]


# [START spanner_create_index]
def add_index(instance_id, database_id):
"""Adds a simple index to the example database."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

request = spanner_database_admin.UpdateDatabaseDdlRequest(
database=database.name,
statements=["CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"],
)

operation = spanner_client.database_admin_api.update_database_ddl(request)

print("Waiting for operation to complete...")
operation.result(OPERATION_TIMEOUT_SECONDS)

print("Added the AlbumsByAlbumTitle index.")


# [END spanner_create_index]


# [START spanner_create_storing_index]
def add_storing_index(instance_id, database_id):
"""Adds an storing index to the example database."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

request = spanner_database_admin.UpdateDatabaseDdlRequest(
database=database.name,
statements=[
"CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle)"
"STORING (MarketingBudget)"
],
)

operation = spanner_client.database_admin_api.update_database_ddl(request)

print("Waiting for operation to complete...")
operation.result(OPERATION_TIMEOUT_SECONDS)

print("Added the AlbumsByAlbumTitle2 index.")


# [END spanner_create_storing_index]
46 changes: 45 additions & 1 deletion samples/samples/admin/samples_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import pytest
from google.api_core import exceptions
from google.cloud import spanner
from google.cloud.spanner_admin_database_v1.types.common import DatabaseDialect
from test_utils.retry import RetryErrors

Expand Down Expand Up @@ -127,6 +128,20 @@ def test_create_instance_explicit(spanner_client, create_instance_id):
retry_429(instance.delete)()


def test_create_instance_with_processing_units(capsys, lci_instance_id):
processing_units = 500
retry_429(samples.create_instance_with_processing_units)(
lci_instance_id,
processing_units,
)
out, _ = capsys.readouterr()
assert lci_instance_id in out
assert "{} processing units".format(processing_units) in out
spanner_client = spanner.Client()
instance = spanner_client.instance(lci_instance_id)
retry_429(instance.delete)()


def test_create_database_explicit(sample_instance, create_database_id):
# Rather than re-use 'sample_database', we create a new database, to
# ensure that the 'create_database' snippet is tested.
Expand All @@ -135,6 +150,17 @@ def test_create_database_explicit(sample_instance, create_database_id):
database.drop()


def test_create_database_with_encryption_config(
capsys, instance_id, cmek_database_id, kms_key_name
):
samples.create_database_with_encryption_key(
instance_id, cmek_database_id, kms_key_name
)
out, _ = capsys.readouterr()
assert cmek_database_id in out
assert kms_key_name in out


def test_create_database_with_default_leader(
capsys,
multi_region_instance,
Expand Down Expand Up @@ -265,8 +291,26 @@ def test_add_column(capsys, instance_id, sample_database):
assert "Added the MarketingBudget column." in out


# @pytest.mark.dependency(name="add_timestamp_column", depends=["create_table_with_datatypes"])
@pytest.mark.dependency(
name="add_timestamp_column", depends=["create_table_with_datatypes"]
)
def test_add_timestamp_column(capsys, instance_id, sample_database):
samples.add_timestamp_column(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert 'Altered table "Albums" on database ' in out


@pytest.mark.dependency(name="add_index", depends=["create_table_with_datatypes"])
def test_add_index(capsys, instance_id, sample_database):
samples.add_index(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "Added the AlbumsByAlbumTitle index" in out


@pytest.mark.dependency(
name="add_storing_index", depends=["create_table_with_datatypes"]
)
def test_add_storing_index(capsys, instance_id, sample_database):
samples.add_storing_index(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "Added the AlbumsByAlbumTitle2 index." in out