Skip to content

Commit 5408406

Browse files
[DLP] Corrected Region tags for 6 DLP samples and made related code changes (#10273)
* Corrected region tag - inspect_phone_number and made related code changes * Corrected region tag - inspect_custom_regex and made related code changes * Corrected region tag - dlp_inspect_hotword_rule and made related code changes * Corrected region tag - dlp_inspect_string_omit_overlap and made related code changes * Corrected region tag - dlp_inspect_string_custom_hotword and made related code changes * Corrected region tag - dlp_list_inspect_templates * removed unused import
1 parent 609e09f commit 5408406

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

dlp/snippets/custom_infotype.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,11 @@ def inspect_string_custom_omit_overlap(project: str, content_string: str) -> Non
412412

413413
# [END dlp_inspect_string_custom_omit_overlap]
414414

415-
# [START dlp_omit_name_if_also_email]
415+
# [START dlp_inspect_string_omit_overlap]
416416
import google.cloud.dlp # noqa: F811, E402
417417

418418

419-
def omit_name_if_also_email(
419+
def inspect_string_omit_overlap(
420420
project: str,
421421
content_string: str,
422422
) -> None:
@@ -486,7 +486,7 @@ def omit_name_if_also_email(
486486
print("No findings.")
487487

488488

489-
# [END dlp_omit_name_if_also_email]
489+
# [END dlp_inspect_string_omit_overlap]
490490

491491

492492
# [START dlp_inspect_string_without_overlap]
@@ -574,25 +574,22 @@ def inspect_string_without_overlap(project: str, content_string: str) -> None:
574574
# [END dlp_inspect_string_without_overlap]
575575

576576

577-
# [START dlp_inspect_with_person_name_w_custom_hotword]
577+
# [START dlp_inspect_string_custom_hotword]
578578
import google.cloud.dlp # noqa: F811, E402
579579

580580

581-
def inspect_with_person_name_w_custom_hotword(
581+
def inspect_string_w_custom_hotword(
582582
project: str, content_string: str, custom_hotword: str = "patient"
583583
) -> None:
584584
"""Uses the Data Loss Prevention API increase likelihood for matches on
585-
PERSON_NAME if the user specified custom hotword is present. Only
585+
PERSON_NAME if the user specified custom hot-word is present. Only
586586
includes findings with the increased likelihood by setting a minimum
587587
likelihood threshold of VERY_LIKELY.
588588
589589
Args:
590590
project: The Google Cloud project id to use as a parent resource.
591591
content_string: The string to inspect.
592-
custom_hotword: The custom hotword used for likelihood boosting.
593-
594-
Returns:
595-
None; the response from the API is printed to the terminal.
592+
custom_hotword: The custom hot-word used for likelihood boosting.
596593
"""
597594

598595
# Instantiate a client.
@@ -644,7 +641,7 @@ def inspect_with_person_name_w_custom_hotword(
644641
print("No findings.")
645642

646643

647-
# [END dlp_inspect_with_person_name_w_custom_hotword]
644+
# [END dlp_inspect_string_custom_hotword]
648645

649646

650647
# [START dlp_inspect_string_multiple_rules]
@@ -736,11 +733,11 @@ def inspect_string_multiple_rules(project: str, content_string: str) -> None:
736733
# [END dlp_inspect_string_multiple_rules]
737734

738735

739-
# [START dlp_inspect_with_medical_record_number_custom_regex_detector]
736+
# [START dlp_inspect_custom_regex]
740737
import google.cloud.dlp # noqa: E402, F811
741738

742739

743-
def inspect_with_medical_record_number_custom_regex_detector(
740+
def inspect_data_with_custom_regex_detector(
744741
project: str,
745742
content_string: str,
746743
) -> None:
@@ -796,14 +793,14 @@ def inspect_with_medical_record_number_custom_regex_detector(
796793
print("No findings.")
797794

798795

799-
# [END dlp_inspect_with_medical_record_number_custom_regex_detector]
796+
# [END dlp_inspect_custom_regex]
800797

801798

802-
# [START dlp_inspect_with_medical_record_number_w_custom_hotwords]
799+
# [START dlp_inspect_hotword_rule]
803800
import google.cloud.dlp # noqa: F811, E402
804801

805802

806-
def inspect_with_medical_record_number_w_custom_hotwords(
803+
def inspect_data_w_custom_hotwords(
807804
project: str,
808805
content_string: str,
809806
) -> None:
@@ -876,4 +873,4 @@ def inspect_with_medical_record_number_w_custom_hotwords(
876873
print("No findings.")
877874

878875

879-
# [END dlp_inspect_with_medical_record_number_w_custom_hotwords]
876+
# [END dlp_inspect_hotword_rule]

dlp/snippets/custom_infotype_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def test_inspect_string_custom_omit_overlap(capsys: pytest.LogCaptureFixture) ->
7676
assert "John Doe" in out
7777

7878

79-
def test_omit_name_if_also_email(capsys: pytest.LogCaptureFixture) -> None:
80-
custom_infotype.omit_name_if_also_email(GCLOUD_PROJECT, "[email protected]")
79+
def test_inspect_string_omit_overlap(capsys: pytest.LogCaptureFixture) -> None:
80+
custom_infotype.inspect_string_omit_overlap(GCLOUD_PROJECT, "[email protected]")
8181

8282
# Ensure we found only EMAIL_ADDRESS, and not PERSON_NAME.
8383
out, _ = capsys.readouterr()
@@ -95,10 +95,10 @@ def test_inspect_string_without_overlap(capsys: pytest.LogCaptureFixture) -> Non
9595
assert "example.org" not in out
9696

9797

98-
def test_inspect_with_person_name_w_custom_hotword(
98+
def test_inspect_string_w_custom_hotword(
9999
capsys: pytest.LogCaptureFixture,
100100
) -> None:
101-
custom_infotype.inspect_with_person_name_w_custom_hotword(
101+
custom_infotype.inspect_string_w_custom_hotword(
102102
GCLOUD_PROJECT, "patient's name is John Doe.", "patient"
103103
)
104104

@@ -147,10 +147,10 @@ def test_inspect_string_multiple_rules_redacted(
147147
assert "No findings" in out
148148

149149

150-
def test_inspect_with_medical_record_number_custom_regex_detector(
150+
def test_inspect_data_with_custom_regex_detector(
151151
capsys: pytest.LogCaptureFixture,
152152
) -> None:
153-
custom_infotype.inspect_with_medical_record_number_custom_regex_detector(
153+
custom_infotype.inspect_data_with_custom_regex_detector(
154154
GCLOUD_PROJECT, "Patients MRN 444-5-22222"
155155
)
156156

@@ -161,7 +161,7 @@ def test_inspect_with_medical_record_number_custom_regex_detector(
161161
def test_inspect_with_medical_record_number_w_custom_hotwords_no_hotwords(
162162
capsys: pytest.LogCaptureFixture,
163163
) -> None:
164-
custom_infotype.inspect_with_medical_record_number_w_custom_hotwords(
164+
custom_infotype.inspect_data_w_custom_hotwords(
165165
GCLOUD_PROJECT, "just a number 444-5-22222"
166166
)
167167

@@ -173,7 +173,7 @@ def test_inspect_with_medical_record_number_w_custom_hotwords_no_hotwords(
173173
def test_inspect_with_medical_record_number_w_custom_hotwords_has_hotwords(
174174
capsys: pytest.LogCaptureFixture,
175175
) -> None:
176-
custom_infotype.inspect_with_medical_record_number_w_custom_hotwords(
176+
custom_infotype.inspect_data_w_custom_hotwords(
177177
GCLOUD_PROJECT, "Patients MRN 444-5-22222"
178178
)
179179

dlp/snippets/inspect_content.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,26 @@
2020
import json
2121
import os
2222

23-
from typing import List
24-
25-
# [START dlp_inspect_string_basic]
23+
# [START dlp_inspect_phone_number]
2624
import google.cloud.dlp
2725

2826

29-
def inspect_string_basic(
27+
def inspect_phone_number(
3028
project: str,
3129
content_string: str,
32-
info_types: List[str] = ["PHONE_NUMBER"],
3330
) -> None:
3431
"""Uses the Data Loss Prevention API to analyze strings for protected data.
3532
Args:
3633
project: The Google Cloud project id to use as a parent resource.
37-
content_string: The string to inspect.
38-
info_types: A list of strings representing info types to look for.
39-
A full list of info type categories can be fetched from the API.
40-
Returns:
41-
None; the response from the API is printed to the terminal.
34+
content_string: The string to inspect phone number from.
4235
"""
4336

4437
# Instantiate a client.
4538
dlp = google.cloud.dlp_v2.DlpServiceClient()
4639

4740
# Prepare info_types by converting the list of strings into a list of
4841
# dictionaries (protos are also accepted).
49-
info_types = [{"name": info_type} for info_type in info_types]
42+
info_types = [{"name": "PHONE_NUMBER"}]
5043

5144
# Construct the configuration dictionary.
5245
inspect_config = {
@@ -75,7 +68,7 @@ def inspect_string_basic(
7568
print("No findings.")
7669

7770

78-
# [END dlp_inspect_string_basic]
71+
# [END dlp_inspect_phone_number]
7972

8073

8174
# [START dlp_inspect_string]
@@ -1416,6 +1409,20 @@ def inspect_data_to_hybrid_job_trigger(
14161409
)
14171410
subparsers.required = True
14181411

1412+
parser_phone_number = subparsers.add_parser(
1413+
"phone_number",
1414+
help="Inspect phone number in a string.",
1415+
)
1416+
parser_phone_number.add_argument(
1417+
"content_string",
1418+
help="The string to inspect phone number from.",
1419+
)
1420+
parser_phone_number.add_argument(
1421+
"--project",
1422+
help="The Google Cloud project id to use as a parent resource.",
1423+
default=default_project,
1424+
)
1425+
14191426
parser_string = subparsers.add_parser("string", help="Inspect a string.")
14201427
parser_string.add_argument("item", help="The string to inspect.")
14211428
parser_string.add_argument(
@@ -2019,6 +2026,11 @@ def inspect_data_to_hybrid_job_trigger(
20192026
max_findings=args.max_findings,
20202027
include_quote=args.include_quote,
20212028
)
2029+
elif args.content == "phone_number":
2030+
inspect_phone_number(
2031+
args.project,
2032+
args.content_string
2033+
)
20222034
elif args.content == "table":
20232035
inspect_table(
20242036
args.project,

dlp/snippets/inspect_content_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ def cleanup() -> None:
180180
cleanup()
181181

182182

183-
def test_inspect_string_basic(capsys: pytest.CaptureFixture) -> None:
183+
def test_inspect_phone_number(capsys: pytest.CaptureFixture) -> None:
184184
test_string = "String with a phone number: 234-555-6789"
185185

186-
inspect_content.inspect_string_basic(GCLOUD_PROJECT, test_string)
186+
inspect_content.inspect_phone_number(GCLOUD_PROJECT, test_string)
187187

188188
out, _ = capsys.readouterr()
189189
assert "Info type: PHONE_NUMBER" in out

dlp/snippets/templates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def create_inspect_template(
9191
# [END dlp_create_inspect_template]
9292

9393

94-
# [START dlp_list_templates]
94+
# [START dlp_list_inspect_templates]
9595
import google.cloud.dlp # noqa: E402, F811
9696

9797

@@ -132,7 +132,7 @@ def list_inspect_templates(project: str) -> None:
132132
)
133133

134134

135-
# [END dlp_list_templates]
135+
# [END dlp_list_inspect_templates]
136136

137137

138138
# [START dlp_delete_inspect_template]

0 commit comments

Comments
 (0)