Search in sources :

Example 1 with InspectTemplate

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

the class Templates method listInspectTemplates.

// [END dlp_create_inspect_template]
// [START dlp_list_inspect_templates]
/**
 * List DLP inspection templates created in a given project
 *
 * @param projectId Google Cloud Project ID
 */
private static void listInspectTemplates(String projectId) {
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        ListInspectTemplatesRequest request = ListInspectTemplatesRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setPageSize(1).build();
        ListInspectTemplatesPagedResponse response = dlpServiceClient.listInspectTemplates(request);
        ListInspectTemplatesPage page = response.getPage();
        ListInspectTemplatesResponse templatesResponse = page.getResponse();
        for (InspectTemplate template : templatesResponse.getInspectTemplatesList()) {
            System.out.printf("Template name: %s\n", template.getName());
            if (template.getDisplayName() != null) {
                System.out.printf("\tDisplay name: %s \n", template.getDisplayName());
                System.out.printf("\tCreate time: %s \n", template.getCreateTime());
                System.out.printf("\tUpdate time: %s \n", template.getUpdateTime());
                // print inspection config
                InspectConfig inspectConfig = template.getInspectConfig();
                for (InfoType infoType : inspectConfig.getInfoTypesList()) {
                    System.out.printf("\tInfoType: %s\n", infoType.getName());
                }
                System.out.printf("\tMin likelihood: %s\n", inspectConfig.getMinLikelihood());
                System.out.printf("\tLimits: %s\n", inspectConfig.getLimits().getMaxFindingsPerRequest());
            }
        }
    } catch (Exception e) {
        System.out.printf("Error creating template: %s", e.getMessage());
    }
}
Also used : ListInspectTemplatesResponse(com.google.privacy.dlp.v2.ListInspectTemplatesResponse) InspectTemplate(com.google.privacy.dlp.v2.InspectTemplate) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) ListInspectTemplatesRequest(com.google.privacy.dlp.v2.ListInspectTemplatesRequest) ListInspectTemplatesPage(com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPage) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) InfoType(com.google.privacy.dlp.v2.InfoType) ParseException(org.apache.commons.cli.ParseException) ListInspectTemplatesPagedResponse(com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPagedResponse)

Example 2 with InspectTemplate

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

the class Templates method createInspectTemplate.

// [START dlp_create_inspect_template]
/**
 * Create a new DLP inspection configuration template.
 *
 * @param displayName (Optional) The human-readable name to give the template
 * @param projectId Google Cloud Project ID to call the API under
 * @param templateId (Optional) The name of the template to be created
 * @param infoTypeList The infoTypes of information to match
 * @param minLikelihood The minimum likelihood required before returning a match
 * @param maxFindings The maximum number of findings to report per request (0 = server maximum)
 */
private static void createInspectTemplate(String displayName, String templateId, String description, String projectId, List<InfoType> infoTypeList, Likelihood minLikelihood, int maxFindings) {
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        // Construct the inspection configuration for the template
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypeList).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
        InspectTemplate inspectTemplate = InspectTemplate.newBuilder().setInspectConfig(inspectConfig).setDisplayName(displayName).setDescription(description).build();
        CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectTemplate(inspectTemplate).setTemplateId(templateId).build();
        InspectTemplate response = dlpServiceClient.createInspectTemplate(createInspectTemplateRequest);
        System.out.printf("Template created: %s", response.getName());
    } catch (Exception e) {
        System.out.printf("Error creating template: %s", e.getMessage());
    }
}
Also used : FindingLimits(com.google.privacy.dlp.v2.InspectConfig.FindingLimits) InspectTemplate(com.google.privacy.dlp.v2.InspectTemplate) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ParseException(org.apache.commons.cli.ParseException) CreateInspectTemplateRequest(com.google.privacy.dlp.v2.CreateInspectTemplateRequest)

Aggregations

DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)2 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)2 InspectTemplate (com.google.privacy.dlp.v2.InspectTemplate)2 ParseException (org.apache.commons.cli.ParseException)2 ListInspectTemplatesPage (com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPage)1 ListInspectTemplatesPagedResponse (com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPagedResponse)1 CreateInspectTemplateRequest (com.google.privacy.dlp.v2.CreateInspectTemplateRequest)1 InfoType (com.google.privacy.dlp.v2.InfoType)1 FindingLimits (com.google.privacy.dlp.v2.InspectConfig.FindingLimits)1 ListInspectTemplatesRequest (com.google.privacy.dlp.v2.ListInspectTemplatesRequest)1 ListInspectTemplatesResponse (com.google.privacy.dlp.v2.ListInspectTemplatesResponse)1