Skip to content

Commit 268796c

Browse files
committed
Update Template samples.
1 parent d44100c commit 268796c

File tree

2 files changed

+119
-25
lines changed

2 files changed

+119
-25
lines changed

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.privacy.dlp.v2.Likelihood;
2626
import com.google.privacy.dlp.v2.ListInspectTemplatesRequest;
2727
import com.google.privacy.dlp.v2.ListInspectTemplatesResponse;
28+
import com.google.privacy.dlp.v2.ProjectName;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031
import org.apache.commons.cli.CommandLine;
@@ -41,20 +42,19 @@ public class Templates {
4142
/**
4243
* [START dlp_create_template]
4344
*
45+
* @param displayName (Optional) The human-readable name to give the template
4446
* @param projectId Google Cloud Project ID to call the API under
4547
* @param templateId (Optional) The name of the template to be created
46-
* @param displayName (Optional) The human-readable name to give the template
4748
* @param infoTypeList The infoTypes of information to match
48-
* @param includeQuote Whether to include the matching string
4949
* @param minLikelihood The minimum likelihood required before returning a match
5050
* @param maxFindings The maximum number of findings to report per request (0 = server maximum)
5151
*/
5252
private static void createInspectTemplate(
53-
String projectId,
54-
String templateId,
5553
String displayName,
54+
String templateId,
55+
String description,
56+
String projectId,
5657
List<InfoType> infoTypeList,
57-
boolean includeQuote,
5858
Likelihood minLikelihood,
5959
int maxFindings) {
6060
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
@@ -67,19 +67,19 @@ private static void createInspectTemplate(
6767
InspectConfig.newBuilder()
6868
.addAllInfoTypes(infoTypeList)
6969
.setMinLikelihood(minLikelihood)
70-
.setIncludeQuote(includeQuote)
7170
.setLimits(findingLimits)
7271
.build();
7372

7473
InspectTemplate inspectTemplate =
7574
InspectTemplate.newBuilder()
7675
.setInspectConfig(inspectConfig)
7776
.setDisplayName(displayName)
77+
.setDescription(description)
7878
.build();
7979

8080
CreateInspectTemplateRequest createInspectTemplateRequest =
8181
CreateInspectTemplateRequest.newBuilder()
82-
.setParent(projectId)
82+
.setParent(ProjectName.of(projectId).toString())
8383
.setInspectTemplate(inspectTemplate)
8484
.setTemplateId(templateId)
8585
.build();
@@ -105,18 +105,20 @@ private static void listInspectTemplates(String projectId) {
105105
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
106106

107107
ListInspectTemplatesRequest request =
108-
ListInspectTemplatesRequest.newBuilder().setParent(projectId).setPageSize(1).build();
108+
ListInspectTemplatesRequest.newBuilder()
109+
.setParent(ProjectName.of(projectId).toString())
110+
.setPageSize(1).build();
109111

110112
ListInspectTemplatesPagedResponse response = dlpServiceClient.listInspectTemplates(request);
111113
ListInspectTemplatesPage page = response.getPage();
112114
ListInspectTemplatesResponse templatesResponse = page.getResponse();
113115

114116
for (InspectTemplate template : templatesResponse.getInspectTemplatesList()) {
115-
System.out.printf("Template name: %s", template.getName());
117+
System.out.printf("Template name: %s\n", template.getName());
116118
if (template.getDisplayName() != null) {
117-
System.out.printf("Template display name: %s", template.getDisplayName());
118-
System.out.printf("Template create time: %s", template.getCreateTime());
119-
System.out.printf("Template update time: %s", template.getUpdateTime());
119+
System.out.printf("Template display name: %s \n", template.getDisplayName());
120+
System.out.printf("Template create time: %s \n", template.getCreateTime());
121+
System.out.printf("Template update time: %s \n", template.getUpdateTime());
120122

121123
// print inspection config
122124
InspectConfig inspectConfig = template.getInspectConfig();
@@ -167,13 +169,13 @@ public static void main(String[] args) throws Exception {
167169
OptionGroup optionsGroup = new OptionGroup();
168170
optionsGroup.setRequired(true);
169171

170-
Option createOption = new Option("c", "create", true, "Create inspect template");
172+
Option createOption = new Option("c", "create", false, "Create inspect template");
171173
optionsGroup.addOption(createOption);
172174

173-
Option listOption = new Option("l", "list", true, "List inspect templates");
175+
Option listOption = new Option("l", "list", false, "List inspect templates");
174176
optionsGroup.addOption(listOption);
175177

176-
Option deleteOption = new Option("d", "delete", true, "Delete inspect template");
178+
Option deleteOption = new Option("d", "delete", false, "Delete inspect template");
177179
optionsGroup.addOption(deleteOption);
178180

179181
commandLineOptions.addOptionGroup(optionsGroup);
@@ -190,6 +192,9 @@ public static void main(String[] args) throws Exception {
190192
Option templateIdOption = Option.builder("templateId").hasArg(true).required(false).build();
191193
commandLineOptions.addOption(templateIdOption);
192194

195+
Option templateDescription = Option.builder("description").hasArg(true).required(false).build();
196+
commandLineOptions.addOption(templateDescription);
197+
193198
Option templateDisplayNameOption =
194199
Option.builder("displayName").hasArg(true).required(false).build();
195200
commandLineOptions.addOption(templateDisplayNameOption);
@@ -219,6 +224,7 @@ public static void main(String[] args) throws Exception {
219224
if (cmd.hasOption(createOption.getOpt())) {
220225
String templateId = cmd.getOptionValue(templateIdOption.getOpt());
221226
String displayName = cmd.getOptionValue(templateDisplayNameOption.getOpt());
227+
String description = cmd.getOptionValue(templateDescription.getOpt());
222228

223229
Likelihood minLikelihood =
224230
Likelihood.valueOf(
@@ -232,17 +238,10 @@ public static void main(String[] args) throws Exception {
232238
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
233239
}
234240
}
235-
Boolean includeQuote =
236-
Boolean.valueOf(cmd.getOptionValue(includeQuoteOption.getOpt(), "false"));
237-
int maxFindings = Integer.valueOf(maxFindingsOption.getOpt(), 0);
241+
int maxFindings = Integer.valueOf(cmd.getOptionValue(maxFindingsOption.getOpt(), "0"));
238242
createInspectTemplate(
239-
projectId,
240-
templateId,
241-
displayName,
242-
infoTypesList,
243-
includeQuote,
244-
minLikelihood,
245-
maxFindings);
243+
displayName, templateId, description, projectId,
244+
infoTypesList, minLikelihood, maxFindings);
246245

247246
} else if (cmd.hasOption(listOption.getOpt())) {
248247
listInspectTemplates(projectId);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 static org.hamcrest.CoreMatchers.containsString;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertThat;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.Date;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
// CHECKSTYLE OFF: AbbreviationAsWordInName
37+
public class TemplatesIT {
38+
// CHECKSTYLE ON: AbbreviationAsWordInName
39+
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
43+
@Before
44+
public void setUp() {
45+
bout = new ByteArrayOutputStream();
46+
out = new PrintStream(bout);
47+
System.setOut(out);
48+
assertNotNull(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
System.setOut(null);
54+
bout.reset();
55+
}
56+
57+
@Test
58+
public void testCreateInspectTemplate() throws Exception {
59+
Templates.main(new String[] {
60+
"-c",
61+
"-displayName", String.format("test-name-%s", new Date()),
62+
"-templateId", String.format("template%s", System.currentTimeMillis()),
63+
"-description", String.format("description-%s", new Date())
64+
});
65+
String output = bout.toString();
66+
assertThat(output, containsString("Template created: "));
67+
}
68+
69+
@Test
70+
public void testListInspectemplate() throws Exception {
71+
Templates.main(new String[] {
72+
"-l"
73+
});
74+
String output = bout.toString();
75+
assertThat(output, containsString("Template name:"));
76+
}
77+
78+
@Test
79+
public void testDeleteInspectTemplate() throws Exception {
80+
// Extract a Template ID
81+
Templates.main(new String[] { "-l" });
82+
String output = bout.toString();
83+
Matcher templateIds = Pattern.compile("template[0-9]+").matcher(output);
84+
assertTrue(templateIds.find());
85+
String templateId = templateIds.group(0);
86+
bout.reset();
87+
Templates.main(new String[] {
88+
"-d",
89+
"-templateId", templateId
90+
});
91+
output = bout.toString();
92+
assertThat(output, containsString("Deleted template:"));
93+
}
94+
95+
}

0 commit comments

Comments
 (0)