-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_data_source.go
More file actions
256 lines (241 loc) · 8.55 KB
/
project_data_source.go
File metadata and controls
256 lines (241 loc) · 8.55 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
package provider
import (
"context"
"fmt"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Interface impl check.
var (
_ datasource.DataSource = &projectDataSource{}
_ datasource.DataSourceWithConfigure = &projectDataSource{}
)
type (
projectDataSource struct {
client *dtrack.Client
semver *Semver
}
projectDataSourceModel struct {
Name types.String `tfsdk:"name"`
Version types.String `tfsdk:"version"`
ID types.String `tfsdk:"id"`
Classifier types.String `tfsdk:"classifier"`
CPE types.String `tfsdk:"cpe"`
Group types.String `tfsdk:"group"`
Parent types.String `tfsdk:"parent"`
PURL types.String `tfsdk:"purl"`
SWID types.String `tfsdk:"swid"`
Properties []projectPropertiesModel `tfsdk:"properties"`
Tags []types.String `tfsdk:"tags"`
IsLatest types.Bool `tfsdk:"is_latest"`
}
projectPropertiesModel 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 NewProjectDataSource() datasource.DataSource {
return &projectDataSource{}
}
func (*projectDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project"
}
func (*projectDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetch an existing Project by name and version, or by name with is_latest set to true.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Name of the project to find.",
Required: true,
},
"version": schema.StringAttribute{
Description: "Version of the project to find. Optional when is_latest is true.",
Optional: true,
Computed: true,
},
"is_latest": schema.BoolAttribute{
Description: "Whether the project is the latest version. When true, version is optional and the latest project version will be returned. Available in API 4.12+.",
Optional: true,
Computed: true,
},
"id": schema.StringAttribute{
Description: "UUID of the project located.",
Computed: true,
},
"properties": schema.ListNestedAttribute{
Description: "Existing properties within the Project.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"group": schema.StringAttribute{
Description: "Group Name for the project Property.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "Property Name for the project Property.",
Computed: true,
},
"value": schema.StringAttribute{
Description: "Property Value for the project Property.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "Property Type for the project Property as a string enum.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "Description for the project Property.",
Computed: true,
},
},
},
},
"classifier": schema.StringAttribute{
Description: "Classifier of the Project. See DependencyTrack for possible enum values.",
Computed: true,
},
"cpe": schema.StringAttribute{
Description: "Common Platform Enumeration for the Project. Standardised format v2.2 / v2.3 from MITRE / NIST",
Computed: true,
},
"group": schema.StringAttribute{
Description: "Namespace / group / vendor of the Project.",
Computed: true,
},
"parent": schema.StringAttribute{
Description: "UUID of a parent project, if nested.",
Computed: true,
Optional: true,
},
"purl": schema.StringAttribute{
Description: "Package URL of the Project. Follows standardised format.",
Computed: true,
},
"swid": schema.StringAttribute{
Description: "SWID Tag ID. ISO/IEC 19770-2:2015",
Computed: true,
},
"tags": schema.ListAttribute{
Description: "Tags on the project.",
Computed: true,
ElementType: types.StringType,
},
},
}
}
func (d *projectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state projectDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
isLatest := state.IsLatest.ValueBool()
name := state.Name.ValueString()
tflog.Debug(ctx, "Reading Project", map[string]any{
"name": name,
"version": state.Version.ValueString(),
"is_latest": isLatest,
})
var project dtrack.Project
var err error
if isLatest && (state.Version.IsNull() || state.Version.IsUnknown()) {
project, err = d.client.Project.Latest(ctx, name)
} else if !state.Version.IsNull() && !state.Version.IsUnknown() {
project, err = d.client.Project.Lookup(ctx, name, state.Version.ValueString())
} else {
resp.Diagnostics.AddError(
"Invalid configuration",
"Either 'version' must be provided, or 'is_latest' must be set to true.",
)
return
}
if err != nil {
resp.Diagnostics.AddError(
"Unable to read Project",
"Error from: "+err.Error(),
)
return
}
// Transform data into model.
projectState := projectDataSourceModel{
Name: types.StringValue(project.Name),
Version: types.StringValue(project.Version),
ID: types.StringValue(project.UUID.String()),
Properties: make([]projectPropertiesModel, 0),
Classifier: types.StringValue(project.Classifier),
CPE: types.StringValue(project.CPE),
Group: types.StringValue(project.Group),
PURL: types.StringValue(project.PURL),
SWID: types.StringValue(project.SWIDTagID),
Parent: types.StringNull(),
Tags: Map(project.Tags, func(item dtrack.Tag) types.String {
return types.StringValue(item.Name)
}),
IsLatest: types.BoolNull(), // Set below.
}
if project.ParentRef != nil {
projectState.Parent = types.StringValue(project.ParentRef.UUID.String())
}
for _, property := range project.Properties {
model := projectPropertiesModel{
Group: types.StringValue(property.Group),
Name: types.StringValue(property.Name),
Value: types.StringValue(property.Value),
Type: types.StringValue(property.Type),
Description: types.StringValue(property.Description),
}
projectState.Properties = append(projectState.Properties, model)
tflog.Debug(ctx, "Read Project's Property", map[string]any{
"group": property.Group,
"name": property.Name,
"value": property.Value,
"type": property.Type,
"description": property.Description,
})
}
if hasProjectIsLatestFeature(*d.semver) {
projectState.IsLatest = types.BoolValue(*project.IsLatest)
}
// Update state.
diags = resp.State.Set(ctx, &projectState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read Project", map[string]any{
"id": projectState.ID.ValueString(),
"name": projectState.Name.ValueString(),
"version": projectState.Version.ValueString(),
"is_latest": projectState.IsLatest.ValueBool(),
"properties.#": len(projectState.Properties),
"classifier": projectState.Classifier.ValueString(),
"cpe": projectState.CPE.ValueString(),
"group": projectState.Group.ValueString(),
"purl": projectState.PURL.ValueString(),
"swid": projectState.SWID.ValueString(),
"parent": projectState.Parent.ValueString(),
"tags": Map(projectState.Tags, func(item types.String) string { return item.ValueString() }),
})
}
func (d *projectDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.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
}
d.client = clientInfoData.client
d.semver = clientInfoData.semver
}