-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Added code samples for stored infoTypes. #1934
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ecc915
Add info_types parameter to deid.py
mwdaub c80a2d9
Update deid tests to use info_types parameter
mwdaub 828fac6
Merge remote-tracking branch 'upstream/master'
mwdaub 748dcb1
Added code samples for stored infoTypes.
mwdaub 898e7f3
Lint errors.
mwdaub f76faa0
Merge branch 'master' into master
engelke 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| (223) 456-7890 | ||
| [email protected] |
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,301 @@ | ||
| # Copyright 2017 Google Inc. | ||
| # | ||
| # 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """Sample app that sets up Data Loss Prevention API stored infoTypes.""" | ||
|
|
||
| from __future__ import print_function | ||
|
|
||
| import argparse | ||
| import os | ||
| import time | ||
|
|
||
|
|
||
| # [START dlp_create_stored_info_type] | ||
| def create_stored_info_type_from_gcs_files( | ||
| project, gcs_input_file_path, | ||
| gcs_output_path, stored_info_type_id=None, | ||
| display_name=None, description=None): | ||
| """Creates a scheduled Data Loss Prevention API stored infoType from a set | ||
| of GCS files. | ||
| Args: | ||
| project: The Google Cloud project id to use as a parent resource. | ||
| gcs_input_file_path: The path specifying the input files containing the | ||
| dictionary words. | ||
| gcs_output_path: The path specifying where the dictionary data files | ||
| should be stored. | ||
| stored_info_type_id: The id of the stored infoType. If omitted, an id | ||
| will be randomly generated. | ||
| display_name: The optional display name of the stored infoType. | ||
| description: The optional description of the stored infoType. | ||
| Returns: | ||
| None; the response from the API is printed to the terminal. | ||
| """ | ||
|
|
||
| # Prepare the dictionary config. | ||
| dictionary_config = { | ||
| 'output_path': {'path': gcs_output_path}, | ||
| 'cloud_storage_file_set': {'url': gcs_input_file_path}, | ||
| } | ||
| create_stored_info_type( | ||
| project, dictionary_config, stored_info_type_id=stored_info_type_id, | ||
| display_name=display_name, description=description) | ||
|
|
||
|
|
||
| def create_stored_info_type_from_bq_table( | ||
| project, bq_input_project_id, bq_input_dataset_id, | ||
| bq_input_table_id, bq_input_table_field, gcs_output_path, | ||
| stored_info_type_id=None, display_name=None, description=None): | ||
| """Creates a scheduled Data Loss Prevention API stored infoType from a | ||
| column of a BigQuery. | ||
| Args: | ||
| project: The Google Cloud project id to use as a parent resource. | ||
| bq_input_project_id: The id of the project owning the input BigQuery | ||
| table. | ||
| bq_input_dataset_id: The dataset of the input BigQuery table. | ||
| bq_input_table_id: The id of the input BigQuery table. | ||
| bq_input_table_field: The name of the field of the BigQuery table_id | ||
| containing the dictionary words. | ||
| gcs_output_path: The path specifying where the dictionary data files | ||
| should be stored. | ||
| stored_info_type_id: The id of the stored infoType. If omitted, an id | ||
| will be randomly generated. | ||
| display_name: The optional display name of the stored infoType. | ||
| description: The optional description of the stored infoType. | ||
| Returns: | ||
| None; the response from the API is printed to the terminal. | ||
| """ | ||
|
|
||
| # Prepare the dictionary config. | ||
| dictionary_config = { | ||
| 'output_path': {'path': gcs_output_path}, | ||
| 'big_query_field': { | ||
| 'table': { | ||
| 'project_id': bq_input_project_id, | ||
| 'dataset_id': bq_input_dataset_id, | ||
| 'table_id': bq_input_table_id, | ||
| }, | ||
| 'field': {'name': bq_input_table_field}, | ||
| } | ||
| } | ||
| create_stored_info_type(project, dictionary_config, | ||
| stored_info_type_id=stored_info_type_id, | ||
| display_name=display_name, description=description) | ||
|
|
||
|
|
||
| def create_stored_info_type(project, dictionary_config, | ||
| stored_info_type_id=None, display_name=None, | ||
| description=None): | ||
| """Creates a scheduled Data Loss Prevention API stored infoType from a | ||
| column of a BigQuery. | ||
| Args: | ||
| project: The Google Cloud project id to use as a parent resource. | ||
| dictionary_config: The config for the large custom dictionary. | ||
| stored_info_type_id: The id of the stored infoType. If omitted, an id | ||
| will be randomly generated. | ||
| display_name: The optional display name of the stored infoType. | ||
| description: The optional description of the stored infoType. | ||
| Returns: | ||
| None; the response from the API is printed to the terminal. | ||
| """ | ||
|
|
||
| # Import the client library | ||
| import google.cloud.dlp | ||
|
|
||
| # Instantiate a client. | ||
| dlp = google.cloud.dlp.DlpServiceClient() | ||
|
|
||
| # Create the stored infoType config. | ||
| stored_info_type_config = { | ||
| 'display_name': display_name, | ||
| 'description': description, | ||
| 'large_custom_dictionary': dictionary_config | ||
| } | ||
|
|
||
| # Convert the project id into a full resource id. | ||
| parent = dlp.project_path(project) | ||
|
|
||
| # Call the API. | ||
| response = dlp.create_stored_info_type( | ||
| parent, config=stored_info_type_config, | ||
| stored_info_type_id=stored_info_type_id) | ||
|
|
||
| print('Successfully created stored infoType {}'.format(response.name)) | ||
|
|
||
| # [END dlp_create_stored_info_type] | ||
|
|
||
|
|
||
| # [START dlp_list_stored_info_types] | ||
| def list_stored_info_types(project): | ||
| """Lists all Data Loss Prevention API stored infoTypes. | ||
| Args: | ||
| project: The Google Cloud project id to use as a parent resource. | ||
| Returns: | ||
| None; the response from the API is printed to the terminal. | ||
| """ | ||
|
|
||
| # Import the client library | ||
| import google.cloud.dlp | ||
|
|
||
| # Instantiate a client. | ||
| dlp = google.cloud.dlp.DlpServiceClient() | ||
|
|
||
| # Convert the project id into a full resource id. | ||
| parent = dlp.project_path(project) | ||
|
|
||
| # Call the API. | ||
| response = dlp.list_stored_info_types(parent) | ||
|
|
||
| # Define a helper function to convert the API's "seconds since the epoch" | ||
| # time format into a human-readable string. | ||
| def human_readable_time(timestamp): | ||
| return str(time.localtime(timestamp.seconds)) | ||
|
|
||
| for stored_info_type in response: | ||
| print('Stored infoType {}:'.format(stored_info_type.name)) | ||
| if stored_info_type.current_version: | ||
| version = stored_info_type.current_version | ||
| print(' Current version:') | ||
| print(' Created: {}'.format( | ||
| human_readable_time(version.create_time))) | ||
| print(' State: {}'.format(version.state)) | ||
| print(' Error count: {}'.format(len(version.errors))) | ||
| if stored_info_type.pending_versions: | ||
| print(' Pending versions:') | ||
| for version in stored_info_type.pending_versions: | ||
| print(' Created: {}'.format( | ||
| human_readable_time(version.create_time))) | ||
| print(' State: {}'.format(version.state)) | ||
| print(' Error count: {}'.format(len(version.errors))) | ||
|
|
||
| # [END dlp_list_stored_info_types] | ||
|
|
||
|
|
||
| # [START dlp_delete_stored_info_type] | ||
| def delete_stored_info_type(project, stored_info_type_id): | ||
| """Deletes a Data Loss Prevention API stored infoType. | ||
| Args: | ||
| project: The id of the Google Cloud project which owns the stored | ||
| infoType. | ||
| stored_info_type_id: The id of the stored infoType to delete. | ||
| Returns: | ||
| None; the response from the API is printed to the terminal. | ||
| """ | ||
|
|
||
| # Import the client library | ||
| import google.cloud.dlp | ||
|
|
||
| # Instantiate a client. | ||
| dlp = google.cloud.dlp.DlpServiceClient() | ||
|
|
||
| # Convert the project id into a full resource id. | ||
| parent = dlp.project_path(project) | ||
|
|
||
| # Combine the stored infoType id with the parent id. | ||
| stored_info_type_resource = '{}/storedInfoTypes/{}'.format( | ||
| parent, stored_info_type_id) | ||
|
|
||
| # Call the API. | ||
| dlp.delete_stored_info_type(stored_info_type_resource) | ||
|
|
||
| print('Stored infoType {} successfully deleted.'.format( | ||
| stored_info_type_resource)) | ||
|
|
||
| # [END dlp_delete_stored_info_type] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| default_project = os.environ.get('GCLOUD_PROJECT') | ||
|
|
||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| subparsers = parser.add_subparsers( | ||
| dest='action', help='Select which action to perform.') | ||
| subparsers.required = True | ||
|
|
||
| parser_create = subparsers.add_parser( | ||
| 'create', | ||
| help='Create a stored infoType.') | ||
| parser_create.add_argument( | ||
| '--gcs_input_file_path', | ||
| help='GCS path of the input files containing the dictionary words.') | ||
| parser_create.add_argument( | ||
| '--bq_input_project_id', | ||
| help='Project of the BigQuery table containing the dictionary words.', | ||
| default=default_project) | ||
| parser_create.add_argument( | ||
| '--bq_input_dataset_id', | ||
| help='Dataset of the BigQuery table containing the dictionary words.') | ||
| parser_create.add_argument( | ||
| '--bq_input_table_id', | ||
| help='ID of the BigQuery table containing the dictionary words.') | ||
| parser_create.add_argument( | ||
| '--bq_input_table_field', | ||
| help='Field of the BigQuery table containing the dictionary words.') | ||
| parser_create.add_argument( | ||
| '--gcs_output_path', | ||
| help='GCS path where the output data files should be stored.') | ||
| parser_create.add_argument( | ||
| '--stored_info_type_id', | ||
| help='The id of the stored infoType. If omitted, an id will be ' | ||
| 'randomly generated') | ||
| parser_create.add_argument( | ||
| '--display_name', | ||
| help='The optional display name of the stored infoType.') | ||
| parser_create.add_argument( | ||
| '--description', | ||
| help='The optional description of the stored infoType.') | ||
| parser_create.add_argument( | ||
| '--project', | ||
| help='The Google Cloud project id to use as a parent resource.', | ||
| default=default_project) | ||
|
|
||
| parser_list = subparsers.add_parser( | ||
| 'list', | ||
| help='List all stored infoTypes.') | ||
| parser_list.add_argument( | ||
| '--project', | ||
| help='The Google Cloud project id to use as a parent resource.', | ||
| default=default_project) | ||
|
|
||
| parser_delete = subparsers.add_parser( | ||
| 'delete', | ||
| help='Delete a stored infoType.') | ||
| parser_delete.add_argument( | ||
| 'stored_info_type_id', | ||
| help='The id of the stored infoType to delete.') | ||
| parser_delete.add_argument( | ||
| '--project', | ||
| help='The Google Cloud project id to use as a parent resource.', | ||
| default=default_project) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if args.action == 'create': | ||
| if args.gcs_input_file_path: | ||
| create_stored_info_type_from_gcs_files( | ||
| args.project, args.gcs_input_file_path, args.gcs_output_path, | ||
| stored_info_type_id=args.stored_info_type_id, | ||
| display_name=args.display_name, description=args.description | ||
| ) | ||
| else: | ||
| create_stored_info_type_from_bq_table( | ||
| args.project, args.bq_input_project_id, | ||
| args.bq_input_dataset_id, args.bq_input_table_id, | ||
| args.bq_input_table_field, args.gcs_output_path, | ||
| stored_info_type_id=args.stored_info_type_id, | ||
| display_name=args.display_name, description=args.description | ||
| ) | ||
| elif args.action == 'list': | ||
| list_stored_info_types(args.project) | ||
| elif args.action == 'delete': | ||
| delete_stored_info_type(args.project, args.stored_info_type_id) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As alternative formatting, you could avoid the hanging indent: