-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathdata_source_datadog_sensitive_data_scanner_standard_pattern.go
110 lines (95 loc) · 3.76 KB
/
data_source_datadog_sensitive_data_scanner_standard_pattern.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
package datadog
import (
"context"
"strings"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)
func dataSourceDatadogSensitiveDataScannerStandardPattern() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve information about an existing sensitive data scanner standard pattern.",
ReadContext: dataSourceDatadogSensitiveDataScannerStandardPatternRead,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"filter": {
Description: "Filter all the Datadog standard patterns by name.",
Type: schema.TypeString,
Required: true,
},
// Computed
"included_keywords": {
Description: "List of recommended keywords to improve rule accuracy.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Description: "Name of the standard pattern.",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "Description of the standard pattern.",
Type: schema.TypeString,
Computed: true,
},
"pattern": {
Description: "Regex to match, optionally documented for older standard rules. ",
Type: schema.TypeString,
Computed: true,
Deprecated: "Refer to the description field to understand what the rule does.",
},
"tags": {
Description: "List of tags.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}
},
}
}
func dataSourceDatadogSensitiveDataScannerStandardPatternRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
resp, httpResponse, err := apiInstances.GetSensitiveDataScannerApiV2().ListStandardPatterns(auth)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error listing standard patterns")
}
searchedName := d.Get("filter").(string)
foundStandardPatterns := make([]datadogV2.SensitiveDataScannerStandardPatternsResponseItem, 0)
for _, resource := range resp.GetData() {
if strings.Contains(strings.ToLower(*resource.Attributes.Name), strings.ToLower(searchedName)) {
foundStandardPatterns = append(foundStandardPatterns, resource)
}
}
if len(foundStandardPatterns) == 0 {
return diag.Errorf("Couldn't find the standard pattern with name %s", searchedName)
}
if len(foundStandardPatterns) > 1 {
return diag.Errorf("Your query returned more than one result, please try a more specific search criteria")
}
d.SetId(foundStandardPatterns[0].GetId())
return dataSourceSensitiveDataScannerStandardPatternUpdate(d, &foundStandardPatterns[0])
}
func dataSourceSensitiveDataScannerStandardPatternUpdate(d *schema.ResourceData, standardPattern *datadogV2.SensitiveDataScannerStandardPatternsResponseItem) diag.Diagnostics {
if err := d.Set("included_keywords", standardPattern.Attributes.GetIncludedKeywords()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", standardPattern.Attributes.GetName()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("description", standardPattern.Attributes.GetDescription()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("pattern", standardPattern.Attributes.GetPattern()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tags", standardPattern.Attributes.GetTags()); err != nil {
return diag.FromErr(err)
}
return nil
}