Skip to content

Commit e66b1fa

Browse files
chloeliu123jakubrauch
authored andcommitted
Create Java code sample for create job (GoogleCloudPlatform#3017)
https://cloud.google.com/dlp/docs/creating-job-triggers Fixes #Create Java code sample for create job > It's a good idea to open an issue first for discussion. > [yes] I have followed Sample Format Guide > [yes] pom.xml parent set to latest shared-configuration > [yes] Appropriate changes to README are included in PR > [nothing new] API's need to be enabled to test (tell us) > [nothing new] Environment Variables need to be set (ask us to set them) > [yes] Tests pass (mvn -P lint clean verify) > (Note- Checkstyle passing is required; Spotbugs, ErrorProne, PMD, etc. ERROR's are advisory only) > [yes] Please merge this PR for me once it is approved
1 parent b4a8225 commit e66b1fa

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2020 Google LLC
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 dlp.snippets;
18+
19+
// [START dlp_create_job]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.privacy.dlp.v2.Action;
23+
import com.google.privacy.dlp.v2.CloudStorageOptions;
24+
import com.google.privacy.dlp.v2.CreateDlpJobRequest;
25+
import com.google.privacy.dlp.v2.InfoType;
26+
import com.google.privacy.dlp.v2.InspectConfig;
27+
import com.google.privacy.dlp.v2.InspectJobConfig;
28+
import com.google.privacy.dlp.v2.Likelihood;
29+
import com.google.privacy.dlp.v2.ProjectName;
30+
import com.google.privacy.dlp.v2.StorageConfig;
31+
import com.google.privacy.dlp.v2.StorageConfig.TimespanConfig;
32+
import java.io.IOException;
33+
import java.util.List;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
36+
37+
public class JobsCreate {
38+
public static void createJobs() throws IOException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String projectId = "your-project-id";
41+
String gcsPath = "gs://" + "your-bucket-name" + "path/to/file.txt";
42+
createJobs(projectId, gcsPath);
43+
}
44+
45+
// Creates a DLP Job
46+
public static void createJobs(String projectId, String gcsPath) throws IOException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
51+
52+
// Set autoPopulateTimespan to true to scan only new content
53+
boolean autoPopulateTimespan = true;
54+
TimespanConfig timespanConfig =
55+
TimespanConfig.newBuilder()
56+
.setEnableAutoPopulationOfTimespanConfig(autoPopulateTimespan)
57+
.build();
58+
59+
// Specify the GCS file to be inspected.
60+
CloudStorageOptions cloudStorageOptions =
61+
CloudStorageOptions.newBuilder()
62+
.setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl(gcsPath))
63+
.build();
64+
StorageConfig storageConfig =
65+
StorageConfig.newBuilder()
66+
.setCloudStorageOptions(cloudStorageOptions)
67+
.setTimespanConfig(timespanConfig)
68+
.build();
69+
70+
// Specify the type of info the inspection will look for.
71+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
72+
List<InfoType> infoTypes =
73+
Stream.of("EMAIL_ADDRESS", "PERSON_NAME", "LOCATION", "PHONE_NUMBER")
74+
.map(it -> InfoType.newBuilder().setName(it).build())
75+
.collect(Collectors.toList());
76+
// The minimum likelihood required before returning a match:
77+
// See: https://cloud.google.com/dlp/docs/likelihood
78+
Likelihood minLikelihood = Likelihood.UNLIKELY;
79+
80+
// The maximum number of findings to report (0 = server maximum)
81+
InspectConfig.FindingLimits findingLimits =
82+
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(100).build();
83+
84+
InspectConfig inspectConfig =
85+
InspectConfig.newBuilder()
86+
.addAllInfoTypes(infoTypes)
87+
.setIncludeQuote(true)
88+
.setMinLikelihood(minLikelihood)
89+
.setLimits(findingLimits)
90+
.build();
91+
92+
// Specify the action that is triggered when the job completes.
93+
Action.PublishSummaryToCscc publishSummaryToCscc =
94+
Action.PublishSummaryToCscc.getDefaultInstance();
95+
Action action = Action.newBuilder().setPublishSummaryToCscc(publishSummaryToCscc).build();
96+
97+
// Configure the inspection job we want the service to perform.
98+
InspectJobConfig inspectJobConfig =
99+
InspectJobConfig.newBuilder()
100+
.setInspectConfig(inspectConfig)
101+
.setStorageConfig(storageConfig)
102+
.addActions(action)
103+
.build();
104+
105+
// Construct the job creation request to be sent by the client.
106+
CreateDlpJobRequest createDlpJobRequest =
107+
CreateDlpJobRequest.newBuilder()
108+
.setParent(ProjectName.of(projectId).toString())
109+
.setInspectJob(inspectJobConfig)
110+
.build();
111+
112+
// Send the job creation request
113+
dlpServiceClient.createDlpJob(createDlpJobRequest);
114+
System.out.println("Job created successfully.");
115+
}
116+
}
117+
}
118+
// [END dlp_create_job]

dlp/src/test/java/dlp/snippets/JobsTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ public void tearDown() {
9898
bout.reset();
9999
}
100100

101+
@Test
102+
public void testCreateJobs() throws Exception {
103+
// Call createJobs to create a Dlp job from project id and gcs path.
104+
JobsCreate.createJobs(PROJECT_ID, GCS_PATH);
105+
String output = bout.toString();
106+
assertThat(output, CoreMatchers.containsString("Job created successfully."));
107+
}
108+
101109
@Test
102110
public void testListJobs() throws Exception {
103111
// Call listJobs to print out a list of jobIds

0 commit comments

Comments
 (0)