/
framework_datasource.go
75 lines (62 loc) · 1.62 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
74
75
package lkeclusters
import (
"context"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/linode/linodego"
"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_lke_clusters",
Schema: &frameworkDatasourceSchema,
}),
}
}
func (d *DataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Read data.linode_lke_clusters")
var data LKEClusterFilterModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
id, diag := filterConfig.GenerateID(data.Filters)
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
data.ID = id
result, diag := filterConfig.GetAndFilter(
ctx, d.Meta.Client, data.Filters, listLKEClusters,
data.Order, data.OrderBy)
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
data.parseLKEClusters(ctx, helper.AnySliceToTyped[linodego.LKECluster](result))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func listLKEClusters(
ctx context.Context,
client *linodego.Client,
filter string,
) ([]any, error) {
tflog.Trace(ctx, "client.ListLKEClusters(...)", map[string]any{
"filter": filter,
})
lkeClusters, err := client.ListLKEClusters(ctx, &linodego.ListOptions{
Filter: filter,
})
if err != nil {
return nil, err
}
return helper.TypedSliceToAny(lkeClusters), nil
}