16
16
17
17
package com .example .dlp ;
18
18
19
+ import com .google .cloud .ServiceOptions ;
19
20
import com .google .cloud .dlp .v2 .DlpServiceClient ;
20
21
import com .google .privacy .dlp .v2 .CloudStorageOptions ;
21
22
import com .google .privacy .dlp .v2 .CreateJobTriggerRequest ;
26
27
import com .google .privacy .dlp .v2 .JobTrigger ;
27
28
import com .google .privacy .dlp .v2 .Likelihood ;
28
29
import com .google .privacy .dlp .v2 .ListJobTriggersRequest ;
30
+ import com .google .privacy .dlp .v2 .ProjectJobTriggerName ;
31
+ import com .google .privacy .dlp .v2 .ProjectName ;
29
32
import com .google .privacy .dlp .v2 .Schedule ;
30
33
import com .google .privacy .dlp .v2 .StorageConfig ;
31
34
import com .google .protobuf .Duration ;
@@ -50,7 +53,6 @@ public class Triggers {
50
53
* @param triggerId (Optional) name of the trigger to be created
51
54
* @param displayName (Optional) display name for the trigger to be created
52
55
* @param description (Optional) description for the trigger to be created
53
- * @param gcsUrl URL path to GCS bucket, eg. gs://my-bucket-name
54
56
* @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
55
57
* @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
56
58
* InfoType.EMAIL_ADDRESS
@@ -62,21 +64,24 @@ private static void createTrigger(
62
64
String triggerId ,
63
65
String displayName ,
64
66
String description ,
65
- String gcsUrl ,
67
+ String bucketName ,
68
+ String fileName ,
66
69
int scanPeriod ,
67
70
List <InfoType > infoTypes ,
68
71
Likelihood minLikelihood ,
69
72
int maxFindings ,
70
- String projectId ) {
73
+ String projectId ) throws Exception {
71
74
72
75
// instantiate a client
73
- try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
76
+ DlpServiceClient dlpServiceClient = DlpServiceClient .create ();
77
+ try {
74
78
75
- CloudStorageOptions .FileSet fileSet =
76
- CloudStorageOptions .FileSet .newBuilder ().setUrl (gcsUrl ).build ();
77
79
CloudStorageOptions cloudStorageOptions =
78
- CloudStorageOptions .newBuilder ().setFileSet (fileSet ).build ();
79
-
80
+ CloudStorageOptions .newBuilder ()
81
+ .setFileSet (
82
+ CloudStorageOptions .FileSet .newBuilder ()
83
+ .setUrl ("gs://" + bucketName + "/" + fileName ))
84
+ .build ();
80
85
StorageConfig storageConfig =
81
86
StorageConfig .newBuilder ().setCloudStorageOptions (cloudStorageOptions ).build ();
82
87
@@ -110,16 +115,17 @@ private static void createTrigger(
110
115
.addTriggers (trigger )
111
116
.build ();
112
117
118
+ System .out .println ("Pause" );
113
119
// Create scan request
114
120
CreateJobTriggerRequest createJobTriggerRequest =
115
121
CreateJobTriggerRequest .newBuilder ()
116
- .setParent (projectId )
122
+ .setParent (ProjectName . of ( projectId ). toString () )
117
123
.setJobTrigger (jobTrigger )
118
124
.build ();
119
125
120
126
JobTrigger createdJobTrigger = dlpServiceClient .createJobTrigger (createJobTriggerRequest );
121
127
122
- System .out .println ("Created Trigger: " + createdJobTrigger .getDisplayName ());
128
+ System .out .println ("Created Trigger: " + createdJobTrigger .getName ());
123
129
} catch (Exception e ) {
124
130
System .out .println ("Error creating trigger :" + e .getMessage ());
125
131
}
@@ -135,7 +141,8 @@ private static void listTriggers(String projectId) {
135
141
// Instantiates a client
136
142
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
137
143
ListJobTriggersRequest listJobTriggersRequest =
138
- ListJobTriggersRequest .newBuilder ().setParent (projectId ).build ();
144
+ ListJobTriggersRequest .newBuilder ()
145
+ .setParent (ProjectName .of (projectId ).toString ()).build ();
139
146
DlpServiceClient .ListJobTriggersPagedResponse response =
140
147
dlpServiceClient .listJobTriggers (listJobTriggersRequest );
141
148
response
@@ -172,11 +179,14 @@ private static void listTriggers(String projectId) {
172
179
private static void deleteTrigger (String projectId , String triggerId ) {
173
180
// Instantiates a client
174
181
// triggerName to provided as projects/project-id/jobTriggers/triggerId
175
- String triggerName = String .format ("projects/%s/jobTriggers/%s" , projectId , triggerId );
182
+
183
+ ProjectJobTriggerName triggerName = ProjectJobTriggerName .of (projectId , triggerId );
176
184
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
177
185
DeleteJobTriggerRequest deleteJobTriggerRequest =
178
- DeleteJobTriggerRequest .newBuilder ().setName (triggerName ).build ();
186
+ DeleteJobTriggerRequest .newBuilder ().setName (triggerName . toString () ).build ();
179
187
dlpServiceClient .deleteJobTrigger (deleteJobTriggerRequest );
188
+
189
+ System .out .println ("Trigger deleted: " + triggerName .toString ());
180
190
} catch (Exception e ) {
181
191
System .out .println ("Error deleting trigger :" + e .getMessage ());
182
192
}
@@ -191,20 +201,23 @@ public static void main(String[] args) throws Exception {
191
201
optionsGroup .setRequired (true );
192
202
193
203
Option createTriggerOption =
194
- new Option ("c" , "create" , true , "Create trigger to scan a GCS bucket" );
204
+ new Option ("c" , "create" , false , "Create trigger to scan a GCS bucket" );
195
205
optionsGroup .addOption (createTriggerOption );
196
206
197
- Option listTriggersOption = new Option ("l" , "list" , true , "List triggers" );
207
+ Option listTriggersOption = new Option ("l" , "list" , false , "List triggers" );
198
208
optionsGroup .addOption (listTriggersOption );
199
209
200
- Option deleteTriggerOption = new Option ("d" , "delete" , true , "Delete trigger" );
210
+ Option deleteTriggerOption = new Option ("d" , "delete" , false , "Delete trigger" );
201
211
optionsGroup .addOption (deleteTriggerOption );
202
212
203
213
Options commandLineOptions = new Options ();
204
214
commandLineOptions .addOptionGroup (optionsGroup );
205
215
206
- Option gcsUrlOption = Option .builder ("gcsUrl" ).hasArg (true ).required (false ).build ();
207
- commandLineOptions .addOption (gcsUrlOption );
216
+ Option bucketNameOption = Option .builder ("bucketName" ).hasArg (true ).required (false ).build ();
217
+ commandLineOptions .addOption (bucketNameOption );
218
+
219
+ Option gcsFileNameOption = Option .builder ("fileName" ).hasArg (true ).required (false ).build ();
220
+ commandLineOptions .addOption (gcsFileNameOption );
208
221
209
222
Option minLikelihoodOption =
210
223
Option .builder ("minLikelihood" ).hasArg (true ).required (false ).build ();
@@ -223,10 +236,14 @@ public static void main(String[] args) throws Exception {
223
236
commandLineOptions .addOption (projectIdOption );
224
237
225
238
Option triggerIdOption = Option .builder ("triggerId" ).hasArg (true ).required (false ).build ();
239
+ commandLineOptions .addOption (triggerIdOption );
226
240
Option displayNameOption = Option .builder ("displayName" ).hasArg (true ).required (false ).build ();
241
+ commandLineOptions .addOption (displayNameOption );
227
242
Option descriptionOption = Option .builder ("description" ).hasArg (true ).required (false ).build ();
243
+ commandLineOptions .addOption (descriptionOption );
228
244
229
245
Option scanPeriodOption = Option .builder ("scanPeriod" ).hasArg (true ).required (false ).build ();
246
+ commandLineOptions .addOption (scanPeriodOption );
230
247
231
248
CommandLineParser parser = new DefaultParser ();
232
249
HelpFormatter formatter = new HelpFormatter ();
@@ -241,7 +258,8 @@ public static void main(String[] args) throws Exception {
241
258
return ;
242
259
}
243
260
244
- String projectId = cmd .getOptionValue (projectIdOption .getOpt ());
261
+ String projectId =
262
+ cmd .getOptionValue (projectIdOption .getOpt (), ServiceOptions .getDefaultProjectId ());
245
263
if (cmd .hasOption ("c" )) {
246
264
Likelihood minLikelihood =
247
265
Likelihood .valueOf (
@@ -251,7 +269,8 @@ public static void main(String[] args) throws Exception {
251
269
String triggerId = cmd .getOptionValue (triggerIdOption .getOpt ());
252
270
String displayName = cmd .getOptionValue (displayNameOption .getOpt (), "" );
253
271
String description = cmd .getOptionValue (descriptionOption .getOpt (), "" );
254
- String gcsUrl = cmd .getOptionValue (gcsUrlOption .getOpt ());
272
+ String bucketName = cmd .getOptionValue (bucketNameOption .getOpt ());
273
+ String fileName = cmd .getOptionValue (gcsFileNameOption .getOpt ());
255
274
int scanPeriod = Integer .valueOf (cmd .getOptionValue (scanPeriodOption .getOpt ()));
256
275
List <InfoType > infoTypesList = new ArrayList <>();
257
276
if (cmd .hasOption (infoTypesOption .getOpt ())) {
@@ -265,7 +284,8 @@ public static void main(String[] args) throws Exception {
265
284
triggerId ,
266
285
displayName ,
267
286
description ,
268
- gcsUrl ,
287
+ bucketName ,
288
+ fileName ,
269
289
scanPeriod ,
270
290
infoTypesList ,
271
291
minLikelihood ,
0 commit comments