Search in sources :

Example 1 with CreateJobTriggerRequest

use of com.google.privacy.dlp.v2.CreateJobTriggerRequest in project java-docs-samples by GoogleCloudPlatform.

the class Triggers method createTrigger.

// [START dlp_create_trigger]
/**
 * Schedule a DLP inspection trigger for a GCS location.
 *
 * @param triggerId (Optional) name of the trigger to be created
 * @param displayName (Optional) display name for the trigger to be created
 * @param description (Optional) description for the trigger to be created
 * @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
 * @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
 *     InfoType.EMAIL_ADDRESS
 * @param minLikelihood minimum likelihood required before returning a match
 * @param maxFindings maximum number of findings to report per request (0 = server maximum)
 * @param projectId The project ID to run the API call under
 */
private static void createTrigger(String triggerId, String displayName, String description, String bucketName, String fileName, int scanPeriod, List<InfoType> infoTypes, Likelihood minLikelihood, int maxFindings, String projectId) throws Exception {
    // instantiate a client
    DlpServiceClient dlpServiceClient = DlpServiceClient.create();
    try {
        CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
        StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
        InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
        InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setInspectConfig(inspectConfig).setStorageConfig(storageConfig).build();
        // Schedule scan of GCS bucket every scanPeriod number of days (minimum = 1 day)
        Duration duration = Duration.newBuilder().setSeconds(scanPeriod * 24 * 3600).build();
        Schedule schedule = Schedule.newBuilder().setRecurrencePeriodDuration(duration).build();
        JobTrigger.Trigger trigger = JobTrigger.Trigger.newBuilder().setSchedule(schedule).build();
        JobTrigger jobTrigger = JobTrigger.newBuilder().setInspectJob(inspectJobConfig).setName(triggerId).setDisplayName(displayName).setDescription(description).setStatus(JobTrigger.Status.HEALTHY).addTriggers(trigger).build();
        // Create scan request
        CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setJobTrigger(jobTrigger).build();
        JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);
        System.out.println("Created Trigger: " + createdJobTrigger.getName());
    } catch (Exception e) {
        System.out.println("Error creating trigger: " + e.getMessage());
    }
}
Also used : StorageConfig(com.google.privacy.dlp.v2.StorageConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Schedule(com.google.privacy.dlp.v2.Schedule) CloudStorageOptions(com.google.privacy.dlp.v2.CloudStorageOptions) Duration(com.google.protobuf.Duration) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) JobTrigger(com.google.privacy.dlp.v2.JobTrigger) ParseException(org.apache.commons.cli.ParseException) InspectJobConfig(com.google.privacy.dlp.v2.InspectJobConfig) CreateJobTriggerRequest(com.google.privacy.dlp.v2.CreateJobTriggerRequest)

Aggregations

DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)1 CloudStorageOptions (com.google.privacy.dlp.v2.CloudStorageOptions)1 CreateJobTriggerRequest (com.google.privacy.dlp.v2.CreateJobTriggerRequest)1 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)1 InspectJobConfig (com.google.privacy.dlp.v2.InspectJobConfig)1 JobTrigger (com.google.privacy.dlp.v2.JobTrigger)1 Schedule (com.google.privacy.dlp.v2.Schedule)1 StorageConfig (com.google.privacy.dlp.v2.StorageConfig)1 Duration (com.google.protobuf.Duration)1 ParseException (org.apache.commons.cli.ParseException)1