From fb1f154299c44821890675a3003964695eb63377 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Nov 2026 15:35:05 -0700 Subject: [PATCH 1/6] remove wait for table from samples --- .../google-cloud-bigtable/samples/data_client/hello/main.py | 3 --- .../samples/data_client_async/hello/async_main.py | 4 ---- 2 files changed, 7 deletions(-) diff --git a/packages/google-cloud-bigtable/samples/data_client/hello/main.py b/packages/google-cloud-bigtable/samples/data_client/hello/main.py index a7faa3bfc2f3..b317b35ec336 100644 --- a/packages/google-cloud-bigtable/samples/data_client/hello/main.py +++ b/packages/google-cloud-bigtable/samples/data_client/hello/main.py @@ -31,7 +31,6 @@ from google.cloud.bigtable.data import row_filters # [END bigtable_hw_imports] -from ...utils import wait_for_table # use to ignore warnings row_filters @@ -73,8 +72,6 @@ def main(project_id, instance_id, table_id): # [END bigtable_hw_create_table] try: - # let table creation complete - wait_for_table(table) # [START bigtable_hw_write_rows] print("Writing some greetings to the table.") greetings = [b"Hello World!", b"Hello Cloud Bigtable!", b"Hello Python!"] diff --git a/packages/google-cloud-bigtable/samples/data_client_async/hello/async_main.py b/packages/google-cloud-bigtable/samples/data_client_async/hello/async_main.py index 8b39920c9686..46c91a2358c9 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/hello/async_main.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/hello/async_main.py @@ -31,8 +31,6 @@ from google.cloud import bigtable from google.cloud.bigtable.data import row_filters -from ...utils import wait_for_table - # [END bigtable_async_hw_imports] # use to ignore warnings @@ -74,8 +72,6 @@ async def main(project_id, instance_id, table_id): # [END bigtable_async_hw_create_table] try: - # let table creation complete - wait_for_table(table) # [START bigtable_async_hw_write_rows] print("Writing some greetings to the table.") greetings = [b"Hello World!", b"Hello Cloud Bigtable!", b"Hello Python!"] From 385a30bc1bae10e2b5ca9a670cebad369dbd6b85 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Nov 2026 15:35:24 -0700 Subject: [PATCH 2/6] added authorized view samples --- .../snippets/data_client_snippets.py | 73 +++++++++++++++++++ .../snippets/data_client_snippets_test.py | 43 +++++++++++ .../snippets/data_client_snippets_async.py | 73 +++++++++++++++++++ .../data_client_snippets_async_test.py | 46 ++++++++++++ 4 files changed, 235 insertions(+) diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py index aa7e35694802..aed4a9c9f7a8 100644 --- a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py @@ -369,3 +369,76 @@ def print_row(row): # [END bigtable_reads_print] + + +def authorized_view_write_rows(table, authorized_view_id): + # [START bigtable_authorized_view_write_rows] + from google.cloud.bigtable.data import BigtableDataClient, SetCell + + def write_rows(project_id, instance_id, table_id, authorized_view_id): + with BigtableDataClient(project=project_id) as client: + with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + family_id = "stats_summary" + for i, name in enumerate(["World", "Bigtable", "Python"]): + greeting = f"Hello {name}!" + row_key = f"greeting{i}" + authorized_view.mutate_row( + row_key, + [ + SetCell(family_id, "name", name), + SetCell(family_id, "greeting", greeting), + ], + ) + print(greeting) + + # [END bigtable_authorized_view_write_rows] + write_rows( + table.client.project, table.instance_id, table.table_id, authorized_view_id + ) + + +def authorized_view_get_by_key(table, authorized_view_id): + # [START bigtable_authorized_view_get_by_key] + from google.cloud.bigtable.data import BigtableDataClient + + def get_by_key(project_id, instance_id, table_id, authorized_view_id): + with BigtableDataClient(project=project_id) as client: + with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + row_key = "greeting0" + row = authorized_view.read_row(row_key) + print(row) + + # [END bigtable_authorized_view_get_by_key] + get_by_key( + table.client.project, table.instance_id, table.table_id, authorized_view_id + ) + + +def authorized_view_scan_with_filter(table, authorized_view_id): + # [START bigtable_authorized_view_scan_with_filter] + from google.cloud.bigtable.data import ( + BigtableDataClient, + ReadRowsQuery, + row_filters, + ) + + def scan_with_filter(project_id, instance_id, table_id, authorized_view_id): + with BigtableDataClient(project=project_id) as client: + with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + # Only return the most recent version of each cell + row_filter = row_filters.CellsColumnLimitFilter(1) + query = ReadRowsQuery(row_filter=row_filter) + + for row in authorized_view.read_rows_stream(query): + print(row) + + # [END bigtable_authorized_view_scan_with_filter] + scan_with_filter( + table.client.project, table.instance_id, table.table_id, authorized_view_id + ) diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py index 54eea31c5d78..18cc0b0b74fa 100644 --- a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py @@ -22,6 +22,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] TABLE_ID = f"data-client-sync-{str(uuid.uuid4())[:16]}" +AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" @pytest.fixture(scope="session") @@ -48,6 +49,30 @@ def table_id(column_family_config): yield TABLE_ID +@pytest.fixture(scope="session") +def authorized_view_id(table_id): + from google.cloud import bigtable_admin_v2 + + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + # A view over every row, restricted to the stats_summary column family + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + "stats_summary": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""] + ) + }, + ) + admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=AUTHORIZED_VIEW_ID, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ).result() + # the view is deleted along with the table + yield AUTHORIZED_VIEW_ID + + @pytest.fixture def table(table_id): from google.cloud.bigtable.data import BigtableDataClient @@ -107,3 +132,21 @@ def test_read_with_filter(table): def test_execute_query(table): data_snippets.execute_query(table) + + +def test_authorized_view_write_rows(capsys, table, authorized_view_id): + data_snippets.authorized_view_write_rows(table, authorized_view_id) + out, _ = capsys.readouterr() + assert "Hello World!" in out + + +def test_authorized_view_get_by_key(capsys, table, authorized_view_id): + data_snippets.authorized_view_get_by_key(table, authorized_view_id) + out, _ = capsys.readouterr() + assert "Hello World!" in out + + +def test_authorized_view_scan_with_filter(capsys, table, authorized_view_id): + data_snippets.authorized_view_scan_with_filter(table, authorized_view_id) + out, _ = capsys.readouterr() + assert "Hello Python!" in out diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py index 15a4e562614d..07f12e9d5ee0 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py @@ -369,3 +369,76 @@ def print_row(row): # [END bigtable_async_reads_print] + + +async def authorized_view_write_rows(table, authorized_view_id): + # [START bigtable_async_authorized_view_write_rows] + from google.cloud.bigtable.data import BigtableDataClientAsync, SetCell + + async def write_rows(project_id, instance_id, table_id, authorized_view_id): + async with BigtableDataClientAsync(project=project_id) as client: + async with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + family_id = "stats_summary" + for i, name in enumerate(["World", "Bigtable", "Python"]): + greeting = f"Hello {name}!" + row_key = f"greeting{i}" + await authorized_view.mutate_row( + row_key, + [ + SetCell(family_id, "name", name), + SetCell(family_id, "greeting", greeting), + ], + ) + print(greeting) + + # [END bigtable_async_authorized_view_write_rows] + await write_rows( + table.client.project, table.instance_id, table.table_id, authorized_view_id + ) + + +async def authorized_view_get_by_key(table, authorized_view_id): + # [START bigtable_async_authorized_view_get_by_key] + from google.cloud.bigtable.data import BigtableDataClientAsync + + async def get_by_key(project_id, instance_id, table_id, authorized_view_id): + async with BigtableDataClientAsync(project=project_id) as client: + async with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + row_key = "greeting0" + row = await authorized_view.read_row(row_key) + print(row) + + # [END bigtable_async_authorized_view_get_by_key] + await get_by_key( + table.client.project, table.instance_id, table.table_id, authorized_view_id + ) + + +async def authorized_view_scan_with_filter(table, authorized_view_id): + # [START bigtable_async_authorized_view_scan_with_filter] + from google.cloud.bigtable.data import ( + BigtableDataClientAsync, + ReadRowsQuery, + row_filters, + ) + + async def scan_with_filter(project_id, instance_id, table_id, authorized_view_id): + async with BigtableDataClientAsync(project=project_id) as client: + async with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + # Only return the most recent version of each cell + row_filter = row_filters.CellsColumnLimitFilter(1) + query = ReadRowsQuery(row_filter=row_filter) + + async for row in await authorized_view.read_rows_stream(query): + print(row) + + # [END bigtable_async_authorized_view_scan_with_filter] + await scan_with_filter( + table.client.project, table.instance_id, table.table_id, authorized_view_id + ) diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py index d7e4d46b2402..e1c1ec543329 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py @@ -22,6 +22,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] TABLE_ID = f"data-client-{str(uuid.uuid4())[:16]}" +AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" @pytest.fixture(scope="session") @@ -48,6 +49,30 @@ def table_id(column_family_config): yield TABLE_ID +@pytest.fixture(scope="session") +def authorized_view_id(table_id): + from google.cloud import bigtable_admin_v2 + + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + # A view over every row, restricted to the stats_summary column family + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + "stats_summary": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""] + ) + }, + ) + admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=AUTHORIZED_VIEW_ID, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ).result() + # the view is deleted along with the table + yield AUTHORIZED_VIEW_ID + + @pytest_asyncio.fixture async def table(table_id): from google.cloud.bigtable.data import BigtableDataClientAsync @@ -120,3 +145,24 @@ async def test_read_with_filter(table): @pytest.mark.asyncio async def test_execute_query(table): await data_snippets.execute_query(table) + + +@pytest.mark.asyncio +async def test_authorized_view_write_rows(capsys, table, authorized_view_id): + await data_snippets.authorized_view_write_rows(table, authorized_view_id) + out, _ = capsys.readouterr() + assert "Hello World!" in out + + +@pytest.mark.asyncio +async def test_authorized_view_get_by_key(capsys, table, authorized_view_id): + await data_snippets.authorized_view_get_by_key(table, authorized_view_id) + out, _ = capsys.readouterr() + assert "Hello World!" in out + + +@pytest.mark.asyncio +async def test_authorized_view_scan_with_filter(capsys, table, authorized_view_id): + await data_snippets.authorized_view_scan_with_filter(table, authorized_view_id) + out, _ = capsys.readouterr() + assert "Hello Python!" in out From 2f6aefd3019b9e731a4c27155b28a9bea63f5684 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Nov 2026 15:35:59 -0700 Subject: [PATCH 3/6] added missing delete samples --- .../snippets/deletes_async_test.py | 14 +++++++ .../snippets/deletes_snippets_async.py | 37 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_async_test.py index f5e93995cff9..bff0a010bab8 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_async_test.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_async_test.py @@ -279,3 +279,17 @@ async def test_streaming_and_batching(capsys, table_id): async def test_check_and_mutate(capsys, table_id): await deletes_snippets_async.check_and_mutate(PROJECT, BIGTABLE_INSTANCE, table_id) assert_output_match(capsys, "") + + +@pytest.mark.asyncio +async def test_drop_row_range(capsys, table_id): + await deletes_snippets_async.drop_row_range(PROJECT, BIGTABLE_INSTANCE, table_id) + assert_output_match(capsys, "") + + +@pytest.mark.asyncio +async def test_delete_column_family(capsys, table_id): + await deletes_snippets_async.delete_column_family( + PROJECT, BIGTABLE_INSTANCE, table_id + ) + assert_output_match(capsys, "") diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_snippets_async.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_snippets_async.py index b70d557e7610..569692d99885 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_snippets_async.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/deletes_snippets_async.py @@ -122,3 +122,40 @@ async def check_and_mutate(project_id, instance_id, table_id): # [END bigtable_check_and_mutate_asyncio] + + +# [START bigtable_drop_row_range_asyncio] +async def drop_row_range(project_id, instance_id, table_id): + from google.cloud import bigtable_admin_v2 + + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as client: + table_path = client.table_path(project_id, instance_id, table_id) + row_key_prefix = b"phone#4c410523" + await client.drop_row_range( + request={ + "name": table_path, + "row_key_prefix": row_key_prefix, + }, + timeout=200, + ) + + +# [END bigtable_drop_row_range_asyncio] + + +# [START bigtable_delete_column_family_asyncio] +async def delete_column_family(project_id, instance_id, table_id): + from google.cloud import bigtable_admin_v2 + + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as client: + table_path = client.table_path(project_id, instance_id, table_id) + column_family_id = "stats_summary" + modification = bigtable_admin_v2.ModifyColumnFamiliesRequest.Modification( + id=column_family_id, drop=True + ) + await client.modify_column_families( + name=table_path, modifications=[modification] + ) + + +# [END bigtable_delete_column_family_asyncio] From f1efbd7f152e007ca99408e167a671c152442b6c Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Nov 2026 16:00:33 -0700 Subject: [PATCH 4/6] updated authorized views --- .../data_client/authorized_views/__init__.py | 0 .../authorized_views/authorized_views.py | 317 +++++++++++++++++ .../authorized_views/authorized_views_test.py | 54 +++ .../authorized_views/requirements.txt | 1 + .../snippets/data_client_snippets.py | 73 ---- .../snippets/data_client_snippets_test.py | 43 --- .../authorized_views/__init__.py | 0 .../authorized_views_async.py | 335 ++++++++++++++++++ .../authorized_views_async_test.py | 58 +++ .../authorized_views/requirements.txt | 1 + .../snippets/data_client_snippets_async.py | 73 ---- .../data_client_snippets_async_test.py | 46 --- 12 files changed, 766 insertions(+), 235 deletions(-) create mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/__init__.py create mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py create mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py create mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt create mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/__init__.py create mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py create mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py create mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/__init__.py b/packages/google-cloud-bigtable/samples/data_client/authorized_views/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py b/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py new file mode 100644 index 000000000000..8b2935879e22 --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py @@ -0,0 +1,317 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# 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. + +"""Demonstrates how to create, manage, and use a Bigtable authorized view. + +Prerequisites: + +- Create a Cloud Bigtable instance. + https://cloud.google.com/bigtable/docs/creating-instance +- Set your Google Application Default Credentials. + https://developers.google.com/identity/protocols/application-default-credentials +""" + +import argparse + +from google.api_core.exceptions import NotFound, PermissionDenied +from google.protobuf import field_mask_pb2 + +from google.cloud import bigtable_admin_v2 +from google.cloud.bigtable.data import ( + BigtableDataClient, + ReadRowsQuery, + SetCell, + row_filters, +) + +COLUMN_FAMILY = "cf" +COLUMN_QUALIFIER_GREETING = "greeting" +COLUMN_QUALIFIER_NAME = "name" +ROW_KEY_PREFIX = "rowKey" + + +def run(project_id, instance_id, table_id, authorized_view_id): + create_table(project_id, instance_id, table_id) + create_authorized_view(project_id, instance_id, table_id, authorized_view_id) + update_authorized_view(project_id, instance_id, table_id, authorized_view_id) + get_authorized_view(project_id, instance_id, table_id, authorized_view_id) + list_authorized_views(project_id, instance_id, table_id) + write_to_authorized_view(project_id, instance_id, table_id, authorized_view_id) + read_single_row_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id + ) + read_rows_with_filter_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id + ) + delete_authorized_view(project_id, instance_id, table_id, authorized_view_id) + delete_table(project_id, instance_id, table_id) + + +def create_table(project_id, instance_id, table_id): + # Checks if the table exists, creates it if it does not exist. + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(project_id, instance_id, table_id) + try: + admin_client.get_table( + request={"name": table_path, "view": bigtable_admin_v2.Table.View.NAME_ONLY} + ) + except NotFound: + print(f"Table does not exist, creating table: {table_id}") + table = admin_client.create_table( + parent=admin_client.instance_path(project_id, instance_id), + table_id=table_id, + table=bigtable_admin_v2.Table( + column_families={COLUMN_FAMILY: bigtable_admin_v2.ColumnFamily()} + ), + ) + print(f"Table: {table.name} created successfully") + + +def delete_table(project_id, instance_id, table_id): + # Deletes the entire table. + print(f"\nDelete table: {table_id}") + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + try: + admin_client.delete_table( + name=admin_client.table_path(project_id, instance_id, table_id) + ) + print(f"Table: {table_id} deleted successfully") + except NotFound as e: + print(f"Failed to delete a non-existent table: {e.message}") + + +def create_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Creates an authorized view under a table with the specified configuration.""" + print(f"\nCreating authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_create_authorized_view] + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(project_id, instance_id, table_id) + + # Permit every row, but only the "name" column of the column family. + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[COLUMN_QUALIFIER_NAME.encode()] + ) + }, + ) + operation = admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=authorized_view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ) + authorized_view = operation.result() + print(f"AuthorizedView: {authorized_view.name} created successfully") + # [END bigtable_create_authorized_view] + + +def update_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Modifies an authorized view.""" + print(f"\nUpdating authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_update_authorized_view] + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + authorized_view_path = admin_client.authorized_view_path( + project_id, instance_id, table_id, authorized_view_id + ) + + # Update to an authorized view permitting everything. + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""] + ) + }, + ) + operation = admin_client.update_authorized_view( + authorized_view=bigtable_admin_v2.AuthorizedView( + name=authorized_view_path, subset_view=subset_view + ), + update_mask=field_mask_pb2.FieldMask(paths=["subset_view"]), + ) + authorized_view = operation.result() + print(f"AuthorizedView: {authorized_view.name} updated successfully") + # [END bigtable_update_authorized_view] + + +def get_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Gets an authorized view's metadata.""" + print(f"\nGetting authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_get_authorized_view] + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + authorized_view_path = admin_client.authorized_view_path( + project_id, instance_id, table_id, authorized_view_id + ) + + try: + authorized_view = admin_client.get_authorized_view(name=authorized_view_path) + subset_view = authorized_view.subset_view + for row_prefix in subset_view.row_prefixes: + print(f"Row Prefix: {row_prefix.decode()}") + for family, family_subsets in subset_view.family_subsets.items(): + for qualifier_prefix in family_subsets.qualifier_prefixes: + print( + f"Column Family: {family}, " + f"Qualifier Prefix: {qualifier_prefix.decode()}" + ) + for qualifier in family_subsets.qualifiers: + print(f"Column Family: {family}, Qualifier: {qualifier.decode()}") + except NotFound as e: + print( + "Failed to retrieve metadata from a non-existent authorized view: " + f"{e.message}" + ) + # [END bigtable_get_authorized_view] + + +def list_authorized_views(project_id, instance_id, table_id): + """Lists all authorized views within a table.""" + print(f"\nListing authorized views in table {table_id}") + # [START bigtable_list_authorized_views] + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(project_id, instance_id, table_id) + + try: + for authorized_view in admin_client.list_authorized_views(parent=table_path): + authorized_view_id = admin_client.parse_authorized_view_path( + authorized_view.name + )["authorized_view"] + print(authorized_view_id) + except NotFound as e: + print(f"Failed to list authorized views from a non-existent table: {e.message}") + # [END bigtable_list_authorized_views] + + +def delete_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Deletes an authorized view.""" + print(f"\nDeleting authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_delete_authorized_view] + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + authorized_view_path = admin_client.authorized_view_path( + project_id, instance_id, table_id, authorized_view_id + ) + + try: + admin_client.delete_authorized_view(name=authorized_view_path) + print(f"AuthorizedView: {authorized_view_id} deleted successfully") + except NotFound as e: + print(f"Failed to delete a non-existent authorized view: {e.message}") + # [END bigtable_delete_authorized_view] + + +def write_to_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Writes some rows to an authorized view.""" + # [START bigtable_authorized_view_write_rows] + with BigtableDataClient(project=project_id) as client: + with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + try: + print("\nWriting to authorized view") + names = ["World", "Bigtable", "Python"] + for i, name in enumerate(names): + greeting = f"Hello {name}!" + authorized_view.mutate_row( + f"{ROW_KEY_PREFIX}{i}", + [ + SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, name), + SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting), + ], + ) + print(greeting) + except NotFound as e: + print(f"Failed to write to non-existent authorized view: {e.message}") + except PermissionDenied as e: + print( + "Failed to apply mutations outside of the authorized view: " + f"{e.message}" + ) + # [END bigtable_authorized_view_write_rows] + + +def read_single_row_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id +): + """Reads a single row from an authorized view.""" + # [START bigtable_authorized_view_get_by_key] + with BigtableDataClient(project=project_id) as client: + with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + try: + print("\nReading a single row by row key from an authorized view") + row = authorized_view.read_row(f"{ROW_KEY_PREFIX}0") + print(f"Row: {row.row_key.decode()}") + for cell in row.cells: + print( + f"Family: {cell.family} " + f"Qualifier: {cell.qualifier.decode()} " + f"Value: {cell.value.decode()}" + ) + except NotFound as e: + print( + f"Failed to read from a non-existent authorized view: {e.message}" + ) + # [END bigtable_authorized_view_get_by_key] + + +def read_rows_with_filter_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id +): + """Reads rows from an authorized view with a filter.""" + # [START bigtable_authorized_view_scan_with_filter] + with BigtableDataClient(project=project_id) as client: + with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + try: + # A filter that matches only the most recent cell within each column + row_filter = row_filters.CellsColumnLimitFilter(1) + print("\nScanning authorized view with filter") + query = ReadRowsQuery(row_filter=row_filter) + for row in authorized_view.read_rows_stream(query): + print(f"Row Key: {row.row_key.decode()}") + for cell in row.cells: + print( + f"Family: {cell.family} " + f"Qualifier: {cell.qualifier.decode()} " + f"Value: {cell.value.decode()}" + ) + except NotFound as e: + print(f"Failed to read a non-existent authorized view: {e.message}") + # [END bigtable_authorized_view_scan_with_filter] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument("project_id", help="Your Cloud Platform project ID.") + parser.add_argument( + "instance_id", help="ID of the Cloud Bigtable instance to connect to." + ) + parser.add_argument( + "--table", help="Table to create and destroy.", default="test-table" + ) + parser.add_argument( + "--authorized_view", + help="Authorized view to create and destroy.", + default="test-authorized-view", + ) + + args = parser.parse_args() + run(args.project_id, args.instance_id, args.table, args.authorized_view) diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py b/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py new file mode 100644 index 000000000000..8eb69f61e9ef --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py @@ -0,0 +1,54 @@ +# Copyright 2026 Google LLC +# +# 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. + +import os +import uuid + +from google.api_core.exceptions import NotFound + +from google.cloud import bigtable_admin_v2 + +from . import authorized_views + +PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] +BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] +TABLE_ID = f"authorized-views-{str(uuid.uuid4())[:16]}" +AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" + + +def test_run(capsys): + try: + authorized_views.run(PROJECT, BIGTABLE_INSTANCE, TABLE_ID, AUTHORIZED_VIEW_ID) + finally: + # the sample deletes the table itself; this only matters if it failed midway + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + try: + admin_client.delete_table( + name=admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, TABLE_ID) + ) + except NotFound: + pass + + out, _ = capsys.readouterr() + assert f"Table does not exist, creating table: {TABLE_ID}" in out + assert f"authorizedViews/{AUTHORIZED_VIEW_ID} created successfully" in out + assert f"authorizedViews/{AUTHORIZED_VIEW_ID} updated successfully" in out + assert "Column Family: cf, Qualifier Prefix: " in out + assert f"\n{AUTHORIZED_VIEW_ID}\n" in out + assert "Hello World!" in out + assert "Row: rowKey0" in out + assert "Family: cf Qualifier: greeting Value: Hello World!" in out + assert "Row Key: rowKey2" in out + assert f"AuthorizedView: {AUTHORIZED_VIEW_ID} deleted successfully" in out + assert f"Table: {TABLE_ID} deleted successfully" in out diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt b/packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt new file mode 100644 index 000000000000..74e55ca10648 --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt @@ -0,0 +1 @@ +google-cloud-bigtable>=2.35.0 diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py index aed4a9c9f7a8..aa7e35694802 100644 --- a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets.py @@ -369,76 +369,3 @@ def print_row(row): # [END bigtable_reads_print] - - -def authorized_view_write_rows(table, authorized_view_id): - # [START bigtable_authorized_view_write_rows] - from google.cloud.bigtable.data import BigtableDataClient, SetCell - - def write_rows(project_id, instance_id, table_id, authorized_view_id): - with BigtableDataClient(project=project_id) as client: - with client.get_authorized_view( - instance_id, table_id, authorized_view_id - ) as authorized_view: - family_id = "stats_summary" - for i, name in enumerate(["World", "Bigtable", "Python"]): - greeting = f"Hello {name}!" - row_key = f"greeting{i}" - authorized_view.mutate_row( - row_key, - [ - SetCell(family_id, "name", name), - SetCell(family_id, "greeting", greeting), - ], - ) - print(greeting) - - # [END bigtable_authorized_view_write_rows] - write_rows( - table.client.project, table.instance_id, table.table_id, authorized_view_id - ) - - -def authorized_view_get_by_key(table, authorized_view_id): - # [START bigtable_authorized_view_get_by_key] - from google.cloud.bigtable.data import BigtableDataClient - - def get_by_key(project_id, instance_id, table_id, authorized_view_id): - with BigtableDataClient(project=project_id) as client: - with client.get_authorized_view( - instance_id, table_id, authorized_view_id - ) as authorized_view: - row_key = "greeting0" - row = authorized_view.read_row(row_key) - print(row) - - # [END bigtable_authorized_view_get_by_key] - get_by_key( - table.client.project, table.instance_id, table.table_id, authorized_view_id - ) - - -def authorized_view_scan_with_filter(table, authorized_view_id): - # [START bigtable_authorized_view_scan_with_filter] - from google.cloud.bigtable.data import ( - BigtableDataClient, - ReadRowsQuery, - row_filters, - ) - - def scan_with_filter(project_id, instance_id, table_id, authorized_view_id): - with BigtableDataClient(project=project_id) as client: - with client.get_authorized_view( - instance_id, table_id, authorized_view_id - ) as authorized_view: - # Only return the most recent version of each cell - row_filter = row_filters.CellsColumnLimitFilter(1) - query = ReadRowsQuery(row_filter=row_filter) - - for row in authorized_view.read_rows_stream(query): - print(row) - - # [END bigtable_authorized_view_scan_with_filter] - scan_with_filter( - table.client.project, table.instance_id, table.table_id, authorized_view_id - ) diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py index 18cc0b0b74fa..54eea31c5d78 100644 --- a/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/data_client_snippets_test.py @@ -22,7 +22,6 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] TABLE_ID = f"data-client-sync-{str(uuid.uuid4())[:16]}" -AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" @pytest.fixture(scope="session") @@ -49,30 +48,6 @@ def table_id(column_family_config): yield TABLE_ID -@pytest.fixture(scope="session") -def authorized_view_id(table_id): - from google.cloud import bigtable_admin_v2 - - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) - # A view over every row, restricted to the stats_summary column family - subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], - family_subsets={ - "stats_summary": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b""] - ) - }, - ) - admin_client.create_authorized_view( - parent=table_path, - authorized_view_id=AUTHORIZED_VIEW_ID, - authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), - ).result() - # the view is deleted along with the table - yield AUTHORIZED_VIEW_ID - - @pytest.fixture def table(table_id): from google.cloud.bigtable.data import BigtableDataClient @@ -132,21 +107,3 @@ def test_read_with_filter(table): def test_execute_query(table): data_snippets.execute_query(table) - - -def test_authorized_view_write_rows(capsys, table, authorized_view_id): - data_snippets.authorized_view_write_rows(table, authorized_view_id) - out, _ = capsys.readouterr() - assert "Hello World!" in out - - -def test_authorized_view_get_by_key(capsys, table, authorized_view_id): - data_snippets.authorized_view_get_by_key(table, authorized_view_id) - out, _ = capsys.readouterr() - assert "Hello World!" in out - - -def test_authorized_view_scan_with_filter(capsys, table, authorized_view_id): - data_snippets.authorized_view_scan_with_filter(table, authorized_view_id) - out, _ = capsys.readouterr() - assert "Hello Python!" in out diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/__init__.py b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py new file mode 100644 index 000000000000..5729b6cb8791 --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py @@ -0,0 +1,335 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# 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. + +"""Demonstrates how to create, manage, and use a Bigtable authorized view +with the asyncio clients. + +Prerequisites: + +- Create a Cloud Bigtable instance. + https://cloud.google.com/bigtable/docs/creating-instance +- Set your Google Application Default Credentials. + https://developers.google.com/identity/protocols/application-default-credentials +""" + +import argparse +import asyncio + +from google.api_core.exceptions import NotFound, PermissionDenied +from google.protobuf import field_mask_pb2 + +from google.cloud import bigtable_admin_v2 +from google.cloud.bigtable.data import ( + BigtableDataClientAsync, + ReadRowsQuery, + SetCell, + row_filters, +) + +COLUMN_FAMILY = "cf" +COLUMN_QUALIFIER_GREETING = "greeting" +COLUMN_QUALIFIER_NAME = "name" +ROW_KEY_PREFIX = "rowKey" + + +async def run(project_id, instance_id, table_id, authorized_view_id): + await create_table(project_id, instance_id, table_id) + await create_authorized_view(project_id, instance_id, table_id, authorized_view_id) + await update_authorized_view(project_id, instance_id, table_id, authorized_view_id) + await get_authorized_view(project_id, instance_id, table_id, authorized_view_id) + await list_authorized_views(project_id, instance_id, table_id) + await write_to_authorized_view( + project_id, instance_id, table_id, authorized_view_id + ) + await read_single_row_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id + ) + await read_rows_with_filter_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id + ) + await delete_authorized_view(project_id, instance_id, table_id, authorized_view_id) + await delete_table(project_id, instance_id, table_id) + + +async def create_table(project_id, instance_id, table_id): + # Checks if the table exists, creates it if it does not exist. + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(project_id, instance_id, table_id) + try: + await admin_client.get_table( + request={ + "name": table_path, + "view": bigtable_admin_v2.Table.View.NAME_ONLY, + } + ) + except NotFound: + print(f"Table does not exist, creating table: {table_id}") + table = await admin_client.create_table( + parent=admin_client.instance_path(project_id, instance_id), + table_id=table_id, + table=bigtable_admin_v2.Table( + column_families={COLUMN_FAMILY: bigtable_admin_v2.ColumnFamily()} + ), + ) + print(f"Table: {table.name} created successfully") + + +async def delete_table(project_id, instance_id, table_id): + # Deletes the entire table. + print(f"\nDelete table: {table_id}") + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + try: + await admin_client.delete_table( + name=admin_client.table_path(project_id, instance_id, table_id) + ) + print(f"Table: {table_id} deleted successfully") + except NotFound as e: + print(f"Failed to delete a non-existent table: {e.message}") + + +async def create_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Creates an authorized view under a table with the specified configuration.""" + print(f"\nCreating authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_async_create_authorized_view] + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(project_id, instance_id, table_id) + + # Permit every row, but only the "name" column of the column family. + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[COLUMN_QUALIFIER_NAME.encode()] + ) + }, + ) + operation = await admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=authorized_view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ) + authorized_view = await operation.result() + print(f"AuthorizedView: {authorized_view.name} created successfully") + # [END bigtable_async_create_authorized_view] + + +async def update_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Modifies an authorized view.""" + print(f"\nUpdating authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_async_update_authorized_view] + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + authorized_view_path = admin_client.authorized_view_path( + project_id, instance_id, table_id, authorized_view_id + ) + + # Update to an authorized view permitting everything. + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""] + ) + }, + ) + operation = await admin_client.update_authorized_view( + authorized_view=bigtable_admin_v2.AuthorizedView( + name=authorized_view_path, subset_view=subset_view + ), + update_mask=field_mask_pb2.FieldMask(paths=["subset_view"]), + ) + authorized_view = await operation.result() + print(f"AuthorizedView: {authorized_view.name} updated successfully") + # [END bigtable_async_update_authorized_view] + + +async def get_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Gets an authorized view's metadata.""" + print(f"\nGetting authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_async_get_authorized_view] + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + authorized_view_path = admin_client.authorized_view_path( + project_id, instance_id, table_id, authorized_view_id + ) + + try: + authorized_view = await admin_client.get_authorized_view( + name=authorized_view_path + ) + subset_view = authorized_view.subset_view + for row_prefix in subset_view.row_prefixes: + print(f"Row Prefix: {row_prefix.decode()}") + for family, family_subsets in subset_view.family_subsets.items(): + for qualifier_prefix in family_subsets.qualifier_prefixes: + print( + f"Column Family: {family}, " + f"Qualifier Prefix: {qualifier_prefix.decode()}" + ) + for qualifier in family_subsets.qualifiers: + print(f"Column Family: {family}, Qualifier: {qualifier.decode()}") + except NotFound as e: + print( + "Failed to retrieve metadata from a non-existent authorized view: " + f"{e.message}" + ) + # [END bigtable_async_get_authorized_view] + + +async def list_authorized_views(project_id, instance_id, table_id): + """Lists all authorized views within a table.""" + print(f"\nListing authorized views in table {table_id}") + # [START bigtable_async_list_authorized_views] + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(project_id, instance_id, table_id) + + try: + async for authorized_view in await admin_client.list_authorized_views( + parent=table_path + ): + authorized_view_id = admin_client.parse_authorized_view_path( + authorized_view.name + )["authorized_view"] + print(authorized_view_id) + except NotFound as e: + print( + "Failed to list authorized views from a non-existent table: " + f"{e.message}" + ) + # [END bigtable_async_list_authorized_views] + + +async def delete_authorized_view(project_id, instance_id, table_id, authorized_view_id): + """Deletes an authorized view.""" + print(f"\nDeleting authorized view {authorized_view_id} in table {table_id}") + # [START bigtable_async_delete_authorized_view] + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + authorized_view_path = admin_client.authorized_view_path( + project_id, instance_id, table_id, authorized_view_id + ) + + try: + await admin_client.delete_authorized_view(name=authorized_view_path) + print(f"AuthorizedView: {authorized_view_id} deleted successfully") + except NotFound as e: + print(f"Failed to delete a non-existent authorized view: {e.message}") + # [END bigtable_async_delete_authorized_view] + + +async def write_to_authorized_view( + project_id, instance_id, table_id, authorized_view_id +): + """Writes some rows to an authorized view.""" + # [START bigtable_async_authorized_view_write_rows] + async with BigtableDataClientAsync(project=project_id) as client: + async with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + try: + print("\nWriting to authorized view") + names = ["World", "Bigtable", "Python"] + for i, name in enumerate(names): + greeting = f"Hello {name}!" + await authorized_view.mutate_row( + f"{ROW_KEY_PREFIX}{i}", + [ + SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, name), + SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting), + ], + ) + print(greeting) + except NotFound as e: + print(f"Failed to write to non-existent authorized view: {e.message}") + except PermissionDenied as e: + print( + "Failed to apply mutations outside of the authorized view: " + f"{e.message}" + ) + # [END bigtable_async_authorized_view_write_rows] + + +async def read_single_row_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id +): + """Reads a single row from an authorized view.""" + # [START bigtable_async_authorized_view_get_by_key] + async with BigtableDataClientAsync(project=project_id) as client: + async with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + try: + print("\nReading a single row by row key from an authorized view") + row = await authorized_view.read_row(f"{ROW_KEY_PREFIX}0") + print(f"Row: {row.row_key.decode()}") + for cell in row.cells: + print( + f"Family: {cell.family} " + f"Qualifier: {cell.qualifier.decode()} " + f"Value: {cell.value.decode()}" + ) + except NotFound as e: + print( + f"Failed to read from a non-existent authorized view: {e.message}" + ) + # [END bigtable_async_authorized_view_get_by_key] + + +async def read_rows_with_filter_from_authorized_view( + project_id, instance_id, table_id, authorized_view_id +): + """Reads rows from an authorized view with a filter.""" + # [START bigtable_async_authorized_view_scan_with_filter] + async with BigtableDataClientAsync(project=project_id) as client: + async with client.get_authorized_view( + instance_id, table_id, authorized_view_id + ) as authorized_view: + try: + # A filter that matches only the most recent cell within each column + row_filter = row_filters.CellsColumnLimitFilter(1) + print("\nScanning authorized view with filter") + query = ReadRowsQuery(row_filter=row_filter) + async for row in await authorized_view.read_rows_stream(query): + print(f"Row Key: {row.row_key.decode()}") + for cell in row.cells: + print( + f"Family: {cell.family} " + f"Qualifier: {cell.qualifier.decode()} " + f"Value: {cell.value.decode()}" + ) + except NotFound as e: + print(f"Failed to read a non-existent authorized view: {e.message}") + # [END bigtable_async_authorized_view_scan_with_filter] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument("project_id", help="Your Cloud Platform project ID.") + parser.add_argument( + "instance_id", help="ID of the Cloud Bigtable instance to connect to." + ) + parser.add_argument( + "--table", help="Table to create and destroy.", default="test-table" + ) + parser.add_argument( + "--authorized_view", + help="Authorized view to create and destroy.", + default="test-authorized-view", + ) + + args = parser.parse_args() + asyncio.run( + run(args.project_id, args.instance_id, args.table, args.authorized_view) + ) diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py new file mode 100644 index 000000000000..f1eb91473b58 --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py @@ -0,0 +1,58 @@ +# Copyright 2026 Google LLC +# +# 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. + +import os +import uuid + +import pytest +from google.api_core.exceptions import NotFound + +from google.cloud import bigtable_admin_v2 + +from . import authorized_views_async + +PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] +BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] +TABLE_ID = f"authorized-views-async-{str(uuid.uuid4())[:16]}" +AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" + + +@pytest.mark.asyncio +async def test_run(capsys): + try: + await authorized_views_async.run( + PROJECT, BIGTABLE_INSTANCE, TABLE_ID, AUTHORIZED_VIEW_ID + ) + finally: + # the sample deletes the table itself; this only matters if it failed midway + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + try: + admin_client.delete_table( + name=admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, TABLE_ID) + ) + except NotFound: + pass + + out, _ = capsys.readouterr() + assert f"Table does not exist, creating table: {TABLE_ID}" in out + assert f"authorizedViews/{AUTHORIZED_VIEW_ID} created successfully" in out + assert f"authorizedViews/{AUTHORIZED_VIEW_ID} updated successfully" in out + assert "Column Family: cf, Qualifier Prefix: " in out + assert f"\n{AUTHORIZED_VIEW_ID}\n" in out + assert "Hello World!" in out + assert "Row: rowKey0" in out + assert "Family: cf Qualifier: greeting Value: Hello World!" in out + assert "Row Key: rowKey2" in out + assert f"AuthorizedView: {AUTHORIZED_VIEW_ID} deleted successfully" in out + assert f"Table: {TABLE_ID} deleted successfully" in out diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt new file mode 100644 index 000000000000..74e55ca10648 --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt @@ -0,0 +1 @@ +google-cloud-bigtable>=2.35.0 diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py index 07f12e9d5ee0..15a4e562614d 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async.py @@ -369,76 +369,3 @@ def print_row(row): # [END bigtable_async_reads_print] - - -async def authorized_view_write_rows(table, authorized_view_id): - # [START bigtable_async_authorized_view_write_rows] - from google.cloud.bigtable.data import BigtableDataClientAsync, SetCell - - async def write_rows(project_id, instance_id, table_id, authorized_view_id): - async with BigtableDataClientAsync(project=project_id) as client: - async with client.get_authorized_view( - instance_id, table_id, authorized_view_id - ) as authorized_view: - family_id = "stats_summary" - for i, name in enumerate(["World", "Bigtable", "Python"]): - greeting = f"Hello {name}!" - row_key = f"greeting{i}" - await authorized_view.mutate_row( - row_key, - [ - SetCell(family_id, "name", name), - SetCell(family_id, "greeting", greeting), - ], - ) - print(greeting) - - # [END bigtable_async_authorized_view_write_rows] - await write_rows( - table.client.project, table.instance_id, table.table_id, authorized_view_id - ) - - -async def authorized_view_get_by_key(table, authorized_view_id): - # [START bigtable_async_authorized_view_get_by_key] - from google.cloud.bigtable.data import BigtableDataClientAsync - - async def get_by_key(project_id, instance_id, table_id, authorized_view_id): - async with BigtableDataClientAsync(project=project_id) as client: - async with client.get_authorized_view( - instance_id, table_id, authorized_view_id - ) as authorized_view: - row_key = "greeting0" - row = await authorized_view.read_row(row_key) - print(row) - - # [END bigtable_async_authorized_view_get_by_key] - await get_by_key( - table.client.project, table.instance_id, table.table_id, authorized_view_id - ) - - -async def authorized_view_scan_with_filter(table, authorized_view_id): - # [START bigtable_async_authorized_view_scan_with_filter] - from google.cloud.bigtable.data import ( - BigtableDataClientAsync, - ReadRowsQuery, - row_filters, - ) - - async def scan_with_filter(project_id, instance_id, table_id, authorized_view_id): - async with BigtableDataClientAsync(project=project_id) as client: - async with client.get_authorized_view( - instance_id, table_id, authorized_view_id - ) as authorized_view: - # Only return the most recent version of each cell - row_filter = row_filters.CellsColumnLimitFilter(1) - query = ReadRowsQuery(row_filter=row_filter) - - async for row in await authorized_view.read_rows_stream(query): - print(row) - - # [END bigtable_async_authorized_view_scan_with_filter] - await scan_with_filter( - table.client.project, table.instance_id, table.table_id, authorized_view_id - ) diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py index e1c1ec543329..d7e4d46b2402 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/data_client_snippets_async_test.py @@ -22,7 +22,6 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] TABLE_ID = f"data-client-{str(uuid.uuid4())[:16]}" -AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" @pytest.fixture(scope="session") @@ -49,30 +48,6 @@ def table_id(column_family_config): yield TABLE_ID -@pytest.fixture(scope="session") -def authorized_view_id(table_id): - from google.cloud import bigtable_admin_v2 - - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) - # A view over every row, restricted to the stats_summary column family - subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], - family_subsets={ - "stats_summary": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b""] - ) - }, - ) - admin_client.create_authorized_view( - parent=table_path, - authorized_view_id=AUTHORIZED_VIEW_ID, - authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), - ).result() - # the view is deleted along with the table - yield AUTHORIZED_VIEW_ID - - @pytest_asyncio.fixture async def table(table_id): from google.cloud.bigtable.data import BigtableDataClientAsync @@ -145,24 +120,3 @@ async def test_read_with_filter(table): @pytest.mark.asyncio async def test_execute_query(table): await data_snippets.execute_query(table) - - -@pytest.mark.asyncio -async def test_authorized_view_write_rows(capsys, table, authorized_view_id): - await data_snippets.authorized_view_write_rows(table, authorized_view_id) - out, _ = capsys.readouterr() - assert "Hello World!" in out - - -@pytest.mark.asyncio -async def test_authorized_view_get_by_key(capsys, table, authorized_view_id): - await data_snippets.authorized_view_get_by_key(table, authorized_view_id) - out, _ = capsys.readouterr() - assert "Hello World!" in out - - -@pytest.mark.asyncio -async def test_authorized_view_scan_with_filter(capsys, table, authorized_view_id): - await data_snippets.authorized_view_scan_with_filter(table, authorized_view_id) - out, _ = capsys.readouterr() - assert "Hello Python!" in out From eb5e8dcbc076ef67d585381e46e16b5aae04046d Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Nov 2026 16:17:10 -0700 Subject: [PATCH 5/6] restructured authorized view samples --- .../data_client/authorized_views/__init__.py | 0 .../authorized_views/authorized_views_test.py | 54 ---- .../authorized_views/requirements.txt | 1 - .../authorized_views_snippets.py} | 226 ++++++----------- .../authorized_views_snippets_test.py | 168 +++++++++++++ .../authorized_views/__init__.py | 0 .../authorized_views_async_test.py | 58 ----- .../authorized_views/requirements.txt | 1 - .../authorized_views_snippets_async.py} | 235 ++++++------------ .../authorized_views_snippets_async_test.py | 190 ++++++++++++++ 10 files changed, 520 insertions(+), 413 deletions(-) delete mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/__init__.py delete mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py delete mode 100644 packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt rename packages/google-cloud-bigtable/samples/data_client/{authorized_views/authorized_views.py => snippets/authorized_views_snippets.py} (52%) create mode 100644 packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py delete mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/__init__.py delete mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py delete mode 100644 packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt rename packages/google-cloud-bigtable/samples/data_client_async/{authorized_views/authorized_views_async.py => snippets/authorized_views_snippets_async.py} (52%) create mode 100644 packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/__init__.py b/packages/google-cloud-bigtable/samples/data_client/authorized_views/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py b/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py deleted file mode 100644 index 8eb69f61e9ef..000000000000 --- a/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views_test.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2026 Google LLC -# -# 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. - -import os -import uuid - -from google.api_core.exceptions import NotFound - -from google.cloud import bigtable_admin_v2 - -from . import authorized_views - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID = f"authorized-views-{str(uuid.uuid4())[:16]}" -AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" - - -def test_run(capsys): - try: - authorized_views.run(PROJECT, BIGTABLE_INSTANCE, TABLE_ID, AUTHORIZED_VIEW_ID) - finally: - # the sample deletes the table itself; this only matters if it failed midway - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - try: - admin_client.delete_table( - name=admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, TABLE_ID) - ) - except NotFound: - pass - - out, _ = capsys.readouterr() - assert f"Table does not exist, creating table: {TABLE_ID}" in out - assert f"authorizedViews/{AUTHORIZED_VIEW_ID} created successfully" in out - assert f"authorizedViews/{AUTHORIZED_VIEW_ID} updated successfully" in out - assert "Column Family: cf, Qualifier Prefix: " in out - assert f"\n{AUTHORIZED_VIEW_ID}\n" in out - assert "Hello World!" in out - assert "Row: rowKey0" in out - assert "Family: cf Qualifier: greeting Value: Hello World!" in out - assert "Row Key: rowKey2" in out - assert f"AuthorizedView: {AUTHORIZED_VIEW_ID} deleted successfully" in out - assert f"Table: {TABLE_ID} deleted successfully" in out diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt b/packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt deleted file mode 100644 index 74e55ca10648..000000000000 --- a/packages/google-cloud-bigtable/samples/data_client/authorized_views/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-bigtable>=2.35.0 diff --git a/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py similarity index 52% rename from packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py rename to packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py index 8b2935879e22..5649c4ab2514 100644 --- a/packages/google-cloud-bigtable/samples/data_client/authorized_views/authorized_views.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py @@ -14,89 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Demonstrates how to create, manage, and use a Bigtable authorized view. - -Prerequisites: - -- Create a Cloud Bigtable instance. - https://cloud.google.com/bigtable/docs/creating-instance -- Set your Google Application Default Credentials. - https://developers.google.com/identity/protocols/application-default-credentials -""" - -import argparse - -from google.api_core.exceptions import NotFound, PermissionDenied -from google.protobuf import field_mask_pb2 - -from google.cloud import bigtable_admin_v2 -from google.cloud.bigtable.data import ( - BigtableDataClient, - ReadRowsQuery, - SetCell, - row_filters, -) - -COLUMN_FAMILY = "cf" -COLUMN_QUALIFIER_GREETING = "greeting" -COLUMN_QUALIFIER_NAME = "name" -ROW_KEY_PREFIX = "rowKey" - - -def run(project_id, instance_id, table_id, authorized_view_id): - create_table(project_id, instance_id, table_id) - create_authorized_view(project_id, instance_id, table_id, authorized_view_id) - update_authorized_view(project_id, instance_id, table_id, authorized_view_id) - get_authorized_view(project_id, instance_id, table_id, authorized_view_id) - list_authorized_views(project_id, instance_id, table_id) - write_to_authorized_view(project_id, instance_id, table_id, authorized_view_id) - read_single_row_from_authorized_view( - project_id, instance_id, table_id, authorized_view_id - ) - read_rows_with_filter_from_authorized_view( - project_id, instance_id, table_id, authorized_view_id - ) - delete_authorized_view(project_id, instance_id, table_id, authorized_view_id) - delete_table(project_id, instance_id, table_id) - - -def create_table(project_id, instance_id, table_id): - # Checks if the table exists, creates it if it does not exist. - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - table_path = admin_client.table_path(project_id, instance_id, table_id) - try: - admin_client.get_table( - request={"name": table_path, "view": bigtable_admin_v2.Table.View.NAME_ONLY} - ) - except NotFound: - print(f"Table does not exist, creating table: {table_id}") - table = admin_client.create_table( - parent=admin_client.instance_path(project_id, instance_id), - table_id=table_id, - table=bigtable_admin_v2.Table( - column_families={COLUMN_FAMILY: bigtable_admin_v2.ColumnFamily()} - ), - ) - print(f"Table: {table.name} created successfully") - - -def delete_table(project_id, instance_id, table_id): - # Deletes the entire table. - print(f"\nDelete table: {table_id}") - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - try: - admin_client.delete_table( - name=admin_client.table_path(project_id, instance_id, table_id) - ) - print(f"Table: {table_id} deleted successfully") - except NotFound as e: - print(f"Failed to delete a non-existent table: {e.message}") - +# [START bigtable_create_authorized_view] def create_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Creates an authorized view under a table with the specified configuration.""" - print(f"\nCreating authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_create_authorized_view] + from google.cloud import bigtable_admin_v2 + admin_client = bigtable_admin_v2.BigtableTableAdminClient() table_path = admin_client.table_path(project_id, instance_id, table_id) @@ -104,8 +26,8 @@ def create_authorized_view(project_id, instance_id, table_id, authorized_view_id subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( row_prefixes=[b""], family_subsets={ - COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[COLUMN_QUALIFIER_NAME.encode()] + "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b"name"] ) }, ) @@ -116,23 +38,27 @@ def create_authorized_view(project_id, instance_id, table_id, authorized_view_id ) authorized_view = operation.result() print(f"AuthorizedView: {authorized_view.name} created successfully") - # [END bigtable_create_authorized_view] +# [END bigtable_create_authorized_view] + + +# [START bigtable_update_authorized_view] def update_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Modifies an authorized view.""" - print(f"\nUpdating authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_update_authorized_view] + from google.protobuf import field_mask_pb2 + + from google.cloud import bigtable_admin_v2 + admin_client = bigtable_admin_v2.BigtableTableAdminClient() authorized_view_path = admin_client.authorized_view_path( project_id, instance_id, table_id, authorized_view_id ) - # Update to an authorized view permitting everything. + # Update to an authorized view permitting everything in the column family. subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( row_prefixes=[b""], family_subsets={ - COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( + "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( qualifier_prefixes=[b""] ) }, @@ -145,13 +71,17 @@ def update_authorized_view(project_id, instance_id, table_id, authorized_view_id ) authorized_view = operation.result() print(f"AuthorizedView: {authorized_view.name} updated successfully") - # [END bigtable_update_authorized_view] +# [END bigtable_update_authorized_view] + + +# [START bigtable_get_authorized_view] def get_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Gets an authorized view's metadata.""" - print(f"\nGetting authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_get_authorized_view] + from google.api_core.exceptions import NotFound + + from google.cloud import bigtable_admin_v2 + admin_client = bigtable_admin_v2.BigtableTableAdminClient() authorized_view_path = admin_client.authorized_view_path( project_id, instance_id, table_id, authorized_view_id @@ -175,13 +105,17 @@ def get_authorized_view(project_id, instance_id, table_id, authorized_view_id): "Failed to retrieve metadata from a non-existent authorized view: " f"{e.message}" ) - # [END bigtable_get_authorized_view] +# [END bigtable_get_authorized_view] + + +# [START bigtable_list_authorized_views] def list_authorized_views(project_id, instance_id, table_id): - """Lists all authorized views within a table.""" - print(f"\nListing authorized views in table {table_id}") - # [START bigtable_list_authorized_views] + from google.api_core.exceptions import NotFound + + from google.cloud import bigtable_admin_v2 + admin_client = bigtable_admin_v2.BigtableTableAdminClient() table_path = admin_client.table_path(project_id, instance_id, table_id) @@ -193,13 +127,17 @@ def list_authorized_views(project_id, instance_id, table_id): print(authorized_view_id) except NotFound as e: print(f"Failed to list authorized views from a non-existent table: {e.message}") - # [END bigtable_list_authorized_views] +# [END bigtable_list_authorized_views] + + +# [START bigtable_delete_authorized_view] def delete_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Deletes an authorized view.""" - print(f"\nDeleting authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_delete_authorized_view] + from google.api_core.exceptions import NotFound + + from google.cloud import bigtable_admin_v2 + admin_client = bigtable_admin_v2.BigtableTableAdminClient() authorized_view_path = admin_client.authorized_view_path( project_id, instance_id, table_id, authorized_view_id @@ -210,26 +148,30 @@ def delete_authorized_view(project_id, instance_id, table_id, authorized_view_id print(f"AuthorizedView: {authorized_view_id} deleted successfully") except NotFound as e: print(f"Failed to delete a non-existent authorized view: {e.message}") - # [END bigtable_delete_authorized_view] +# [END bigtable_delete_authorized_view] + + +# [START bigtable_authorized_view_write_rows] def write_to_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Writes some rows to an authorized view.""" - # [START bigtable_authorized_view_write_rows] + from google.api_core.exceptions import NotFound, PermissionDenied + + from google.cloud.bigtable.data import BigtableDataClient, SetCell + with BigtableDataClient(project=project_id) as client: with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: try: - print("\nWriting to authorized view") names = ["World", "Bigtable", "Python"] for i, name in enumerate(names): greeting = f"Hello {name}!" authorized_view.mutate_row( - f"{ROW_KEY_PREFIX}{i}", + f"rowKey{i}", [ - SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, name), - SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting), + SetCell("cf", "name", name), + SetCell("cf", "greeting", greeting), ], ) print(greeting) @@ -240,48 +182,61 @@ def write_to_authorized_view(project_id, instance_id, table_id, authorized_view_ "Failed to apply mutations outside of the authorized view: " f"{e.message}" ) - # [END bigtable_authorized_view_write_rows] +# [END bigtable_authorized_view_write_rows] + + +# [START bigtable_authorized_view_get_by_key] def read_single_row_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - """Reads a single row from an authorized view.""" - # [START bigtable_authorized_view_get_by_key] + from google.api_core.exceptions import NotFound + + from google.cloud.bigtable.data import BigtableDataClient + with BigtableDataClient(project=project_id) as client: with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: try: - print("\nReading a single row by row key from an authorized view") - row = authorized_view.read_row(f"{ROW_KEY_PREFIX}0") - print(f"Row: {row.row_key.decode()}") - for cell in row.cells: - print( - f"Family: {cell.family} " - f"Qualifier: {cell.qualifier.decode()} " - f"Value: {cell.value.decode()}" - ) + row = authorized_view.read_row("rowKey0") + if row: + print(f"Row: {row.row_key.decode()}") + for cell in row.cells: + print( + f"Family: {cell.family} " + f"Qualifier: {cell.qualifier.decode()} " + f"Value: {cell.value.decode()}" + ) except NotFound as e: print( f"Failed to read from a non-existent authorized view: {e.message}" ) - # [END bigtable_authorized_view_get_by_key] +# [END bigtable_authorized_view_get_by_key] + + +# [START bigtable_authorized_view_scan_with_filter] def read_rows_with_filter_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - """Reads rows from an authorized view with a filter.""" - # [START bigtable_authorized_view_scan_with_filter] + from google.api_core.exceptions import NotFound + + from google.cloud.bigtable.data import ( + BigtableDataClient, + ReadRowsQuery, + row_filters, + ) + with BigtableDataClient(project=project_id) as client: with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: try: - # A filter that matches only the most recent cell within each column + # A filter that matches only the most recent cell within each column. row_filter = row_filters.CellsColumnLimitFilter(1) - print("\nScanning authorized view with filter") query = ReadRowsQuery(row_filter=row_filter) for row in authorized_view.read_rows_stream(query): print(f"Row Key: {row.row_key.decode()}") @@ -293,25 +248,6 @@ def read_rows_with_filter_from_authorized_view( ) except NotFound as e: print(f"Failed to read a non-existent authorized view: {e.message}") - # [END bigtable_authorized_view_scan_with_filter] -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter - ) - parser.add_argument("project_id", help="Your Cloud Platform project ID.") - parser.add_argument( - "instance_id", help="ID of the Cloud Bigtable instance to connect to." - ) - parser.add_argument( - "--table", help="Table to create and destroy.", default="test-table" - ) - parser.add_argument( - "--authorized_view", - help="Authorized view to create and destroy.", - default="test-authorized-view", - ) - - args = parser.parse_args() - run(args.project_id, args.instance_id, args.table, args.authorized_view) +# [END bigtable_authorized_view_scan_with_filter] diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py new file mode 100644 index 000000000000..4fe34dcebd0e --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py @@ -0,0 +1,168 @@ +# Copyright 2026 Google LLC +# +# 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. + +import os +import uuid +from typing import Generator + +import pytest +from google.api_core.exceptions import NotFound + +from google.cloud import bigtable_admin_v2 +from google.cloud.bigtable.data import BigtableDataClient, SetCell + +from ...utils import create_table_cm +from . import authorized_views_snippets + +PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] +BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] +TABLE_ID = f"authorized-views-{str(uuid.uuid4())[:16]}" +AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" + + +@pytest.fixture(scope="module") +def table_id() -> Generator[str, None, None]: + with create_table_cm( + PROJECT, + BIGTABLE_INSTANCE, + TABLE_ID, + {"cf": None}, + verbose=False, + ): + yield TABLE_ID + + +@pytest.fixture(scope="module") +def authorized_view_id(table_id: str) -> Generator[str, None, None]: + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""] + ) + }, + ) + admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=AUTHORIZED_VIEW_ID, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ).result() + + with BigtableDataClient(project=PROJECT) as client: + with client.get_table(BIGTABLE_INSTANCE, table_id) as table: + for i, name in enumerate(["World", "Bigtable", "Python"]): + table.mutate_row( + f"rowKey{i}", + [ + SetCell("cf", "name", name), + SetCell("cf", "greeting", f"Hello {name}!"), + ], + ) + + yield AUTHORIZED_VIEW_ID + + +def test_create_authorized_view(capsys, table_id): + view_id = f"view-create-{str(uuid.uuid4())[:12]}" + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + try: + authorized_views_snippets.create_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"authorizedViews/{view_id} created successfully" in out + finally: + try: + admin_client.delete_authorized_view( + name=admin_client.authorized_view_path( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + ) + except NotFound: + pass + + +def test_update_authorized_view(capsys, table_id, authorized_view_id): + authorized_views_snippets.update_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert f"authorizedViews/{authorized_view_id} updated successfully" in out + + +def test_get_authorized_view(capsys, table_id, authorized_view_id): + authorized_views_snippets.get_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Row Prefix: " in out + assert "Column Family: cf, Qualifier Prefix: " in out + + +def test_list_authorized_views(capsys, table_id, authorized_view_id): + authorized_views_snippets.list_authorized_views( + PROJECT, BIGTABLE_INSTANCE, table_id + ) + out, _ = capsys.readouterr() + assert authorized_view_id in out.splitlines() + + +def test_delete_authorized_view(capsys, table_id): + view_id = f"view-delete-{str(uuid.uuid4())[:12]}" + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView(row_prefixes=[b""]) + admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ).result() + + authorized_views_snippets.delete_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"AuthorizedView: {view_id} deleted successfully" in out + + +def test_write_to_authorized_view(capsys, table_id, authorized_view_id): + authorized_views_snippets.write_to_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Hello World!" in out + assert "Hello Bigtable!" in out + assert "Hello Python!" in out + + +def test_read_single_row_from_authorized_view(capsys, table_id, authorized_view_id): + authorized_views_snippets.read_single_row_from_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Row: rowKey0" in out + assert "Family: cf Qualifier: greeting Value: Hello World!" in out + + +def test_read_rows_with_filter_from_authorized_view( + capsys, table_id, authorized_view_id +): + authorized_views_snippets.read_rows_with_filter_from_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Row Key: rowKey0" in out + assert "Row Key: rowKey2" in out diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/__init__.py b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py deleted file mode 100644 index f1eb91473b58..000000000000 --- a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async_test.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright 2026 Google LLC -# -# 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. - -import os -import uuid - -import pytest -from google.api_core.exceptions import NotFound - -from google.cloud import bigtable_admin_v2 - -from . import authorized_views_async - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID = f"authorized-views-async-{str(uuid.uuid4())[:16]}" -AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" - - -@pytest.mark.asyncio -async def test_run(capsys): - try: - await authorized_views_async.run( - PROJECT, BIGTABLE_INSTANCE, TABLE_ID, AUTHORIZED_VIEW_ID - ) - finally: - # the sample deletes the table itself; this only matters if it failed midway - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - try: - admin_client.delete_table( - name=admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, TABLE_ID) - ) - except NotFound: - pass - - out, _ = capsys.readouterr() - assert f"Table does not exist, creating table: {TABLE_ID}" in out - assert f"authorizedViews/{AUTHORIZED_VIEW_ID} created successfully" in out - assert f"authorizedViews/{AUTHORIZED_VIEW_ID} updated successfully" in out - assert "Column Family: cf, Qualifier Prefix: " in out - assert f"\n{AUTHORIZED_VIEW_ID}\n" in out - assert "Hello World!" in out - assert "Row: rowKey0" in out - assert "Family: cf Qualifier: greeting Value: Hello World!" in out - assert "Row Key: rowKey2" in out - assert f"AuthorizedView: {AUTHORIZED_VIEW_ID} deleted successfully" in out - assert f"Table: {TABLE_ID} deleted successfully" in out diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt b/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt deleted file mode 100644 index 74e55ca10648..000000000000 --- a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-bigtable>=2.35.0 diff --git a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py similarity index 52% rename from packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py rename to packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py index 5729b6cb8791..f679c171a6aa 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/authorized_views/authorized_views_async.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py @@ -14,96 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Demonstrates how to create, manage, and use a Bigtable authorized view -with the asyncio clients. - -Prerequisites: - -- Create a Cloud Bigtable instance. - https://cloud.google.com/bigtable/docs/creating-instance -- Set your Google Application Default Credentials. - https://developers.google.com/identity/protocols/application-default-credentials -""" - -import argparse -import asyncio - -from google.api_core.exceptions import NotFound, PermissionDenied -from google.protobuf import field_mask_pb2 - -from google.cloud import bigtable_admin_v2 -from google.cloud.bigtable.data import ( - BigtableDataClientAsync, - ReadRowsQuery, - SetCell, - row_filters, -) - -COLUMN_FAMILY = "cf" -COLUMN_QUALIFIER_GREETING = "greeting" -COLUMN_QUALIFIER_NAME = "name" -ROW_KEY_PREFIX = "rowKey" - - -async def run(project_id, instance_id, table_id, authorized_view_id): - await create_table(project_id, instance_id, table_id) - await create_authorized_view(project_id, instance_id, table_id, authorized_view_id) - await update_authorized_view(project_id, instance_id, table_id, authorized_view_id) - await get_authorized_view(project_id, instance_id, table_id, authorized_view_id) - await list_authorized_views(project_id, instance_id, table_id) - await write_to_authorized_view( - project_id, instance_id, table_id, authorized_view_id - ) - await read_single_row_from_authorized_view( - project_id, instance_id, table_id, authorized_view_id - ) - await read_rows_with_filter_from_authorized_view( - project_id, instance_id, table_id, authorized_view_id - ) - await delete_authorized_view(project_id, instance_id, table_id, authorized_view_id) - await delete_table(project_id, instance_id, table_id) - - -async def create_table(project_id, instance_id, table_id): - # Checks if the table exists, creates it if it does not exist. - async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: - table_path = admin_client.table_path(project_id, instance_id, table_id) - try: - await admin_client.get_table( - request={ - "name": table_path, - "view": bigtable_admin_v2.Table.View.NAME_ONLY, - } - ) - except NotFound: - print(f"Table does not exist, creating table: {table_id}") - table = await admin_client.create_table( - parent=admin_client.instance_path(project_id, instance_id), - table_id=table_id, - table=bigtable_admin_v2.Table( - column_families={COLUMN_FAMILY: bigtable_admin_v2.ColumnFamily()} - ), - ) - print(f"Table: {table.name} created successfully") - - -async def delete_table(project_id, instance_id, table_id): - # Deletes the entire table. - print(f"\nDelete table: {table_id}") - async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: - try: - await admin_client.delete_table( - name=admin_client.table_path(project_id, instance_id, table_id) - ) - print(f"Table: {table_id} deleted successfully") - except NotFound as e: - print(f"Failed to delete a non-existent table: {e.message}") - +# [START bigtable_async_create_authorized_view] async def create_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Creates an authorized view under a table with the specified configuration.""" - print(f"\nCreating authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_async_create_authorized_view] + from google.cloud import bigtable_admin_v2 + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: table_path = admin_client.table_path(project_id, instance_id, table_id) @@ -111,8 +26,8 @@ async def create_authorized_view(project_id, instance_id, table_id, authorized_v subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( row_prefixes=[b""], family_subsets={ - COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[COLUMN_QUALIFIER_NAME.encode()] + "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b"name"] ) }, ) @@ -123,23 +38,27 @@ async def create_authorized_view(project_id, instance_id, table_id, authorized_v ) authorized_view = await operation.result() print(f"AuthorizedView: {authorized_view.name} created successfully") - # [END bigtable_async_create_authorized_view] +# [END bigtable_async_create_authorized_view] + + +# [START bigtable_async_update_authorized_view] async def update_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Modifies an authorized view.""" - print(f"\nUpdating authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_async_update_authorized_view] + from google.protobuf import field_mask_pb2 + + from google.cloud import bigtable_admin_v2 + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: authorized_view_path = admin_client.authorized_view_path( project_id, instance_id, table_id, authorized_view_id ) - # Update to an authorized view permitting everything. + # Update to an authorized view permitting everything in the column family. subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( row_prefixes=[b""], family_subsets={ - COLUMN_FAMILY: bigtable_admin_v2.AuthorizedView.FamilySubsets( + "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( qualifier_prefixes=[b""] ) }, @@ -152,13 +71,17 @@ async def update_authorized_view(project_id, instance_id, table_id, authorized_v ) authorized_view = await operation.result() print(f"AuthorizedView: {authorized_view.name} updated successfully") - # [END bigtable_async_update_authorized_view] +# [END bigtable_async_update_authorized_view] + + +# [START bigtable_async_get_authorized_view] async def get_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Gets an authorized view's metadata.""" - print(f"\nGetting authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_async_get_authorized_view] + from google.api_core.exceptions import NotFound + + from google.cloud import bigtable_admin_v2 + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: authorized_view_path = admin_client.authorized_view_path( project_id, instance_id, table_id, authorized_view_id @@ -184,13 +107,17 @@ async def get_authorized_view(project_id, instance_id, table_id, authorized_view "Failed to retrieve metadata from a non-existent authorized view: " f"{e.message}" ) - # [END bigtable_async_get_authorized_view] +# [END bigtable_async_get_authorized_view] + + +# [START bigtable_async_list_authorized_views] async def list_authorized_views(project_id, instance_id, table_id): - """Lists all authorized views within a table.""" - print(f"\nListing authorized views in table {table_id}") - # [START bigtable_async_list_authorized_views] + from google.api_core.exceptions import NotFound + + from google.cloud import bigtable_admin_v2 + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: table_path = admin_client.table_path(project_id, instance_id, table_id) @@ -207,13 +134,17 @@ async def list_authorized_views(project_id, instance_id, table_id): "Failed to list authorized views from a non-existent table: " f"{e.message}" ) - # [END bigtable_async_list_authorized_views] +# [END bigtable_async_list_authorized_views] + + +# [START bigtable_async_delete_authorized_view] async def delete_authorized_view(project_id, instance_id, table_id, authorized_view_id): - """Deletes an authorized view.""" - print(f"\nDeleting authorized view {authorized_view_id} in table {table_id}") - # [START bigtable_async_delete_authorized_view] + from google.api_core.exceptions import NotFound + + from google.cloud import bigtable_admin_v2 + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: authorized_view_path = admin_client.authorized_view_path( project_id, instance_id, table_id, authorized_view_id @@ -224,28 +155,32 @@ async def delete_authorized_view(project_id, instance_id, table_id, authorized_v print(f"AuthorizedView: {authorized_view_id} deleted successfully") except NotFound as e: print(f"Failed to delete a non-existent authorized view: {e.message}") - # [END bigtable_async_delete_authorized_view] +# [END bigtable_async_delete_authorized_view] + + +# [START bigtable_async_authorized_view_write_rows] async def write_to_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - """Writes some rows to an authorized view.""" - # [START bigtable_async_authorized_view_write_rows] + from google.api_core.exceptions import NotFound, PermissionDenied + + from google.cloud.bigtable.data import BigtableDataClientAsync, SetCell + async with BigtableDataClientAsync(project=project_id) as client: async with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: try: - print("\nWriting to authorized view") names = ["World", "Bigtable", "Python"] for i, name in enumerate(names): greeting = f"Hello {name}!" await authorized_view.mutate_row( - f"{ROW_KEY_PREFIX}{i}", + f"rowKey{i}", [ - SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, name), - SetCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting), + SetCell("cf", "name", name), + SetCell("cf", "greeting", greeting), ], ) print(greeting) @@ -256,48 +191,61 @@ async def write_to_authorized_view( "Failed to apply mutations outside of the authorized view: " f"{e.message}" ) - # [END bigtable_async_authorized_view_write_rows] +# [END bigtable_async_authorized_view_write_rows] + + +# [START bigtable_async_authorized_view_get_by_key] async def read_single_row_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - """Reads a single row from an authorized view.""" - # [START bigtable_async_authorized_view_get_by_key] + from google.api_core.exceptions import NotFound + + from google.cloud.bigtable.data import BigtableDataClientAsync + async with BigtableDataClientAsync(project=project_id) as client: async with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: try: - print("\nReading a single row by row key from an authorized view") - row = await authorized_view.read_row(f"{ROW_KEY_PREFIX}0") - print(f"Row: {row.row_key.decode()}") - for cell in row.cells: - print( - f"Family: {cell.family} " - f"Qualifier: {cell.qualifier.decode()} " - f"Value: {cell.value.decode()}" - ) + row = await authorized_view.read_row("rowKey0") + if row: + print(f"Row: {row.row_key.decode()}") + for cell in row.cells: + print( + f"Family: {cell.family} " + f"Qualifier: {cell.qualifier.decode()} " + f"Value: {cell.value.decode()}" + ) except NotFound as e: print( f"Failed to read from a non-existent authorized view: {e.message}" ) - # [END bigtable_async_authorized_view_get_by_key] +# [END bigtable_async_authorized_view_get_by_key] + + +# [START bigtable_async_authorized_view_scan_with_filter] async def read_rows_with_filter_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - """Reads rows from an authorized view with a filter.""" - # [START bigtable_async_authorized_view_scan_with_filter] + from google.api_core.exceptions import NotFound + + from google.cloud.bigtable.data import ( + BigtableDataClientAsync, + ReadRowsQuery, + row_filters, + ) + async with BigtableDataClientAsync(project=project_id) as client: async with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: try: - # A filter that matches only the most recent cell within each column + # A filter that matches only the most recent cell within each column. row_filter = row_filters.CellsColumnLimitFilter(1) - print("\nScanning authorized view with filter") query = ReadRowsQuery(row_filter=row_filter) async for row in await authorized_view.read_rows_stream(query): print(f"Row Key: {row.row_key.decode()}") @@ -309,27 +257,6 @@ async def read_rows_with_filter_from_authorized_view( ) except NotFound as e: print(f"Failed to read a non-existent authorized view: {e.message}") - # [END bigtable_async_authorized_view_scan_with_filter] -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter - ) - parser.add_argument("project_id", help="Your Cloud Platform project ID.") - parser.add_argument( - "instance_id", help="ID of the Cloud Bigtable instance to connect to." - ) - parser.add_argument( - "--table", help="Table to create and destroy.", default="test-table" - ) - parser.add_argument( - "--authorized_view", - help="Authorized view to create and destroy.", - default="test-authorized-view", - ) - - args = parser.parse_args() - asyncio.run( - run(args.project_id, args.instance_id, args.table, args.authorized_view) - ) +# [END bigtable_async_authorized_view_scan_with_filter] diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py new file mode 100644 index 000000000000..23eee229439e --- /dev/null +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py @@ -0,0 +1,190 @@ +# Copyright 2026 Google LLC +# +# 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. + +import os +import uuid +from typing import AsyncGenerator + +import pytest +import pytest_asyncio +from google.api_core.exceptions import NotFound + +from google.cloud import bigtable_admin_v2 +from google.cloud.bigtable.data import BigtableDataClientAsync, SetCell + +from ...utils import create_table_cm +from . import authorized_views_snippets_async + +PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] +BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] +TABLE_ID = f"authorized-views-async-{str(uuid.uuid4())[:16]}" +AUTHORIZED_VIEW_ID = f"view-{str(uuid.uuid4())[:16]}" + + +@pytest.fixture(scope="module") +def event_loop(): + import asyncio + + loop = asyncio.get_event_loop_policy().new_event_loop() + yield loop + loop.close() + + +@pytest_asyncio.fixture(scope="module") +async def table_id() -> AsyncGenerator[str, None]: + with create_table_cm( + PROJECT, + BIGTABLE_INSTANCE, + TABLE_ID, + {"cf": None}, + verbose=False, + ): + yield TABLE_ID + + +@pytest_asyncio.fixture(scope="module") +async def authorized_view_id(table_id: str) -> AsyncGenerator[str, None]: + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b""], + family_subsets={ + "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""] + ) + }, + ) + operation = await admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=AUTHORIZED_VIEW_ID, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ) + await operation.result() + + async with BigtableDataClientAsync(project=PROJECT) as client: + async with client.get_table(BIGTABLE_INSTANCE, table_id) as table: + for i, name in enumerate(["World", "Bigtable", "Python"]): + await table.mutate_row( + f"rowKey{i}", + [ + SetCell("cf", "name", name), + SetCell("cf", "greeting", f"Hello {name}!"), + ], + ) + + yield AUTHORIZED_VIEW_ID + + +@pytest.mark.asyncio +async def test_create_authorized_view(capsys, table_id): + view_id = f"view-create-{str(uuid.uuid4())[:12]}" + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + try: + await authorized_views_snippets_async.create_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"authorizedViews/{view_id} created successfully" in out + finally: + try: + await admin_client.delete_authorized_view( + name=admin_client.authorized_view_path( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + ) + except NotFound: + pass + + +@pytest.mark.asyncio +async def test_update_authorized_view(capsys, table_id, authorized_view_id): + await authorized_views_snippets_async.update_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert f"authorizedViews/{authorized_view_id} updated successfully" in out + + +@pytest.mark.asyncio +async def test_get_authorized_view(capsys, table_id, authorized_view_id): + await authorized_views_snippets_async.get_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Row Prefix: " in out + assert "Column Family: cf, Qualifier Prefix: " in out + + +@pytest.mark.asyncio +async def test_list_authorized_views(capsys, table_id, authorized_view_id): + await authorized_views_snippets_async.list_authorized_views( + PROJECT, BIGTABLE_INSTANCE, table_id + ) + out, _ = capsys.readouterr() + assert authorized_view_id in out.splitlines() + + +@pytest.mark.asyncio +async def test_delete_authorized_view(capsys, table_id): + view_id = f"view-delete-{str(uuid.uuid4())[:12]}" + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView(row_prefixes=[b""]) + operation = await admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ) + await operation.result() + + await authorized_views_snippets_async.delete_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"AuthorizedView: {view_id} deleted successfully" in out + + +@pytest.mark.asyncio +async def test_write_to_authorized_view(capsys, table_id, authorized_view_id): + await authorized_views_snippets_async.write_to_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Hello World!" in out + assert "Hello Bigtable!" in out + assert "Hello Python!" in out + + +@pytest.mark.asyncio +async def test_read_single_row_from_authorized_view( + capsys, table_id, authorized_view_id +): + await authorized_views_snippets_async.read_single_row_from_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Row: rowKey0" in out + assert "Family: cf Qualifier: greeting Value: Hello World!" in out + + +@pytest.mark.asyncio +async def test_read_rows_with_filter_from_authorized_view( + capsys, table_id, authorized_view_id +): + await authorized_views_snippets_async.read_rows_with_filter_from_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id + ) + out, _ = capsys.readouterr() + assert "Row Key: rowKey0" in out + assert "Row Key: rowKey2" in out From 810632625e966b6e2df470fea05354f2c39cf06a Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 13 Nov 2026 20:08:27 -0700 Subject: [PATCH 6/6] added more context to samples --- .../snippets/authorized_views_snippets.py | 120 ++++++++++-------- .../authorized_views_snippets_test.py | 106 +++++++++------- .../authorized_views_snippets_async.py | 120 ++++++++++-------- .../authorized_views_snippets_async_test.py | 117 ++++++++++------- 4 files changed, 272 insertions(+), 191 deletions(-) diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py index 5649c4ab2514..895751735dc6 100644 --- a/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets.py @@ -22,14 +22,19 @@ def create_authorized_view(project_id, instance_id, table_id, authorized_view_id admin_client = bigtable_admin_v2.BigtableTableAdminClient() table_path = admin_client.table_path(project_id, instance_id, table_id) - # Permit every row, but only the "name" column of the column family. + column_family_id = "stats_summary" + # Include only rows with the "phone#" prefix. Use b"" to include all rows. + row_prefixes = [b"phone#"] + + # Include only "os_build" and columns starting with "connected_". + family_subset = bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifiers=[b"os_build"], + qualifier_prefixes=[b"connected_"], + ) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], - family_subsets={ - "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b"name"] - ) - }, + row_prefixes=row_prefixes, + family_subsets={column_family_id: family_subset}, ) operation = admin_client.create_authorized_view( parent=table_path, @@ -54,20 +59,25 @@ def update_authorized_view(project_id, instance_id, table_id, authorized_view_id project_id, instance_id, table_id, authorized_view_id ) - # Update to an authorized view permitting everything in the column family. + column_family_id = "stats_summary" + row_prefixes = [b"phone#", b"tablet#"] + # An empty prefix includes all columns in the family. + family_subset = bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""], + ) subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], - family_subsets={ - "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b""] - ) - }, + row_prefixes=row_prefixes, + family_subsets={column_family_id: family_subset}, ) + + # Only update the subset_view field. + update_mask = field_mask_pb2.FieldMask(paths=["subset_view"]) + operation = admin_client.update_authorized_view( authorized_view=bigtable_admin_v2.AuthorizedView( name=authorized_view_path, subset_view=subset_view ), - update_mask=field_mask_pb2.FieldMask(paths=["subset_view"]), + update_mask=update_mask, ) authorized_view = operation.result() print(f"AuthorizedView: {authorized_view.name} updated successfully") @@ -121,6 +131,7 @@ def list_authorized_views(project_id, instance_id, table_id): try: for authorized_view in admin_client.list_authorized_views(parent=table_path): + # Extract the authorized view ID from the full resource name. authorized_view_id = admin_client.parse_authorized_view_path( authorized_view.name )["authorized_view"] @@ -163,23 +174,22 @@ def write_to_authorized_view(project_id, instance_id, table_id, authorized_view_ with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: + column_family_id = "stats_summary" + # The row key and columns must be within the authorized view. + row_key = b"phone#4c410523#20190501" + mutations = [ + SetCell(column_family_id, b"connected_cell", 1), + SetCell(column_family_id, b"connected_wifi", 1), + SetCell(column_family_id, b"os_build", b"PQ2A.190405.003"), + ] try: - names = ["World", "Bigtable", "Python"] - for i, name in enumerate(names): - greeting = f"Hello {name}!" - authorized_view.mutate_row( - f"rowKey{i}", - [ - SetCell("cf", "name", name), - SetCell("cf", "greeting", greeting), - ], - ) - print(greeting) + authorized_view.mutate_row(row_key, mutations) + print(f"Successfully wrote row {row_key.decode()} to authorized view") except NotFound as e: - print(f"Failed to write to non-existent authorized view: {e.message}") + print(f"Authorized view does not exist: {e.message}") except PermissionDenied as e: print( - "Failed to apply mutations outside of the authorized view: " + "Mutation targets rows or columns outside the authorized view: " f"{e.message}" ) @@ -191,7 +201,7 @@ def write_to_authorized_view(project_id, instance_id, table_id, authorized_view_ def read_single_row_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - from google.api_core.exceptions import NotFound + from google.api_core.exceptions import NotFound, PermissionDenied from google.cloud.bigtable.data import BigtableDataClient @@ -199,19 +209,18 @@ def read_single_row_from_authorized_view( with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: + row_key = b"phone#4c410523#20190501" try: - row = authorized_view.read_row("rowKey0") - if row: - print(f"Row: {row.row_key.decode()}") - for cell in row.cells: - print( - f"Family: {cell.family} " - f"Qualifier: {cell.qualifier.decode()} " - f"Value: {cell.value.decode()}" - ) + row = authorized_view.read_row(row_key) + if row is not None: + print(f"Row key: {row.row_key.decode()}") + print(row) except NotFound as e: + print(f"Authorized view does not exist: {e.message}") + except PermissionDenied as e: print( - f"Failed to read from a non-existent authorized view: {e.message}" + "Row key is outside the authorized view or access was denied: " + f"{e.message}" ) @@ -222,11 +231,12 @@ def read_single_row_from_authorized_view( def read_rows_with_filter_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - from google.api_core.exceptions import NotFound + from google.api_core.exceptions import NotFound, PermissionDenied from google.cloud.bigtable.data import ( BigtableDataClient, ReadRowsQuery, + RowRange, row_filters, ) @@ -234,20 +244,28 @@ def read_rows_with_filter_from_authorized_view( with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: + # The row range must be within the authorized view. + row_range = RowRange( + start_key=b"phone#4c410523#20190501", + end_key=b"phone#4c410523#20190601", + ) + + # Return only the most recent cell version for each column. + max_cells_per_column = 1 + row_filter = row_filters.CellsColumnLimitFilter(max_cells_per_column) + query = ReadRowsQuery(row_ranges=[row_range], row_filter=row_filter) + try: - # A filter that matches only the most recent cell within each column. - row_filter = row_filters.CellsColumnLimitFilter(1) - query = ReadRowsQuery(row_filter=row_filter) for row in authorized_view.read_rows_stream(query): - print(f"Row Key: {row.row_key.decode()}") - for cell in row.cells: - print( - f"Family: {cell.family} " - f"Qualifier: {cell.qualifier.decode()} " - f"Value: {cell.value.decode()}" - ) + print(f"Row key: {row.row_key.decode()}") + print(row) except NotFound as e: - print(f"Failed to read a non-existent authorized view: {e.message}") + print(f"Authorized view does not exist: {e.message}") + except PermissionDenied as e: + print( + "Query targets rows or columns outside the authorized view: " + f"{e.message}" + ) # [END bigtable_authorized_view_scan_with_filter] diff --git a/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py index 4fe34dcebd0e..4a9656d1138a 100644 --- a/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py +++ b/packages/google-cloud-bigtable/samples/data_client/snippets/authorized_views_snippets_test.py @@ -37,7 +37,7 @@ def table_id() -> Generator[str, None, None]: PROJECT, BIGTABLE_INSTANCE, TABLE_ID, - {"cf": None}, + {"stats_summary": None}, verbose=False, ): yield TABLE_ID @@ -48,10 +48,11 @@ def authorized_view_id(table_id: str) -> Generator[str, None, None]: admin_client = bigtable_admin_v2.BigtableTableAdminClient() table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], + row_prefixes=[b"phone#"], family_subsets={ - "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b""] + "stats_summary": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifiers=[b"os_build"], + qualifier_prefixes=[b"connected_"], ) }, ) @@ -63,12 +64,13 @@ def authorized_view_id(table_id: str) -> Generator[str, None, None]: with BigtableDataClient(project=PROJECT) as client: with client.get_table(BIGTABLE_INSTANCE, table_id) as table: - for i, name in enumerate(["World", "Bigtable", "Python"]): + for row_key in [b"phone#4c410523#20190501", b"phone#4c410523#20190502"]: table.mutate_row( - f"rowKey{i}", + row_key, [ - SetCell("cf", "name", name), - SetCell("cf", "greeting", f"Hello {name}!"), + SetCell("stats_summary", b"connected_cell", 1), + SetCell("stats_summary", b"connected_wifi", 1), + SetCell("stats_summary", b"os_build", b"PQ2A.190405.003"), ], ) @@ -95,21 +97,14 @@ def test_create_authorized_view(capsys, table_id): pass -def test_update_authorized_view(capsys, table_id, authorized_view_id): - authorized_views_snippets.update_authorized_view( - PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id - ) - out, _ = capsys.readouterr() - assert f"authorizedViews/{authorized_view_id} updated successfully" in out - - def test_get_authorized_view(capsys, table_id, authorized_view_id): authorized_views_snippets.get_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Row Prefix: " in out - assert "Column Family: cf, Qualifier Prefix: " in out + assert "Row Prefix: phone#" in out + assert "Column Family: stats_summary, Qualifier Prefix: connected_" in out + assert "Column Family: stats_summary, Qualifier: os_build" in out def test_list_authorized_views(capsys, table_id, authorized_view_id): @@ -120,32 +115,12 @@ def test_list_authorized_views(capsys, table_id, authorized_view_id): assert authorized_view_id in out.splitlines() -def test_delete_authorized_view(capsys, table_id): - view_id = f"view-delete-{str(uuid.uuid4())[:12]}" - admin_client = bigtable_admin_v2.BigtableTableAdminClient() - table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) - subset_view = bigtable_admin_v2.AuthorizedView.SubsetView(row_prefixes=[b""]) - admin_client.create_authorized_view( - parent=table_path, - authorized_view_id=view_id, - authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), - ).result() - - authorized_views_snippets.delete_authorized_view( - PROJECT, BIGTABLE_INSTANCE, table_id, view_id - ) - out, _ = capsys.readouterr() - assert f"AuthorizedView: {view_id} deleted successfully" in out - - def test_write_to_authorized_view(capsys, table_id, authorized_view_id): authorized_views_snippets.write_to_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Hello World!" in out - assert "Hello Bigtable!" in out - assert "Hello Python!" in out + assert "Successfully wrote row phone#4c410523#20190501 to authorized view" in out def test_read_single_row_from_authorized_view(capsys, table_id, authorized_view_id): @@ -153,8 +128,8 @@ def test_read_single_row_from_authorized_view(capsys, table_id, authorized_view_ PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Row: rowKey0" in out - assert "Family: cf Qualifier: greeting Value: Hello World!" in out + assert "phone#4c410523#20190501" in out + assert "PQ2A.190405.003" in out def test_read_rows_with_filter_from_authorized_view( @@ -164,5 +139,50 @@ def test_read_rows_with_filter_from_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Row Key: rowKey0" in out - assert "Row Key: rowKey2" in out + assert "phone#4c410523#20190501" in out + assert "phone#4c410523#20190502" in out + + +def test_update_authorized_view(capsys, table_id): + view_id = f"view-update-{str(uuid.uuid4())[:12]}" + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView(row_prefixes=[b"phone#"]) + admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ).result() + try: + authorized_views_snippets.update_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"authorizedViews/{view_id} updated successfully" in out + finally: + try: + admin_client.delete_authorized_view( + name=admin_client.authorized_view_path( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + ) + except NotFound: + pass + + +def test_delete_authorized_view(capsys, table_id): + view_id = f"view-delete-{str(uuid.uuid4())[:12]}" + admin_client = bigtable_admin_v2.BigtableTableAdminClient() + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView(row_prefixes=[b"phone#"]) + admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ).result() + + authorized_views_snippets.delete_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"AuthorizedView: {view_id} deleted successfully" in out diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py index f679c171a6aa..c25b28741092 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async.py @@ -22,14 +22,19 @@ async def create_authorized_view(project_id, instance_id, table_id, authorized_v async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: table_path = admin_client.table_path(project_id, instance_id, table_id) - # Permit every row, but only the "name" column of the column family. + column_family_id = "stats_summary" + # Include only rows with the "phone#" prefix. Use b"" to include all rows. + row_prefixes = [b"phone#"] + + # Include only "os_build" and columns starting with "connected_". + family_subset = bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifiers=[b"os_build"], + qualifier_prefixes=[b"connected_"], + ) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], - family_subsets={ - "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b"name"] - ) - }, + row_prefixes=row_prefixes, + family_subsets={column_family_id: family_subset}, ) operation = await admin_client.create_authorized_view( parent=table_path, @@ -54,20 +59,25 @@ async def update_authorized_view(project_id, instance_id, table_id, authorized_v project_id, instance_id, table_id, authorized_view_id ) - # Update to an authorized view permitting everything in the column family. + column_family_id = "stats_summary" + row_prefixes = [b"phone#", b"tablet#"] + # An empty prefix includes all columns in the family. + family_subset = bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifier_prefixes=[b""], + ) subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], - family_subsets={ - "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b""] - ) - }, + row_prefixes=row_prefixes, + family_subsets={column_family_id: family_subset}, ) + + # Only update the subset_view field. + update_mask = field_mask_pb2.FieldMask(paths=["subset_view"]) + operation = await admin_client.update_authorized_view( authorized_view=bigtable_admin_v2.AuthorizedView( name=authorized_view_path, subset_view=subset_view ), - update_mask=field_mask_pb2.FieldMask(paths=["subset_view"]), + update_mask=update_mask, ) authorized_view = await operation.result() print(f"AuthorizedView: {authorized_view.name} updated successfully") @@ -125,6 +135,7 @@ async def list_authorized_views(project_id, instance_id, table_id): async for authorized_view in await admin_client.list_authorized_views( parent=table_path ): + # Extract the authorized view ID from the full resource name. authorized_view_id = admin_client.parse_authorized_view_path( authorized_view.name )["authorized_view"] @@ -172,23 +183,22 @@ async def write_to_authorized_view( async with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: + column_family_id = "stats_summary" + # The row key and columns must be within the authorized view. + row_key = b"phone#4c410523#20190501" + mutations = [ + SetCell(column_family_id, b"connected_cell", 1), + SetCell(column_family_id, b"connected_wifi", 1), + SetCell(column_family_id, b"os_build", b"PQ2A.190405.003"), + ] try: - names = ["World", "Bigtable", "Python"] - for i, name in enumerate(names): - greeting = f"Hello {name}!" - await authorized_view.mutate_row( - f"rowKey{i}", - [ - SetCell("cf", "name", name), - SetCell("cf", "greeting", greeting), - ], - ) - print(greeting) + await authorized_view.mutate_row(row_key, mutations) + print(f"Successfully wrote row {row_key.decode()} to authorized view") except NotFound as e: - print(f"Failed to write to non-existent authorized view: {e.message}") + print(f"Authorized view does not exist: {e.message}") except PermissionDenied as e: print( - "Failed to apply mutations outside of the authorized view: " + "Mutation targets rows or columns outside the authorized view: " f"{e.message}" ) @@ -200,7 +210,7 @@ async def write_to_authorized_view( async def read_single_row_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - from google.api_core.exceptions import NotFound + from google.api_core.exceptions import NotFound, PermissionDenied from google.cloud.bigtable.data import BigtableDataClientAsync @@ -208,19 +218,18 @@ async def read_single_row_from_authorized_view( async with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: + row_key = b"phone#4c410523#20190501" try: - row = await authorized_view.read_row("rowKey0") - if row: - print(f"Row: {row.row_key.decode()}") - for cell in row.cells: - print( - f"Family: {cell.family} " - f"Qualifier: {cell.qualifier.decode()} " - f"Value: {cell.value.decode()}" - ) + row = await authorized_view.read_row(row_key) + if row is not None: + print(f"Row key: {row.row_key.decode()}") + print(row) except NotFound as e: + print(f"Authorized view does not exist: {e.message}") + except PermissionDenied as e: print( - f"Failed to read from a non-existent authorized view: {e.message}" + "Row key is outside the authorized view or access was denied: " + f"{e.message}" ) @@ -231,11 +240,12 @@ async def read_single_row_from_authorized_view( async def read_rows_with_filter_from_authorized_view( project_id, instance_id, table_id, authorized_view_id ): - from google.api_core.exceptions import NotFound + from google.api_core.exceptions import NotFound, PermissionDenied from google.cloud.bigtable.data import ( BigtableDataClientAsync, ReadRowsQuery, + RowRange, row_filters, ) @@ -243,20 +253,28 @@ async def read_rows_with_filter_from_authorized_view( async with client.get_authorized_view( instance_id, table_id, authorized_view_id ) as authorized_view: + # The row range must be within the authorized view. + row_range = RowRange( + start_key=b"phone#4c410523#20190501", + end_key=b"phone#4c410523#20190601", + ) + + # Return only the most recent cell version for each column. + max_cells_per_column = 1 + row_filter = row_filters.CellsColumnLimitFilter(max_cells_per_column) + query = ReadRowsQuery(row_ranges=[row_range], row_filter=row_filter) + try: - # A filter that matches only the most recent cell within each column. - row_filter = row_filters.CellsColumnLimitFilter(1) - query = ReadRowsQuery(row_filter=row_filter) async for row in await authorized_view.read_rows_stream(query): - print(f"Row Key: {row.row_key.decode()}") - for cell in row.cells: - print( - f"Family: {cell.family} " - f"Qualifier: {cell.qualifier.decode()} " - f"Value: {cell.value.decode()}" - ) + print(f"Row key: {row.row_key.decode()}") + print(row) except NotFound as e: - print(f"Failed to read a non-existent authorized view: {e.message}") + print(f"Authorized view does not exist: {e.message}") + except PermissionDenied as e: + print( + "Query targets rows or columns outside the authorized view: " + f"{e.message}" + ) # [END bigtable_async_authorized_view_scan_with_filter] diff --git a/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py index 23eee229439e..ef567de0fb54 100644 --- a/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py +++ b/packages/google-cloud-bigtable/samples/data_client_async/snippets/authorized_views_snippets_async_test.py @@ -47,7 +47,7 @@ async def table_id() -> AsyncGenerator[str, None]: PROJECT, BIGTABLE_INSTANCE, TABLE_ID, - {"cf": None}, + {"stats_summary": None}, verbose=False, ): yield TABLE_ID @@ -58,10 +58,11 @@ async def authorized_view_id(table_id: str) -> AsyncGenerator[str, None]: async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( - row_prefixes=[b""], + row_prefixes=[b"phone#"], family_subsets={ - "cf": bigtable_admin_v2.AuthorizedView.FamilySubsets( - qualifier_prefixes=[b""] + "stats_summary": bigtable_admin_v2.AuthorizedView.FamilySubsets( + qualifiers=[b"os_build"], + qualifier_prefixes=[b"connected_"], ) }, ) @@ -74,12 +75,13 @@ async def authorized_view_id(table_id: str) -> AsyncGenerator[str, None]: async with BigtableDataClientAsync(project=PROJECT) as client: async with client.get_table(BIGTABLE_INSTANCE, table_id) as table: - for i, name in enumerate(["World", "Bigtable", "Python"]): + for row_key in [b"phone#4c410523#20190501", b"phone#4c410523#20190502"]: await table.mutate_row( - f"rowKey{i}", + row_key, [ - SetCell("cf", "name", name), - SetCell("cf", "greeting", f"Hello {name}!"), + SetCell("stats_summary", b"connected_cell", 1), + SetCell("stats_summary", b"connected_wifi", 1), + SetCell("stats_summary", b"os_build", b"PQ2A.190405.003"), ], ) @@ -107,23 +109,15 @@ async def test_create_authorized_view(capsys, table_id): pass -@pytest.mark.asyncio -async def test_update_authorized_view(capsys, table_id, authorized_view_id): - await authorized_views_snippets_async.update_authorized_view( - PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id - ) - out, _ = capsys.readouterr() - assert f"authorizedViews/{authorized_view_id} updated successfully" in out - - @pytest.mark.asyncio async def test_get_authorized_view(capsys, table_id, authorized_view_id): await authorized_views_snippets_async.get_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Row Prefix: " in out - assert "Column Family: cf, Qualifier Prefix: " in out + assert "Row Prefix: phone#" in out + assert "Column Family: stats_summary, Qualifier Prefix: connected_" in out + assert "Column Family: stats_summary, Qualifier: os_build" in out @pytest.mark.asyncio @@ -135,35 +129,13 @@ async def test_list_authorized_views(capsys, table_id, authorized_view_id): assert authorized_view_id in out.splitlines() -@pytest.mark.asyncio -async def test_delete_authorized_view(capsys, table_id): - view_id = f"view-delete-{str(uuid.uuid4())[:12]}" - async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: - table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) - subset_view = bigtable_admin_v2.AuthorizedView.SubsetView(row_prefixes=[b""]) - operation = await admin_client.create_authorized_view( - parent=table_path, - authorized_view_id=view_id, - authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), - ) - await operation.result() - - await authorized_views_snippets_async.delete_authorized_view( - PROJECT, BIGTABLE_INSTANCE, table_id, view_id - ) - out, _ = capsys.readouterr() - assert f"AuthorizedView: {view_id} deleted successfully" in out - - @pytest.mark.asyncio async def test_write_to_authorized_view(capsys, table_id, authorized_view_id): await authorized_views_snippets_async.write_to_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Hello World!" in out - assert "Hello Bigtable!" in out - assert "Hello Python!" in out + assert "Successfully wrote row phone#4c410523#20190501 to authorized view" in out @pytest.mark.asyncio @@ -174,8 +146,8 @@ async def test_read_single_row_from_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Row: rowKey0" in out - assert "Family: cf Qualifier: greeting Value: Hello World!" in out + assert "phone#4c410523#20190501" in out + assert "PQ2A.190405.003" in out @pytest.mark.asyncio @@ -186,5 +158,58 @@ async def test_read_rows_with_filter_from_authorized_view( PROJECT, BIGTABLE_INSTANCE, table_id, authorized_view_id ) out, _ = capsys.readouterr() - assert "Row Key: rowKey0" in out - assert "Row Key: rowKey2" in out + assert "phone#4c410523#20190501" in out + assert "phone#4c410523#20190502" in out + + +@pytest.mark.asyncio +async def test_update_authorized_view(capsys, table_id): + view_id = f"view-update-{str(uuid.uuid4())[:12]}" + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b"phone#"] + ) + operation = await admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ) + await operation.result() + try: + await authorized_views_snippets_async.update_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"authorizedViews/{view_id} updated successfully" in out + finally: + try: + await admin_client.delete_authorized_view( + name=admin_client.authorized_view_path( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + ) + except NotFound: + pass + + +@pytest.mark.asyncio +async def test_delete_authorized_view(capsys, table_id): + view_id = f"view-delete-{str(uuid.uuid4())[:12]}" + async with bigtable_admin_v2.BigtableTableAdminAsyncClient() as admin_client: + table_path = admin_client.table_path(PROJECT, BIGTABLE_INSTANCE, table_id) + subset_view = bigtable_admin_v2.AuthorizedView.SubsetView( + row_prefixes=[b"phone#"] + ) + operation = await admin_client.create_authorized_view( + parent=table_path, + authorized_view_id=view_id, + authorized_view=bigtable_admin_v2.AuthorizedView(subset_view=subset_view), + ) + await operation.result() + + await authorized_views_snippets_async.delete_authorized_view( + PROJECT, BIGTABLE_INSTANCE, table_id, view_id + ) + out, _ = capsys.readouterr() + assert f"AuthorizedView: {view_id} deleted successfully" in out