|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.cloud.bigquery.dataset import AccessEntry |
| 16 | + |
| 17 | + |
| 18 | +def grant_access_to_dataset( |
| 19 | + dataset_id: str, |
| 20 | + entity_id: str, |
| 21 | + role: str |
| 22 | +) -> list[AccessEntry]: |
| 23 | + # [START bigquery_grant_access_to_dataset] |
| 24 | + from google.api_core.exceptions import PreconditionFailed |
| 25 | + from google.cloud import bigquery |
| 26 | + from google.cloud.bigquery.enums import EntityTypes |
| 27 | + |
| 28 | + # TODO(developer): Update and un-comment below lines |
| 29 | + |
| 30 | + # ID of the dataset to revoke access to. |
| 31 | + # dataset_id = "my_project_id.my_dataset" |
| 32 | + |
| 33 | + # ID of the user or group from whom you are adding access. |
| 34 | + # Alternatively, the JSON REST API representation of the entity, |
| 35 | + # such as a view's table reference. |
| 36 | + # entity_id = "user-or-group-to-add@example.com" |
| 37 | + |
| 38 | + # One of the "Basic roles for datasets" described here: |
| 39 | + # https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles |
| 40 | + # role = "READER" |
| 41 | + |
| 42 | + # Type of entity you are granting access to. |
| 43 | + # Find allowed allowed entity type names here: |
| 44 | + # https://cloud.google.com/python/docs/reference/bigquery/latest/enums#class-googlecloudbigqueryenumsentitytypesvalue |
| 45 | + entity_type = EntityTypes.GROUP_BY_EMAIL |
| 46 | + |
| 47 | + # Instantiate a client. |
| 48 | + client = bigquery.Client() |
| 49 | + |
| 50 | + # Get a reference to the dataset. |
| 51 | + dataset = client.get_dataset(dataset_id) |
| 52 | + |
| 53 | + # The `access_entries` list is immutable. Create a copy for modifications. |
| 54 | + entries = list(dataset.access_entries) |
| 55 | + |
| 56 | + # Append an AccessEntry to grant the role to a dataset. |
| 57 | + # Find more details about the AccessEntry object here: |
| 58 | + # https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry |
| 59 | + entries.append( |
| 60 | + bigquery.AccessEntry( |
| 61 | + role=role, |
| 62 | + entity_type=entity_type, |
| 63 | + entity_id=entity_id, |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + # Assign the list of AccessEntries back to the dataset. |
| 68 | + dataset.access_entries = entries |
| 69 | + |
| 70 | + # Update will only succeed if the dataset |
| 71 | + # has not been modified externally since retrieval. |
| 72 | + # |
| 73 | + # See the BigQuery client library documentation for more details on `update_dataset`: |
| 74 | + # https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client#google_cloud_bigquery_client_Client_update_dataset |
| 75 | + try: |
| 76 | + # Update just the `access_entries` property of the dataset. |
| 77 | + dataset = client.update_dataset( |
| 78 | + dataset, |
| 79 | + ["access_entries"], |
| 80 | + ) |
| 81 | + |
| 82 | + # Show a success message. |
| 83 | + full_dataset_id = f"{dataset.project}.{dataset.dataset_id}" |
| 84 | + print( |
| 85 | + f"Role '{role}' granted for entity '{entity_id}'" |
| 86 | + f" in dataset '{full_dataset_id}'." |
| 87 | + ) |
| 88 | + except PreconditionFailed: # A read-modify-write error |
| 89 | + print( |
| 90 | + f"Dataset '{dataset.dataset_id}' was modified remotely before this update. " |
| 91 | + "Fetch the latest version and retry." |
| 92 | + ) |
| 93 | + # [END bigquery_grant_access_to_dataset] |
| 94 | + |
| 95 | + return dataset.access_entries |
0 commit comments