-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdatasource.go
More file actions
344 lines (322 loc) · 12.2 KB
/
Copy pathdatasource.go
File metadata and controls
344 lines (322 loc) · 12.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
package image
import (
"context"
"fmt"
"net/http"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &imageDataSource{}
)
type DataSourceModel struct {
Id types.String `tfsdk:"id"` // needed by TF
ProjectId types.String `tfsdk:"project_id"`
ImageId types.String `tfsdk:"image_id"`
Name types.String `tfsdk:"name"`
DiskFormat types.String `tfsdk:"disk_format"`
MinDiskSize types.Int64 `tfsdk:"min_disk_size"`
MinRAM types.Int64 `tfsdk:"min_ram"`
Protected types.Bool `tfsdk:"protected"`
Scope types.String `tfsdk:"scope"`
Config types.Object `tfsdk:"config"`
Checksum types.Object `tfsdk:"checksum"`
Labels types.Map `tfsdk:"labels"`
}
// NewImageDataSource is a helper function to simplify the provider implementation.
func NewImageDataSource() datasource.DataSource {
return &imageDataSource{}
}
// imageDataSource is the data source implementation.
type imageDataSource struct {
client *iaas.APIClient
}
// Metadata returns the data source type name.
func (d *imageDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_image"
}
func (d *imageDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient
tflog.Info(ctx, "iaas client configured")
}
// Schema defines the schema for the datasource.
func (r *imageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
description := "Image datasource schema. Must have a `region` specified in the provider configuration."
resp.Schema = schema.Schema{
MarkdownDescription: description,
Description: description,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`image_id`\".",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "STACKIT project ID to which the image is associated.",
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"image_id": schema.StringAttribute{
Description: "The image ID.",
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"name": schema.StringAttribute{
Description: "The name of the image.",
Computed: true,
},
"disk_format": schema.StringAttribute{
Description: "The disk format of the image.",
Computed: true,
},
"min_disk_size": schema.Int64Attribute{
Description: "The minimum disk size of the image in GB.",
Computed: true,
},
"min_ram": schema.Int64Attribute{
Description: "The minimum RAM of the image in MB.",
Computed: true,
},
"protected": schema.BoolAttribute{
Description: "Whether the image is protected.",
Computed: true,
},
"scope": schema.StringAttribute{
Description: "The scope of the image.",
Computed: true,
},
"config": schema.SingleNestedAttribute{
Description: "Properties to set hardware and scheduling settings for an image.",
Computed: true,
Attributes: map[string]schema.Attribute{
"boot_menu": schema.BoolAttribute{
Description: "Enables the BIOS bootmenu.",
Computed: true,
},
"cdrom_bus": schema.StringAttribute{
Description: "Sets CDROM bus controller type.",
Computed: true,
},
"disk_bus": schema.StringAttribute{
Description: "Sets Disk bus controller type.",
Computed: true,
},
"nic_model": schema.StringAttribute{
Description: "Sets virtual network interface model.",
Computed: true,
},
"operating_system": schema.StringAttribute{
Description: "Enables operating system specific optimizations.",
Computed: true,
},
"operating_system_distro": schema.StringAttribute{
Description: "Operating system distribution.",
Computed: true,
},
"operating_system_version": schema.StringAttribute{
Description: "Version of the operating system.",
Computed: true,
},
"rescue_bus": schema.StringAttribute{
Description: "Sets the device bus when the image is used as a rescue image.",
Computed: true,
},
"rescue_device": schema.StringAttribute{
Description: "Sets the device when the image is used as a rescue image.",
Computed: true,
},
"secure_boot": schema.BoolAttribute{
Description: "Enables Secure Boot.",
Computed: true,
},
"uefi": schema.BoolAttribute{
Description: "Enables UEFI boot.",
Computed: true,
},
"video_model": schema.StringAttribute{
Description: "Sets Graphic device model.",
Computed: true,
},
"virtio_scsi": schema.BoolAttribute{
Description: "Enables the use of VirtIO SCSI to provide block device access. By default instances use VirtIO Block.",
Computed: true,
},
},
},
"checksum": schema.SingleNestedAttribute{
Description: "Representation of an image checksum.",
Computed: true,
Attributes: map[string]schema.Attribute{
"algorithm": schema.StringAttribute{
Description: "Algorithm for the checksum of the image data.",
Computed: true,
},
"digest": schema.StringAttribute{
Description: "Hexdigest of the checksum of the image data.",
Computed: true,
},
},
},
"labels": schema.MapAttribute{
Description: "Labels are key-value string pairs which can be attached to a resource container",
ElementType: types.StringType,
Computed: true,
},
},
}
}
// // Read refreshes the Terraform state with the latest data.
func (r *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
var model DataSourceModel
diags := req.Config.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
projectId := model.ProjectId.ValueString()
imageId := model.ImageId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "image_id", imageId)
imageResp, err := r.client.GetImage(ctx, projectId, imageId).Execute()
if err != nil {
utils.LogError(
ctx,
&resp.Diagnostics,
err,
"Reading image",
fmt.Sprintf("Image with ID %q does not exist in project %q.", imageId, projectId),
map[int]string{
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
},
)
resp.State.RemoveResource(ctx)
return
}
// Map response body to schema
err = mapDataSourceFields(ctx, imageResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading image", fmt.Sprintf("Processing API payload: %v", err))
return
}
// Set refreshed state
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "image read")
}
func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *DataSourceModel) error {
if imageResp == nil {
return fmt.Errorf("response input is nil")
}
if model == nil {
return fmt.Errorf("model input is nil")
}
var imageId string
if model.ImageId.ValueString() != "" {
imageId = model.ImageId.ValueString()
} else if imageResp.Id != nil {
imageId = *imageResp.Id
} else {
return fmt.Errorf("image id not present")
}
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), imageId)
// Map config
var configModel = &configModel{}
var configObject basetypes.ObjectValue
diags := diag.Diagnostics{}
if imageResp.Config != nil {
configModel.BootMenu = types.BoolPointerValue(imageResp.Config.BootMenu)
configModel.CDROMBus = types.StringPointerValue(imageResp.Config.GetCdromBus())
configModel.DiskBus = types.StringPointerValue(imageResp.Config.GetDiskBus())
configModel.NICModel = types.StringPointerValue(imageResp.Config.GetNicModel())
configModel.OperatingSystem = types.StringPointerValue(imageResp.Config.OperatingSystem)
configModel.OperatingSystemDistro = types.StringPointerValue(imageResp.Config.GetOperatingSystemDistro())
configModel.OperatingSystemVersion = types.StringPointerValue(imageResp.Config.GetOperatingSystemVersion())
configModel.RescueBus = types.StringPointerValue(imageResp.Config.GetRescueBus())
configModel.RescueDevice = types.StringPointerValue(imageResp.Config.GetRescueDevice())
configModel.SecureBoot = types.BoolPointerValue(imageResp.Config.SecureBoot)
configModel.UEFI = types.BoolPointerValue(imageResp.Config.Uefi)
configModel.VideoModel = types.StringPointerValue(imageResp.Config.GetVideoModel())
configModel.VirtioScsi = types.BoolPointerValue(iaas.PtrBool(imageResp.Config.GetVirtioScsi()))
configObject, diags = types.ObjectValue(configTypes, map[string]attr.Value{
"boot_menu": configModel.BootMenu,
"cdrom_bus": configModel.CDROMBus,
"disk_bus": configModel.DiskBus,
"nic_model": configModel.NICModel,
"operating_system": configModel.OperatingSystem,
"operating_system_distro": configModel.OperatingSystemDistro,
"operating_system_version": configModel.OperatingSystemVersion,
"rescue_bus": configModel.RescueBus,
"rescue_device": configModel.RescueDevice,
"secure_boot": configModel.SecureBoot,
"uefi": configModel.UEFI,
"video_model": configModel.VideoModel,
"virtio_scsi": configModel.VirtioScsi,
})
} else {
configObject = types.ObjectNull(configTypes)
}
if diags.HasError() {
return fmt.Errorf("creating config: %w", core.DiagsToError(diags))
}
// Map checksum
var checksumModel = &checksumModel{}
var checksumObject basetypes.ObjectValue
if imageResp.Checksum != nil {
checksumModel.Algorithm = types.StringPointerValue(imageResp.Checksum.Algorithm)
checksumModel.Digest = types.StringPointerValue(imageResp.Checksum.Digest)
checksumObject, diags = types.ObjectValue(checksumTypes, map[string]attr.Value{
"algorithm": checksumModel.Algorithm,
"digest": checksumModel.Digest,
})
} else {
checksumObject = types.ObjectNull(checksumTypes)
}
if diags.HasError() {
return fmt.Errorf("creating checksum: %w", core.DiagsToError(diags))
}
// Map labels
labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
if err != nil {
return err
}
model.ImageId = types.StringValue(imageId)
model.Name = types.StringPointerValue(imageResp.Name)
model.DiskFormat = types.StringPointerValue(imageResp.DiskFormat)
model.MinDiskSize = types.Int64PointerValue(imageResp.MinDiskSize)
model.MinRAM = types.Int64PointerValue(imageResp.MinRam)
model.Protected = types.BoolPointerValue(imageResp.Protected)
model.Scope = types.StringPointerValue(imageResp.Scope)
model.Labels = labels
model.Config = configObject
model.Checksum = checksumObject
return nil
}