From 0abac4c3be55c8051508a5050ebb56a8050a15fc Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Thu, 1 Jul 2021 16:50:09 -0700 Subject: [PATCH 01/38] revoke dataset access setup --- samples/revoke_dataset_access.py | 14 ++++++++++++++ samples/revoke_dataset_access_test.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 samples/revoke_dataset_access.py create mode 100644 samples/revoke_dataset_access_test.py diff --git a/samples/revoke_dataset_access.py b/samples/revoke_dataset_access.py new file mode 100644 index 000000000..2c31d84ad --- /dev/null +++ b/samples/revoke_dataset_access.py @@ -0,0 +1,14 @@ +# Copyright 2019 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 +# +# https://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. + diff --git a/samples/revoke_dataset_access_test.py b/samples/revoke_dataset_access_test.py new file mode 100644 index 000000000..2c31d84ad --- /dev/null +++ b/samples/revoke_dataset_access_test.py @@ -0,0 +1,14 @@ +# Copyright 2019 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 +# +# https://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. + From d699bec9bbc5961618e72fad9112f9bdf7145dec Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 2 Jul 2021 12:02:49 -0700 Subject: [PATCH 02/38] basic template for sample --- samples/revoke_dataset_access.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/samples/revoke_dataset_access.py b/samples/revoke_dataset_access.py index 2c31d84ad..b5e95f8e4 100644 --- a/samples/revoke_dataset_access.py +++ b/samples/revoke_dataset_access.py @@ -12,3 +12,33 @@ # See the License for the specific language governing permissions and # limitations under the License. +def revoke_dataset_access(dataset_id): + + # [START bigquery_revoke_dataset_access] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set dataset_id to the ID of the dataset to fetch. + # dataset_id = 'your-project.your_dataset' + + dataset = client.get_dataset(dataset_id) # Make an API request. + + entry = bigquery.AccessEntry( + role="READER", + entity_type="userByEmail", + entity_id="sample.bigquery.dev@gmail.com", + ) + + entries = list(dataset.access_entries) + entries.append(entry) + dataset.access_entries = entries + + dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request. + + full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) + print( + "Updated dataset '{}' with modified user permissions.".format(full_dataset_id) + ) + # [END bigquery_revoke_dataset_access] From 98b245d0ce4ece85e9af2fe015066b2319bf24c5 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Wed, 7 Jul 2021 16:26:50 -0700 Subject: [PATCH 03/38] sample + test --- samples/revoke_dataset_access.py | 16 +++++++--------- .../test_revoke_dataset_access.py} | 13 +++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) rename samples/{revoke_dataset_access_test.py => tests/test_revoke_dataset_access.py} (58%) diff --git a/samples/revoke_dataset_access.py b/samples/revoke_dataset_access.py index b5e95f8e4..6c2e91315 100644 --- a/samples/revoke_dataset_access.py +++ b/samples/revoke_dataset_access.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -def revoke_dataset_access(dataset_id): +def revoke_dataset_access(dataset_id, entity_id): # [START bigquery_revoke_dataset_access] from google.cloud import bigquery @@ -24,15 +24,13 @@ def revoke_dataset_access(dataset_id): # dataset_id = 'your-project.your_dataset' dataset = client.get_dataset(dataset_id) # Make an API request. - - entry = bigquery.AccessEntry( - role="READER", - entity_type="userByEmail", - entity_id="sample.bigquery.dev@gmail.com", - ) - entries = list(dataset.access_entries) - entries.append(entry) + + for entry in entries: + if entry.entity_id == entity_id: + entry.role = None + break + dataset.access_entries = entries dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request. diff --git a/samples/revoke_dataset_access_test.py b/samples/tests/test_revoke_dataset_access.py similarity index 58% rename from samples/revoke_dataset_access_test.py rename to samples/tests/test_revoke_dataset_access.py index 2c31d84ad..3ab7d1e7e 100644 --- a/samples/revoke_dataset_access_test.py +++ b/samples/tests/test_revoke_dataset_access.py @@ -12,3 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +from .. import update_dataset_access +from .. import revoke_dataset_access + + +def test_revoke_dataset_access(capsys, dataset_id, entity_id): + + update_dataset_access.update_dataset_access(dataset_id) + revoke_dataset_access.revoke_dataset_access(dataset_id, entity_id) + + out, err = capsys.readouterr() + assert ( + "Updated dataset '{}' with modified user permissions.".format(dataset_id) in out + ) From f1adee71af9040868e59d4239931ac8fe5ad7b26 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 16 Jul 2021 15:40:45 -0700 Subject: [PATCH 04/38] revoke dataset access sample --- samples/revoke_dataset_access.py | 3 ++- samples/snippets/noxfile.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/revoke_dataset_access.py b/samples/revoke_dataset_access.py index 6c2e91315..ae598b866 100644 --- a/samples/revoke_dataset_access.py +++ b/samples/revoke_dataset_access.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + def revoke_dataset_access(dataset_id, entity_id): # [START bigquery_revoke_dataset_access] @@ -25,7 +26,7 @@ def revoke_dataset_access(dataset_id, entity_id): dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) - + for entry in entries: if entry.entity_id == entity_id: entry.role = None diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index 160fe7286..b3c8658a3 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -226,7 +226,7 @@ def py(session: nox.sessions.Session) -> None: def _get_repo_root() -> Optional[str]: - """ Returns the root folder of the project. """ + """Returns the root folder of the project.""" # Get root of this repository. Assume we don't have directories nested deeper than 10 items. p = Path(os.getcwd()) for i in range(10): From e729bbd4cd8313ea1c056fe4e1e02c653c7e31d0 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 16 Jul 2021 22:45:35 +0000 Subject: [PATCH 05/38] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md --- samples/snippets/noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index b3c8658a3..160fe7286 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -226,7 +226,7 @@ def py(session: nox.sessions.Session) -> None: def _get_repo_root() -> Optional[str]: - """Returns the root folder of the project.""" + """ Returns the root folder of the project. """ # Get root of this repository. Assume we don't have directories nested deeper than 10 items. p = Path(os.getcwd()) for i in range(10): From b80c4bfdea68d8bebb1e4a22184671028bf117c7 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 23 Jul 2021 14:39:51 -0700 Subject: [PATCH 06/38] docs: add sample for revoking dataset access - update year and string formatting --- samples/revoke_dataset_access.py | 8 +++----- samples/tests/test_revoke_dataset_access.py | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/samples/revoke_dataset_access.py b/samples/revoke_dataset_access.py index ae598b866..1d58259a9 100644 --- a/samples/revoke_dataset_access.py +++ b/samples/revoke_dataset_access.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -36,8 +36,6 @@ def revoke_dataset_access(dataset_id, entity_id): dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request. - full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) - print( - "Updated dataset '{}' with modified user permissions.".format(full_dataset_id) - ) + full_dataset_id = f"{dataset.project}.{dataset.dataset_id}" + print(f"Updated dataset '{full_dataset_id}' with modified user permissions.") # [END bigquery_revoke_dataset_access] diff --git a/samples/tests/test_revoke_dataset_access.py b/samples/tests/test_revoke_dataset_access.py index 3ab7d1e7e..9f3a4e76d 100644 --- a/samples/tests/test_revoke_dataset_access.py +++ b/samples/tests/test_revoke_dataset_access.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -22,6 +22,4 @@ def test_revoke_dataset_access(capsys, dataset_id, entity_id): revoke_dataset_access.revoke_dataset_access(dataset_id, entity_id) out, err = capsys.readouterr() - assert ( - "Updated dataset '{}' with modified user permissions.".format(dataset_id) in out - ) + assert f"Updated dataset '{dataset_id}' with modified user permissions." in out From 02bd2b4db2d0e3dbe8509eaadc6c0416f7ac5b34 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 23 Jul 2021 15:00:54 -0700 Subject: [PATCH 07/38] docs: add sample for revoking dataset access - move to snippets and change parameter pattern for readibility --- samples/{ => snippets}/revoke_dataset_access.py | 15 ++++++++++++--- .../revoke_dataset_access_test.py} | 0 2 files changed, 12 insertions(+), 3 deletions(-) rename samples/{ => snippets}/revoke_dataset_access.py (71%) rename samples/{tests/test_revoke_dataset_access.py => snippets/revoke_dataset_access_test.py} (100%) diff --git a/samples/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py similarity index 71% rename from samples/revoke_dataset_access.py rename to samples/snippets/revoke_dataset_access.py index 1d58259a9..73c6347a6 100644 --- a/samples/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -13,7 +13,7 @@ # limitations under the License. -def revoke_dataset_access(dataset_id, entity_id): +def revoke_dataset_access(your_dataset_id, your_entity_id): # [START bigquery_revoke_dataset_access] from google.cloud import bigquery @@ -21,8 +21,17 @@ def revoke_dataset_access(dataset_id, entity_id): # Construct a BigQuery client object. client = bigquery.Client() - # TODO(developer): Set dataset_id to the ID of the dataset to fetch. - # dataset_id = 'your-project.your_dataset' + original_your_dataset_id = your_dataset_id + original_your_entity_id = your_entity_id + # [START bigquery_revoke_dataset_access_read_session] + your_dataset_id = "dataset-for-read-session" + your_entity_id = "entity-for-read-session" + # [END bigquery_revoke_dataset_access_read_session] + your_dataset_id = original_your_dataset_id + your_entity_id = original_your_entity_id + + dataset_id = your_dataset_id + entity_id = your_entity_id dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) diff --git a/samples/tests/test_revoke_dataset_access.py b/samples/snippets/revoke_dataset_access_test.py similarity index 100% rename from samples/tests/test_revoke_dataset_access.py rename to samples/snippets/revoke_dataset_access_test.py From fa0e48cdc7eb9cf338bd01ea383d7265a737cace Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Tue, 27 Jul 2021 13:45:49 -0700 Subject: [PATCH 08/38] moving update_dataset to /snippets and adjusting imports on both revoke_access and update_access --- .../snippets/revoke_dataset_access_test.py | 4 +- samples/snippets/update_dataset_access.py | 45 +++++++++++++++++++ .../snippets/update_dataset_access_test.py | 24 ++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/update_dataset_access.py create mode 100644 samples/snippets/update_dataset_access_test.py diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/revoke_dataset_access_test.py index 9f3a4e76d..0181f9e69 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/revoke_dataset_access_test.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .. import update_dataset_access -from .. import revoke_dataset_access +from . import update_dataset_access +from . import revoke_dataset_access def test_revoke_dataset_access(capsys, dataset_id, entity_id): diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py new file mode 100644 index 000000000..6e844cc90 --- /dev/null +++ b/samples/snippets/update_dataset_access.py @@ -0,0 +1,45 @@ +# Copyright 2019 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 +# +# https://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. + + +def update_dataset_access(dataset_id): + + # [START bigquery_update_dataset_access] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set dataset_id to the ID of the dataset to fetch. + # dataset_id = 'your-project.your_dataset' + + dataset = client.get_dataset(dataset_id) # Make an API request. + + entry = bigquery.AccessEntry( + role="READER", + entity_type="userByEmail", + entity_id="sample.bigquery.dev@gmail.com", + ) + + entries = list(dataset.access_entries) + entries.append(entry) + dataset.access_entries = entries + + dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request. + + full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) + print( + "Updated dataset '{}' with modified user permissions.".format(full_dataset_id) + ) + # [END bigquery_update_dataset_access] diff --git a/samples/snippets/update_dataset_access_test.py b/samples/snippets/update_dataset_access_test.py new file mode 100644 index 000000000..8fd8ba61f --- /dev/null +++ b/samples/snippets/update_dataset_access_test.py @@ -0,0 +1,24 @@ +# Copyright 2019 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 +# +# https://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. + +from . import update_dataset_access + + +def test_update_dataset_access(capsys, dataset_id): + + update_dataset_access.update_dataset_access(dataset_id) + out, err = capsys.readouterr() + assert ( + "Updated dataset '{}' with modified user permissions.".format(dataset_id) in out + ) From 98bb4a2726465a41fb7ce1401cfe0585a32a0918 Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Tue, 27 Jul 2021 13:47:53 -0700 Subject: [PATCH 09/38] Update samples/snippets/revoke_dataset_access.py removed nested START/END tags Co-authored-by: Tim Swast --- samples/snippets/revoke_dataset_access.py | 25 ++++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 73c6347a6..aaa4c4397 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -13,7 +13,19 @@ # limitations under the License. -def revoke_dataset_access(your_dataset_id, your_entity_id): +def revoke_dataset_access(dataset_id, entity_id): + original_dataset_id = dataset_id + original_entity_id = entity_id + # [START bigquery_revoke_dataset_access] + # TODO(developer): Set the dataset ID to the dataset where you are revoking access. + dataset_id = "your_dataset_id" + + # TODO(developer): Set the entity ID of the email or group from whom you are revoking access. + entity_id = "user-or-group-to-remove@example.com" + + # [END bigquery_revoke_dataset_access] + dataset_id = original_dataset_id + entity_id = original_entity_id # [START bigquery_revoke_dataset_access] from google.cloud import bigquery @@ -21,17 +33,6 @@ def revoke_dataset_access(your_dataset_id, your_entity_id): # Construct a BigQuery client object. client = bigquery.Client() - original_your_dataset_id = your_dataset_id - original_your_entity_id = your_entity_id - # [START bigquery_revoke_dataset_access_read_session] - your_dataset_id = "dataset-for-read-session" - your_entity_id = "entity-for-read-session" - # [END bigquery_revoke_dataset_access_read_session] - your_dataset_id = original_your_dataset_id - your_entity_id = original_your_entity_id - - dataset_id = your_dataset_id - entity_id = your_entity_id dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) From 8c4bc7a1b952e80f7ab5c60db47bee541ced198d Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Tue, 27 Jul 2021 13:48:49 -0700 Subject: [PATCH 10/38] Update samples/snippets/revoke_dataset_access.py update readability in API request Co-authored-by: Tim Swast --- samples/snippets/revoke_dataset_access.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index aaa4c4397..50a9dc96b 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -44,7 +44,11 @@ def revoke_dataset_access(dataset_id, entity_id): dataset.access_entries = entries - dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request. + dataset = client.update_dataset( + dataset, + # Update just the `access_entries` property of the dataset. + ["access_entries"], + ) # Make an API request. full_dataset_id = f"{dataset.project}.{dataset.dataset_id}" print(f"Updated dataset '{full_dataset_id}' with modified user permissions.") From 5197d0fe228190fef38a08dc00a4441a9933c07a Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 27 Jul 2021 20:50:38 +0000 Subject: [PATCH 11/38] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md --- samples/snippets/revoke_dataset_access.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 50a9dc96b..60727e3dc 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -33,7 +33,6 @@ def revoke_dataset_access(dataset_id, entity_id): # Construct a BigQuery client object. client = bigquery.Client() - dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) From cfb38058f54abf9ab201f2bdcdde040ed156dd00 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Thu, 29 Jul 2021 14:56:55 -0700 Subject: [PATCH 12/38] updated test --- samples/snippets/README.rst | 29 ++++--------------- .../snippets/authenticate_service_account.py | 8 +++-- samples/snippets/noxfile.py | 2 +- samples/snippets/revoke_dataset_access.py | 11 +++---- .../snippets/revoke_dataset_access_test.py | 24 +++++++++++++-- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/samples/snippets/README.rst b/samples/snippets/README.rst index 7c3e19e68..9446125cd 100644 --- a/samples/snippets/README.rst +++ b/samples/snippets/README.rst @@ -1,4 +1,3 @@ - .. This file is automatically generated. Do not edit this file directly. Google BigQuery Python Samples @@ -16,11 +15,14 @@ This directory contains samples for Google BigQuery. `Google BigQuery`_ is Googl .. _Google BigQuery: https://cloud.google.com/bigquery/docs +To run the sample, you need to have `BigQuery Admin` role. + + + Setup ------------------------------------------------------------------------------- - Authentication ++++++++++++++ @@ -31,9 +33,6 @@ credentials for applications. .. _Authentication Getting Started Guide: https://cloud.google.com/docs/authentication/getting-started - - - Install Dependencies ++++++++++++++++++++ @@ -48,7 +47,7 @@ Install Dependencies .. _Python Development Environment Setup Guide: https://cloud.google.com/python/setup -#. Create a virtualenv. Samples are compatible with Python 3.6+. +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. .. code-block:: bash @@ -64,15 +63,9 @@ Install Dependencies .. _pip: https://pip.pypa.io/ .. _virtualenv: https://virtualenv.pypa.io/ - - - - - Samples ------------------------------------------------------------------------------- - Quickstart +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -89,8 +82,6 @@ To run this sample: $ python quickstart.py - - Simple Application +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -107,8 +98,6 @@ To run this sample: $ python simple_app.py - - User Credentials +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -124,7 +113,6 @@ To run this sample: $ python user_credentials.py - usage: user_credentials.py [-h] [--launch-browser] project Command-line application to run a query using user credentials. @@ -143,10 +131,6 @@ To run this sample: - - - - The client library ------------------------------------------------------------------------------- @@ -162,5 +146,4 @@ to `browse the source`_ and `report issues`_. https://github.com/GoogleCloudPlatform/google-cloud-python/issues - -.. _Google Cloud SDK: https://cloud.google.com/sdk/ +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/samples/snippets/authenticate_service_account.py b/samples/snippets/authenticate_service_account.py index c07848bee..fa3c53cda 100644 --- a/samples/snippets/authenticate_service_account.py +++ b/samples/snippets/authenticate_service_account.py @@ -27,7 +27,8 @@ def main(): # key_path = "path/to/service_account.json" credentials = service_account.Credentials.from_service_account_file( - key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"], + key_path, + scopes=["https://www.googleapis.com/auth/cloud-platform"], ) # Alternatively, use service_account.Credentials.from_service_account_info() @@ -35,7 +36,10 @@ def main(): # TODO(developer): Set key_json to the content of the service account key file. # credentials = service_account.Credentials.from_service_account_info(key_json) - client = bigquery.Client(credentials=credentials, project=credentials.project_id,) + client = bigquery.Client( + credentials=credentials, + project=credentials.project_id, + ) # [END bigquery_client_json_credentials] return client diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index 160fe7286..b3c8658a3 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -226,7 +226,7 @@ def py(session: nox.sessions.Session) -> None: def _get_repo_root() -> Optional[str]: - """ Returns the root folder of the project. """ + """Returns the root folder of the project.""" # Get root of this repository. Assume we don't have directories nested deeper than 10 items. p = Path(os.getcwd()) for i in range(10): diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 60727e3dc..84b537c4c 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -36,12 +36,9 @@ def revoke_dataset_access(dataset_id, entity_id): dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) - for entry in entries: - if entry.entity_id == entity_id: - entry.role = None - break - - dataset.access_entries = entries + dataset.access_entries = entries.filter( + lambda entry: entry.entity_id != entity_id, entries + ) dataset = client.update_dataset( dataset, @@ -50,5 +47,5 @@ def revoke_dataset_access(dataset_id, entity_id): ) # Make an API request. full_dataset_id = f"{dataset.project}.{dataset.dataset_id}" - print(f"Updated dataset '{full_dataset_id}' with modified user permissions.") + print(f"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'") # [END bigquery_revoke_dataset_access] diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/revoke_dataset_access_test.py index 0181f9e69..34cbff933 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/revoke_dataset_access_test.py @@ -14,12 +14,30 @@ from . import update_dataset_access from . import revoke_dataset_access +from google.cloud import bigquery +from functools import reduce -def test_revoke_dataset_access(capsys, dataset_id, entity_id): - +def test_revoke_dataset_access( + capsys, dataset_id, entity_id, bigquery_client: bigquery.Client +): update_dataset_access.update_dataset_access(dataset_id) + updated_dataset = bigquery_client.get_dataset(dataset_id) + updated_dataset_entries = list(updated_dataset.access_entries) revoke_dataset_access.revoke_dataset_access(dataset_id, entity_id) + revoked_dataset = bigquery_client.get_dataset(dataset_id) + revoked_dataset_entries = list(revoked_dataset.access_entries) + full_dataset_id = f"{updated_dataset.project}.{updated_dataset.dataset_id}" out, err = capsys.readouterr() - assert f"Updated dataset '{dataset_id}' with modified user permissions." in out + assert ( + f"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'" + in out + ) + assert len(revoked_dataset_entries) == len(updated_dataset_entries) - 1 + assert ( + reduce( + lambda entry: bool(entry.entity_id == entity_id), revoked_dataset_entries + ) + == False + ) From 4be97600e6ff7492a25f57ffc141ee9fa0ebcec7 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 29 Jul 2021 21:58:39 +0000 Subject: [PATCH 13/38] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md --- samples/snippets/authenticate_service_account.py | 8 ++------ samples/snippets/noxfile.py | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/samples/snippets/authenticate_service_account.py b/samples/snippets/authenticate_service_account.py index fa3c53cda..c07848bee 100644 --- a/samples/snippets/authenticate_service_account.py +++ b/samples/snippets/authenticate_service_account.py @@ -27,8 +27,7 @@ def main(): # key_path = "path/to/service_account.json" credentials = service_account.Credentials.from_service_account_file( - key_path, - scopes=["https://www.googleapis.com/auth/cloud-platform"], + key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"], ) # Alternatively, use service_account.Credentials.from_service_account_info() @@ -36,10 +35,7 @@ def main(): # TODO(developer): Set key_json to the content of the service account key file. # credentials = service_account.Credentials.from_service_account_info(key_json) - client = bigquery.Client( - credentials=credentials, - project=credentials.project_id, - ) + client = bigquery.Client(credentials=credentials, project=credentials.project_id,) # [END bigquery_client_json_credentials] return client diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index b3c8658a3..160fe7286 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -226,7 +226,7 @@ def py(session: nox.sessions.Session) -> None: def _get_repo_root() -> Optional[str]: - """Returns the root folder of the project.""" + """ Returns the root folder of the project. """ # Get root of this repository. Assume we don't have directories nested deeper than 10 items. p = Path(os.getcwd()) for i in range(10): From b9e35aa98e7224121f4bc98514d129160b150dc1 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Mon, 30 Aug 2021 11:11:22 -0700 Subject: [PATCH 14/38] change after running test --- samples/snippets/authenticate_service_account.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/samples/snippets/authenticate_service_account.py b/samples/snippets/authenticate_service_account.py index fa3c53cda..c07848bee 100644 --- a/samples/snippets/authenticate_service_account.py +++ b/samples/snippets/authenticate_service_account.py @@ -27,8 +27,7 @@ def main(): # key_path = "path/to/service_account.json" credentials = service_account.Credentials.from_service_account_file( - key_path, - scopes=["https://www.googleapis.com/auth/cloud-platform"], + key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"], ) # Alternatively, use service_account.Credentials.from_service_account_info() @@ -36,10 +35,7 @@ def main(): # TODO(developer): Set key_json to the content of the service account key file. # credentials = service_account.Credentials.from_service_account_info(key_json) - client = bigquery.Client( - credentials=credentials, - project=credentials.project_id, - ) + client = bigquery.Client(credentials=credentials, project=credentials.project_id,) # [END bigquery_client_json_credentials] return client From 2209d8e64835131abb8e99bb115372ad0a100dbb Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 10 Sep 2021 18:24:52 -0700 Subject: [PATCH 15/38] resolving linting failure, rewriting test --- samples/snippets/revoke_dataset_access_test.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/revoke_dataset_access_test.py index 34cbff933..96b315c15 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/revoke_dataset_access_test.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import update_dataset_access -from . import revoke_dataset_access from google.cloud import bigquery -from functools import reduce + +from . import revoke_dataset_access +from . import update_dataset_access def test_revoke_dataset_access( @@ -35,9 +35,8 @@ def test_revoke_dataset_access( in out ) assert len(revoked_dataset_entries) == len(updated_dataset_entries) - 1 - assert ( - reduce( - lambda entry: bool(entry.entity_id == entity_id), revoked_dataset_entries - ) - == False - ) + is_revoked = 0 + for entry in revoke_dataset_access: + if entry.entity_id == entity_id: + is_revoked += 1 + assert is_revoked == 0 From 0da230175ee1b8a2962c7247431ff6b0979cbe65 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Tue, 14 Sep 2021 18:09:36 -0700 Subject: [PATCH 16/38] removed relative import errors --- samples/snippets/revoke_dataset_access_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/revoke_dataset_access_test.py index 96b315c15..df5a0423e 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/revoke_dataset_access_test.py @@ -14,8 +14,8 @@ from google.cloud import bigquery -from . import revoke_dataset_access -from . import update_dataset_access +import revoke_dataset_access +import update_dataset_access def test_revoke_dataset_access( From 77066a56ab3e999eedcd5df6e97731e3d368e3f6 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Tue, 14 Sep 2021 18:31:31 -0700 Subject: [PATCH 17/38] remove relative mport from update_dataset_access --- samples/snippets/update_dataset_access_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/update_dataset_access_test.py b/samples/snippets/update_dataset_access_test.py index 8fd8ba61f..a5addafc5 100644 --- a/samples/snippets/update_dataset_access_test.py +++ b/samples/snippets/update_dataset_access_test.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import update_dataset_access +import update_dataset_access def test_update_dataset_access(capsys, dataset_id): From 32d9b71b05cc7f3de2fee17b99ce168f157bf7de Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Wed, 15 Sep 2021 15:34:50 -0700 Subject: [PATCH 18/38] adding fixture to conftest.py --- samples/snippets/conftest.py | 8 ++++++++ samples/snippets/revoke_dataset_access_test.py | 6 +----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/samples/snippets/conftest.py b/samples/snippets/conftest.py index 74984f902..405d05614 100644 --- a/samples/snippets/conftest.py +++ b/samples/snippets/conftest.py @@ -50,6 +50,14 @@ def dataset_id(bigquery_client: bigquery.Client, project_id: str): bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True) +@pytest.fixture(scope="session") +def entity_id(bigquery_client: bigquery.Client, dataset_id: dataset_id): + dataset = bigquery_client.get_dataset(dataset_id) + entries = list(dataset.access_entries) + yield entries[0].entity_id + bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True) + + @pytest.fixture(scope="session") def dataset_id_us_east1(bigquery_client: bigquery.Client, project_id: str): dataset_id = prefixer.create_prefix() diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/revoke_dataset_access_test.py index df5a0423e..2ef956efb 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/revoke_dataset_access_test.py @@ -12,15 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from google.cloud import bigquery - import revoke_dataset_access import update_dataset_access -def test_revoke_dataset_access( - capsys, dataset_id, entity_id, bigquery_client: bigquery.Client -): +def test_revoke_dataset_access(capsys, dataset_id, entity_id, bigquery_client): update_dataset_access.update_dataset_access(dataset_id) updated_dataset = bigquery_client.get_dataset(dataset_id) updated_dataset_entries = list(updated_dataset.access_entries) From fbcc09f635633e616907798abd8ee42a061abf69 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Thu, 18 Nov 2021 14:01:55 -0800 Subject: [PATCH 19/38] updated sample --- samples/snippets/revoke_dataset_access.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 84b537c4c..c83cd7eff 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -36,8 +36,8 @@ def revoke_dataset_access(dataset_id, entity_id): dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) - dataset.access_entries = entries.filter( - lambda entry: entry.entity_id != entity_id, entries + dataset.access_entries = list( + entries.filter(lambda entry: entry.entity_id != entity_id, entries) ) dataset = client.update_dataset( From 4234450e4cfde21532e273fadde8adb83ba49826 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 19 Nov 2021 15:10:57 -0800 Subject: [PATCH 20/38] updating sample to match new update_access sample --- samples/snippets/conftest.py | 5 +--- samples/snippets/revoke_dataset_access.py | 24 +++++++++++-------- samples/snippets/update_dataset_access.py | 4 ++-- .../snippets/update_dataset_access_test.py | 2 +- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/samples/snippets/conftest.py b/samples/snippets/conftest.py index 405d05614..1794459a7 100644 --- a/samples/snippets/conftest.py +++ b/samples/snippets/conftest.py @@ -52,10 +52,7 @@ def dataset_id(bigquery_client: bigquery.Client, project_id: str): @pytest.fixture(scope="session") def entity_id(bigquery_client: bigquery.Client, dataset_id: dataset_id): - dataset = bigquery_client.get_dataset(dataset_id) - entries = list(dataset.access_entries) - yield entries[0].entity_id - bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True) + return "cloud-developer-relations@google.com" @pytest.fixture(scope="session") diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index c83cd7eff..db528d812 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -14,19 +14,14 @@ def revoke_dataset_access(dataset_id, entity_id): - original_dataset_id = dataset_id - original_entity_id = entity_id - # [START bigquery_revoke_dataset_access] - # TODO(developer): Set the dataset ID to the dataset where you are revoking access. - dataset_id = "your_dataset_id" - # TODO(developer): Set the entity ID of the email or group from whom you are revoking access. - entity_id = "user-or-group-to-remove@example.com" + # TODO(developer): Set dataset_id to the ID of the dataset to fetch. + # dataset_id = 'your-project.your_dataset' - # [END bigquery_revoke_dataset_access] - dataset_id = original_dataset_id - entity_id = original_entity_id + # TODO(developer): Set entity_id to the ID of the email or group from whom you are revoking access. + # entity_id = "user-or-group-to-remove@example.com" + # [END bigquery_revoke_dataset_access] # [START bigquery_revoke_dataset_access] from google.cloud import bigquery @@ -34,7 +29,16 @@ def revoke_dataset_access(dataset_id, entity_id): client = bigquery.Client() dataset = client.get_dataset(dataset_id) # Make an API request. + + entry = bigquery.AccessEntry( + role="READER", + entity_type="groupByEmail", + entity_id="cloud-developer-relations@google.com", + ) + entries = list(dataset.access_entries) + entries.append(entry) + dataset.access_entries = entries dataset.access_entries = list( entries.filter(lambda entry: entry.entity_id != entity_id, entries) diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py index 6e844cc90..a5c2670e7 100644 --- a/samples/snippets/update_dataset_access.py +++ b/samples/snippets/update_dataset_access.py @@ -28,8 +28,8 @@ def update_dataset_access(dataset_id): entry = bigquery.AccessEntry( role="READER", - entity_type="userByEmail", - entity_id="sample.bigquery.dev@gmail.com", + entity_type="groupByEmail", + entity_id="cloud-developer-relations@google.com", ) entries = list(dataset.access_entries) diff --git a/samples/snippets/update_dataset_access_test.py b/samples/snippets/update_dataset_access_test.py index a5addafc5..4c0aa835b 100644 --- a/samples/snippets/update_dataset_access_test.py +++ b/samples/snippets/update_dataset_access_test.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import update_dataset_access +from .. import update_dataset_access def test_update_dataset_access(capsys, dataset_id): From 2ea0f4e658fbaa39c8c0780c6c64d85fef48fa22 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 19 Nov 2021 15:19:09 -0800 Subject: [PATCH 21/38] fixing region tags --- samples/snippets/revoke_dataset_access.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index db528d812..77bd17030 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -14,6 +14,7 @@ def revoke_dataset_access(dataset_id, entity_id): + # [START bigquery_revoke_dataset_access] # TODO(developer): Set dataset_id to the ID of the dataset to fetch. # dataset_id = 'your-project.your_dataset' @@ -21,8 +22,6 @@ def revoke_dataset_access(dataset_id, entity_id): # TODO(developer): Set entity_id to the ID of the email or group from whom you are revoking access. # entity_id = "user-or-group-to-remove@example.com" - # [END bigquery_revoke_dataset_access] - # [START bigquery_revoke_dataset_access] from google.cloud import bigquery # Construct a BigQuery client object. From 6bbdc33285686134676696b23b18deab93c27dc0 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 19 Nov 2021 15:23:04 -0800 Subject: [PATCH 22/38] consolidated tests into one file for both methods --- ....py => dataset_access_permissions_test.py} | 8 ++++++- .../snippets/update_dataset_access_test.py | 24 ------------------- 2 files changed, 7 insertions(+), 25 deletions(-) rename samples/snippets/{revoke_dataset_access_test.py => dataset_access_permissions_test.py} (86%) delete mode 100644 samples/snippets/update_dataset_access_test.py diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/dataset_access_permissions_test.py similarity index 86% rename from samples/snippets/revoke_dataset_access_test.py rename to samples/snippets/dataset_access_permissions_test.py index 2ef956efb..19fcca8b9 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/dataset_access_permissions_test.py @@ -16,8 +16,14 @@ import update_dataset_access -def test_revoke_dataset_access(capsys, dataset_id, entity_id, bigquery_client): +def test_dataset_access_permissions(capsys, dataset_id, entity_id, bigquery_client): + update_dataset_access.update_dataset_access(dataset_id) + out, err = capsys.readouterr() + assert ( + "Updated dataset '{}' with modified user permissions.".format(dataset_id) in out + ) + updated_dataset = bigquery_client.get_dataset(dataset_id) updated_dataset_entries = list(updated_dataset.access_entries) revoke_dataset_access.revoke_dataset_access(dataset_id, entity_id) diff --git a/samples/snippets/update_dataset_access_test.py b/samples/snippets/update_dataset_access_test.py deleted file mode 100644 index 4c0aa835b..000000000 --- a/samples/snippets/update_dataset_access_test.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2019 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 -# -# https://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. - -from .. import update_dataset_access - - -def test_update_dataset_access(capsys, dataset_id): - - update_dataset_access.update_dataset_access(dataset_id) - out, err = capsys.readouterr() - assert ( - "Updated dataset '{}' with modified user permissions.".format(dataset_id) in out - ) From 575260afdb0cc7b4113f591a4a5489e073c1872f Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 19 Nov 2021 15:39:28 -0800 Subject: [PATCH 23/38] updating test to full_dataset format --- samples/snippets/dataset_access_permissions_test.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/samples/snippets/dataset_access_permissions_test.py b/samples/snippets/dataset_access_permissions_test.py index 19fcca8b9..caaae26f6 100644 --- a/samples/snippets/dataset_access_permissions_test.py +++ b/samples/snippets/dataset_access_permissions_test.py @@ -17,11 +17,16 @@ def test_dataset_access_permissions(capsys, dataset_id, entity_id, bigquery_client): - + original_dataset = bigquery_client.get_dataset(dataset_id) update_dataset_access.update_dataset_access(dataset_id) + full_dataset_id = "{}.{}".format( + original_dataset.project, original_dataset.dataset_id + ) + out, err = capsys.readouterr() assert ( - "Updated dataset '{}' with modified user permissions.".format(dataset_id) in out + "Updated dataset '{}' with modified user permissions.".format(full_dataset_id) + in out ) updated_dataset = bigquery_client.get_dataset(dataset_id) From 27d817000370c0f9fa0c039c6df53589ab1c7fd3 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 19 Nov 2021 16:03:30 -0800 Subject: [PATCH 24/38] updated revoke sample --- samples/snippets/revoke_dataset_access.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 77bd17030..7d51263bb 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -39,9 +39,8 @@ def revoke_dataset_access(dataset_id, entity_id): entries.append(entry) dataset.access_entries = entries - dataset.access_entries = list( - entries.filter(lambda entry: entry.entity_id != entity_id, entries) - ) + entries_remaining = filter(lambda entry: entry.entity_id != entity_id, entries) + dataset.access_entries = list(entries_remaining) dataset = client.update_dataset( dataset, From adc1c761dcbbcb411905f4d5e8ea873c71b4bdbc Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Fri, 19 Nov 2021 16:14:41 -0800 Subject: [PATCH 25/38] updating test --- samples/snippets/dataset_access_permissions_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/dataset_access_permissions_test.py b/samples/snippets/dataset_access_permissions_test.py index caaae26f6..6dd83959e 100644 --- a/samples/snippets/dataset_access_permissions_test.py +++ b/samples/snippets/dataset_access_permissions_test.py @@ -43,7 +43,7 @@ def test_dataset_access_permissions(capsys, dataset_id, entity_id, bigquery_clie ) assert len(revoked_dataset_entries) == len(updated_dataset_entries) - 1 is_revoked = 0 - for entry in revoke_dataset_access: + for entry in revoked_dataset_entries: if entry.entity_id == entity_id: is_revoked += 1 assert is_revoked == 0 From 62ec25e4cbabda3483a8e9cc17f3e8fe3692d211 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Mon, 22 Nov 2021 11:09:51 -0800 Subject: [PATCH 26/38] refactored sample --- samples/snippets/revoke_dataset_access.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 7d51263bb..4c1e64708 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -39,8 +39,9 @@ def revoke_dataset_access(dataset_id, entity_id): entries.append(entry) dataset.access_entries = entries - entries_remaining = filter(lambda entry: entry.entity_id != entity_id, entries) - dataset.access_entries = list(entries_remaining) + dataset.access_entries = list( + filter(lambda entry: entry.entity_id != entity_id, entries) + ) dataset = client.update_dataset( dataset, From 072b785fce6c3d77252a65d8c69df7f12183a1d1 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 23 Nov 2021 10:34:20 -0600 Subject: [PATCH 27/38] Update samples/snippets/conftest.py --- samples/snippets/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/conftest.py b/samples/snippets/conftest.py index 1794459a7..e8aa08487 100644 --- a/samples/snippets/conftest.py +++ b/samples/snippets/conftest.py @@ -51,7 +51,7 @@ def dataset_id(bigquery_client: bigquery.Client, project_id: str): @pytest.fixture(scope="session") -def entity_id(bigquery_client: bigquery.Client, dataset_id: dataset_id): +def entity_id(bigquery_client: bigquery.Client, dataset_id: str): return "cloud-developer-relations@google.com" From dade3b8615ecb399beddc48d6230a0367b50af98 Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Tue, 23 Nov 2021 09:49:23 -0800 Subject: [PATCH 28/38] Update samples/snippets/revoke_dataset_access.py Co-authored-by: Tim Swast --- samples/snippets/revoke_dataset_access.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 4c1e64708..fd416e3db 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -14,13 +14,20 @@ def revoke_dataset_access(dataset_id, entity_id): + original_dataset_id = dataset_id + original_entity_id = entity_id + # [START bigquery_revoke_dataset_access] # TODO(developer): Set dataset_id to the ID of the dataset to fetch. - # dataset_id = 'your-project.your_dataset' + dataset_id = "your-project.your_dataset" # TODO(developer): Set entity_id to the ID of the email or group from whom you are revoking access. - # entity_id = "user-or-group-to-remove@example.com" + entity_id = "user-or-group-to-remove@example.com" + # [END bigquery_revoke_dataset_access] + dataset_id = original_dataset_id + entity_id = original_entity_id + # [START bigquery_revoke_dataset_access] from google.cloud import bigquery From c74022e1dabcac782180efc0b4eae467186925d1 Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Tue, 23 Nov 2021 09:49:40 -0800 Subject: [PATCH 29/38] Update samples/snippets/update_dataset_access.py Co-authored-by: Tim Swast --- samples/snippets/update_dataset_access.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py index a5c2670e7..8d588595f 100644 --- a/samples/snippets/update_dataset_access.py +++ b/samples/snippets/update_dataset_access.py @@ -13,7 +13,7 @@ # limitations under the License. -def update_dataset_access(dataset_id): +def update_dataset_access(dataset_id: str): # [START bigquery_update_dataset_access] from google.cloud import bigquery From 9da6de6f43d1711356339ee7d42b4289a60c6a46 Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Tue, 23 Nov 2021 09:50:01 -0800 Subject: [PATCH 30/38] Update samples/snippets/revoke_dataset_access.py Co-authored-by: Tim Swast --- samples/snippets/revoke_dataset_access.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index fd416e3db..de750a380 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -13,7 +13,7 @@ # limitations under the License. -def revoke_dataset_access(dataset_id, entity_id): +def revoke_dataset_access(dataset_id: str, entity_id: str): original_dataset_id = dataset_id original_entity_id = entity_id From ede91580782e4b391b0213816043afc9eff1573e Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Tue, 23 Nov 2021 09:50:27 -0800 Subject: [PATCH 31/38] Update samples/snippets/revoke_dataset_access.py Co-authored-by: Tim Swast --- samples/snippets/revoke_dataset_access.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index de750a380..f064494c8 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -46,9 +46,9 @@ def revoke_dataset_access(dataset_id: str, entity_id: str): entries.append(entry) dataset.access_entries = entries - dataset.access_entries = list( - filter(lambda entry: entry.entity_id != entity_id, entries) - ) + dataset.access_entries = [ + entry for entry in entries if entry.entity_id != entity_id + ] dataset = client.update_dataset( dataset, From 05a4f19d4a5e8b0bc18f535bbcabbb523ad14f4e Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Tue, 23 Nov 2021 16:43:48 -0800 Subject: [PATCH 32/38] refactoring entry --- samples/snippets/README.rst | 2 +- ...rmissions_test.py => dataset_access_test.py} | 2 +- samples/snippets/revoke_dataset_access.py | 17 +++++------------ samples/snippets/update_dataset_access.py | 7 +++---- 4 files changed, 10 insertions(+), 18 deletions(-) rename samples/snippets/{dataset_access_permissions_test.py => dataset_access_test.py} (96%) diff --git a/samples/snippets/README.rst b/samples/snippets/README.rst index 9446125cd..0837c2967 100644 --- a/samples/snippets/README.rst +++ b/samples/snippets/README.rst @@ -47,7 +47,7 @@ Install Dependencies .. _Python Development Environment Setup Guide: https://cloud.google.com/python/setup -#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. +#. Create a virtualenv. Samples are compatible with Python 3.6+. .. code-block:: bash diff --git a/samples/snippets/dataset_access_permissions_test.py b/samples/snippets/dataset_access_test.py similarity index 96% rename from samples/snippets/dataset_access_permissions_test.py rename to samples/snippets/dataset_access_test.py index 6dd83959e..1eefb7173 100644 --- a/samples/snippets/dataset_access_permissions_test.py +++ b/samples/snippets/dataset_access_test.py @@ -18,7 +18,7 @@ def test_dataset_access_permissions(capsys, dataset_id, entity_id, bigquery_client): original_dataset = bigquery_client.get_dataset(dataset_id) - update_dataset_access.update_dataset_access(dataset_id) + update_dataset_access.update_dataset_access(dataset_id, entity_id) full_dataset_id = "{}.{}".format( original_dataset.project, original_dataset.dataset_id ) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index f064494c8..de0e6ae99 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -16,7 +16,7 @@ def revoke_dataset_access(dataset_id: str, entity_id: str): original_dataset_id = dataset_id original_entity_id = entity_id - + # [START bigquery_revoke_dataset_access] # TODO(developer): Set dataset_id to the ID of the dataset to fetch. @@ -24,10 +24,10 @@ def revoke_dataset_access(dataset_id: str, entity_id: str): # TODO(developer): Set entity_id to the ID of the email or group from whom you are revoking access. entity_id = "user-or-group-to-remove@example.com" - # [END bigquery_revoke_dataset_access] - dataset_id = original_dataset_id - entity_id = original_entity_id - # [START bigquery_revoke_dataset_access] + # [END bigquery_revoke_dataset_access] + dataset_id = original_dataset_id + entity_id = original_entity_id + # [START bigquery_revoke_dataset_access] from google.cloud import bigquery @@ -36,14 +36,7 @@ def revoke_dataset_access(dataset_id: str, entity_id: str): dataset = client.get_dataset(dataset_id) # Make an API request. - entry = bigquery.AccessEntry( - role="READER", - entity_type="groupByEmail", - entity_id="cloud-developer-relations@google.com", - ) - entries = list(dataset.access_entries) - entries.append(entry) dataset.access_entries = entries dataset.access_entries = [ diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py index 8d588595f..b1353cb04 100644 --- a/samples/snippets/update_dataset_access.py +++ b/samples/snippets/update_dataset_access.py @@ -13,7 +13,7 @@ # limitations under the License. -def update_dataset_access(dataset_id: str): +def update_dataset_access(dataset_id: str, entity_id: str): # [START bigquery_update_dataset_access] from google.cloud import bigquery @@ -23,13 +23,12 @@ def update_dataset_access(dataset_id: str): # TODO(developer): Set dataset_id to the ID of the dataset to fetch. # dataset_id = 'your-project.your_dataset' + # entity_id = 'your-email@email.com" dataset = client.get_dataset(dataset_id) # Make an API request. entry = bigquery.AccessEntry( - role="READER", - entity_type="groupByEmail", - entity_id="cloud-developer-relations@google.com", + role="READER", entity_type="groupByEmail", entity_id=entity_id, ) entries = list(dataset.access_entries) From 6c04f287e222eb191eba2ee910fd9f978e74d469 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Tue, 23 Nov 2021 17:03:02 -0800 Subject: [PATCH 33/38] added comment for entry access --- samples/snippets/update_dataset_access.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py index b1353cb04..443b1aef0 100644 --- a/samples/snippets/update_dataset_access.py +++ b/samples/snippets/update_dataset_access.py @@ -27,6 +27,7 @@ def update_dataset_access(dataset_id: str, entity_id: str): dataset = client.get_dataset(dataset_id) # Make an API request. + # This is an example entry that will have "reader" dataset access. For other entry options, see https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.dataset.AccessEntry.html?highlight=accessentry#google.cloud.bigquery.dataset.AccessEntry entry = bigquery.AccessEntry( role="READER", entity_type="groupByEmail", entity_id=entity_id, ) From 023eb96fac517c0a03d51e168817d74eed7ed036 Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Mon, 29 Nov 2021 11:34:25 -0800 Subject: [PATCH 34/38] Update samples/snippets/README.rst Co-authored-by: Tim Swast --- samples/snippets/README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/README.rst b/samples/snippets/README.rst index 0837c2967..05af1e812 100644 --- a/samples/snippets/README.rst +++ b/samples/snippets/README.rst @@ -15,7 +15,7 @@ This directory contains samples for Google BigQuery. `Google BigQuery`_ is Googl .. _Google BigQuery: https://cloud.google.com/bigquery/docs -To run the sample, you need to have `BigQuery Admin` role. +To run the sample, you need to have the `BigQuery Admin` role. From 6d9f2747458ebafc7e7badd0cc094430c94ca201 Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Mon, 29 Nov 2021 11:35:15 -0800 Subject: [PATCH 35/38] Update samples/snippets/dataset_access_test.py Co-authored-by: Tim Swast --- samples/snippets/dataset_access_test.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/samples/snippets/dataset_access_test.py b/samples/snippets/dataset_access_test.py index 1eefb7173..a98bb96de 100644 --- a/samples/snippets/dataset_access_test.py +++ b/samples/snippets/dataset_access_test.py @@ -42,8 +42,5 @@ def test_dataset_access_permissions(capsys, dataset_id, entity_id, bigquery_clie in out ) assert len(revoked_dataset_entries) == len(updated_dataset_entries) - 1 - is_revoked = 0 - for entry in revoked_dataset_entries: - if entry.entity_id == entity_id: - is_revoked += 1 - assert is_revoked == 0 + revoked_dataset_entity_ids = {entry.entity_id for entry in revoked_dataset_entries} + assert entity_id not in revoked_dataset_entity_ids From 3fd6856de98e06aeaa9f07abecb3eb73d1a8ad9e Mon Sep 17 00:00:00 2001 From: Lo Ferris <50979514+loferris@users.noreply.github.com> Date: Mon, 29 Nov 2021 11:35:43 -0800 Subject: [PATCH 36/38] Update samples/snippets/dataset_access_test.py Co-authored-by: Tim Swast --- samples/snippets/dataset_access_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/snippets/dataset_access_test.py b/samples/snippets/dataset_access_test.py index a98bb96de..21776c149 100644 --- a/samples/snippets/dataset_access_test.py +++ b/samples/snippets/dataset_access_test.py @@ -31,6 +31,8 @@ def test_dataset_access_permissions(capsys, dataset_id, entity_id, bigquery_clie updated_dataset = bigquery_client.get_dataset(dataset_id) updated_dataset_entries = list(updated_dataset.access_entries) + updated_dataset_entity_ids = {entry.entity_id for entry in updated_dataset_entries} + assert entity_id in updated_dataset_entity_ids revoke_dataset_access.revoke_dataset_access(dataset_id, entity_id) revoked_dataset = bigquery_client.get_dataset(dataset_id) revoked_dataset_entries = list(revoked_dataset.access_entries) From 6ce391d5be0d61148b82177c9d10c201fb1a4267 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Mon, 29 Nov 2021 11:39:59 -0800 Subject: [PATCH 37/38] added develper TODO in sample --- samples/snippets/update_dataset_access.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py index 443b1aef0..9c5e0ee66 100644 --- a/samples/snippets/update_dataset_access.py +++ b/samples/snippets/update_dataset_access.py @@ -23,6 +23,7 @@ def update_dataset_access(dataset_id: str, entity_id: str): # TODO(developer): Set dataset_id to the ID of the dataset to fetch. # dataset_id = 'your-project.your_dataset' + # TODO(developer): Set entity_id to the ID of the entry to update. # entity_id = 'your-email@email.com" dataset = client.get_dataset(dataset_id) # Make an API request. From 04497a157f0811982fb29cb717eb924d064bd70e Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 6 Dec 2021 10:51:10 -0600 Subject: [PATCH 38/38] add comments to samples --- samples/snippets/revoke_dataset_access.py | 2 - samples/snippets/update_dataset_access.py | 46 +++++++++++++++++------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index de0e6ae99..ce78f5750 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -37,8 +37,6 @@ def revoke_dataset_access(dataset_id: str, entity_id: str): dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) - dataset.access_entries = entries - dataset.access_entries = [ entry for entry in entries if entry.entity_id != entity_id ] diff --git a/samples/snippets/update_dataset_access.py b/samples/snippets/update_dataset_access.py index 9c5e0ee66..fb3bfa14f 100644 --- a/samples/snippets/update_dataset_access.py +++ b/samples/snippets/update_dataset_access.py @@ -14,27 +14,51 @@ def update_dataset_access(dataset_id: str, entity_id: str): + original_dataset_id = dataset_id + original_entity_id = entity_id # [START bigquery_update_dataset_access] + + # TODO(developer): Set dataset_id to the ID of the dataset to fetch. + dataset_id = "your-project.your_dataset" + + # TODO(developer): Set entity_id to the ID of the email or group from whom + # you are adding access. Alternatively, to the JSON REST API representation + # of the entity, such as a view's table reference. + entity_id = "user-or-group-to-add@example.com" + + # TODO(developer): Set entity_type to the type of entity you are granting access to. + # Common types include: + # + # * "userByEmail" -- A single user or service account. For example "fred@example.com" + # * "groupByEmail" -- A group of users. For example "example@googlegroups.com" + # * "view" -- An authorized view. For example + # {"projectId": "p", "datasetId": "d", "tableId": "v"} + # + # For a complete reference, see the REST API reference documentation: + # https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#Dataset.FIELDS.access + entity_type = "groupByEmail" + + # TODO(developer): Set role to a one of the "Basic roles for datasets" + # described here: + # https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles + role = "READER" + # [END bigquery_update_dataset_access] + dataset_id = original_dataset_id + entity_id = original_entity_id + # [START bigquery_update_dataset_access] + from google.cloud import bigquery # Construct a BigQuery client object. client = bigquery.Client() - # TODO(developer): Set dataset_id to the ID of the dataset to fetch. - # dataset_id = 'your-project.your_dataset' - # TODO(developer): Set entity_id to the ID of the entry to update. - # entity_id = 'your-email@email.com" - dataset = client.get_dataset(dataset_id) # Make an API request. - # This is an example entry that will have "reader" dataset access. For other entry options, see https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.dataset.AccessEntry.html?highlight=accessentry#google.cloud.bigquery.dataset.AccessEntry - entry = bigquery.AccessEntry( - role="READER", entity_type="groupByEmail", entity_id=entity_id, - ) - entries = list(dataset.access_entries) - entries.append(entry) + entries.append( + bigquery.AccessEntry(role=role, entity_type=entity_type, entity_id=entity_id,) + ) dataset.access_entries = entries dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request.