Skip to content
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 a simplified inspect string example to DLP code samples #4069

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions dlp/inspect_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,60 @@
import os


# [START dlp_inspect_string_basic]
def inspect_string_basic(
project,
content_string,
info_types=["PHONE_NUMBER"],
):
"""Uses the Data Loss Prevention API to analyze strings for protected data.
Args:
project: The Google Cloud project id to use as a parent resource.
content_string: The string to inspect.
info_types: A list of strings representing info types to look for.
A full list of info type categories can be fetched from the API.
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_v2.DlpServiceClient()

# Prepare info_types by converting the list of strings into a list of
# dictionaries (protos are also accepted).
info_types = [{"name": info_type} for info_type in info_types]

# Construct the configuration dictionary.
inspect_config = {
"info_types": info_types,
"include_quote": True,
}

# Construct the `item`.
item = {"value": content_string}

# Convert the project id into a full resource id.
parent = dlp.project_path(project)

# Call the API.
response = dlp.inspect_content(parent, inspect_config, item)

# Print out the results.
if response.result.findings:
for finding in response.result.findings:
print("Quote: {}".format(finding.quote))
print("Info type: {}".format(finding.info_type.name))
print("Likelihood: {}".format(finding.likelihood))
else:
print("No findings.")


# [END dlp_inspect_string_basic]


# [START dlp_inspect_string]
def inspect_string(
project,
Expand Down
10 changes: 10 additions & 0 deletions dlp/inspect_content_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ def bigquery_project():
bigquery_client.delete_dataset(dataset_ref, delete_contents=True)


def test_inspect_string_basic(capsys):
test_string = "String with a phone number: 234-555-6789"

inspect_content.inspect_string_basic(GCLOUD_PROJECT, test_string)

out, _ = capsys.readouterr()
assert "Info type: PHONE_NUMBER" in out
assert "Quote: 234-555-6789" in out


def test_inspect_string(capsys):
test_string = "My name is Gary Smith and my email is [email protected]"

Expand Down