/
framework_datasource.go
83 lines (70 loc) · 1.61 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
76
77
78
79
80
81
82
83
package vlan
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"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_vlans",
Schema: &frameworkDatasourceSchema,
IsEarlyAccess: true,
},
),
}
}
func (d *DataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Read data.linode_vlans")
var data VLANsFilterModel
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
results, diag := filterConfig.GetAndFilter(
ctx, d.Meta.Client,
data.Filters,
listVLANs,
data.Order,
data.OrderBy,
)
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
data.parseVLANs(ctx, helper.AnySliceToTyped[linodego.VLAN](results))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func listVLANs(
ctx context.Context,
client *linodego.Client,
filter string,
) ([]any, error) {
tflog.Trace(ctx, "client.ListVLANs(...)", map[string]any{
"filter": filter,
})
vlans, err := client.ListVLANs(
ctx,
&linodego.ListOptions{Filter: filter},
)
if err != nil {
return nil, err
}
return helper.TypedSliceToAny(vlans), nil
}