-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_resource.go
More file actions
544 lines (513 loc) · 18.2 KB
/
component_resource.go
File metadata and controls
544 lines (513 loc) · 18.2 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package provider
import (
"context"
"fmt"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/diag"
"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/stringdefault"
"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 = &componentResource{}
_ resource.ResourceWithConfigure = &componentResource{}
_ resource.ResourceWithImportState = &componentResource{}
)
type (
componentResource struct {
client *dtrack.Client
semver *Semver
}
componentResourceModel struct {
Hashes *componentHashesResourceModel `tfsdk:"hashes"`
ID types.String `tfsdk:"id"`
Project types.String `tfsdk:"project"`
Author types.String `tfsdk:"author"`
Publisher types.String `tfsdk:"publisher"`
Group types.String `tfsdk:"group"`
Name types.String `tfsdk:"name"`
Version types.String `tfsdk:"version"`
Classifier types.String `tfsdk:"classifier"`
Filename types.String `tfsdk:"filename"`
Extension types.String `tfsdk:"extension"`
CPE types.String `tfsdk:"cpe"`
PURL types.String `tfsdk:"purl"`
SWID types.String `tfsdk:"swid"`
Description types.String `tfsdk:"description"`
Copyright types.String `tfsdk:"copyright"`
License types.String `tfsdk:"license"`
Notes types.String `tfsdk:"notes"`
}
componentHashesResourceModel struct {
MD5 types.String `tfsdk:"md5"`
SHA1 types.String `tfsdk:"sha1"`
SHA256 types.String `tfsdk:"sha256"`
SHA384 types.String `tfsdk:"sha384"`
SHA512 types.String `tfsdk:"sha512"`
SHA3_256 types.String `tfsdk:"sha3_256"`
SHA3_384 types.String `tfsdk:"sha3_384"`
SHA3_512 types.String `tfsdk:"sha3_512"`
BLAKE2b_256 types.String `tfsdk:"blake2b_256"`
BLAKE2b_384 types.String `tfsdk:"blake2b_384"`
BLAKE2b_512 types.String `tfsdk:"blake2b_512"`
BLAKE3 types.String `tfsdk:"blake3"`
}
)
func NewComponentResource() resource.Resource {
return &componentResource{}
}
func (*componentResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_component"
}
func (*componentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a Component, within a Project.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "UUID for the Component, as generated by DependencyTrack.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"project": schema.StringAttribute{
Description: "UUID for the Project that contains this Component.",
Required: true,
},
"name": schema.StringAttribute{
Description: "Name of the Component.",
Required: true,
},
"version": schema.StringAttribute{
Description: "Version of the Component.",
Required: true,
},
"description": schema.StringAttribute{
Description: "Description of the Component.",
Optional: true,
Computed: true,
},
"author": schema.StringAttribute{
Description: "Author of the Component.",
Optional: true,
Computed: true,
},
"publisher": schema.StringAttribute{
Description: "Publisher of the Component.",
Optional: true,
Computed: true,
},
"group": schema.StringAttribute{
Description: "Group of the Component.",
Optional: true,
Computed: true,
},
"classifier": schema.StringAttribute{
Description: "Classifier of the Component. Defaults to LIBRARY. See DependencyTrack for valid options.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("LIBRARY"),
},
"filename": schema.StringAttribute{
Description: "Filename of the Component.",
Optional: true,
Computed: true,
},
"extension": schema.StringAttribute{
Description: "Filename extension of the Component.",
Optional: true,
Computed: true,
},
"cpe": schema.StringAttribute{
Description: "Common Platform Enumeration of the Component. Standardised format v2.2 / v2.3 from MITRE / NIST.",
Optional: true,
Computed: true,
},
"purl": schema.StringAttribute{
Description: "Package URL of the Component. MUST be in standardised format to be saved. See DependencyTrack for format.",
Optional: true,
Computed: true,
},
"swid": schema.StringAttribute{
Description: "SWID Tag ID. ISO/IEC 19770-2:2015.",
Optional: true,
Computed: true,
},
"copyright": schema.StringAttribute{
Description: "Copyright of the Component.",
Optional: true,
Computed: true,
},
"license": schema.StringAttribute{
Description: "License of the Component. See DependencyTrack for valid options.",
Optional: true,
Computed: true,
},
"notes": schema.StringAttribute{
Description: "Notes to associate with the Component.",
Optional: true,
Computed: true,
},
"hashes": schema.SingleNestedAttribute{
Description: "Hashes of the Component.",
Required: true,
Attributes: map[string]schema.Attribute{
"md5": schema.StringAttribute{
Description: "MD5 hash of the Component.",
Optional: true,
Computed: true,
},
"sha1": schema.StringAttribute{
Description: "SHA1 hash of the Component.",
Optional: true,
Computed: true,
},
"sha256": schema.StringAttribute{
Description: "SHA256 hash of the Component.",
Optional: true,
Computed: true,
},
"sha384": schema.StringAttribute{
Description: "SHA384 hash of the Component.",
Optional: true,
Computed: true,
},
"sha512": schema.StringAttribute{
Description: "SHA512 hash of the Component.",
Optional: true,
Computed: true,
},
"sha3_256": schema.StringAttribute{
Description: "SHA3-256 hash of the Component.",
Optional: true,
Computed: true,
},
"sha3_384": schema.StringAttribute{
Description: "SHA3-384 hash of the Component.",
Optional: true,
Computed: true,
},
"sha3_512": schema.StringAttribute{
Description: "SHA3-512 hash of the Component.",
Optional: true,
Computed: true,
},
"blake2b_256": schema.StringAttribute{
Description: "BLAKE2b-256 hash of the Component.",
Optional: true,
Computed: true,
},
"blake2b_384": schema.StringAttribute{
Description: "BLAKE2b-384 hash of the Component.",
Optional: true,
Computed: true,
},
"blake2b_512": schema.StringAttribute{
Description: "BLAKE2b-512 hash of the Component.",
Optional: true,
Computed: true,
},
"blake3": schema.StringAttribute{
Description: "BLAKE3 hash of the Component.",
Optional: true,
Computed: true,
},
},
},
},
}
}
func (r *componentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan componentResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
componentReq := plan.ToSdk(LifecycleCreate, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Creating Component", componentDebug(*componentReq))
componentRes, err := r.client.Component.Create(ctx, componentReq.Project.UUID, *componentReq)
if err != nil {
resp.Diagnostics.AddError(
"Error creating Component.",
"Unexpected error: "+err.Error(),
)
return
}
plan = componentToModel(componentRes)
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Created Component", plan.debug())
}
func (r *componentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state componentResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if state.Hashes == nil {
// Account for when called from `ImportState`.
state.Hashes = &componentHashesResourceModel{}
}
id, diagnostic := TryParseUUID(state.ID, LifecycleRead, path.Root("id"))
if diagnostic != nil {
resp.Diagnostics.Append(diagnostic)
return
}
tflog.Debug(ctx, "Reading Component", state.debug())
component, err := r.client.Component.Get(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Within Read, unable to get Component",
"Error in Component: "+id.String()+", from error: "+err.Error(),
)
return
}
state = componentToModel(component)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read Component", state.debug())
}
func (r *componentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan componentResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
component := plan.ToSdk(LifecycleUpdate, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Updating Component", componentDebug(*component))
componentRes, err := r.client.Component.Update(ctx, *component)
if err != nil {
resp.Diagnostics.AddError(
"Unable to update Component.",
"Error in: "+component.UUID.String()+", from: "+err.Error(),
)
return
}
plan = componentToModel(componentRes)
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Updated Component", plan.debug())
}
func (r *componentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state componentResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id, diagnostic := TryParseUUID(state.ID, LifecycleDelete, path.Root("id"))
if diagnostic != nil {
resp.Diagnostics.Append(diagnostic)
return
}
tflog.Debug(ctx, "Deleting Component", state.debug())
err := r.client.Component.Delete(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete component",
"Unexpected error when trying to delete component with id: "+id.String()+", error: "+err.Error(),
)
return
}
tflog.Debug(ctx, "Deleted Component", state.debug())
}
func (*componentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
tflog.Debug(ctx, "Importing Component", map[string]any{
"id": req.ID,
})
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Imported Component", map[string]any{
"id": req.ID,
})
}
func (r *componentResource) 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
}
func (model componentResourceModel) ToSdk(lifecycle LifecycleAction, d *diag.Diagnostics) *dtrack.Component {
projectID, diagnostic := TryParseUUID(model.Project, lifecycle, path.Root("project"))
if diagnostic != nil {
d.Append(diagnostic)
return nil
}
component := dtrack.Component{
Author: model.Author.ValueString(),
Publisher: model.Publisher.ValueString(),
Group: model.Group.ValueString(),
Name: model.Name.ValueString(),
Version: model.Version.ValueString(),
Classifier: model.Classifier.ValueString(),
FileName: model.Filename.ValueString(),
Extension: model.Extension.ValueString(),
MD5: model.Hashes.MD5.ValueString(),
SHA1: model.Hashes.SHA1.ValueString(),
SHA256: model.Hashes.SHA256.ValueString(),
SHA384: model.Hashes.SHA384.ValueString(),
SHA512: model.Hashes.SHA512.ValueString(),
SHA3_256: model.Hashes.SHA3_256.ValueString(),
SHA3_384: model.Hashes.SHA3_384.ValueString(),
SHA3_512: model.Hashes.SHA3_512.ValueString(),
BLAKE2b_256: model.Hashes.BLAKE2b_256.ValueString(),
BLAKE2b_384: model.Hashes.BLAKE2b_384.ValueString(),
BLAKE2b_512: model.Hashes.BLAKE2b_512.ValueString(),
BLAKE3: model.Hashes.BLAKE3.ValueString(),
CPE: model.CPE.ValueString(),
PURL: model.PURL.ValueString(),
SWIDTagID: model.SWID.ValueString(),
Description: model.Description.ValueString(),
Copyright: model.Copyright.ValueString(),
License: model.License.ValueString(),
Notes: model.Notes.ValueString(),
Project: &dtrack.Project{
UUID: projectID,
},
}
if lifecycle != LifecycleCreate {
componentID, diagnostic := TryParseUUID(model.ID, lifecycle, path.Root("id"))
if diagnostic != nil {
d.Append(diagnostic)
return nil
}
component.UUID = componentID
}
return &component
}
func componentToModel(component dtrack.Component) componentResourceModel {
model := componentResourceModel{
ID: types.StringValue(component.UUID.String()),
Project: types.StringValue(component.Project.UUID.String()),
Author: types.StringValue(component.Author),
Publisher: types.StringValue(component.Publisher),
Group: types.StringValue(component.Group),
Name: types.StringValue(component.Name),
Version: types.StringValue(component.Version),
Classifier: types.StringValue(component.Classifier),
Filename: types.StringValue(component.FileName),
Extension: types.StringValue(component.Extension),
CPE: types.StringValue(component.CPE),
PURL: types.StringValue(component.PURL),
SWID: types.StringValue(component.SWIDTagID),
Description: types.StringValue(component.Description),
Copyright: types.StringValue(component.Copyright),
License: types.StringValue(component.License),
Notes: types.StringValue(component.Notes),
Hashes: &componentHashesResourceModel{
MD5: types.StringValue(component.MD5),
SHA1: types.StringValue(component.SHA1),
SHA256: types.StringValue(component.SHA256),
SHA384: types.StringValue(component.SHA384),
SHA512: types.StringValue(component.SHA512),
SHA3_256: types.StringValue(component.SHA3_256),
SHA3_384: types.StringValue(component.SHA3_384),
SHA3_512: types.StringValue(component.SHA3_512),
BLAKE2b_256: types.StringValue(component.BLAKE2b_256),
BLAKE2b_384: types.StringValue(component.BLAKE2b_384),
BLAKE2b_512: types.StringValue(component.BLAKE2b_512),
BLAKE3: types.StringValue(component.BLAKE3),
},
}
return model
}
func componentDebug(component dtrack.Component) map[string]any {
return map[string]any{
"id": component.UUID.String(),
"author": component.Author,
"publisher": component.Publisher,
"group": component.Group,
"name": component.Name,
"version": component.Version,
"classifier": component.Classifier,
"filename": component.FileName,
"extension": component.Extension,
"md5": component.MD5,
"sha1": component.SHA1,
"sha256": component.SHA256,
"sha384": component.SHA384,
"sha512": component.SHA512,
"sha3_256": component.SHA3_256,
"sha3_384": component.SHA3_384,
"sha3_512": component.SHA3_512,
"blake2b_256": component.BLAKE2b_256,
"blake2b_384": component.BLAKE2b_384,
"blake2b_512": component.BLAKE2b_512,
"blake3": component.BLAKE3,
"cpe": component.CPE,
"purl": component.PURL,
"swid": component.SWIDTagID,
"description": component.Description,
"copyright": component.Copyright,
"license": component.License,
"notes": component.Notes,
"project": component.Project.UUID.String(),
}
}
func (model componentResourceModel) debug() map[string]any {
return map[string]any{
"id": model.ID.ValueString(),
"author": model.Author.ValueString(),
"publisher": model.Publisher.ValueString(),
"group": model.Group.ValueString(),
"name": model.Name.ValueString(),
"version": model.Version.ValueString(),
"classifier": model.Classifier.ValueString(),
"filename": model.Filename.ValueString(),
"extension": model.Extension.ValueString(),
"md5": model.Hashes.MD5.ValueString(),
"sha1": model.Hashes.SHA1.ValueString(),
"sha256": model.Hashes.SHA256.ValueString(),
"sha384": model.Hashes.SHA384.ValueString(),
"sha512": model.Hashes.SHA512.ValueString(),
"sha3_256": model.Hashes.SHA3_256.ValueString(),
"sha3_384": model.Hashes.SHA3_384.ValueString(),
"sha3_512": model.Hashes.SHA3_512.ValueString(),
"blake2b_256": model.Hashes.BLAKE2b_256.ValueString(),
"blake2b_384": model.Hashes.BLAKE2b_384.ValueString(),
"blake2b_512": model.Hashes.BLAKE2b_512.ValueString(),
"blake3": model.Hashes.BLAKE3.ValueString(),
"cpe": model.CPE.ValueString(),
"purl": model.PURL.ValueString(),
"swid": model.SWID.ValueString(),
"description": model.Description.ValueString(),
"copyright": model.Copyright.ValueString(),
"license": model.License.ValueString(),
"notes": model.Notes.ValueString(),
"project": model.Project.ValueString(),
}
}