Skip to content

Commit cd75921

Browse files
author
Ace Nassri
committed
Address PR feedback
1 parent cef37d5 commit cd75921

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ private static void inspectString(
132132
}
133133
// [END dlp_inspect_string]
134134

135+
// [START dlp_inspect_file]
135136
/**
136-
* [START dlp_inspect_file]
137+
* Inspect a local file
137138
*
138139
* @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
139140
* @param minLikelihood The minimum likelihood required before returning a match
@@ -217,15 +218,13 @@ private static void inspectFile(
217218
System.out.println("No findings.");
218219
}
219220
} catch (Exception e) {
220-
e.printStackTrace();
221221
System.out.println("Error in inspectFile: " + e.getMessage());
222222
}
223223
}
224224
// [END dlp_inspect_file]
225225

226+
// [START dlp_inspect_gcs]
226227
/**
227-
* [START dlp_inspect_gcs]
228-
*
229228
* Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub
230229
* notification
231230
*
@@ -285,7 +284,7 @@ private static void inspectGcsFile(
285284
.addActions(action)
286285
.build();
287286

288-
// asynchronously submit an inspect job, and wait on results
287+
// Semi-synchronously submit an inspect job, and wait on results
289288
CreateDlpJobRequest createDlpJobRequest =
290289
CreateDlpJobRequest.newBuilder()
291290
.setParent(ProjectName.of(projectId).toString())
@@ -298,7 +297,7 @@ private static void inspectGcsFile(
298297

299298
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
300299

301-
// setup a Pub/Sub subscriber to listen on the job completion status
300+
// Set up a Pub/Sub subscriber to listen on the job completion status
302301
Subscriber subscriber =
303302
Subscriber.newBuilder(
304303
ProjectSubscriptionName.of(projectId, subscriptionId),
@@ -313,10 +312,11 @@ private static void inspectGcsFile(
313312
.build();
314313
subscriber.startAsync();
315314

316-
// wait for job completion
315+
// Wait for job completion semi-synchronously
316+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
317317
try{
318318
done.get(1, TimeUnit.MINUTES);
319-
Thread.sleep(500);
319+
Thread.sleep(500); // Wait for the job to become available
320320
} catch (Exception e){
321321
System.out.println("Unable to verify job completion.");
322322
}
@@ -338,8 +338,8 @@ private static void inspectGcsFile(
338338
System.out.println("No findings.");
339339
}
340340
}
341-
// [END dlp_inspect_gcs]
342341
}
342+
// [END dlp_inspect_gcs]
343343

344344
// [START dlp_inspect_datastore]
345345
/**
@@ -403,7 +403,7 @@ private static void inspectDatastore(
403403
.addActions(action)
404404
.build();
405405

406-
// asynchronously submit an inspect job, and wait on results
406+
// Asynchronously submit an inspect job, and wait on results
407407
CreateDlpJobRequest createDlpJobRequest =
408408
CreateDlpJobRequest.newBuilder()
409409
.setParent(ProjectName.of(projectId).toString())
@@ -416,7 +416,7 @@ private static void inspectDatastore(
416416

417417
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
418418

419-
// setup a Pub/Sub subscriber to listen on the job completion status
419+
// Set up a Pub/Sub subscriber to listen on the job completion status
420420
Subscriber subscriber =
421421
Subscriber.newBuilder(
422422
ProjectSubscriptionName.of(projectId, subscriptionId),
@@ -431,10 +431,11 @@ private static void inspectDatastore(
431431
.build();
432432
subscriber.startAsync();
433433

434-
// wait for job completion
434+
// Wait for job completion semi-synchronously
435+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
435436
try{
436437
done.get(1, TimeUnit.MINUTES);
437-
Thread.sleep(500);
438+
Thread.sleep(500); // Wait for the job to become available
438439
} catch (Exception e){
439440
System.out.println("Unable to verify job completion.");
440441
}
@@ -462,8 +463,9 @@ private static void inspectDatastore(
462463
}
463464
// [END dlp_inspect_datastore]
464465

466+
// [START dlp_inspect_bigquery]
465467
/**
466-
* [START dlp_inspect_bigquery]
468+
* Inspect a BigQuery table
467469
*
468470
* @param projectId The project ID to run the API call under
469471
* @param datasetId The ID of the dataset to inspect, e.g. 'my_dataset'
@@ -524,7 +526,7 @@ private static void inspectBigquery(
524526
.addActions(action)
525527
.build();
526528

527-
// asynchronously submit an inspect job, and wait on results
529+
// Asynchronously submit an inspect job, and wait on results
528530
CreateDlpJobRequest createDlpJobRequest =
529531
CreateDlpJobRequest.newBuilder()
530532
.setParent(ProjectName.of(projectId).toString())
@@ -535,10 +537,11 @@ private static void inspectBigquery(
535537

536538
System.out.println("Job created with ID:" + dlpJob.getName());
537539

538-
// wait on job completion
540+
// Wait for job completion semi-synchronously
541+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
539542
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
540543

541-
// setup a Pub/Sub subscriber to listen on the job completion status
544+
// Set up a Pub/Sub subscriber to listen on the job completion status
542545
Subscriber subscriber =
543546
Subscriber.newBuilder(
544547
ProjectSubscriptionName.of(projectId, subscriptionId),
@@ -555,7 +558,7 @@ private static void inspectBigquery(
555558

556559
try{
557560
done.get(1, TimeUnit.MINUTES);
558-
Thread.sleep(500);
561+
Thread.sleep(500); // Wait for the job to become available
559562
} catch (Exception e){
560563
System.out.println("Unable to verify job completion.");
561564
}

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static void numericalStatsAnalysis(
8383
String subscriptionId)
8484
throws Exception {
8585

86-
// instantiate a client
86+
// Instantiates a client
8787
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
8888
BigQueryTable bigQueryTable =
8989
BigQueryTable.newBuilder()
@@ -104,7 +104,7 @@ private static void numericalStatsAnalysis(
104104

105105
PublishToPubSub publishToPubSub = PublishToPubSub.newBuilder().setTopic(topicName).build();
106106

107-
// create action to publish job status notifications over Google Cloud Pub/Sub
107+
// Create action to publish job status notifications over Google Cloud Pub/Sub
108108
Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
109109

110110
RiskAnalysisJobConfig riskAnalysisJobConfig =
@@ -125,7 +125,7 @@ private static void numericalStatsAnalysis(
125125

126126
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
127127

128-
// setup a Pub/Sub subscriber to listen on the job completion status
128+
// Set up a Pub/Sub subscriber to listen on the job completion status
129129
Subscriber subscriber =
130130
Subscriber.newBuilder(
131131
ProjectSubscriptionName.newBuilder()
@@ -143,15 +143,16 @@ private static void numericalStatsAnalysis(
143143
.build();
144144
subscriber.startAsync();
145145

146-
// wait for job completion
146+
// Wait for job completion semi-synchronously
147+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
147148
try{
148149
done.get(1, TimeUnit.MINUTES);
149-
Thread.sleep(500);
150+
Thread.sleep(500); // Wait for the job to become available
150151
} catch (TimeoutException e) {
151152
System.out.println("Unable to verify job completion.");
152153
}
153154

154-
// retrieve completed job status
155+
// Retrieve completed job status
155156
DlpJob completedJob =
156157
dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJobName).build());
157158

@@ -198,7 +199,7 @@ private static void categoricalStatsAnalysis(
198199
String topicId,
199200
String subscriptionId){
200201

201-
// instantiate a client
202+
// Instantiates a client
202203
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
203204

204205
FieldId fieldId = FieldId.newBuilder().setName(columnName).build();
@@ -222,7 +223,7 @@ private static void categoricalStatsAnalysis(
222223
.setTopic(topicName.toString())
223224
.build();
224225

225-
// create action to publish job status notifications over Google Cloud Pub/Sub
226+
// Create action to publish job status notifications over Google Cloud Pub/Sub
226227
Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
227228

228229
RiskAnalysisJobConfig riskAnalysisJobConfig =
@@ -243,7 +244,7 @@ private static void categoricalStatsAnalysis(
243244

244245
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
245246

246-
// setup a Pub/Sub subscriber to listen on the job completion status
247+
// Set up a Pub/Sub subscriber to listen on the job completion status
247248
Subscriber subscriber =
248249
Subscriber.newBuilder(
249250
ProjectSubscriptionName.newBuilder()
@@ -261,15 +262,16 @@ private static void categoricalStatsAnalysis(
261262
.build();
262263
subscriber.startAsync();
263264

264-
// wait for job completion
265+
// Wait for job completion semi-synchronously
266+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
265267
try{
266268
done.get(1, TimeUnit.MINUTES);
267-
Thread.sleep(500);
269+
Thread.sleep(500); // Wait for the job to become available
268270
} catch (TimeoutException e) {
269271
System.out.println("Unable to verify job completion.");
270272
}
271273

272-
// retrieve completed job status
274+
// Retrieve completed job status
273275
DlpJob completedJob =
274276
dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJobName).build());
275277

@@ -316,7 +318,7 @@ private static void calculateKAnonymity(
316318
String topicId,
317319
String subscriptionId)
318320
throws Exception {
319-
// instantiate a client
321+
// Instantiates a client
320322
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
321323

322324
List<FieldId> quasiIdFields =
@@ -342,7 +344,7 @@ private static void calculateKAnonymity(
342344

343345
PublishToPubSub publishToPubSub = PublishToPubSub.newBuilder().setTopic(topicName).build();
344346

345-
// create action to publish job status notifications over Google Cloud Pub/Sub
347+
// Create action to publish job status notifications over Google Cloud Pub/Sub
346348
Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
347349

348350
RiskAnalysisJobConfig riskAnalysisJobConfig =
@@ -363,7 +365,7 @@ private static void calculateKAnonymity(
363365

364366
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
365367

366-
// setup a Pub/Sub subscriber to listen on the job completion status
368+
// Set up a Pub/Sub subscriber to listen on the job completion status
367369
Subscriber subscriber =
368370
Subscriber.newBuilder(
369371
ProjectSubscriptionName.newBuilder()
@@ -381,15 +383,16 @@ private static void calculateKAnonymity(
381383
.build();
382384
subscriber.startAsync();
383385

384-
// wait for job completion
386+
// Wait for job completion semi-synchronously
387+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
385388
try{
386389
done.get(1, TimeUnit.MINUTES);
387-
Thread.sleep(500);
390+
Thread.sleep(500); // Wait for the job to become available
388391
} catch (TimeoutException e) {
389392
System.out.println("Unable to verify job completion.");
390393
}
391394

392-
// retrieve completed job status
395+
// Retrieve completed job status
393396
DlpJob completedJob =
394397
dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJobName).build());
395398

@@ -421,9 +424,8 @@ private static void calculateKAnonymity(
421424
}
422425
// [END dlp_k_anonymity]
423426

427+
// [START dlp_l_diversity]
424428
/**
425-
* [START dlp_l_diversity]
426-
*
427429
* Calculate l-diversity for an attribute relative to quasi-identifiers in a BigQuery table.
428430
*
429431
* @param projectId The Google Cloud Platform project ID to run the API call under.
@@ -445,7 +447,7 @@ private static void calculateLDiversity(
445447
String subscriptionId)
446448
throws Exception {
447449

448-
// instantiate a client
450+
// Instantiates a client
449451
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
450452

451453
FieldId sensitiveAttributeField = FieldId.newBuilder().setName(sensitiveAttribute).build();
@@ -476,7 +478,7 @@ private static void calculateLDiversity(
476478

477479
PublishToPubSub publishToPubSub = PublishToPubSub.newBuilder().setTopic(topicName).build();
478480

479-
// create action to publish job status notifications over Google Cloud Pub/Sub
481+
// Create action to publish job status notifications over Google Cloud Pub/Sub
480482
Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
481483

482484
RiskAnalysisJobConfig riskAnalysisJobConfig =
@@ -497,7 +499,7 @@ private static void calculateLDiversity(
497499

498500
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
499501

500-
// setup a Pub/Sub subscriber to listen on the job completion status
502+
// Set up a Pub/Sub subscriber to listen on the job completion status
501503
Subscriber subscriber =
502504
Subscriber.newBuilder(
503505
ProjectSubscriptionName.newBuilder()
@@ -515,10 +517,11 @@ private static void calculateLDiversity(
515517
.build();
516518
subscriber.startAsync();
517519

518-
// wait for job completion
520+
// Wait for job completion semi-synchronously
521+
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
519522
try{
520523
done.get(1, TimeUnit.MINUTES);
521-
Thread.sleep(500);
524+
Thread.sleep(500); // Wait for the job to become available
522525
} catch (TimeoutException e) {
523526
System.out.println("Unable to verify job completion.");
524527
}
@@ -554,8 +557,8 @@ private static void calculateLDiversity(
554557
} catch (Exception e) {
555558
System.out.println("Error in lDiversityAnalysis: " + e.getMessage());
556559
}
557-
// [END dlp_l_diversity]
558560
}
561+
// [END dlp_l_diversity]
559562

560563
/**
561564
* Command line application to perform risk analysis using the Data Loss Prevention API. Supported

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343

4444
public class Templates {
4545

46+
// [START dlp_create_inspect_template]
4647
/**
47-
* [START dlp_create_inspect_template]
48+
* Create a new DLP inspection configuration template.
4849
*
4950
* @param displayName (Optional) The human-readable name to give the template
5051
* @param projectId Google Cloud Project ID to call the API under

dlp/src/test/resources/dates.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ name,birth_date,credit_card,register_date
22
Ann,01/01/1970,4532908762519852,07/21/1996
33
James,03/06/1988,4301261899725540,04/09/2001
44
Dan,08/14/1945,4620761856015295,11/15/2011
5-
Laura,11/03/1992,4564981067258901,01/04/2017
5+
Laura,11/03/1992,4564981067258901,01/04/2017

0 commit comments

Comments
 (0)