Skip to content

Commit a88f70d

Browse files
fix(bigquery): improve cloud-client samples (#13180)
* fix(bigquery): fix sample view_dataset_access_policy - Add missing period. - Add 'found' word to console output. * fix(bigquery): fix sample view_table_or_view_access_policy.py - Rename resource_name to resource_id, to make it consistent with Python client-library documentation. * fix(bigquery): unify style across samples * fix(bigquery): style fixing * fix(bigquery): lint fixes
1 parent 48f37ab commit a88f70d

8 files changed

+31
-24
lines changed

bigquery/cloud-client/grant_access_to_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def grant_access_to_dataset(
2525
from google.cloud import bigquery
2626
from google.cloud.bigquery.enums import EntityTypes
2727

28-
# TODO(developer): Update and un-comment below lines
28+
# TODO(developer): Update and uncomment the lines below.
2929

3030
# ID of the dataset to revoke access to.
3131
# dataset_id = "my_project_id.my_dataset"

bigquery/cloud-client/grant_access_to_table_or_view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def grant_access_to_table_or_view(
2626
# [START bigquery_grant_access_to_table_or_view]
2727
from google.cloud import bigquery
2828

29-
# TODO(developer): Update and un-comment below lines
29+
# TODO(developer): Update and uncomment the lines below.
3030

3131
# Google Cloud Platform project.
3232
# project_id = "my_project_id"
@@ -66,7 +66,7 @@ def grant_access_to_table_or_view(
6666
}
6767
policy.bindings.append(binding)
6868

69-
# Set the IAM acces spolicy with updated bindings
69+
# Set the IAM access spolicy with updated bindings.
7070
updated_policy = client.set_iam_policy(full_resource_name, policy)
7171

7272
# Show a success message.

bigquery/cloud-client/revoke_access_to_table_or_view.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def revoke_access_to_table_or_view(
2828
# Imports the Google Cloud client library.
2929
from google.cloud import bigquery
3030

31-
# TODO(developer): Update and un-comment below lines
31+
# TODO(developer): Update and uncomment the lines below.
32+
3233
# Google Cloud Platform project.
3334
# project_id = "my_project_id"
3435

@@ -44,7 +45,7 @@ def revoke_access_to_table_or_view(
4445
# (Optional) Principal to revoke access to the table or view.
4546
# principal_to_remove = "user:[email protected]"
4647

47-
# Find more information about roles and principals (refered as members) here:
48+
# Find more information about roles and principals (referred to as members) here:
4849
# https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Binding
4950

5051
# Instantiates a client.

bigquery/cloud-client/revoke_access_to_table_or_view_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
def test_revoke_access_to_table_or_view_for_role(
2424
client: bigquery.Client,
2525
dataset: Dataset,
26-
project_id: str,
2726
table: Table,
2827
entity_id: str,
2928
) -> None:
@@ -34,7 +33,7 @@ def test_revoke_access_to_table_or_view_for_role(
3433
assert not empty_policy.bindings
3534

3635
policy_with_role = grant_access_to_table_or_view(
37-
project_id,
36+
dataset.project,
3837
dataset.dataset_id,
3938
table.table_id,
4039
principal_id=PRINCIPAL_ID,
@@ -45,7 +44,7 @@ def test_revoke_access_to_table_or_view_for_role(
4544
assert any(p for p in policy_with_role if p["role"] == ROLE)
4645

4746
policy_with_revoked_role = revoke_access_to_table_or_view(
48-
project_id,
47+
dataset.project,
4948
dataset.dataset_id,
5049
resource_name=table.table_id,
5150
role_to_remove=ROLE,

bigquery/cloud-client/revoke_dataset_access.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def revoke_dataset_access(dataset_id: str, entity_id: str) -> list[AccessEntry]:
2121
from google.cloud import bigquery
2222
from google.api_core.exceptions import PreconditionFailed
2323

24-
# TODO(developer): Update and un-comment below lines
24+
# TODO(developer): Update and uncomment the lines below.
2525

2626
# ID of the dataset to revoke access to.
2727
# dataset_id = "your-project.your_dataset"
@@ -63,7 +63,7 @@ def revoke_dataset_access(dataset_id: str, entity_id: str) -> list[AccessEntry]:
6363

6464
full_dataset_id = f"{dataset.project}.{dataset.dataset_id}"
6565
print(f"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'")
66-
except PreconditionFailed: # A read-modify-write error
66+
except PreconditionFailed: # A read-modify-write error.
6767
print(
6868
f"Dataset '{dataset.dataset_id}' was modified remotely before this update. "
6969
"Fetch the latest version and retry."

bigquery/cloud-client/view_dataset_access_policy.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def view_dataset_access_policy(dataset_id: str) -> list[AccessEntry]:
2323
# Instantiate a client.
2424
client = bigquery.Client()
2525

26-
# TODO(developer): Update and un-comment below lines
27-
# Dataset from which to get the access policy
26+
# TODO(developer): Update and uncomment the lines below.
27+
28+
# Dataset from which to get the access policy.
2829
# dataset_id = "my_dataset"
2930

3031
# Get a reference to the dataset.
@@ -33,7 +34,10 @@ def view_dataset_access_policy(dataset_id: str) -> list[AccessEntry]:
3334
# Show the list of AccessEntry objects.
3435
# More details about the AccessEntry object here:
3536
# https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry
36-
print(f"{len(dataset.access_entries)} Access entries in dataset '{dataset_id}':")
37+
print(
38+
f"{len(dataset.access_entries)} Access entries found "
39+
f"in dataset '{dataset_id}':"
40+
)
3741
for access_entry in dataset.access_entries:
3842
print()
3943
print(f"Role: {access_entry.role}")

bigquery/cloud-client/view_table_or_view_access_policy.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,35 @@
1515
from google.api_core.iam import Policy
1616

1717

18-
def view_table_or_view_access_policy(project_id: str, dataset_id: str, resource_name: str) -> Policy:
18+
def view_table_or_view_access_policy(project_id: str, dataset_id: str, resource_id: str) -> Policy:
1919
# [START bigquery_view_table_or_view_access_policy]
20-
# Imports the Google Cloud client library
20+
# Imports the Google Cloud client library.
2121
from google.cloud import bigquery
2222

23-
# TODO(developer): Update and un-comment below lines
23+
# TODO(developer): Update and uncomment the lines below.
24+
2425
# Google Cloud Platform project.
2526
# project_id = "my_project_id"
27+
2628
# Dataset where the table or view is.
2729
# dataset_id = "my_dataset_id"
28-
# Table or view name to get the access policy.
29-
# resource_name = "my_table_name"
30+
31+
# Table or view from which to get the access policy.
32+
# resource_id = "my_table_id"
3033

3134
# Instantiate a client.
3235
client = bigquery.Client()
3336

34-
# Get the full table or view name.
35-
full_resource_name = f"{project_id}.{dataset_id}.{resource_name}"
37+
# Get the full table or view id.
38+
full_resource_id = f"{project_id}.{dataset_id}.{resource_id}"
3639

3740
# Get the IAM access policy for the table or view.
38-
policy = client.get_iam_policy(full_resource_name)
41+
policy = client.get_iam_policy(full_resource_id)
3942

4043
# Show policy details.
4144
# Find more details for the Policy object here:
4245
# https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy
43-
print(f"Access Policy details for table or view '{resource_name}'.")
46+
print(f"Access Policy details for table or view '{resource_id}'.")
4447
print(f"Bindings: {policy.bindings}")
4548
print(f"etag: {policy.etag}")
4649
print(f"Version: {policy.version}")

bigquery/cloud-client/view_table_or_view_access_policy_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_view_dataset_access_policies_with_table(
2929
policy: Policy = view_table_or_view_access_policy(project_id, dataset.dataset_id, table.table_id)
3030

3131
assert policy.etag == EMPTY_POLICY_ETAG
32-
assert not policy.bindings # Bindings list is empty
32+
assert not policy.bindings # Empty bindings list
3333

3434

3535
def test_view_dataset_access_policies_with_view(
@@ -40,4 +40,4 @@ def test_view_dataset_access_policies_with_view(
4040
policy: Policy = view_table_or_view_access_policy(project_id, dataset.dataset_id, view.table_id)
4141

4242
assert policy.etag == EMPTY_POLICY_ETAG
43-
assert not policy.bindings # Bindings list is empty
43+
assert not policy.bindings # Empty bindings list

0 commit comments

Comments
 (0)