-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathproject_resource.go
More file actions
723 lines (689 loc) · 24.4 KB
/
project_resource.go
File metadata and controls
723 lines (689 loc) · 24.4 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package provider
import (
"context"
"fmt"
"strings"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/attr"
"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 = &projectResource{}
_ resource.ResourceWithConfigure = &projectResource{}
_ resource.ResourceWithImportState = &projectResource{}
)
type (
projectResource struct {
client *dtrack.Client
semver *Semver
}
projectResourceModel struct {
Collection *projectResourceModelCollection `tfsdk:"collection"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Version types.String `tfsdk:"version"`
Parent types.String `tfsdk:"parent"`
Classifier types.String `tfsdk:"classifier"`
Group types.String `tfsdk:"group"`
PURL types.String `tfsdk:"purl"`
CPE types.String `tfsdk:"cpe"`
SWID types.String `tfsdk:"swid"`
Tags types.List `tfsdk:"tags"`
Active types.Bool `tfsdk:"active"`
IsLatest types.Bool `tfsdk:"is_latest"`
}
projectResourceModelCollection struct {
Logic types.String `tfsdk:"logic"`
Tag types.String `tfsdk:"tag"`
}
)
func NewProjectResource() resource.Resource {
return &projectResource{}
}
func (*projectResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project"
}
func (*projectResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a Project.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "UUID for the Project as generated by DependencyTrack.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Description: "Name of the Project.",
Required: true,
},
"description": schema.StringAttribute{
Description: "Description of the Project.",
Optional: true,
Computed: true,
},
"active": schema.BoolAttribute{
Description: "Whether the Project is active. Defaults to true.",
Optional: true,
Computed: true,
},
"version": schema.StringAttribute{
Description: "Version of the project.",
Optional: true,
Computed: true,
},
"is_latest": schema.BoolAttribute{
Description: "Whether the Project is the latest version. Available in API 4.12+.",
Optional: true,
Computed: true,
},
"parent": schema.StringAttribute{
Description: "UUID of a parent project, to allow for nesting. Available in API 4.7+.",
Optional: true,
},
"classifier": schema.StringAttribute{
Description: "Classifier of the Project. Defaults to APPLICATION. See DependencyTrack for valid options.",
Optional: true,
Computed: true,
},
"group": schema.StringAttribute{
Description: "Namespace / group / vendor of the Project.",
Optional: true,
Computed: true,
},
"purl": schema.StringAttribute{
Description: "Package URL of the Project. MUST be in standardised format to be saved. See DependencyTrack for format.",
Optional: true,
Computed: true,
},
"cpe": schema.StringAttribute{
Description: "Common Platform Enumeration of the Project. Standardised format v2.2 / v2.3 from MITRE / NIST.",
Optional: true,
Computed: true,
},
"swid": schema.StringAttribute{
Description: "SWID Tag ID. ISO/IEC 19770-2:2015.",
Optional: true,
Computed: true,
},
"tags": schema.ListAttribute{
Description: "Tags to assign to a project. " +
"If unset, retains existing tags on project. " +
"If set, and `dependencytrack_tag_projects` is used with any of the tags, it must include this project's `id`.",
Optional: true,
Computed: true,
ElementType: types.StringType,
},
"collection": schema.SingleNestedAttribute{
Description: "Project Collection Logic for Aggregate Projects. Available in API 4.13+.",
Optional: true,
Attributes: map[string]schema.Attribute{
"logic": schema.StringAttribute{
Description: "Logic used for collecting sub-projects. See DependencyTrack for valid values.",
Required: true,
},
"tag": schema.StringAttribute{
Description: "Tag used for selecting which projects to collect, when 'logic' is 'AGGREGATE_DIRECT_CHILDREN_WITH_TAG'.",
Optional: true,
},
},
},
},
}
}
func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan projectResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
projectReq := dtrack.Project{
Name: plan.Name.ValueString(),
Description: plan.Description.ValueString(),
Active: plan.Active.ValueBool(),
Version: plan.Version.ValueString(),
ParentRef: nil, // Set Below.
Classifier: plan.Classifier.ValueString(),
Group: plan.Group.ValueString(),
PURL: plan.PURL.ValueString(),
CPE: plan.CPE.ValueString(),
SWIDTagID: plan.SWID.ValueString(),
Tags: []dtrack.Tag{}, // Set Below.
}
if !plan.Parent.IsNull() {
parentID, diag := TryParseUUID(plan.Parent, LifecycleCreate, path.Root("parent"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
projectReq.ParentRef = &dtrack.ParentRef{
UUID: parentID,
}
}
if !plan.Tags.IsUnknown() && !plan.Tags.IsNull() {
tagStrings, err := GetStringList(ctx, &resp.Diagnostics, plan.Tags)
if resp.Diagnostics.HasError() {
return
}
if err != nil {
resp.Diagnostics.AddAttributeError(
path.Root("tags"),
"Within Create, unable to convert `tags` list into slice of string, in project: "+projectReq.Name,
"Error from: "+err.Error(),
)
return
}
projectReq.Tags = Map(tagStrings, func(item string) dtrack.Tag { return dtrack.Tag{Name: item} })
}
if plan.Active.IsUnknown() {
projectReq.Active = true
}
if plan.Classifier.IsUnknown() {
projectReq.Classifier = "APPLICATION"
}
if hasProjectCollectionFeature(*r.semver) && plan.Collection != nil {
collectionLogic := dtrack.CollectionLogic(plan.Collection.Logic.ValueString())
projectReq.CollectionLogic = &collectionLogic
tagName := plan.Collection.Tag.ValueString()
if tagName != "" {
projectReq.CollectionTag = &dtrack.Tag{Name: tagName}
} else {
projectReq.CollectionTag = nil
}
}
if hasProjectIsLatestFeature(*r.semver) && !plan.IsLatest.IsNull() {
projectReq.IsLatest = plan.IsLatest.ValueBoolPointer()
}
tflog.Debug(ctx, "Creating a Project", map[string]any{
"name": projectReq.Name,
"description": projectReq.Description,
"active": projectReq.Active,
"version": projectReq.Version,
"is_latest": projectReq.IsLatest,
"parent": projectReq.ParentRef,
"classifier": projectReq.Classifier,
"group": projectReq.Group,
"purl": projectReq.PURL,
"cpe": projectReq.CPE,
"swid": projectReq.SWIDTagID,
"tags": projectReq.Tags,
"collection.logic": projectReq.CollectionLogic,
"collection.tag": projectReq.CollectionTag,
})
projectRes, err := r.client.Project.Create(ctx, projectReq)
if err != nil {
resp.Diagnostics.AddError(
"Error creating Project",
"Error from: "+err.Error(),
)
return
}
resTags := projectRes.Tags
if SliceUnorderedEqual(projectReq.Tags, projectRes.Tags, func(req, res dtrack.Tag) int {
return strings.Compare(req.Name, res.Name)
}) {
resTags = projectReq.Tags
}
tagValueList := Map(resTags, func(tag dtrack.Tag) attr.Value {
return types.StringValue(tag.Name)
})
tagList, diags := types.ListValue(types.StringType, tagValueList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
plan = projectResourceModel{
ID: types.StringValue(projectRes.UUID.String()),
Name: types.StringValue(projectRes.Name),
Description: types.StringValue(projectRes.Description),
Active: types.BoolValue(projectRes.Active),
Version: types.StringValue(projectRes.Version),
IsLatest: types.BoolNull(), // Updated below.
Parent: types.StringNull(), // Updated below.
Classifier: types.StringValue(projectRes.Classifier),
Group: types.StringValue(projectRes.Group),
PURL: types.StringValue(projectRes.PURL),
CPE: types.StringValue(projectRes.CPE),
SWID: types.StringValue(projectRes.SWIDTagID),
Tags: tagList,
Collection: nil, // Updated below.
}
if projectRes.ParentRef != nil {
plan.Parent = types.StringValue(projectRes.ParentRef.UUID.String())
} else if projectReq.ParentRef != nil && r.semver.Major == 4 && r.semver.Minor < 12 {
// Creates a project with the Parent, but does not return Parent within Create Endpoint.
// The parent being set is validated by the Read method, in combination with tests.
plan.Parent = types.StringValue(projectReq.ParentRef.UUID.String())
} else {
plan.Parent = types.StringNull()
}
if hasProjectCollectionFeature(*r.semver) {
if projectRes.CollectionLogic == nil || (*projectRes.CollectionLogic == "NONE" && projectReq.CollectionLogic == nil) {
plan.Collection = nil
} else {
plan.Collection = &projectResourceModelCollection{
Logic: types.StringValue(string(*projectRes.CollectionLogic)),
Tag: types.StringNull(), // Updated below.
}
if projectRes.CollectionTag == nil || projectRes.CollectionTag.Name == "" {
plan.Collection.Tag = types.StringNull()
} else {
plan.Collection.Tag = types.StringValue(projectRes.CollectionTag.Name)
}
}
}
if hasProjectIsLatestFeature(*r.semver) {
plan.IsLatest = types.BoolValue(*projectRes.IsLatest)
}
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Created a Project", map[string]any{
"id": projectRes.UUID.String(),
"name": projectRes.Name,
"description": projectRes.Description,
"active": projectRes.Active,
"version": projectRes.Version,
"is_latest": projectRes.IsLatest,
"parent": projectRes.ParentRef,
"classifier": projectRes.Classifier,
"group": projectRes.Group,
"purl": projectRes.PURL,
"cpe": projectRes.CPE,
"swid": projectRes.SWIDTagID,
"tags": projectRes.Tags,
"collection.logic": projectRes.CollectionLogic,
"collection.tag": projectRes.CollectionTag,
})
}
func (r *projectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Fetch state.
var state projectResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Refresh.
id, diag := TryParseUUID(state.ID, LifecycleRead, path.Root("id"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
tflog.Debug(ctx, "Reading Project", map[string]any{
"id": id.String(),
"name": state.Name.ValueString(),
"description": state.Description.ValueString(),
"active": state.Active.ValueBool(),
"version": state.Version.ValueString(),
"is_latest": state.IsLatest.ValueBool(),
"parent": state.Parent.ValueString(),
"classifier": state.Classifier.ValueString(),
"group": state.Group.ValueString(),
"purl": state.PURL.ValueString(),
"cpe": state.CPE.ValueString(),
"swid": state.SWID.ValueString(),
"tags": state.Tags.Elements(),
})
project, err := r.client.Project.Get(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Unable to get updated project",
"Error with reading project: "+id.String()+", in original error: "+err.Error(),
)
return
}
stateTags, err := GetStringList(ctx, &resp.Diagnostics, state.Tags)
if err != nil {
resp.Diagnostics.AddError(
"Unable to load current tags on project",
"Error with transforming stored tags on project: "+id.String()+", in original error: "+err.Error(),
)
return
}
returnedTags := Map(project.Tags, func(tag dtrack.Tag) string { return tag.Name })
newStateTags := returnedTags
if SliceUnorderedEqual(stateTags, returnedTags, strings.Compare) {
newStateTags = stateTags
}
tagValueList := Map(newStateTags, func(name string) attr.Value { return types.StringValue(name) })
tagList, diags := types.ListValue(types.StringType, tagValueList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
newState := projectResourceModel{
ID: types.StringValue(project.UUID.String()),
Name: types.StringValue(project.Name),
Description: types.StringValue(project.Description),
Active: types.BoolValue(project.Active),
Version: types.StringValue(project.Version),
IsLatest: types.BoolNull(), // Updated below.
Parent: types.StringNull(), // Updated below.
Classifier: types.StringValue(project.Classifier),
Group: types.StringValue(project.Group),
PURL: types.StringValue(project.PURL),
CPE: types.StringValue(project.CPE),
SWID: types.StringValue(project.SWIDTagID),
Tags: tagList,
Collection: nil, // Updated below.
}
if project.ParentRef != nil {
newState.Parent = types.StringValue(project.ParentRef.UUID.String())
} else {
newState.Parent = types.StringNull()
}
if hasProjectCollectionFeature(*r.semver) {
if project.CollectionLogic == nil || (*project.CollectionLogic == "NONE" && state.Collection == nil) {
newState.Collection = nil
} else {
newState.Collection = &projectResourceModelCollection{
Logic: types.StringValue(string(*project.CollectionLogic)),
Tag: types.StringNull(), // Updated below.
}
if project.CollectionTag == nil || project.CollectionTag.Name == "" {
newState.Collection.Tag = types.StringNull()
} else {
newState.Collection.Tag = types.StringValue(project.CollectionTag.Name)
}
}
}
if hasProjectIsLatestFeature(*r.semver) {
newState.IsLatest = types.BoolValue(*project.IsLatest)
}
// Update state.
diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read Project", map[string]any{
"id": project.UUID.String(),
"name": project.Name,
"description": project.Description,
"active": project.Active,
"version": project.Version,
"is_latest": project.IsLatest,
"parent": project.ParentRef,
"classifier": project.Classifier,
"group": project.Group,
"purl": project.PURL,
"cpe": project.CPE,
"swid": project.SWIDTagID,
"tags": project.Tags,
"collection.logic": project.CollectionLogic,
"collection.tag": project.CollectionTag,
})
}
func (r *projectResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get State.
var plan projectResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map TF to SDK.
id, diag := TryParseUUID(plan.ID, LifecycleUpdate, path.Root("id"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
tflog.Debug(ctx, "Within Update, Fetching current Project information", map[string]any{
"id": id.String(),
})
project, err := r.client.Project.Get(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Within Update, unable to retrieve current Project",
"Error from: "+err.Error(),
)
return
}
project.Name = plan.Name.ValueString()
project.Description = plan.Description.ValueString()
project.Active = plan.Active.ValueBool()
project.Version = plan.Version.ValueString()
project.Classifier = plan.Classifier.ValueString()
project.Group = plan.Group.ValueString()
project.PURL = plan.PURL.ValueString()
project.CPE = plan.CPE.ValueString()
project.SWIDTagID = plan.SWID.ValueString()
if plan.Active.IsUnknown() {
project.Active = true
}
if plan.Classifier.IsUnknown() {
project.Classifier = "APPLICATION"
}
if !plan.Parent.IsNull() {
parentID, diag := TryParseUUID(plan.Parent, LifecycleUpdate, path.Root("parent"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
project.ParentRef = &dtrack.ParentRef{
UUID: parentID,
}
} else {
project.ParentRef = nil
}
if !plan.Tags.IsUnknown() && !plan.Tags.IsNull() {
var stringList []string
stringList, err = GetStringList(ctx, &resp.Diagnostics, plan.Tags)
if resp.Diagnostics.HasError() {
return
}
if err != nil {
resp.Diagnostics.AddAttributeError(
path.Root("tags"),
"Within Create, unable to convert `tags` list into slice of string, in project: "+project.UUID.String(),
"Error from: "+err.Error(),
)
return
}
project.Tags = Map(stringList, func(item string) dtrack.Tag { return dtrack.Tag{Name: item} })
}
if hasProjectCollectionFeature(*r.semver) && plan.Collection != nil {
collectionLogic := dtrack.CollectionLogic(plan.Collection.Logic.ValueString())
project.CollectionLogic = &collectionLogic
project.CollectionTag = &dtrack.Tag{Name: plan.Collection.Tag.ValueString()}
}
if hasProjectIsLatestFeature(*r.semver) && !plan.IsLatest.IsNull() {
project.IsLatest = plan.IsLatest.ValueBoolPointer()
}
// Execute.
tflog.Debug(ctx, "Updating Project", map[string]any{
"id": project.UUID.String(),
"name": project.Name,
"description": project.Description,
"active": project.Active,
"version": project.Version,
"is_latest": project.IsLatest,
"parent": project.ParentRef,
"classifier": project.Classifier,
"group": project.Group,
"purl": project.PURL,
"cpe": project.CPE,
"swid": project.SWIDTagID,
"tags": project.Tags,
"collection.logic": project.CollectionLogic,
"collection.tag": project.CollectionTag,
})
projectRes, err := r.client.Project.Update(ctx, project)
if err != nil {
resp.Diagnostics.AddError(
"Unable to update project",
"Error in: "+id.String()+", from: "+err.Error(),
)
return
}
tagValueList := Map(project.Tags, func(tag dtrack.Tag) attr.Value {
return types.StringValue(tag.Name)
})
tagList, diags := types.ListValue(types.StringType, tagValueList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map SDK to TF.
newPlan := projectResourceModel{
ID: types.StringValue(projectRes.UUID.String()),
Name: types.StringValue(projectRes.Name),
Description: types.StringValue(projectRes.Description),
Active: types.BoolValue(projectRes.Active),
Version: types.StringValue(projectRes.Version),
IsLatest: types.BoolNull(), // Updated below.
Parent: types.StringNull(), // Updated below.
Classifier: types.StringValue(projectRes.Classifier),
Group: types.StringValue(projectRes.Group),
PURL: types.StringValue(projectRes.PURL),
CPE: types.StringValue(projectRes.CPE),
SWID: types.StringValue(projectRes.SWIDTagID),
Tags: tagList,
Collection: nil, // Updated below.
}
if projectRes.ParentRef != nil {
newPlan.Parent = types.StringValue(projectRes.ParentRef.UUID.String())
} else {
newPlan.Parent = types.StringNull()
}
if hasProjectCollectionFeature(*r.semver) {
if *projectRes.CollectionLogic == "NONE" && plan.Collection == nil {
newPlan.Collection = nil
} else {
newPlan.Collection = &projectResourceModelCollection{
Logic: types.StringValue(string(*projectRes.CollectionLogic)),
Tag: types.StringNull(), // Updated below.
}
if projectRes.CollectionTag == nil || projectRes.CollectionTag.Name == "" {
newPlan.Collection.Tag = types.StringNull()
} else {
newPlan.Collection.Tag = types.StringValue(projectRes.CollectionTag.Name)
}
}
}
if hasProjectIsLatestFeature(*r.semver) {
newPlan.IsLatest = types.BoolValue(*projectRes.IsLatest)
}
// Update State.
diags = resp.State.Set(ctx, newPlan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Updated Project", map[string]any{
"id": projectRes.UUID.String(),
"name": projectRes.Name,
"description": projectRes.Description,
"active": projectRes.Active,
"version": projectRes.Version,
"is_latest": projectRes.IsLatest,
"parent": projectRes.ParentRef,
"classifier": projectRes.Classifier,
"group": projectRes.Group,
"purl": projectRes.PURL,
"cpe": projectRes.CPE,
"swid": projectRes.SWIDTagID,
"tags": projectRes.Tags,
"collection.logic": projectRes.CollectionLogic,
"collection.tag": projectRes.CollectionTag,
})
}
func (r *projectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Load state.
var state projectResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map TF to SDK.
id, diag := TryParseUUID(state.ID, LifecycleDelete, path.Root("id"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
// Execute.
tflog.Debug(ctx, "Deleting Project", map[string]any{
"id": id.String(),
"name": state.Name.ValueString(),
"description": state.Description.ValueString(),
"active": state.Active.ValueBool(),
"version": state.Version.ValueString(),
"is_latest": state.IsLatest.ValueBool(),
"parent": state.Parent.ValueString(),
"classifier": state.Classifier.ValueString(),
"group": state.Group.ValueString(),
"purl": state.PURL.ValueString(),
"cpe": state.CPE.ValueString(),
"swid": state.SWID.ValueString(),
"tags": state.Tags.Elements(),
})
err := r.client.Project.Delete(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete project",
"Unexpected error when trying to delete project: "+id.String()+", error: "+err.Error(),
)
return
}
tflog.Debug(ctx, "Deleted Project", map[string]any{
"id": state.ID.ValueString(),
"name": state.Name.ValueString(),
"description": state.Description.ValueString(),
"active": state.Active.ValueBool(),
"version": state.Version.ValueString(),
"is_latest": state.IsLatest.ValueBool(),
"parent": state.Parent.ValueString(),
"classifier": state.Classifier.ValueString(),
"group": state.Group.ValueString(),
"purl": state.PURL.ValueString(),
"cpe": state.CPE.ValueString(),
"swid": state.SWID.ValueString(),
"tags": state.Tags.Elements(),
})
}
func (*projectResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
tflog.Debug(ctx, "Importing Project", map[string]any{
"id": req.ID,
})
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Imported Project", map[string]any{
"id": req.ID,
})
}
func (r *projectResource) 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 hasProjectCollectionFeature(semver Semver) bool {
return semver.Major >= 4 && semver.Minor >= 13
}
func hasProjectIsLatestFeature(semver Semver) bool {
return semver.Major >= 4 && semver.Minor >= 12
}