Skip to content

Commit a615cb0

Browse files
authored
Update DLP metadata samples and fix DLP quickstart
1 parent 90a1166 commit a615cb0

File tree

3 files changed

+30
-60
lines changed

3 files changed

+30
-60
lines changed

dlp/metadata.py

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,79 +21,43 @@
2121

2222

2323
# [START list_info_types]
24-
def list_info_types(category, language_code='en-US'):
24+
def list_info_types(language_code=None, result_filter=None):
2525
"""List types of sensitive information within a category.
2626
Args:
27-
category: The category of info types to list; e.g. 'PII'.
2827
language_code: The BCP-47 language code to use, e.g. 'en-US'.
28+
filter: An optional filter to only return info types supported by
29+
certain parts of the API. Defaults to "supported_by=INSPECT".
2930
Returns:
3031
None; the response from the API is printed to the terminal.
3132
"""
3233
# Import the client library
33-
import google.cloud.dlp_v2beta1
34+
import google.cloud.dlp
3435

3536
# Instantiate a client.
36-
dlp = google.cloud.dlp_v2beta1.DlpServiceClient()
37+
dlp = google.cloud.dlp.DlpServiceClient()
3738

3839
# Make the API call.
39-
response = dlp.list_info_types(category, language_code)
40+
response = dlp.list_info_types(language_code, result_filter)
4041

4142
# Print the results to the console.
42-
print('Info types in {category}:'.format(category=category))
43+
print('Info types:')
4344
for info_type in response.info_types:
4445
print('{name}: {display_name}'.format(
4546
name=info_type.name, display_name=info_type.display_name))
4647
# [END list_info_types]
4748

4849

49-
# [START list_categories]
50-
def list_categories(language_code='en-US'):
51-
"""List root categories of sensitive information.
52-
Args:
53-
language_code: The BCP-47 language code to use, e.g. 'en-US'.
54-
Returns:
55-
None; the response from the API is printed to the terminal.
56-
"""
57-
# Import the client library
58-
import google.cloud.dlp_v2beta1
59-
60-
# Instantiate a client.
61-
dlp = google.cloud.dlp_v2beta1.DlpServiceClient()
62-
63-
# Make the API call.
64-
response = dlp.list_root_categories(language_code)
65-
66-
# Print the results to the console.
67-
print('Categories:')
68-
for category in response.categories:
69-
print('{name}: {display_name}'.format(
70-
name=category.name, display_name=category.display_name))
71-
# [END list_categories]
72-
73-
7450
if __name__ == '__main__':
7551
parser = argparse.ArgumentParser(description=__doc__)
76-
subparsers = parser.add_subparsers(
77-
dest='metadata', help='Select which type of metadata to view.')
78-
79-
parser_categories = subparsers.add_parser(
80-
'categories', help='Fetch the list of info type categories.')
81-
parser_categories.add_argument(
82-
'--language_code',
83-
help='The BCP-47 language code to use, e.g. \'en-US\'.')
84-
85-
parser_info_types = subparsers.add_parser(
86-
'info_types',
87-
help='Fetch the list of info types in a specified category.')
88-
parser_info_types.add_argument(
89-
'category', help='The category of info types to list; e.g. \'PII\'.')
90-
parser_info_types.add_argument(
52+
parser.add_argument(
9153
'--language_code',
9254
help='The BCP-47 language code to use, e.g. \'en-US\'.')
55+
parser.add_argument(
56+
'--filter',
57+
help='An optional filter to only return info types supported by '
58+
'certain parts of the API. Defaults to "supported_by=INSPECT".')
9359

9460
args = parser.parse_args()
9561

96-
if args.metadata == 'categories':
97-
list_categories(language_code=args.language_code)
98-
elif args.metadata == 'info_types':
99-
list_info_types(args.category, language_code=args.language_code)
62+
list_info_types(
63+
language_code=args.language_code, result_filter=args.filter)

dlp/metadata_test.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@
1515
import metadata
1616

1717

18-
def test_fetch_categories(capsys):
19-
metadata.list_categories()
20-
21-
out, _ = capsys.readouterr()
22-
assert 'PII' in out
23-
24-
2518
def test_fetch_info_types(capsys):
26-
metadata.list_info_types('PII')
19+
metadata.list_info_types()
2720

2821
out, _ = capsys.readouterr()
2922
assert 'EMAIL_ADDRESS' in out

dlp/quickstart_test.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import mock
16+
import os
17+
18+
import google.cloud.dlp
19+
1520
import quickstart
1621

1722

23+
GCLOUD_PROJECT = os.getenv('GCLOUD_PROJECT')
24+
1825
def test_quickstart(capsys):
19-
quickstart.quickstart()
26+
# Mock out project_path to use the test runner's project ID.
27+
with mock.patch.object(
28+
google.cloud.dlp.DlpServiceClient,
29+
'project_path',
30+
return_value='projects/{}'.format(GCLOUD_PROJECT)):
31+
quickstart.quickstart()
2032

2133
out, _ = capsys.readouterr()
22-
assert 'US_MALE_NAME' in out
34+
assert 'FIRST_NAME' in out
35+
assert 'LAST_NAME' in out

0 commit comments

Comments
 (0)