-
Notifications
You must be signed in to change notification settings - Fork 6.7k
add Jobs samples #1405
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
add Jobs samples #1405
Changes from 1 commit
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,141 @@ | ||
| # 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 to list and delete DLP jobs using the Data Loss Prevent API. """ | ||
|
|
||
| from __future__ import print_function | ||
|
|
||
| import argparse | ||
|
|
||
|
|
||
| def list_dlp_jobs(project, filter_string=None, job_type=None): | ||
| """Uses the Data Loss Prevention API to lists DLP jobs that match the | ||
| specified filter in the request. | ||
| Args: | ||
| project: The Google Cloud project id to use as a parent resource. | ||
| filter: (Optional) Filter expressions are made up of one or more | ||
| restrictions. | ||
| type: (Optional) The type of job. Defaults to 'INSPECT'. | ||
| Choices: | ||
| DLP_JOB_TYPE_UNSPECIFIED | ||
| INSPECT_JOB: The job inspected Google Cloud for sensitive data. | ||
|
||
| RISK_ANALYSIS_JOB: The job executed a Risk Analysis computation. | ||
| Returns: | ||
|
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. Newline before Returns:
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 |
||
| 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) | ||
|
|
||
| # If job type is specified, convert job type to number through enums. | ||
| from google.cloud.dlp_v2 import enums | ||
|
||
| # Job type dictionary | ||
| job_type_to_int = { | ||
|
Member
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. The keys here (INSPECT, etc.) don't match the keys in the docstring above (INSPECT_JOB, etc) and it looks like they need to be exact matches to work. Let's change either the keys or the docstring.
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 |
||
| 'UNSPECIFIED': enums.DlpJobType.DLP_JOB_TYPE_UNSPECIFIED, | ||
| 'INSPECT': enums.DlpJobType.INSPECT_JOB, | ||
| 'RISK_ANALYSIS': enums.DlpJobType.RISK_ANALYSIS_JOB | ||
| } | ||
| if job_type: | ||
| job_type = job_type_to_int[job_type] | ||
|
|
||
| # Call the API to get a list of jobs. | ||
| response = dlp.list_dlp_jobs( | ||
| parent, | ||
| filter_=filter_string, | ||
| type_=job_type) | ||
|
|
||
| # Iterate over results. | ||
| for job in response: | ||
| print('Job: %s; status: %s' % (job.name, job.JobState.Name(job.state))) | ||
|
Member
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. Can we use the .format() method of string replacement for consistency with other samples in this directory?
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 |
||
| info_type_stats = job.inspect_details.result.info_type_stats | ||
|
||
| if len(info_type_stats) > 0: | ||
| for info_type_stat in info_type_stats: | ||
| print( | ||
| ' Found %i instance(s) of info_type %s' % | ||
| (info_type_stat.count, info_type_stat.info_type.name)) | ||
| else: | ||
| print(' No findings.') | ||
|
|
||
|
|
||
| def delete_dlp_job(project, job_name): | ||
| """Uses the Data Loss Prevention API to delete a long-running DLP job. | ||
| Args: | ||
| project: The Google Cloud project id to use as a parent resource. | ||
| job_name: The name of the DlpJob resource to be deleted. | ||
| Returns: | ||
|
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. ditto |
||
| 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 and job name into a full resource id. | ||
| name = dlp.dlp_job_path(project, job_name) | ||
|
|
||
| # Call the API to delete job. | ||
| dlp.delete_dlp_job(name) | ||
|
|
||
| print('Successfully deleted %s' % job_name) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| subparsers = parser.add_subparsers( | ||
| dest='content', help='Select how to submit content to the API.') | ||
| subparsers.required = True | ||
|
|
||
| list_parser = subparsers.add_parser( | ||
| 'list', | ||
| help='List Data Loss Prevention API jobs corresponding to a given ' | ||
| 'filter.') | ||
| list_parser.add_argument( | ||
| 'project', | ||
| help='The Google Cloud project id to use as a parent resource.') | ||
|
||
| list_parser.add_argument( | ||
| '-f', '--filter', | ||
| help='Filter expressions are made up of one or more restrictions.') | ||
| list_parser.add_argument( | ||
| '-t', '--type', | ||
| choices=['UNSPECIFIED', 'INSPECT', 'RISK_ANALYSIS'], | ||
| help='The type of job. API defaults to "INSPECT"') | ||
|
|
||
| delete_parser = subparsers.add_parser( | ||
| 'delete', | ||
| help='Delete results of a Data Loss Prevention API job.') | ||
| delete_parser.add_argument( | ||
| 'project', | ||
| help='The Google Cloud project id to use as a parent resource.') | ||
|
||
| delete_parser.add_argument( | ||
| 'job_name', | ||
| help='The name of the DlpJob resource to be deleted. ' | ||
| 'Example: X-#####') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if args.content == 'list': | ||
| list_dlp_jobs( | ||
| args.project, | ||
| filter_string=args.filter, | ||
| job_type=args.type) | ||
| elif args.content == 'delete': | ||
| delete_dlp_job(args.project, args.job_name) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # 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. | ||
|
|
||
| import jobs | ||
|
|
||
| import pytest | ||
| import os | ||
|
|
||
| GCLOUD_PROJECT = os.getenv('GCLOUD_PROJECT') | ||
|
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 believe we prefer GOOGLE_CLOUD_PROJECT for both sides
Member
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 agree in principle, but unfortunately the other languages such as Node are still using GCLOUD_PROJECT; perhaps we should standardize across all languages at once rather than try to change it in this PR.
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. @jonparrott PTAL We were supposed to standardized on GOOGLE_CLOUD_PROJECT over two years ago. |
||
| TEST_COLUMN_NAME = 'zip_code' | ||
| TEST_TABLE_PROJECT_ID = 'bigquery-public-data' | ||
| TEST_DATASET_ID = 'san_francisco' | ||
| TEST_TABLE_ID = 'bikeshare_trips' | ||
|
|
||
|
|
||
| @pytest.fixture(scope='session') | ||
| def create_test_job(): | ||
| import google.cloud.dlp | ||
| dlp = google.cloud.dlp.DlpServiceClient() | ||
|
|
||
| parent = dlp.project_path(GCLOUD_PROJECT) | ||
|
|
||
| # Construct job request | ||
| risk_job = { | ||
| 'privacy_metric': { | ||
| 'categorical_stats_config': { | ||
| 'field': { | ||
| 'name': TEST_COLUMN_NAME | ||
| } | ||
| } | ||
| }, | ||
| 'source_table': { | ||
| 'project_id': TEST_TABLE_PROJECT_ID, | ||
| 'dataset_id': TEST_DATASET_ID, | ||
| 'table_id': TEST_TABLE_ID | ||
| } | ||
| } | ||
|
|
||
| response = dlp.create_dlp_job(parent, risk_job=risk_job) | ||
| full_path = response.name | ||
| # API expects only job name, not full project path | ||
| job_name = full_path[full_path.rfind('/')+1:] | ||
| return job_name | ||
|
|
||
|
|
||
| def test_list_dlp_jobs(capsys): | ||
| jobs.list_dlp_jobs(GCLOUD_PROJECT) | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert 'Job: projects/' in out | ||
|
|
||
|
|
||
| def test_list_dlp_jobs_with_filter(capsys): | ||
| jobs.list_dlp_jobs(GCLOUD_PROJECT, filter_string='state=DONE') | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert 'Job: projects/' in out | ||
|
|
||
|
|
||
| def test_list_dlp_jobs_with_job_type(capsys): | ||
| jobs.list_dlp_jobs(GCLOUD_PROJECT, job_type='INSPECT') | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert 'Job: projects/' in out | ||
|
|
||
|
|
||
| def test_delete_dlp_job(capsys): | ||
| test_job_name = create_test_job() | ||
| jobs.delete_dlp_job(GCLOUD_PROJECT, test_job_name) | ||
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.
Do we have any further advice to give on what valid filters look like? It's okay if the answer is "no".
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.
done