|
| 1 | +/* |
| 2 | + * Copyright 2020 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 dlp.snippets; |
| 18 | + |
| 19 | +// [START dlp_create_inspect_template] |
| 20 | +import com.google.cloud.dlp.v2.DlpServiceClient; |
| 21 | +import com.google.privacy.dlp.v2.CreateInspectTemplateRequest; |
| 22 | +import com.google.privacy.dlp.v2.InfoType; |
| 23 | +import com.google.privacy.dlp.v2.InspectConfig; |
| 24 | +import com.google.privacy.dlp.v2.InspectTemplate; |
| 25 | +import com.google.privacy.dlp.v2.ProjectName; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.Stream; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +class TemplatesCreate { |
| 33 | + |
| 34 | + public static void createInspectTemplate() throws IOException { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String projectId = "your-project-id"; |
| 37 | + createInspectTemplate(projectId); |
| 38 | + } |
| 39 | + |
| 40 | + // Creates a template to persist configuration information |
| 41 | + public static void createInspectTemplate(String projectId) throws IOException { |
| 42 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 43 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 44 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 45 | + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
| 46 | + // Specify the type of info the inspection will look for. |
| 47 | + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types |
| 48 | + List<InfoType> infoTypes = |
| 49 | + Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER") |
| 50 | + .map(it -> InfoType.newBuilder().setName(it).build()) |
| 51 | + .collect(Collectors.toList()); |
| 52 | + |
| 53 | + // Construct the inspection configuration for the template |
| 54 | + InspectConfig inspectConfig = |
| 55 | + InspectConfig.newBuilder() |
| 56 | + .addAllInfoTypes(infoTypes) |
| 57 | + .build(); |
| 58 | + |
| 59 | + // Optionally set a display name and a description for the template |
| 60 | + String displayName = "Inspection Config Template"; |
| 61 | + String description = "Save configuration for future inspection jobs"; |
| 62 | + |
| 63 | + // Build the template |
| 64 | + InspectTemplate inspectTemplate = |
| 65 | + InspectTemplate.newBuilder() |
| 66 | + .setInspectConfig(inspectConfig) |
| 67 | + .setDisplayName(displayName) |
| 68 | + .setDescription(description) |
| 69 | + .build(); |
| 70 | + |
| 71 | + // Create the request to be sent by the client |
| 72 | + CreateInspectTemplateRequest createInspectTemplateRequest = |
| 73 | + CreateInspectTemplateRequest.newBuilder() |
| 74 | + .setParent(ProjectName.of(projectId).toString()) |
| 75 | + .setInspectTemplate(inspectTemplate) |
| 76 | + .build(); |
| 77 | + |
| 78 | + // Send the request to the API and process the response |
| 79 | + InspectTemplate response = |
| 80 | + dlpServiceClient.createInspectTemplate(createInspectTemplateRequest); |
| 81 | + System.out.printf("Template created: %s", response.getName()); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +// [END dlp_create_inspect_template] |
0 commit comments