Skip to content

Commit 892a7c3

Browse files
authored
Create Java code sample for get jobs (GoogleCloudPlatform#3027)
* Update JobsTests.java * Add files via upload * Update JobsCreate.java * Update JobsTests.java * Update JobsTests.java * Update JobsTests.java * Update JobsTests.java
1 parent d0da3bf commit 892a7c3

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

dlp/src/main/java/dlp/snippets/JobsCreate.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.privacy.dlp.v2.Action;
2323
import com.google.privacy.dlp.v2.CloudStorageOptions;
2424
import com.google.privacy.dlp.v2.CreateDlpJobRequest;
25+
import com.google.privacy.dlp.v2.DlpJob;
2526
import com.google.privacy.dlp.v2.InfoType;
2627
import com.google.privacy.dlp.v2.InspectConfig;
2728
import com.google.privacy.dlp.v2.InspectJobConfig;
@@ -109,9 +110,9 @@ public static void createJobs(String projectId, String gcsPath) throws IOExcepti
109110
.setInspectJob(inspectJobConfig)
110111
.build();
111112

112-
// Send the job creation request
113-
dlpServiceClient.createDlpJob(createDlpJobRequest);
114-
System.out.println("Job created successfully.");
113+
// Send the job creation request and process the response.
114+
DlpJob createdDlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
115+
System.out.println("Job created successfully: " + createdDlpJob.getName());
115116
}
116117
}
117118
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_get_job]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.privacy.dlp.v2.DlpJobName;
23+
import com.google.privacy.dlp.v2.GetDlpJobRequest;
24+
import java.io.IOException;
25+
26+
public class JobsGet {
27+
public static void getJobs() throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-project-id";
30+
String jobId = "your-job-id";
31+
getJobs(projectId, jobId);
32+
}
33+
34+
// Gets a DLP Job with the given jobId
35+
public static void getJobs(String projectId, String jobId) throws IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
40+
41+
// Construct the complete job name from the projectId and jobId
42+
DlpJobName jobName = DlpJobName.of(projectId, jobId);
43+
44+
// Construct the get job request to be sent by the client.
45+
GetDlpJobRequest getDlpJobRequest =
46+
GetDlpJobRequest.newBuilder().setName(jobName.toString()).build();
47+
48+
// Send the get job request
49+
dlpServiceClient.getDlpJob(getDlpJobRequest);
50+
System.out.println("Job got successfully.");
51+
}
52+
}
53+
}
54+
// [END dlp_get_job]

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.privacy.dlp.v2.CloudStorageOptions;
2424
import com.google.privacy.dlp.v2.CloudStorageOptions.FileSet;
2525
import com.google.privacy.dlp.v2.CreateDlpJobRequest;
26+
import com.google.privacy.dlp.v2.DeleteDlpJobRequest;
2627
import com.google.privacy.dlp.v2.DlpJob;
2728
import com.google.privacy.dlp.v2.InspectConfig;
2829
import com.google.privacy.dlp.v2.InspectJobConfig;
@@ -103,7 +104,35 @@ public void testCreateJobs() throws Exception {
103104
// Call createJobs to create a Dlp job from project id and gcs path.
104105
JobsCreate.createJobs(PROJECT_ID, GCS_PATH);
105106
String output = bout.toString();
106-
assertThat(output, CoreMatchers.containsString("Job created successfully."));
107+
assertThat(output, CoreMatchers.containsString("Job created successfully:"));
108+
109+
// Delete the created Dlp Job
110+
String dlpJobName = output.split("Job created successfully: ")[1].split("\n")[0];
111+
DeleteDlpJobRequest deleteDlpJobRequest =
112+
DeleteDlpJobRequest.newBuilder().setName(dlpJobName).build();
113+
try (DlpServiceClient client = DlpServiceClient.create()) {
114+
client.deleteDlpJob(deleteDlpJobRequest);
115+
}
116+
}
117+
118+
@Test
119+
public void testGetJobs() throws Exception {
120+
// Create a job with a unique UUID to be gotten
121+
String jobId = UUID.randomUUID().toString();
122+
DlpJob createdDlpJob = createJob(jobId);
123+
124+
// Get the job with the specified ID
125+
JobsGet.getJobs(PROJECT_ID, "i-" + jobId);
126+
String output = bout.toString();
127+
assertThat(output, CoreMatchers.containsString("Job got successfully."));
128+
129+
// Delete the created Dlp Job
130+
String dlpJobName = createdDlpJob.getName();
131+
DeleteDlpJobRequest deleteDlpJobRequest =
132+
DeleteDlpJobRequest.newBuilder().setName(dlpJobName).build();
133+
try (DlpServiceClient client = DlpServiceClient.create()) {
134+
client.deleteDlpJob(deleteDlpJobRequest);
135+
}
107136
}
108137

109138
@Test

0 commit comments

Comments
 (0)