|
21 | 21 |
|
22 | 22 |
|
23 | 23 | # [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): |
25 | 25 | """List types of sensitive information within a category. |
26 | 26 | Args: |
27 | | - category: The category of info types to list; e.g. 'PII'. |
28 | 27 | 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". |
29 | 30 | Returns: |
30 | 31 | None; the response from the API is printed to the terminal. |
31 | 32 | """ |
32 | 33 | # Import the client library |
33 | | - import google.cloud.dlp_v2beta1 |
| 34 | + import google.cloud.dlp |
34 | 35 |
|
35 | 36 | # Instantiate a client. |
36 | | - dlp = google.cloud.dlp_v2beta1.DlpServiceClient() |
| 37 | + dlp = google.cloud.dlp.DlpServiceClient() |
37 | 38 |
|
38 | 39 | # Make the API call. |
39 | | - response = dlp.list_info_types(category, language_code) |
| 40 | + response = dlp.list_info_types(language_code, result_filter) |
40 | 41 |
|
41 | 42 | # Print the results to the console. |
42 | | - print('Info types in {category}:'.format(category=category)) |
| 43 | + print('Info types:') |
43 | 44 | for info_type in response.info_types: |
44 | 45 | print('{name}: {display_name}'.format( |
45 | 46 | name=info_type.name, display_name=info_type.display_name)) |
46 | 47 | # [END list_info_types] |
47 | 48 |
|
48 | 49 |
|
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 | | - |
74 | 50 | if __name__ == '__main__': |
75 | 51 | 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( |
91 | 53 | '--language_code', |
92 | 54 | 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".') |
93 | 59 |
|
94 | 60 | args = parser.parse_args() |
95 | 61 |
|
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) |
0 commit comments