Skip to content

Commit 0c770b4

Browse files
jabubakechingor13
authored andcommitted
samples: DLP => v2 (#1056)
* DLP => v2 (WIP) Pending tasks: -> Update / Add Tests -> Region tag / comment review -> Submit for code review + fixes -> Merge once google-cloud-java PR : googleapis/google-cloud-java#2958 is released * Update to most recent versioning. * Updated DeIdentification samples and tests. * Revert pubsub to public version. * Fix Inspect samples/tests (minus pubsub). * Updated Jobs and add tests. * Updated Metadata classes. * Updated QuickStart tests and samples. * Updated Redact samples and tests. * Updated RiskAnalysis. * Update Template samples. * Update trigger tests. * Make Checkstyle Happy Again. * Fix (and ignore) tests using pubsub. * Update PR tests to complete all tests before returning results. (#1065) * Return results of all tests. * Use for loop instead of while. * WIP: Address PR feedback, part 1 * Update deps * Address PR feedback * Remove mvn clean verify failure * Add ReID FPE sample * Address PR feedback * Add k-map sample * checkstyle fixes
1 parent a695637 commit 0c770b4

20 files changed

+2982
-906
lines changed

dlp/snippets/snippets/src/main/java/com/example/dlp/DeIdentification.java

Lines changed: 445 additions & 87 deletions
Large diffs are not rendered by default.

dlp/snippets/snippets/src/main/java/com/example/dlp/Inspect.java

Lines changed: 445 additions & 238 deletions
Large diffs are not rendered by default.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.dlp;
18+
19+
import com.google.cloud.ServiceOptions;
20+
import com.google.cloud.dlp.v2.DlpServiceClient;
21+
import com.google.privacy.dlp.v2.DeleteDlpJobRequest;
22+
import com.google.privacy.dlp.v2.DlpJob;
23+
import com.google.privacy.dlp.v2.DlpJobName;
24+
import com.google.privacy.dlp.v2.DlpJobType;
25+
import com.google.privacy.dlp.v2.ListDlpJobsRequest;
26+
import com.google.privacy.dlp.v2.ProjectName;
27+
import org.apache.commons.cli.CommandLine;
28+
import org.apache.commons.cli.CommandLineParser;
29+
import org.apache.commons.cli.DefaultParser;
30+
import org.apache.commons.cli.HelpFormatter;
31+
import org.apache.commons.cli.Option;
32+
import org.apache.commons.cli.OptionGroup;
33+
import org.apache.commons.cli.Options;
34+
import org.apache.commons.cli.ParseException;
35+
36+
public class Jobs {
37+
38+
// [START dlp_list_jobs]
39+
/*
40+
* List DLP jobs
41+
*
42+
* @param projectId The project ID to run the API call under
43+
* @param filter The filter expression to use, eg. state=DONE For more information on filter
44+
* syntax see https://cloud.google.com/dlp/docs/reference/rest/v2/projects.dlpJobs/list
45+
* @param jobType The type of job to list (either 'INSPECT_JOB' or 'RISK_ANALYSIS_JOB')
46+
*/
47+
private static void listJobs(String projectId, String filter, DlpJobType jobType)
48+
throws Exception {
49+
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
50+
ListDlpJobsRequest listDlpJobsRequest =
51+
ListDlpJobsRequest.newBuilder()
52+
.setParent(ProjectName.of(projectId).toString())
53+
.setFilter(filter)
54+
.setType(jobType)
55+
.build();
56+
DlpServiceClient.ListDlpJobsPagedResponse response =
57+
dlpServiceClient.listDlpJobs(listDlpJobsRequest);
58+
for (DlpJob dlpJob : response.getPage().getValues()) {
59+
System.out.println(dlpJob.getName() + " -- " + dlpJob.getState());
60+
}
61+
}
62+
}
63+
// [END dlp_list_jobs]
64+
65+
/**
66+
* Delete a DLP Job
67+
*
68+
* @param projectId Google Cloud ProjectID
69+
* @param jobId DLP Job ID
70+
*/
71+
// [START dlp_delete_job]
72+
private static void deleteJob(String projectId, String jobId) {
73+
74+
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
75+
// construct complete job name
76+
DlpJobName job = DlpJobName.of(projectId, jobId);
77+
78+
DeleteDlpJobRequest deleteDlpJobRequest =
79+
DeleteDlpJobRequest.newBuilder().setName(job.toString()).build();
80+
81+
// submit job deletion request
82+
dlpServiceClient.deleteDlpJob(deleteDlpJobRequest);
83+
84+
System.out.println("Job deleted successfully.");
85+
} catch (Exception e) {
86+
System.err.println("Error deleting DLP job: " + e.getMessage());
87+
}
88+
}
89+
// [END dlp_delete_job]
90+
91+
/** Command line application to list and delete DLP jobs the Data Loss Prevention API. */
92+
public static void main(String[] args) throws Exception {
93+
94+
OptionGroup optionsGroup = new OptionGroup();
95+
optionsGroup.setRequired(true);
96+
Option listOption = new Option("l", "list", false, "List DLP Jobs");
97+
optionsGroup.addOption(listOption);
98+
99+
Option deleteOption = new Option("d", "delete", false, "Delete DLP Jobs");
100+
optionsGroup.addOption(deleteOption);
101+
102+
Options commandLineOptions = new Options();
103+
commandLineOptions.addOptionGroup(optionsGroup);
104+
105+
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
106+
commandLineOptions.addOption(projectIdOption);
107+
108+
Option filterOption = Option.builder("filter").hasArg(true).required(false).build();
109+
commandLineOptions.addOption(filterOption);
110+
111+
Option jobTypeOption = Option.builder("jobType").hasArg(true).required(false).build();
112+
commandLineOptions.addOption(jobTypeOption);
113+
114+
Option jobIdOption = Option.builder("jobId").hasArg(true).required(false).build();
115+
commandLineOptions.addOption(jobIdOption);
116+
117+
CommandLineParser parser = new DefaultParser();
118+
HelpFormatter formatter = new HelpFormatter();
119+
CommandLine cmd;
120+
121+
try {
122+
cmd = parser.parse(commandLineOptions, args);
123+
} catch (ParseException e) {
124+
System.out.println(e.getMessage());
125+
formatter.printHelp(Inspect.class.getName(), commandLineOptions);
126+
System.exit(1);
127+
return;
128+
}
129+
130+
String projectId =
131+
cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
132+
133+
if (cmd.hasOption(listOption.getOpt())) {
134+
String filter = cmd.getOptionValue(filterOption.getOpt(), "");
135+
DlpJobType jobType =
136+
DlpJobType.valueOf(
137+
cmd.getOptionValue(
138+
jobTypeOption.getOpt(), DlpJobType.DLP_JOB_TYPE_UNSPECIFIED.name()));
139+
listJobs(projectId, filter, jobType);
140+
}
141+
142+
if (cmd.hasOption(deleteOption.getOpt())) {
143+
String jobId = cmd.getOptionValue(jobIdOption.getOpt());
144+
deleteJob(projectId, jobId);
145+
}
146+
}
147+
}

dlp/snippets/snippets/src/main/java/com/example/dlp/Metadata.java

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616

1717
package com.example.dlp;
1818

19-
import com.google.cloud.dlp.v2beta1.DlpServiceClient;
20-
import com.google.privacy.dlp.v2beta1.CategoryDescription;
21-
import com.google.privacy.dlp.v2beta1.InfoTypeDescription;
22-
import com.google.privacy.dlp.v2beta1.ListInfoTypesResponse;
23-
import com.google.privacy.dlp.v2beta1.ListRootCategoriesResponse;
19+
import com.google.cloud.dlp.v2.DlpServiceClient;
20+
import com.google.privacy.dlp.v2.InfoTypeDescription;
21+
import com.google.privacy.dlp.v2.ListInfoTypesRequest;
22+
import com.google.privacy.dlp.v2.ListInfoTypesResponse;
2423
import java.util.List;
2524
import org.apache.commons.cli.CommandLine;
2625
import org.apache.commons.cli.CommandLineParser;
@@ -32,48 +31,37 @@
3231

3332
public class Metadata {
3433

35-
private static void listInfoTypes(String category, String languageCode) throws Exception {
36-
// [START dlp_list_info_types]
34+
// [START dlp_list_info_types]
35+
/*
36+
* List the types of sensitive information the DLP API supports.
37+
*
38+
* @param filter The filter to use, e.g. "supported_by=INSPECT"
39+
* @param languageCode The BCP-47 language code to use, e.g. 'en-US'
40+
*/
41+
private static void listInfoTypes(String filter, String languageCode) throws Exception {
42+
3743
// Instantiate a DLP client
3844
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
39-
// The category of info types to list, e.g. category = 'GOVERNMENT';
40-
// Optional BCP-47 language code for localized info type friendly names, e.g. 'en-US'
41-
ListInfoTypesResponse infoTypesResponse = dlpClient.listInfoTypes(category, languageCode);
45+
ListInfoTypesRequest listInfoTypesRequest =
46+
ListInfoTypesRequest.newBuilder().setFilter(filter).setLanguageCode(languageCode).build();
47+
ListInfoTypesResponse infoTypesResponse = dlpClient.listInfoTypes(listInfoTypesRequest);
4248
List<InfoTypeDescription> infoTypeDescriptions = infoTypesResponse.getInfoTypesList();
4349
for (InfoTypeDescription infoTypeDescription : infoTypeDescriptions) {
4450
System.out.println("Name : " + infoTypeDescription.getName());
4551
System.out.println("Display name : " + infoTypeDescription.getDisplayName());
4652
}
4753
}
48-
// [END dlp_list_info_types]
49-
}
50-
51-
private static void listRootCategories(String languageCode) throws Exception {
52-
// [START dlp_list_categories]
53-
// Instantiate a DLP client
54-
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
55-
// The BCP-47 language code to use, e.g. 'en-US'
56-
// languageCode = 'en-US'
57-
ListRootCategoriesResponse rootCategoriesResponse =
58-
dlpClient.listRootCategories(languageCode);
59-
for (CategoryDescription categoryDescription : rootCategoriesResponse.getCategoriesList()) {
60-
System.out.println("Name : " + categoryDescription.getName());
61-
System.out.println("Display name : " + categoryDescription.getDisplayName());
62-
}
63-
}
64-
// [END dlp_list_categories]
6554
}
55+
// [END dlp_list_info_types]
6656

6757
/** Retrieve infoTypes. */
6858
public static void main(String[] args) throws Exception {
6959
Options options = new Options();
70-
Option languageCodeOption = new Option("language", null, true, "BCP-47 language code");
71-
languageCodeOption.setRequired(false);
60+
Option languageCodeOption = Option.builder("language").hasArg(true).required(false).build();
7261
options.addOption(languageCodeOption);
7362

74-
Option categoryOption = new Option("category", null, true, "Category of info types to list.");
75-
categoryOption.setRequired(false);
76-
options.addOption(categoryOption);
63+
Option filterOption = Option.builder("filter").hasArg(true).required(false).build();
64+
options.addOption(filterOption);
7765

7866
CommandLineParser parser = new DefaultParser();
7967
HelpFormatter formatter = new HelpFormatter();
@@ -87,11 +75,8 @@ public static void main(String[] args) throws Exception {
8775
return;
8876
}
8977
String languageCode = cmd.getOptionValue(languageCodeOption.getOpt(), "en-US");
90-
if (cmd.hasOption(categoryOption.getOpt())) {
91-
String category = cmd.getOptionValue(categoryOption.getOpt());
92-
listInfoTypes(category, languageCode);
93-
} else {
94-
listRootCategories(languageCode);
95-
}
78+
String filter = cmd.getOptionValue(filterOption.getOpt(), "");
79+
80+
listInfoTypes(filter, languageCode);
9681
}
9782
}

dlp/snippets/snippets/src/main/java/com/example/dlp/QuickStart.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616

1717
package com.example.dlp;
1818

19-
import com.google.cloud.dlp.v2beta1.DlpServiceClient;
20-
import com.google.privacy.dlp.v2beta1.ContentItem;
21-
import com.google.privacy.dlp.v2beta1.Finding;
22-
import com.google.privacy.dlp.v2beta1.InfoType;
23-
import com.google.privacy.dlp.v2beta1.InspectConfig;
24-
import com.google.privacy.dlp.v2beta1.InspectContentRequest;
25-
import com.google.privacy.dlp.v2beta1.InspectContentResponse;
26-
import com.google.privacy.dlp.v2beta1.InspectResult;
27-
import com.google.privacy.dlp.v2beta1.Likelihood;
19+
import com.google.cloud.ServiceOptions;
20+
import com.google.cloud.dlp.v2.DlpServiceClient;
21+
import com.google.privacy.dlp.v2.ByteContentItem;
22+
import com.google.privacy.dlp.v2.ContentItem;
23+
import com.google.privacy.dlp.v2.Finding;
24+
import com.google.privacy.dlp.v2.InfoType;
25+
import com.google.privacy.dlp.v2.InspectConfig;
26+
import com.google.privacy.dlp.v2.InspectContentRequest;
27+
import com.google.privacy.dlp.v2.InspectContentResponse;
28+
import com.google.privacy.dlp.v2.InspectResult;
29+
import com.google.privacy.dlp.v2.Likelihood;
30+
import com.google.privacy.dlp.v2.ProjectName;
31+
import com.google.protobuf.ByteString;
2832
import java.util.Arrays;
2933
import java.util.List;
3034

@@ -35,61 +39,68 @@ public class QuickStart {
3539
public static void main(String[] args) throws Exception {
3640

3741
// string to inspect
38-
String text = "Robert Frost";
42+
String text = "His name was Robert Frost";
3943

4044
// The minimum likelihood required before returning a match:
4145
// LIKELIHOOD_UNSPECIFIED, VERY_UNLIKELY, UNLIKELY, POSSIBLE, LIKELY, VERY_LIKELY, UNRECOGNIZED
42-
Likelihood minLikelihood = Likelihood.VERY_LIKELY;
46+
Likelihood minLikelihood = Likelihood.POSSIBLE;
4347

4448
// The maximum number of findings to report (0 = server maximum)
4549
int maxFindings = 0;
4650

4751
// The infoTypes of information to match
4852
List<InfoType> infoTypes =
4953
Arrays.asList(
50-
InfoType.newBuilder().setName("US_MALE_NAME").build(),
51-
InfoType.newBuilder().setName("US_FEMALE_NAME").build());
54+
InfoType.newBuilder().setName("PERSON_NAME").build(),
55+
InfoType.newBuilder().setName("US_STATE").build());
5256

5357
// Whether to include the matching string
5458
boolean includeQuote = true;
5559

5660
// instantiate a client
5761
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
5862

63+
InspectConfig.FindingLimits findingLimits =
64+
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(maxFindings).build();
65+
5966
InspectConfig inspectConfig =
6067
InspectConfig.newBuilder()
6168
.addAllInfoTypes(infoTypes)
6269
.setMinLikelihood(minLikelihood)
63-
.setMaxFindings(maxFindings)
70+
.setLimits(findingLimits)
6471
.setIncludeQuote(includeQuote)
6572
.build();
6673

67-
ContentItem contentItem =
68-
ContentItem.newBuilder().setType("text/plain").setValue(text).build();
74+
ByteContentItem byteContentItem =
75+
ByteContentItem.newBuilder()
76+
.setType(ByteContentItem.BytesType.TEXT_UTF8)
77+
.setData(ByteString.copyFromUtf8(text))
78+
.build();
79+
ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
6980

81+
String projectId = ServiceOptions.getDefaultProjectId();
7082
InspectContentRequest request =
7183
InspectContentRequest.newBuilder()
84+
.setParent(ProjectName.of(projectId).toString())
7285
.setInspectConfig(inspectConfig)
73-
.addItems(contentItem)
86+
.setItem(contentItem)
7487
.build();
7588

7689
// Inspect the text for info types
7790
InspectContentResponse response = dlpServiceClient.inspectContent(request);
7891

79-
// Print the response
80-
for (InspectResult result : response.getResultsList()) {
81-
if (result.getFindingsCount() > 0) {
82-
System.out.println("Findings: ");
83-
for (Finding finding : result.getFindingsList()) {
84-
if (includeQuote) {
85-
System.out.print("Quote: " + finding.getQuote());
86-
}
87-
System.out.print("\tInfo type: " + finding.getInfoType().getName());
88-
System.out.println("\tLikelihood: " + finding.getLikelihood());
92+
InspectResult result = response.getResult();
93+
if (result.getFindingsCount() > 0) {
94+
System.out.println("Findings: ");
95+
for (Finding finding : result.getFindingsList()) {
96+
if (includeQuote) {
97+
System.out.print("\tQuote: " + finding.getQuote());
8998
}
90-
} else {
91-
System.out.println("No findings.");
99+
System.out.print("\tInfo type: " + finding.getInfoType().getName());
100+
System.out.println("\tLikelihood: " + finding.getLikelihood());
92101
}
102+
} else {
103+
System.out.println("No findings.");
93104
}
94105
} catch (Exception e) {
95106
System.out.println("Error in inspectString: " + e.getMessage());

0 commit comments

Comments
 (0)