-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathresource_datadog_sensitive_data_scanner_group.go
267 lines (224 loc) · 9.24 KB
/
resource_datadog_sensitive_data_scanner_group.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package datadog
import (
"context"
"sync"
"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"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/validators"
)
var sensitiveDataScannerMutex = sync.Mutex{}
func resourceDatadogSensitiveDataScannerGroup() *schema.Resource {
return &schema.Resource{
Description: "Provides a Sensitive Data Scanner group resource.",
ReadContext: resourceDatadogSensitiveDataScannerGroupRead,
CreateContext: resourceDatadogSensitiveDataScannerGroupCreate,
UpdateContext: resourceDatadogSensitiveDataScannerGroupUpdate,
DeleteContext: resourceDatadogSensitiveDataScannerGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Description: "Name of the Datadog scanning group.",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description of the Datadog scanning group.",
Type: schema.TypeString,
Optional: true,
},
"product_list": {
Description: "List of products the scanning group applies.",
Type: schema.TypeSet,
Required: true,
MaxItems: 4,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV2.NewSensitiveDataScannerProductFromValue),
},
},
"is_enabled": {
Description: "Whether or not the scanning group is enabled. If the group doesn't contain any rule or if all the rules in it are disabled, the group is force-disabled by our backend",
Type: schema.TypeBool,
Required: true,
},
"filter": {
Description: "Filter object the scanning group applies.",
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"query": {
Description: "Query to filter the events.",
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: func(_, oldVal, newVal string, d *schema.ResourceData) bool {
if (oldVal == "" && newVal == "*") || (oldVal == "*" && newVal == "") {
return true
}
return false
},
},
},
},
},
}
},
}
}
func buildTerraformGroupFilter(ddFilter datadogV2.SensitiveDataScannerFilter) *[]map[string]interface{} {
tfFilter := map[string]interface{}{
"query": ddFilter.GetQuery(),
}
return &[]map[string]interface{}{tfFilter}
}
func buildDatadogGroupFilter(tfFilter map[string]interface{}) *datadogV2.SensitiveDataScannerFilter {
ddFilter := datadogV2.NewSensitiveDataScannerFilterWithDefaults()
if tfQuery, exists := tfFilter["query"].(string); exists {
ddFilter.SetQuery(tfQuery)
}
return ddFilter
}
func buildScanningGroupAttributes(d *schema.ResourceData) *datadogV2.SensitiveDataScannerGroupAttributes {
attributes := &datadogV2.SensitiveDataScannerGroupAttributes{}
attributes.SetIsEnabled(d.Get("is_enabled").(bool))
attributes.SetName(d.Get("name").(string))
attributes.SetDescription(d.Get("description").(string))
if description, ok := d.GetOk("description"); ok {
attributes.SetDescription(description.(string))
}
if tfFilter := d.Get("filter").([]interface{}); len(tfFilter) > 0 && tfFilter[0] != nil {
attributes.SetFilter(*buildDatadogGroupFilter(tfFilter[0].(map[string]interface{})))
} else {
filter := datadogV2.NewSensitiveDataScannerFilterWithDefaults()
filter.SetQuery("*")
attributes.SetFilter(*filter)
}
productList := make([]datadogV2.SensitiveDataScannerProduct, 0)
if pList, ok := d.GetOk("product_list"); ok {
for _, s := range pList.(*schema.Set).List() {
sensitiveDataScannerProductItem, _ := datadogV2.NewSensitiveDataScannerProductFromValue(s.(string))
productList = append(productList, *sensitiveDataScannerProductItem)
}
attributes.SetProductList(productList)
} else {
attributes.SetProductList(nil)
}
return attributes
}
func resourceDatadogSensitiveDataScannerGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
resp, httpResponse, err := apiInstances.GetSensitiveDataScannerApiV2().ListScanningGroups(auth)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error calling ListScanningGroups")
}
groupId := d.Id()
if groupFound := findSensitiveDataScannerGroupHelper(groupId, resp); groupFound != nil {
return updateSensitiveDataScannerGroupState(d, groupFound.Attributes)
} else {
d.SetId("")
}
return nil
}
func resourceDatadogSensitiveDataScannerGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
sensitiveDataScannerMutex.Lock()
defer sensitiveDataScannerMutex.Unlock()
body := buildSensitiveDataScannerGroupCreateRequestBody(d)
resp, httpResp, err := apiInstances.GetSensitiveDataScannerApiV2().CreateScanningGroup(auth, *body)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResp, "error creating SensitiveDataScannerGroup")
}
if err := utils.CheckForUnparsed(resp); err != nil {
return diag.FromErr(err)
}
d.SetId(resp.Data.GetId())
return updateSensitiveDataScannerGroupState(d, resp.Data.Attributes)
}
func buildSensitiveDataScannerGroupCreateRequestBody(d *schema.ResourceData) *datadogV2.SensitiveDataScannerGroupCreateRequest {
attributes := buildScanningGroupAttributes(d)
req := datadogV2.NewSensitiveDataScannerGroupCreateRequestWithDefaults()
req.Data = datadogV2.NewSensitiveDataScannerGroupCreateWithDefaults()
req.Data.SetAttributes(*attributes)
req.Meta = datadogV2.NewSensitiveDataScannerMetaVersionOnly()
return req
}
func resourceDatadogSensitiveDataScannerGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
sensitiveDataScannerMutex.Lock()
defer sensitiveDataScannerMutex.Unlock()
id := d.Id()
body := buildSensitiveDataScannerGroupUpdateRequestBody(d)
resp, httpResp, err := apiInstances.GetSensitiveDataScannerApiV2().UpdateScanningGroup(auth, id, *body)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResp, "error updating SensitiveDataScannerGroup")
}
if err := utils.CheckForUnparsed(resp); err != nil {
return diag.FromErr(err)
}
d.SetId(id)
return updateSensitiveDataScannerGroupState(d, body.Data.Attributes)
}
func buildSensitiveDataScannerGroupUpdateRequestBody(d *schema.ResourceData) *datadogV2.SensitiveDataScannerGroupUpdateRequest {
attributes := buildScanningGroupAttributes(d)
req := datadogV2.NewSensitiveDataScannerGroupUpdateRequestWithDefaults()
req.Data = *datadogV2.NewSensitiveDataScannerGroupUpdateWithDefaults()
req.Data.SetAttributes(*attributes)
req.Data.SetId(d.Id())
req.Meta = *datadogV2.NewSensitiveDataScannerMetaVersionOnly()
return req
}
func resourceDatadogSensitiveDataScannerGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
sensitiveDataScannerMutex.Lock()
defer sensitiveDataScannerMutex.Unlock()
id := d.Id()
body := datadogV2.NewSensitiveDataScannerGroupDeleteRequestWithDefaults()
metaVar := datadogV2.NewSensitiveDataScannerMetaVersionOnlyWithDefaults()
body.SetMeta(*metaVar)
_, httpResp, err := apiInstances.GetSensitiveDataScannerApiV2().DeleteScanningGroup(auth, id, *body)
if err != nil {
// The resource is assumed to still exist, and all prior state is preserved.
return utils.TranslateClientErrorDiag(err, httpResp, "error deleting SensitiveDataScannerGroup")
}
return nil
}
func updateSensitiveDataScannerGroupState(d *schema.ResourceData, groupAttributes *datadogV2.SensitiveDataScannerGroupAttributes) diag.Diagnostics {
if err := d.Set("name", groupAttributes.GetName()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("description", groupAttributes.GetDescription()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("is_enabled", groupAttributes.GetIsEnabled()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("product_list", groupAttributes.GetProductList()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("filter", buildTerraformGroupFilter(groupAttributes.GetFilter())); err != nil {
return diag.FromErr(err)
}
return nil
}
func findSensitiveDataScannerGroupHelper(groupId string, response datadogV2.SensitiveDataScannerGetConfigResponse) *datadogV2.SensitiveDataScannerGroupIncludedItem {
for _, resource := range response.GetIncluded() {
if resource.SensitiveDataScannerGroupIncludedItem.GetId() == groupId {
return resource.SensitiveDataScannerGroupIncludedItem
}
}
return nil
}