-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
110 lines (97 loc) · 3.47 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"io"
"log"
"time"
dlp "cloud.google.com/go/dlp/apiv2"
"google.golang.org/api/iterator"
dlppb "google.golang.org/genproto/googleapis/privacy/dlp/v2"
)
// [START dlp_create_inspect_template]
// createInspectTemplate creates a template with the given configuration.
func createInspectTemplate(w io.Writer, client *dlp.Client, project string, minLikelihood dlppb.Likelihood, maxFindings int32, templateID, displayName, description string, infoTypes []string) {
// Convert the info type strings to a list of InfoTypes.
var i []*dlppb.InfoType
for _, it := range infoTypes {
i = append(i, &dlppb.InfoType{Name: it})
}
// Create a configured request.
req := &dlppb.CreateInspectTemplateRequest{
Parent: "projects/" + project,
TemplateId: templateID,
InspectTemplate: &dlppb.InspectTemplate{
DisplayName: displayName,
Description: description,
InspectConfig: &dlppb.InspectConfig{
InfoTypes: i,
MinLikelihood: minLikelihood,
Limits: &dlppb.InspectConfig_FindingLimits{
MaxFindingsPerRequest: maxFindings,
},
},
},
}
// Send the request.
resp, err := client.CreateInspectTemplate(context.Background(), req)
if err != nil {
log.Fatalf("error creating inspect template: %v", err)
}
// Print the result.
fmt.Fprintf(w, "Successfully created inspect template: %v", resp.GetName())
}
// [END dlp_create_inspect_template]
// [START dlp_list_inspect_templates]
// listInspectTemplates lists the inspect templates in the project.
func listInspectTemplates(w io.Writer, client *dlp.Client, project string) {
// Create a configured request.
req := &dlppb.ListInspectTemplatesRequest{
Parent: "projects/" + project,
}
// Send the request and iterate over the results.
it := client.ListInspectTemplates(context.Background(), req)
for {
t, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("error getting inspect templates: %v", err)
}
c := t.GetCreateTime()
u := t.GetUpdateTime()
fmt.Fprintf(w, "Inspect template %v\n", t.GetName())
fmt.Fprintf(w, " Created: %v\n", time.Unix(c.GetSeconds(), int64(c.GetNanos())).Format(time.RFC1123))
fmt.Fprintf(w, " Updated: %v\n", time.Unix(u.GetSeconds(), int64(u.GetNanos())).Format(time.RFC1123))
fmt.Fprintf(w, " Display Name: %q\n", t.GetDisplayName())
fmt.Fprintf(w, " Description: %q\n", t.GetDescription())
}
}
// [END dlp_list_inspect_templates]
// [START dlp_delete_inspect_template]
// deleteInspectTemplate deletes the given template.
func deleteInspectTemplate(w io.Writer, client *dlp.Client, templateID string) {
req := &dlppb.DeleteInspectTemplateRequest{
Name: templateID,
}
err := client.DeleteInspectTemplate(context.Background(), req)
if err != nil {
log.Fatalf("error deleting inspect template: %v", err)
}
fmt.Fprintf(w, "Successfully deleted inspect template %v", templateID)
}
// [END dlp_delete_inspect_template]