-
Notifications
You must be signed in to change notification settings - Fork 322
docs: add sample for revoking dataset access #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
0abac4c
revoke dataset access setup
d699bec
basic template for sample
98b245d
sample + test
f1adee7
revoke dataset access sample
e729bbd
🦉 Updates from OwlBot
gcf-owl-bot[bot] b80c4bf
docs: add sample for revoking dataset access - update year and string…
2960b48
Merge branch 'bigquery_remove_view_access' of github.com:googleapis/p…
02bd2b4
docs: add sample for revoking dataset access - move to snippets and c…
fa0e48c
moving update_dataset to /snippets and adjusting imports on both revo…
98bb4a2
Update samples/snippets/revoke_dataset_access.py
loferris 8c4bc7a
Update samples/snippets/revoke_dataset_access.py
loferris 5197d0f
🦉 Updates from OwlBot
gcf-owl-bot[bot] cfb3805
updated test
4be9760
🦉 Updates from OwlBot
gcf-owl-bot[bot] 0a57eba
Merge branch 'main' of github.com:googleapis/python-bigquery into big…
d4672f2
Merge branch 'main' into bigquery_remove_view_access
loferris b9e35aa
change after running test
b076972
Merge branch 'bigquery_remove_view_access' of github.com:googleapis/p…
4ec3512
Merge branch 'main' into bigquery_remove_view_access
tswast 15d84b1
Merge branch 'main' into bigquery_remove_view_access
loferris 2209d8e
resolving linting failure, rewriting test
0da2301
removed relative import errors
77066a5
remove relative mport from update_dataset_access
32d9b71
adding fixture to conftest.py
b7e91eb
Merge branch 'main' into bigquery_remove_view_access
plamut 318242d
Merge branch 'main' into bigquery_remove_view_access
tswast 5fe08d0
Merge branch 'main' into bigquery_remove_view_access
parthea 1c4469b
Merge branch 'main' into bigquery_remove_view_access
meredithslota fe66df2
Merge branch 'main' into bigquery_remove_view_access
loferris fbcc09f
updated sample
4234450
updating sample to match new update_access sample
2b82c45
Merge branch 'main' into bigquery_remove_view_access
loferris 2ea0f4e
fixing region tags
6bbdc33
consolidated tests into one file for both methods
a493adc
Merge branch 'bigquery_remove_view_access' of github.com:googleapis/p…
575260a
updating test to full_dataset format
27d8170
updated revoke sample
adc1c76
updating test
62ec25e
refactored sample
072b785
Update samples/snippets/conftest.py
tswast dade3b8
Update samples/snippets/revoke_dataset_access.py
loferris c74022e
Update samples/snippets/update_dataset_access.py
loferris 9da6de6
Update samples/snippets/revoke_dataset_access.py
loferris ede9158
Update samples/snippets/revoke_dataset_access.py
loferris 05a4f19
refactoring entry
6c04f28
added comment for entry access
d030e21
Merge branch 'main' into bigquery_remove_view_access
tswast 023eb96
Update samples/snippets/README.rst
loferris 6d9f274
Update samples/snippets/dataset_access_test.py
loferris 3fd6856
Update samples/snippets/dataset_access_test.py
loferris 6ce391d
added develper TODO in sample
04497a1
add comments to samples
tswast 2e98eca
Merge branch 'main' into bigquery_remove_view_access
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # 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. | ||
|
|
||
| import revoke_dataset_access | ||
| import update_dataset_access | ||
|
|
||
|
|
||
| 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, entity_id) | ||
| full_dataset_id = "{}.{}".format( | ||
| original_dataset.project, original_dataset.dataset_id | ||
| ) | ||
|
|
||
| out, err = capsys.readouterr() | ||
| assert ( | ||
| "Updated dataset '{}' with modified user permissions.".format(full_dataset_id) | ||
| in out | ||
| ) | ||
|
|
||
| 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) | ||
|
|
||
| full_dataset_id = f"{updated_dataset.project}.{updated_dataset.dataset_id}" | ||
| out, err = capsys.readouterr() | ||
| 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 | ||
| revoked_dataset_entity_ids = {entry.entity_id for entry in revoked_dataset_entries} | ||
| assert entity_id not in revoked_dataset_entity_ids | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # 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(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. | ||
| 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" | ||
| # [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 | ||
|
|
||
| # Construct a BigQuery client object. | ||
| client = bigquery.Client() | ||
|
|
||
| dataset = client.get_dataset(dataset_id) # Make an API request. | ||
|
|
||
| entries = list(dataset.access_entries) | ||
| dataset.access_entries = [ | ||
| entry for entry in entries if entry.entity_id != entity_id | ||
| ] | ||
|
|
||
| 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"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'") | ||
| # [END bigquery_revoke_dataset_access] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # 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: 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() | ||
|
|
||
| dataset = client.get_dataset(dataset_id) # Make an API request. | ||
|
|
||
| entries = list(dataset.access_entries) | ||
| 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. | ||
|
|
||
| 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] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.