Skip to content

Commit ba79255

Browse files
Cleanup DLP Metadata and Jobs snippets (#2054)
* refactored Metadata.java and corresponding tests to snippet format * Refactored Jobs.java into snippets and updated integration tests * removed unnecessary imports * added step to create job in JobTests.java * Update dlp/src/main/java/dlp/snippets/InspectTextFile.java Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * Update dlp/src/main/java/dlp/snippets/InfoTypesList.java Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * Update dlp/src/main/java/dlp/snippets/InfoTypesList.java Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * Update dlp/src/main/java/dlp/snippets/InspectGcsFile.java Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * minor refactoring and fixups * added @BeforeClass annotation to checkRequirements * updated documentation * removed GCS_PATH env variable * updated jobs tests * updated imports Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
1 parent 36af560 commit ba79255

20 files changed

+427
-399
lines changed

dlp/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ Commands:
6969
## Integration tests
7070
### Setup
7171
- [Create a Google Cloud Storage bucket](https://console.cloud.google.com/storage) and upload [test.txt](src/test/resources/test.txt).
72+
- Set the `GCS_PATH` environment variable to point to the path for the bucket.
7273
- [Create a Google Cloud Datastore](https://console.cloud.google.com/datastore) kind and add an entity with properties:
7374
- `property1` : john@doe.com
7475
- `property2` : 343-343-3435
75-
- Update the Google Cloud Storage path and Datastore kind in [InspectIT.java](src/test/java/com/example/dlp/InspectIT.java).
76+
- [Create a Google Cloud Pub/Sub](https://console.cloud.google.com/datastore) topic with the id `dlp-tests` and a subscription with the id `dlp-test`
77+
- Set the `PUB_SUB_TOPIC_ID` and `PUB_SUB_SUBSCRIPTION_ID` to the corresponding values.
78+
- Update the Google Cloud Storage path and Datastore kind in [InspectTests.java](src/test/java/dlp/snippets/InspectTests.java).
7679
- Ensure that `GOOGLE_APPLICATION_CREDENTIALS` points to authorized service account credentials file.
7780

7881
## Run

dlp/src/main/java/com/example/dlp/Jobs.java

Lines changed: 0 additions & 146 deletions
This file was deleted.

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

Lines changed: 0 additions & 82 deletions
This file was deleted.

dlp/src/main/java/dlp/snippets/DeIdentifyWithDateShift.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class DeIdentifyWithDateShift {
3333

3434
public static void deIdentifyWithDateShift() throws IOException {
3535
// TODO(developer): Replace these variables before running the sample.
36-
String projectId = "YOUR_PROJECT_ID";
36+
String projectId = "your-project-id";
3737
Path inputCsvFile = Paths.get("path/to/your/input/file.csv");
3838
Path outputCsvFile = Paths.get("path/to/your/output/file.csv");
3939
deIdentifyWithDateShift(projectId, inputCsvFile, outputCsvFile);

dlp/src/main/java/dlp/snippets/DeIdentifyWithFpe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DeIdentifyWithFpe {
2626

2727
public static void deIdentifyWithFpe() throws IOException {
2828
// TODO(developer): Replace these variables before running the sample.
29-
String projectId = "YOUR_PROJECT_ID";
29+
String projectId = "your-project-id";
3030
String textToDeIdentify = "I'm Gary and my email is gary@example.com";
3131
String kmsKeyName =
3232
"projects/YOUR_PROJECT/"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_list_info_types]
20+
import com.google.cloud.dlp.v2.DlpServiceClient;
21+
import com.google.privacy.dlp.v2.InfoTypeDescription;
22+
import com.google.privacy.dlp.v2.ListInfoTypesRequest;
23+
import com.google.privacy.dlp.v2.ListInfoTypesResponse;
24+
25+
import java.io.IOException;
26+
import java.util.List;
27+
28+
public class InfoTypesList {
29+
30+
// Lists the types of sensitive information the DLP API supports.
31+
public static void listInfoTypes() throws IOException {
32+
// Initialize client that will be used to send requests. This client only needs to be created
33+
// once, and can be reused for multiple requests. After completing all of your requests, call
34+
// the "close" method on the client to safely clean up any remaining background resources.
35+
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
36+
37+
// Construct the request to be sent by the client
38+
ListInfoTypesRequest listInfoTypesRequest = ListInfoTypesRequest.newBuilder()
39+
// Only return infoTypes supported by certain parts of the API.
40+
// Supported filters are "supported_by=INSPECT" and "supported_by=RISK_ANALYSIS"
41+
// Defaults to "supported_by=INSPECT"
42+
.setFilter("supported_by=INSPECT")
43+
// BCP-47 language code for localized infoType friendly names.
44+
// Defaults to "en_US"
45+
.setLanguageCode("en-US").build();
46+
47+
// Use the client to send the API request.
48+
ListInfoTypesResponse response = dlpClient.listInfoTypes(listInfoTypesRequest);
49+
50+
// Parse the response and process the results
51+
System.out.println("Infotypes found:");
52+
for (InfoTypeDescription infoTypeDescription : response.getInfoTypesList()) {
53+
System.out.println("Name : " + infoTypeDescription.getName());
54+
System.out.println("Display name : " + infoTypeDescription.getDisplayName());
55+
}
56+
}
57+
}
58+
}
59+
// [END dlp_list_info_types]

dlp/src/main/java/dlp/snippets/InspectGcsFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class InspectGcsFile {
5151
public static void inspectGcsFile() throws InterruptedException, ExecutionException, IOException {
5252
// TODO(developer): Replace these variables before running the sample.
5353
String projectId = "your-project-id";
54-
String gcsUri = "gs://" + "your-bucket-name" + "/path/to/your/image.png";
54+
String gcsUri = "gs://" + "your-bucket-name" + "/path/to/your/file.txt";
5555
String pubSubTopicId = "your-pubsub-topic-id";
5656
String pubSubSubscriptionId = "your-pubsub-subscription-id";
5757
inspectGcsFile(projectId, gcsUri, pubSubTopicId, pubSubSubscriptionId);

dlp/src/main/java/dlp/snippets/InspectImageFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class InspectImageFile {
3636

3737
public static void inspectImageFile() {
3838
// TODO(developer): Replace these variables before running the sample.
39-
String projectId = "my-project-id";
39+
String projectId = "your-project-id";
4040
String filePath = "path/to/image.png";
4141
inspectImageFile(projectId, filePath);
4242
}

dlp/src/main/java/dlp/snippets/InspectString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class InspectString {
3535

3636
public static void inspectString() {
3737
// TODO(developer): Replace these variables before running the sample.
38-
String projectId = "my-project-id";
38+
String projectId = "your-project-id";
3939
String textToInspect = "My name is Gary and my email is gary@example.com";
4040
inspectString(projectId, textToInspect);
4141
}

dlp/src/main/java/dlp/snippets/InspectTextFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public class InspectTextFile {
3636

3737
public static void inspectTextFile() {
3838
// TODO(developer): Replace these variables before running the sample.
39-
String projectId = "my-project-id";
40-
String filePath = "path/to/image.png";
39+
String projectId = "your-project-id";
40+
String filePath = "path/to/file.txt";
4141
inspectTextFile(projectId, filePath);
4242
}
4343

0 commit comments

Comments
 (0)