/
resource.go
230 lines (193 loc) · 7.26 KB
/
resource.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
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
package firewall
import (
"context"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
linodediffs "github.com/linode/terraform-provider-linode/v2/linode/helper/customdiffs"
)
func resourceFirewallRules() *schema.Resource {
return &schema.Resource{
Schema: resourceRuleSchema,
}
}
func resourceFirewallDevice() *schema.Resource {
return &schema.Resource{
Schema: resourceDeviceSchema,
}
}
func Resource() *schema.Resource {
return &schema.Resource{
Schema: resourceSchema,
ReadContext: readResource,
CreateContext: createResource,
UpdateContext: updateResource,
DeleteContext: deleteResource,
CustomizeDiff: customdiff.All(
linodediffs.ComputedWithDefault("tags", []string{}),
linodediffs.CaseInsensitiveSet("tags"),
),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func readResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
ctx = populateLogAttributes(ctx, d)
tflog.Debug(ctx, "Read linode_firewall")
client := meta.(*helper.ProviderMeta).Client
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.Errorf("failed to parse Firewall %s as int: %s", d.Id(), err)
}
tflog.Trace(ctx, "client.GetFirewall(...)")
firewall, err := client.GetFirewall(ctx, id)
if err != nil {
if apiErr, ok := err.(*linodego.Error); ok && apiErr.Code == 404 {
log.Printf("[WARN] removing Linode Firewall ID %q from state because it no longer exists", d.Id())
d.SetId("")
return nil
}
return diag.Errorf("failed to get firewall %d: %s", id, err)
}
tflog.Trace(ctx, "client.GetFirewallRules(...)")
rules, err := client.GetFirewallRules(ctx, id)
if err != nil {
return diag.Errorf("failed to get rules for firewall %d: %s", id, err)
}
tflog.Trace(ctx, "client.ListFirewallDevices(...)")
devices, err := client.ListFirewallDevices(ctx, id, nil)
if err != nil {
return diag.Errorf("failed to get devices for firewall %d: %s", id, err)
}
d.Set("label", firewall.Label)
d.Set("disabled", firewall.Status == linodego.FirewallDisabled)
d.Set("tags", firewall.Tags)
d.Set("status", firewall.Status)
d.Set("created", firewall.Created.Format(helper.TIME_FORMAT))
d.Set("updated", firewall.Updated.Format(helper.TIME_FORMAT))
d.Set("inbound", flattenFirewallRules(rules.Inbound))
d.Set("outbound", flattenFirewallRules(rules.Outbound))
d.Set("inbound_policy", firewall.Rules.InboundPolicy)
d.Set("outbound_policy", firewall.Rules.OutboundPolicy)
d.Set("linodes", AggregateEntityIDs(devices, linodego.FirewallDeviceLinode))
d.Set("nodebalancers", AggregateEntityIDs(devices, linodego.FirewallDeviceNodeBalancer))
d.Set("devices", flattenFirewallDevices(devices))
return nil
}
func createResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*helper.ProviderMeta).Client
createOpts := linodego.FirewallCreateOptions{
Label: d.Get("label").(string),
Tags: helper.ExpandStringSet(d.Get("tags").(*schema.Set)),
}
createOpts.Devices.Linodes = helper.ExpandIntSet(d.Get("linodes").(*schema.Set))
createOpts.Devices.NodeBalancers = helper.ExpandIntSet(d.Get("nodebalancers").(*schema.Set))
createOpts.Rules.Inbound = expandFirewallRules(d.Get("inbound").([]any))
createOpts.Rules.InboundPolicy = d.Get("inbound_policy").(string)
createOpts.Rules.Outbound = expandFirewallRules(d.Get("outbound").([]any))
createOpts.Rules.OutboundPolicy = d.Get("outbound_policy").(string)
tflog.Debug(ctx, "client.CreateFirewall(...)", map[string]any{
"options": createOpts,
})
firewall, err := client.CreateFirewall(ctx, createOpts)
if err != nil {
return diag.Errorf("failed to create Firewall: %s", err)
}
d.SetId(strconv.Itoa(firewall.ID))
ctx = populateLogAttributes(ctx, d)
if d.Get("disabled").(bool) {
updateOpts := linodego.FirewallUpdateOptions{
Status: linodego.FirewallDisabled,
}
tflog.Debug(ctx, "client.UpdateFirewall(...)", map[string]any{
"options": updateOpts,
})
if _, err := client.UpdateFirewall(ctx, firewall.ID, updateOpts); err != nil {
return diag.Errorf("failed to disable firewall %d: %s", firewall.ID, err)
}
}
return readResource(ctx, d, meta)
}
func updateResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
ctx = populateLogAttributes(ctx, d)
tflog.Debug(ctx, "Update linode_firewall")
client := meta.(*helper.ProviderMeta).Client
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.Errorf("failed to parse Firewall %s as int: %s", d.Id(), err)
}
if d.HasChanges("label", "tags", "disabled") {
updateOpts := linodego.FirewallUpdateOptions{}
if d.HasChange("label") {
updateOpts.Label = d.Get("label").(string)
}
if d.HasChange("tags") {
tags := helper.ExpandStringSet(d.Get("tags").(*schema.Set))
updateOpts.Tags = &tags
}
if d.HasChange("disabled") {
updateOpts.Status = expandFirewallStatus(d.Get("disabled"))
}
tflog.Debug(ctx, "client.UpdateFirewall(...)", map[string]any{
"options": updateOpts,
})
if _, err := client.UpdateFirewall(ctx, id, updateOpts); err != nil {
return diag.Errorf("failed to update firewall %d: %s", id, err)
}
}
inboundRules := expandFirewallRules(d.Get("inbound").([]any))
outboundRules := expandFirewallRules(d.Get("outbound").([]any))
ruleSet := linodego.FirewallRuleSet{
Inbound: inboundRules,
InboundPolicy: d.Get("inbound_policy").(string),
Outbound: outboundRules,
OutboundPolicy: d.Get("outbound_policy").(string),
}
tflog.Debug(ctx, "client.UpdateFirewallRules(...)", map[string]any{
"options": ruleSet,
})
if _, err := client.UpdateFirewallRules(ctx, id, ruleSet); err != nil {
return diag.Errorf("failed to update rules for firewall %d: %s", id, err)
}
linodes, linodesOk := d.GetOk("linodes")
nodebalancers, nodebalancersOk := d.GetOk("nodebalancers")
if linodesOk || nodebalancersOk {
assignments := make([]firewallDeviceAssignment, 0)
for _, entityID := range helper.ExpandIntSet(linodes.(*schema.Set)) {
assignments = append(assignments, firewallDeviceAssignment{
ID: entityID,
Type: linodego.FirewallDeviceLinode,
})
}
for _, entityID := range helper.ExpandIntSet(nodebalancers.(*schema.Set)) {
assignments = append(assignments, firewallDeviceAssignment{
ID: entityID,
Type: linodego.FirewallDeviceNodeBalancer,
})
}
tflog.Debug(ctx, "Reconciling firewall device assignments")
if err := updateFirewallDevices(ctx, d, client, id, assignments); err != nil {
return diag.Errorf("failed to update firewall devices: %s", err)
}
}
return nil
}
func deleteResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
ctx = populateLogAttributes(ctx, d)
client := meta.(*helper.ProviderMeta).Client
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.Errorf("failed to parse Firewall %s as int: %s", d.Id(), err)
}
tflog.Debug(ctx, "client.DeleteFirewall(...)")
if err := client.DeleteFirewall(ctx, id); err != nil {
return diag.Errorf("failed to delete Firewall %d: %s", id, err)
}
return nil
}