|
| 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 com.google.cloud.ServiceOptions; |
| 20 | +import com.google.cloud.dlp.v2.DlpServiceClient; |
| 21 | +import com.google.privacy.dlp.v2.DeleteDlpJobRequest; |
| 22 | +import com.google.privacy.dlp.v2.DlpJob; |
| 23 | +import com.google.privacy.dlp.v2.DlpJobName; |
| 24 | +import com.google.privacy.dlp.v2.DlpJobType; |
| 25 | +import com.google.privacy.dlp.v2.ListDlpJobsRequest; |
| 26 | +import com.google.privacy.dlp.v2.ProjectName; |
| 27 | +import org.apache.commons.cli.CommandLine; |
| 28 | +import org.apache.commons.cli.CommandLineParser; |
| 29 | +import org.apache.commons.cli.DefaultParser; |
| 30 | +import org.apache.commons.cli.HelpFormatter; |
| 31 | +import org.apache.commons.cli.Option; |
| 32 | +import org.apache.commons.cli.OptionGroup; |
| 33 | +import org.apache.commons.cli.Options; |
| 34 | +import org.apache.commons.cli.ParseException; |
| 35 | + |
| 36 | +public class Jobs { |
| 37 | + |
| 38 | + // [START dlp_list_jobs] |
| 39 | + /* |
| 40 | + * List DLP jobs |
| 41 | + * |
| 42 | + * @param projectId The project ID to run the API call under |
| 43 | + * @param filter The filter expression to use, eg. state=DONE For more information on filter |
| 44 | + * syntax see https://cloud.google.com/dlp/docs/reference/rest/v2/projects.dlpJobs/list |
| 45 | + * @param jobType The type of job to list (either 'INSPECT_JOB' or 'RISK_ANALYSIS_JOB') |
| 46 | + */ |
| 47 | + private static void listJobs(String projectId, String filter, DlpJobType jobType) |
| 48 | + throws Exception { |
| 49 | + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
| 50 | + ListDlpJobsRequest listDlpJobsRequest = |
| 51 | + ListDlpJobsRequest.newBuilder() |
| 52 | + .setParent(ProjectName.of(projectId).toString()) |
| 53 | + .setFilter(filter) |
| 54 | + .setType(jobType) |
| 55 | + .build(); |
| 56 | + DlpServiceClient.ListDlpJobsPagedResponse response = |
| 57 | + dlpServiceClient.listDlpJobs(listDlpJobsRequest); |
| 58 | + for (DlpJob dlpJob : response.getPage().getValues()) { |
| 59 | + System.out.println(dlpJob.getName() + " -- " + dlpJob.getState()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + // [END dlp_list_jobs] |
| 64 | + |
| 65 | + /** |
| 66 | + * Delete a DLP Job |
| 67 | + * |
| 68 | + * @param projectId Google Cloud ProjectID |
| 69 | + * @param jobId DLP Job ID |
| 70 | + */ |
| 71 | + // [START dlp_delete_job] |
| 72 | + private static void deleteJob(String projectId, String jobId) { |
| 73 | + |
| 74 | + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
| 75 | + // construct complete job name |
| 76 | + DlpJobName job = DlpJobName.of(projectId, jobId); |
| 77 | + |
| 78 | + DeleteDlpJobRequest deleteDlpJobRequest = |
| 79 | + DeleteDlpJobRequest.newBuilder().setName(job.toString()).build(); |
| 80 | + |
| 81 | + // submit job deletion request |
| 82 | + dlpServiceClient.deleteDlpJob(deleteDlpJobRequest); |
| 83 | + |
| 84 | + System.out.println("Job deleted successfully."); |
| 85 | + } catch (Exception e) { |
| 86 | + System.err.println("Error deleting DLP job: " + e.getMessage()); |
| 87 | + } |
| 88 | + } |
| 89 | + // [END dlp_delete_job] |
| 90 | + |
| 91 | + /** Command line application to list and delete DLP jobs the Data Loss Prevention API. */ |
| 92 | + public static void main(String[] args) throws Exception { |
| 93 | + |
| 94 | + OptionGroup optionsGroup = new OptionGroup(); |
| 95 | + optionsGroup.setRequired(true); |
| 96 | + Option listOption = new Option("l", "list", false, "List DLP Jobs"); |
| 97 | + optionsGroup.addOption(listOption); |
| 98 | + |
| 99 | + Option deleteOption = new Option("d", "delete", false, "Delete DLP Jobs"); |
| 100 | + optionsGroup.addOption(deleteOption); |
| 101 | + |
| 102 | + Options commandLineOptions = new Options(); |
| 103 | + commandLineOptions.addOptionGroup(optionsGroup); |
| 104 | + |
| 105 | + Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build(); |
| 106 | + commandLineOptions.addOption(projectIdOption); |
| 107 | + |
| 108 | + Option filterOption = Option.builder("filter").hasArg(true).required(false).build(); |
| 109 | + commandLineOptions.addOption(filterOption); |
| 110 | + |
| 111 | + Option jobTypeOption = Option.builder("jobType").hasArg(true).required(false).build(); |
| 112 | + commandLineOptions.addOption(jobTypeOption); |
| 113 | + |
| 114 | + Option jobIdOption = Option.builder("jobId").hasArg(true).required(false).build(); |
| 115 | + commandLineOptions.addOption(jobIdOption); |
| 116 | + |
| 117 | + CommandLineParser parser = new DefaultParser(); |
| 118 | + HelpFormatter formatter = new HelpFormatter(); |
| 119 | + CommandLine cmd; |
| 120 | + |
| 121 | + try { |
| 122 | + cmd = parser.parse(commandLineOptions, args); |
| 123 | + } catch (ParseException e) { |
| 124 | + System.out.println(e.getMessage()); |
| 125 | + formatter.printHelp(Inspect.class.getName(), commandLineOptions); |
| 126 | + System.exit(1); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + String projectId = |
| 131 | + cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId()); |
| 132 | + |
| 133 | + if (cmd.hasOption(listOption.getOpt())) { |
| 134 | + String filter = cmd.getOptionValue(filterOption.getOpt(), ""); |
| 135 | + DlpJobType jobType = |
| 136 | + DlpJobType.valueOf( |
| 137 | + cmd.getOptionValue( |
| 138 | + jobTypeOption.getOpt(), DlpJobType.DLP_JOB_TYPE_UNSPECIFIED.name())); |
| 139 | + listJobs(projectId, filter, jobType); |
| 140 | + } |
| 141 | + |
| 142 | + if (cmd.hasOption(deleteOption.getOpt())) { |
| 143 | + String jobId = cmd.getOptionValue(jobIdOption.getOpt()); |
| 144 | + deleteJob(projectId, jobId); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments