Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0abac4c
revoke dataset access setup
Jul 1, 2021
d699bec
basic template for sample
Jul 2, 2021
98b245d
sample + test
Jul 7, 2021
f1adee7
revoke dataset access sample
Jul 16, 2021
e729bbd
🦉 Updates from OwlBot
gcf-owl-bot[bot] Jul 16, 2021
b80c4bf
docs: add sample for revoking dataset access - update year and string…
Jul 23, 2021
2960b48
Merge branch 'bigquery_remove_view_access' of github.com:googleapis/p…
Jul 23, 2021
02bd2b4
docs: add sample for revoking dataset access - move to snippets and c…
Jul 23, 2021
fa0e48c
moving update_dataset to /snippets and adjusting imports on both revo…
Jul 27, 2021
98bb4a2
Update samples/snippets/revoke_dataset_access.py
loferris Jul 27, 2021
8c4bc7a
Update samples/snippets/revoke_dataset_access.py
loferris Jul 27, 2021
5197d0f
🦉 Updates from OwlBot
gcf-owl-bot[bot] Jul 27, 2021
cfb3805
updated test
Jul 29, 2021
4be9760
🦉 Updates from OwlBot
gcf-owl-bot[bot] Jul 29, 2021
0a57eba
Merge branch 'main' of github.com:googleapis/python-bigquery into big…
Aug 27, 2021
d4672f2
Merge branch 'main' into bigquery_remove_view_access
loferris Aug 30, 2021
b9e35aa
change after running test
Aug 30, 2021
b076972
Merge branch 'bigquery_remove_view_access' of github.com:googleapis/p…
Aug 30, 2021
4ec3512
Merge branch 'main' into bigquery_remove_view_access
tswast Sep 10, 2021
15d84b1
Merge branch 'main' into bigquery_remove_view_access
loferris Sep 11, 2021
2209d8e
resolving linting failure, rewriting test
Sep 11, 2021
0da2301
removed relative import errors
Sep 15, 2021
77066a5
remove relative mport from update_dataset_access
Sep 15, 2021
32d9b71
adding fixture to conftest.py
Sep 15, 2021
b7e91eb
Merge branch 'main' into bigquery_remove_view_access
plamut Sep 17, 2021
318242d
Merge branch 'main' into bigquery_remove_view_access
tswast Oct 6, 2021
5fe08d0
Merge branch 'main' into bigquery_remove_view_access
parthea Oct 6, 2021
1c4469b
Merge branch 'main' into bigquery_remove_view_access
meredithslota Oct 13, 2021
fe66df2
Merge branch 'main' into bigquery_remove_view_access
loferris Nov 18, 2021
fbcc09f
updated sample
Nov 18, 2021
4234450
updating sample to match new update_access sample
Nov 19, 2021
2b82c45
Merge branch 'main' into bigquery_remove_view_access
loferris Nov 19, 2021
2ea0f4e
fixing region tags
Nov 19, 2021
6bbdc33
consolidated tests into one file for both methods
Nov 19, 2021
a493adc
Merge branch 'bigquery_remove_view_access' of github.com:googleapis/p…
Nov 19, 2021
575260a
updating test to full_dataset format
Nov 19, 2021
27d8170
updated revoke sample
Nov 20, 2021
adc1c76
updating test
Nov 20, 2021
62ec25e
refactored sample
Nov 22, 2021
072b785
Update samples/snippets/conftest.py
tswast Nov 23, 2021
dade3b8
Update samples/snippets/revoke_dataset_access.py
loferris Nov 23, 2021
c74022e
Update samples/snippets/update_dataset_access.py
loferris Nov 23, 2021
9da6de6
Update samples/snippets/revoke_dataset_access.py
loferris Nov 23, 2021
ede9158
Update samples/snippets/revoke_dataset_access.py
loferris Nov 23, 2021
05a4f19
refactoring entry
Nov 24, 2021
6c04f28
added comment for entry access
Nov 24, 2021
d030e21
Merge branch 'main' into bigquery_remove_view_access
tswast Nov 29, 2021
023eb96
Update samples/snippets/README.rst
loferris Nov 29, 2021
6d9f274
Update samples/snippets/dataset_access_test.py
loferris Nov 29, 2021
3fd6856
Update samples/snippets/dataset_access_test.py
loferris Nov 29, 2021
6ce391d
added develper TODO in sample
Nov 29, 2021
04497a1
add comments to samples
tswast Dec 6, 2021
2e98eca
Merge branch 'main' into bigquery_remove_view_access
tswast Dec 6, 2021
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
50 changes: 50 additions & 0 deletions samples/snippets/revoke_dataset_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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.
# 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 revoke_dataset_access(your_dataset_id, your_entity_id):

# [START bigquery_revoke_dataset_access]
from google.cloud import bigquery

# 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
Comment thread
loferris marked this conversation as resolved.
Outdated

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should we look at entity_type, too? Maybe not? I suppose there shouldn't be much conflict between whether an email address is associated with a group, service account, or user.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once I get the tests updated I'll play around with it.

entry.role = None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious to see if this works as expected. The alternative is to remove the whole entry from the list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to remove it...

break

dataset.access_entries = entries

dataset = client.update_dataset(dataset, ["access_entries"]) # Make an API request.
Comment thread
loferris marked this conversation as resolved.
Outdated

full_dataset_id = f"{dataset.project}.{dataset.dataset_id}"
print(f"Updated dataset '{full_dataset_id}' with modified user permissions.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including the word "revoked" and possibly the entity ID here could give us a better idea that this actually worked.

# [END bigquery_revoke_dataset_access]
25 changes: 25 additions & 0 deletions samples/snippets/revoke_dataset_access_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.
# 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the comment, adding samples/snippets/__init__.py should fix this, but it'll break the other sample tests. You'll need to also update the tests in samples/snippets/ to use relative imports.

Alternatively, we can start making progress on migrating the snippets to samples/snippets (#790). You could make a copy of update_dataset_access.py and it's corresponding tests and put them in the samples/snippets directory in this PR. Then once you've pointed cloud.google.com at the new sample location, send a PR to remove the duplicate sample.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the latter approach!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in latest commit!

from .. import revoke_dataset_access
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be from . import revoke_dataset_access since it's the same directory as the test. (Also will need __init__.py for that to work.) If you go with the "copy" alternative (without __init__.py), then this should be 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 f"Updated dataset '{dataset_id}' with modified user permissions." in out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you look for a message specific to "revoke"? I worry that this could catch the original update and the revoke could be failing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, let's add some additional steps to call get_dataset and check that the dataset access was actually updated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you recommend the pytest fixture pattern to create a bigquery client that I can call this on? I'm seeing it in some other snippets but not the update_access sample. I also need to move get_dataset to /snippets as well!