-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig_properties_resource.go
More file actions
458 lines (420 loc) · 15.1 KB
/
config_properties_resource.go
File metadata and controls
458 lines (420 loc) · 15.1 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package provider
import (
"context"
"fmt"
"slices"
"strings"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var (
_ resource.Resource = &configPropertiesResource{}
_ resource.ResourceWithConfigure = &configPropertiesResource{}
)
type (
configPropertiesResource struct {
client *dtrack.Client
semver *Semver
}
configPropertiesResourceModel struct {
Properties []configPropertiesElementResourceModel `tfsdk:"properties"`
}
configPropertiesElementResourceModel struct {
Group types.String `tfsdk:"group"`
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
Type types.String `tfsdk:"type"`
Description types.String `tfsdk:"description"`
}
)
func NewConfigPropertiesResource() resource.Resource {
return &configPropertiesResource{}
}
func (*configPropertiesResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_config_properties"
}
func (*configPropertiesResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages multiple Config Properties.",
Attributes: map[string]schema.Attribute{
"properties": schema.ListNestedAttribute{
Description: "Config properties, to be bulk managed.",
Required: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"group": schema.StringAttribute{
Description: "Group name of the Config Property.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"name": schema.StringAttribute{
Description: "Property name of the Config Property.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"value": schema.StringAttribute{
Description: "Value of the Config Property.",
Required: true,
},
"type": schema.StringAttribute{
Description: "Type of the Config Property. See DependencyTrack for valid enum values.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"description": schema.StringAttribute{
Description: "Description of the Config Property.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
},
},
}
}
func (r *configPropertiesResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan configPropertiesResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
type Identity struct {
group string
name string
}
configPropertiesReq := make([]dtrack.ConfigProperty, 0, len(plan.Properties))
// Used to retain EncryptesStrings, and values that are ordered lists, encoded as strings.
valueStringRetention := map[Identity]string{}
for _, propertyReq := range plan.Properties {
configProperty := dtrack.ConfigProperty{
GroupName: propertyReq.Group.ValueString(),
Name: propertyReq.Name.ValueString(),
Value: propertyReq.Value.ValueString(),
Type: propertyReq.Type.ValueString(),
}
if configProperty.Type == PropertyTypeEncryptedString {
valueStringRetention[Identity{
group: configProperty.GroupName,
name: configProperty.Name,
}] = configProperty.Value
}
if configProperty.GroupName == "vuln-source" && configProperty.Name == "google.osv.enabled" {
valueStringRetention[Identity{
group: configProperty.GroupName,
name: configProperty.Name,
}] = configProperty.Value
}
configPropertiesReq = append(configPropertiesReq, configProperty)
tflog.Debug(ctx, "Creating bulk config property", map[string]any{
"group": configProperty.GroupName,
"name": configProperty.Name,
"value": configProperty.Value,
"type": configProperty.Type,
})
}
tflog.Debug(ctx, "Creating bulk config properties", map[string]any{
"count": len(configPropertiesReq),
})
configPropertiesRes, err := r.client.Config.UpdateAll(ctx, configPropertiesReq)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring config properties.",
"Unexpected error: "+err.Error(),
)
return
}
configPropertiesState := configPropertiesResourceModel{
Properties: []configPropertiesElementResourceModel{},
}
for _, propertyRes := range configPropertiesRes {
model := configPropertiesElementResourceModel{
Group: types.StringValue(propertyRes.GroupName),
Name: types.StringValue(propertyRes.Name),
Value: types.StringValue(propertyRes.Value),
Type: types.StringValue(propertyRes.Type),
Description: types.StringValue(propertyRes.Description),
}
if propertyRes.Type == PropertyTypeEncryptedString {
model.Value = types.StringValue(valueStringRetention[Identity{
group: propertyRes.GroupName,
name: propertyRes.Name,
}])
}
if propertyRes.GroupName == "vuln-source" && propertyRes.Name == "google.osv.enabled" {
retained := valueStringRetention[Identity{
group: propertyRes.GroupName,
name: propertyRes.Name,
}]
retainedSorted := slices.SortedStableFunc(strings.SplitSeq(retained, ";"), strings.Compare)
responseSorted := slices.SortedStableFunc(strings.SplitSeq(propertyRes.Value, ";"), strings.Compare)
if slices.EqualFunc(retainedSorted, responseSorted, strings.EqualFold) {
model.Value = types.StringValue(retained)
}
}
configPropertiesState.Properties = append(configPropertiesState.Properties, model)
tflog.Debug(ctx, "Created bulk config property", map[string]any{
"group": propertyRes.GroupName,
"name": propertyRes.Name,
"value": propertyRes.Value,
"type": propertyRes.Type,
})
}
diags = resp.State.Set(ctx, &configPropertiesState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Created bulk config properties", map[string]any{
"count": len(configPropertiesRes),
})
}
func (r *configPropertiesResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state configPropertiesResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Reading all config properties")
configPropertiesAll, err := r.client.Config.GetAll(ctx)
if err != nil {
resp.Diagnostics.AddAttributeError(
path.Root("properties"),
"Within Read, unable to fetch config properties.",
"Error from: "+err.Error(),
)
return
}
for idx, configPropertyModel := range state.Properties {
groupName := configPropertyModel.Group.ValueString()
propertyName := configPropertyModel.Name.ValueString()
propertyType := configPropertyModel.Type.ValueString()
value := configPropertyModel.Value
configProperty, err := Find(configPropertiesAll, func(configProperty dtrack.ConfigProperty) bool {
if configProperty.GroupName != groupName {
return false
}
if configProperty.Name != propertyName {
return false
}
return true
})
if err != nil {
resp.Diagnostics.AddAttributeError(
path.Root("properties"),
"Within Read, unable to match config properties: "+groupName+" "+propertyName,
"Error from: "+err.Error(),
)
continue
}
if configProperty.Type != propertyType {
resp.Diagnostics.AddAttributeError(
path.Root("properties"),
"Within Read, unable to match config property type",
"Group: "+groupName+", Name: "+propertyName+", Type: "+configProperty.Type,
)
continue
}
state.Properties[idx] = configPropertiesElementResourceModel{
Group: types.StringValue(configProperty.GroupName),
Name: types.StringValue(configProperty.Name),
Value: types.StringValue(configProperty.Value),
Type: types.StringValue(configProperty.Type),
Description: types.StringValue(configProperty.Description),
}
if configProperty.Type == PropertyTypeEncryptedString {
state.Properties[idx].Value = value
}
if configProperty.GroupName == "vuln-source" && configProperty.Name == "google.osv.enabled" {
retained := configPropertyModel.Value.ValueString()
retainedSorted := slices.SortedStableFunc(strings.SplitSeq(retained, ";"), strings.Compare)
responseSorted := slices.SortedStableFunc(strings.SplitSeq(configProperty.Value, ";"), strings.Compare)
if slices.EqualFunc(retainedSorted, responseSorted, strings.EqualFold) {
state.Properties[idx].Value = types.StringValue(retained)
}
}
tflog.Debug(ctx, "Read bulk config property", map[string]any{
"group": state.Properties[idx].Group.ValueString(),
"name": state.Properties[idx].Name.ValueString(),
"value": state.Properties[idx].Value.ValueString(),
"type": state.Properties[idx].Type.ValueString(),
"description": state.Properties[idx].Description.ValueString(),
})
}
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read bulk config properties", map[string]any{
"count": len(state.Properties),
})
}
func (r *configPropertiesResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan configPropertiesResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
type Identity struct {
group string
name string
}
configPropertiesReq := make([]dtrack.ConfigProperty, 0, len(plan.Properties))
valueStringRetention := map[Identity]string{}
for _, propertyReq := range plan.Properties {
configProperty := dtrack.ConfigProperty{
GroupName: propertyReq.Group.ValueString(),
Name: propertyReq.Name.ValueString(),
Value: propertyReq.Value.ValueString(),
Type: propertyReq.Type.ValueString(),
}
if configProperty.Type == PropertyTypeEncryptedString {
valueStringRetention[Identity{
group: configProperty.GroupName,
name: configProperty.Name,
}] = configProperty.Value
}
if configProperty.GroupName == "vuln-source" && configProperty.Name == "google.osv.enabled" {
valueStringRetention[Identity{
group: configProperty.GroupName,
name: configProperty.Name,
}] = configProperty.Value
}
configPropertiesReq = append(configPropertiesReq, configProperty)
tflog.Debug(ctx, "Updating bulk config property", map[string]any{
"group": configProperty.GroupName,
"name": configProperty.Name,
"value": configProperty.Value,
"type": configProperty.Type,
})
}
tflog.Debug(ctx, "Updating bulk config properties", map[string]any{
"count": len(configPropertiesReq),
})
configPropertiesRes, err := r.client.Config.UpdateAll(ctx, configPropertiesReq)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring config properties.",
"Unexpected error: "+err.Error(),
)
return
}
configPropertiesState := configPropertiesResourceModel{
Properties: []configPropertiesElementResourceModel{},
}
for _, propertyRes := range configPropertiesRes {
model := configPropertiesElementResourceModel{
Group: types.StringValue(propertyRes.GroupName),
Name: types.StringValue(propertyRes.Name),
Value: types.StringValue(propertyRes.Value),
Type: types.StringValue(propertyRes.Type),
Description: types.StringValue(propertyRes.Description),
}
if propertyRes.Type == PropertyTypeEncryptedString {
model.Value = types.StringValue(valueStringRetention[Identity{
group: propertyRes.GroupName,
name: propertyRes.Name,
}])
}
if propertyRes.GroupName == "vuln-source" && propertyRes.Name == "google.osv.enabled" {
requested := valueStringRetention[Identity{
group: propertyRes.GroupName,
name: propertyRes.Name,
}]
requestedSorted := slices.SortedStableFunc(strings.SplitSeq(requested, ";"), strings.Compare)
responseSorted := slices.SortedStableFunc(strings.SplitSeq(propertyRes.Value, ";"), strings.Compare)
if slices.EqualFunc(requestedSorted, responseSorted, strings.EqualFold) {
model.Value = types.StringValue(requested)
}
}
configPropertiesState.Properties = append(configPropertiesState.Properties, model)
tflog.Debug(ctx, "Updated bulk config property", map[string]any{
"group": model.Group.ValueString(),
"name": model.Name.ValueString(),
"value": model.Type.ValueString(),
"type": model.Type.ValueString(),
"description": model.Description.ValueString(),
})
}
diags = resp.State.Set(ctx, configPropertiesState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Updated bulk config properties", map[string]any{
"count": len(configPropertiesState.Properties),
})
}
func (r *configPropertiesResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state configPropertiesResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
configPropertiesReq := make([]dtrack.ConfigProperty, 0, len(state.Properties))
for _, propertyReq := range state.Properties {
configProperty := dtrack.ConfigProperty{
GroupName: propertyReq.Group.ValueString(),
Name: propertyReq.Name.ValueString(),
Value: "",
Type: propertyReq.Type.ValueString(),
}
configPropertiesReq = append(configPropertiesReq, configProperty)
tflog.Debug(ctx, "Deleting bulk config property", map[string]any{
"group": configProperty.GroupName,
"name": configProperty.Name,
"value": configProperty.Value,
"type": configProperty.Type,
})
}
tflog.Debug(ctx, "Deleting bulk config properties", map[string]any{
"count": len(configPropertiesReq),
})
configPropertiesRes, err := r.client.Config.UpdateAll(ctx, configPropertiesReq)
if err != nil {
resp.Diagnostics.AddError(
"Error deleting bulk config properties.",
"Unexpected error: "+err.Error(),
)
return
}
tflog.Debug(ctx, "Deleted bulk config properties", map[string]any{
"count": len(configPropertiesRes),
})
}
func (r *configPropertiesResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
clientInfoData, ok := req.ProviderData.(clientInfo)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Configure Type",
fmt.Sprintf("Expected provider.clientInfo, got %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = clientInfoData.client
r.semver = clientInfoData.semver
}