Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bigquery/cloud-client/grant_access_to_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def grant_access_to_dataset(
from google.cloud import bigquery
from google.cloud.bigquery.enums import EntityTypes

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

# ID of the dataset to revoke access to.
# dataset_id = "my_project_id.my_dataset"
Expand Down
4 changes: 2 additions & 2 deletions bigquery/cloud-client/grant_access_to_table_or_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def grant_access_to_table_or_view(
# [START bigquery_grant_access_to_table_or_view]
from google.cloud import bigquery

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

# Google Cloud Platform project.
# project_id = "my_project_id"
Expand Down Expand Up @@ -66,7 +66,7 @@ def grant_access_to_table_or_view(
}
policy.bindings.append(binding)

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

# Show a success message.
Expand Down
5 changes: 3 additions & 2 deletions bigquery/cloud-client/revoke_access_to_table_or_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def revoke_access_to_table_or_view(
# Imports the Google Cloud client library.
from google.cloud import bigquery

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

# Google Cloud Platform project.
# project_id = "my_project_id"

Expand All @@ -44,7 +45,7 @@ def revoke_access_to_table_or_view(
# (Optional) Principal to revoke access to the table or view.
# principal_to_remove = "user:alice@example.com"

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

# Instantiates a client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
def test_revoke_access_to_table_or_view_for_role(
client: bigquery.Client,
dataset: Dataset,
project_id: str,
table: Table,
entity_id: str,
) -> None:
Expand All @@ -34,7 +33,7 @@ def test_revoke_access_to_table_or_view_for_role(
assert not empty_policy.bindings

policy_with_role = grant_access_to_table_or_view(
project_id,
dataset.project,
dataset.dataset_id,
table.table_id,
principal_id=PRINCIPAL_ID,
Expand All @@ -45,7 +44,7 @@ def test_revoke_access_to_table_or_view_for_role(
assert any(p for p in policy_with_role if p["role"] == ROLE)

policy_with_revoked_role = revoke_access_to_table_or_view(
project_id,
dataset.project,
dataset.dataset_id,
resource_name=table.table_id,
role_to_remove=ROLE,
Expand Down
4 changes: 2 additions & 2 deletions bigquery/cloud-client/revoke_dataset_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def revoke_dataset_access(dataset_id: str, entity_id: str) -> list[AccessEntry]:
from google.cloud import bigquery
from google.api_core.exceptions import PreconditionFailed

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

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

full_dataset_id = f"{dataset.project}.{dataset.dataset_id}"
print(f"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'")
except PreconditionFailed: # A read-modify-write error
except PreconditionFailed: # A read-modify-write error.
print(
f"Dataset '{dataset.dataset_id}' was modified remotely before this update. "
"Fetch the latest version and retry."
Expand Down
10 changes: 7 additions & 3 deletions bigquery/cloud-client/view_dataset_access_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def view_dataset_access_policy(dataset_id: str) -> list[AccessEntry]:
# Instantiate a client.
client = bigquery.Client()

# TODO(developer): Update and un-comment below lines
# Dataset from which to get the access policy
# TODO(developer): Update and uncomment the lines below.

# Dataset from which to get the access policy.
# dataset_id = "my_dataset"

# Get a reference to the dataset.
Expand All @@ -33,7 +34,10 @@ def view_dataset_access_policy(dataset_id: str) -> list[AccessEntry]:
# Show the list of AccessEntry objects.
# More details about the AccessEntry object here:
# https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry
print(f"{len(dataset.access_entries)} Access entries in dataset '{dataset_id}':")
print(
f"{len(dataset.access_entries)} Access entries found "
f"in dataset '{dataset_id}':"
)
for access_entry in dataset.access_entries:
print()
print(f"Role: {access_entry.role}")
Expand Down
21 changes: 12 additions & 9 deletions bigquery/cloud-client/view_table_or_view_access_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@
from google.api_core.iam import Policy


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

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

# Google Cloud Platform project.
# project_id = "my_project_id"

# Dataset where the table or view is.
# dataset_id = "my_dataset_id"
# Table or view name to get the access policy.
# resource_name = "my_table_name"

# Table or view from which to get the access policy.
# resource_id = "my_table_id"

# Instantiate a client.
client = bigquery.Client()

# Get the full table or view name.
full_resource_name = f"{project_id}.{dataset_id}.{resource_name}"
# Get the full table or view id.
full_resource_id = f"{project_id}.{dataset_id}.{resource_id}"

# Get the IAM access policy for the table or view.
policy = client.get_iam_policy(full_resource_name)
policy = client.get_iam_policy(full_resource_id)

# Show policy details.
# Find more details for the Policy object here:
# https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy
print(f"Access Policy details for table or view '{resource_name}'.")
print(f"Access Policy details for table or view '{resource_id}'.")
print(f"Bindings: {policy.bindings}")
print(f"etag: {policy.etag}")
print(f"Version: {policy.version}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_view_dataset_access_policies_with_table(
policy: Policy = view_table_or_view_access_policy(project_id, dataset.dataset_id, table.table_id)

assert policy.etag == EMPTY_POLICY_ETAG
assert not policy.bindings # Bindings list is empty
assert not policy.bindings # Empty bindings list


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

assert policy.etag == EMPTY_POLICY_ETAG
assert not policy.bindings # Bindings list is empty
assert not policy.bindings # Empty bindings list