-
Notifications
You must be signed in to change notification settings - Fork 324
docs: add sample for revoking dataset access #778
Changes from 8 commits
0abac4c
d699bec
98b245d
f1adee7
e729bbd
b80c4bf
2960b48
02bd2b4
fa0e48c
98bb4a2
8c4bc7a
5197d0f
cfb3805
4be9760
0a57eba
d4672f2
b9e35aa
b076972
4ec3512
15d84b1
2209d8e
0da2301
77066a5
32d9b71
b7e91eb
318242d
5fe08d0
1c4469b
fe66df2
fbcc09f
4234450
2b82c45
2ea0f4e
6bbdc33
a493adc
575260a
27d8170
adc1c76
62ec25e
072b785
dade3b8
c74022e
9da6de6
ede9158
05a4f19
6c04f28
d030e21
023eb96
6d9f274
3fd6856
6ce391d
04497a1
2e98eca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we should we look at
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
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.") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the comment, adding Alternatively, we can start making progress on migrating the snippets to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the latter approach!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in latest commit! |
||
| from .. import revoke_dataset_access | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
|
|
||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, let's add some additional steps to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
Uh oh!
There was an error while loading. Please reload this page.