/
framework_datasource.go
86 lines (71 loc) · 1.91 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
84
85
86
package firewall
import (
"context"
"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_firewall",
Schema: &frameworkDatasourceSchema,
},
),
}
}
func (d *DataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
tflog.Debug(ctx, "Read data.linode_firewall")
var data FirewallModel
client := d.Meta.Client
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
firewallID := helper.FrameworkSafeInt64ToInt(data.ID.ValueInt64(), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "firewall_id", firewallID)
tflog.Trace(ctx, "client.GetFirewall(...)")
firewall, err := client.GetFirewall(ctx, firewallID)
if err != nil {
resp.Diagnostics.AddError(
"Failed to get firewall",
err.Error(),
)
return
}
tflog.Trace(ctx, "client.GetFirewallRules(...)")
rules, err := client.GetFirewallRules(ctx, firewallID)
if err != nil {
resp.Diagnostics.AddError(
"Failed to get firewall rules",
err.Error(),
)
return
}
tflog.Trace(ctx, "client.ListFirewallDevices(...)")
devices, err := client.ListFirewallDevices(ctx, firewallID, nil)
if err != nil {
resp.Diagnostics.AddError(
"Failed to get firewall devices",
err.Error(),
)
return
}
resp.Diagnostics.Append(data.parseComputedAttributes(ctx, firewall, rules, devices)...)
resp.Diagnostics.Append(data.parseNonComputedAttributes(ctx, firewall, rules, devices)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}