/
framework_datasource.go
73 lines (60 loc) · 1.46 KB
/
framework_datasource.go
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
package image
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
)
type DataSource struct {
helper.BaseDataSource
}
func NewDataSource() datasource.DataSource {
return &DataSource{
BaseDataSource: helper.NewBaseDataSource(
helper.BaseDataSourceConfig{
Name: "linode_image",
Schema: &frameworkDatasourceSchema,
},
),
}
}
func (d *DataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Read data.linode_image")
client := d.Meta.Client
var data ImageModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if data.ID.ValueString() == "" {
resp.Diagnostics.AddError(
"Image ID is required.",
"Image ID can't be empty.",
)
return
}
ctx = tflog.SetField(ctx, "image_id", data.ID.ValueString())
tflog.Trace(ctx, "client.GetImage(...)")
image, err := client.GetImage(ctx, data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error listing images: %s", data.ID.ValueString()),
err.Error(),
)
return
}
if image == nil {
resp.Diagnostics.AddError(
"Image not found.",
fmt.Sprintf("Image %s was not found", data.ID.ValueString()),
)
return
}
data.ParseImage(image)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}