/
firewall_ip_alias_resource.go
346 lines (284 loc) · 9.88 KB
/
firewall_ip_alias_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/marshallford/terraform-provider-pfsense/pkg/pfsense"
)
var _ resource.Resource = &FirewallIPAliasResource{}
var _ resource.ResourceWithImportState = &FirewallIPAliasResource{}
func NewFirewallIPAliasResource() resource.Resource {
return &FirewallIPAliasResource{}
}
type FirewallIPAliasResource struct {
client *pfsense.Client
}
type FirewallIPAliasResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Type types.String `tfsdk:"type"`
Apply types.Bool `tfsdk:"apply"`
Entries types.List `tfsdk:"entries"`
}
type FirewallIPAliasEntryResourceModel struct {
Address types.String `tfsdk:"address"`
Description types.String `tfsdk:"description"`
}
func (r FirewallIPAliasEntryResourceModel) GetAttrType() attr.Type {
return types.ObjectType{AttrTypes: map[string]attr.Type{
"address": types.StringType,
"description": types.StringType,
}}
}
func (r *FirewallIPAliasResourceModel) SetFromValue(ctx context.Context, ipAlias *pfsense.FirewallIPAlias) diag.Diagnostics {
var diags diag.Diagnostics
r.Name = types.StringValue(ipAlias.Name)
if ipAlias.Description != "" {
r.Description = types.StringValue(ipAlias.Description)
}
r.Type = types.StringValue(ipAlias.Type)
entries := []FirewallIPAliasEntryResourceModel{}
for _, entry := range ipAlias.Entries {
var entryModel FirewallIPAliasEntryResourceModel
entryModel.Address = types.StringValue(entry.Address)
if entry.Description != "" {
entryModel.Description = types.StringValue(entry.Description)
}
entries = append(entries, entryModel)
}
r.Entries, diags = types.ListValueFrom(ctx, FirewallIPAliasEntryResourceModel{}.GetAttrType(), entries)
return diags
}
func (r FirewallIPAliasResourceModel) Value(ctx context.Context) (*pfsense.FirewallIPAlias, diag.Diagnostics) {
var ipAlias pfsense.FirewallIPAlias
var err error
var diags diag.Diagnostics
var entryModels []*FirewallIPAliasEntryResourceModel
diags = r.Entries.ElementsAs(ctx, &entryModels, false)
if diags.HasError() {
return nil, diags
}
err = ipAlias.SetName(r.Name.ValueString())
if err != nil {
diags.AddAttributeError(
path.Root("name"),
"Name cannot be parsed",
err.Error(),
)
}
if !r.Description.IsNull() {
err = ipAlias.SetDescription(r.Description.ValueString())
if err != nil {
diags.AddAttributeError(
path.Root("description"),
"Description cannot be parsed",
err.Error(),
)
}
}
err = ipAlias.SetType(r.Type.ValueString())
if err != nil {
diags.AddAttributeError(
path.Root("type"),
"Type cannot be parsed",
err.Error(),
)
}
for i, entryModel := range entryModels {
var entry pfsense.FirewallIPAliasEntry
err = entry.SetAddress(entryModel.Address.ValueString())
if err != nil {
diags.AddAttributeError(
path.Root("entries").AtListIndex(i).AtName("address"),
"Entry address cannot be parsed",
err.Error(),
)
}
if !entryModel.Description.IsNull() {
err = entry.SetDescription(entryModel.Description.ValueString())
if err != nil {
diags.AddAttributeError(
path.Root("entries").AtListIndex(i).AtName("description"),
"Entry description cannot be parsed",
err.Error(),
)
}
}
ipAlias.Entries = append(ipAlias.Entries, entry)
}
return &ipAlias, diags
}
func (r *FirewallIPAliasResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_firewall_ip_alias", req.ProviderTypeName)
}
func (r *FirewallIPAliasResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Firewall IP alias, defines a group of hosts or networks. Aliases can be referenced by firewall rules, port forwards, outbound NAT rules, and other places in the firewall.",
MarkdownDescription: "Firewall IP [alias](https://docs.netgate.com/pfsense/en/latest/firewall/aliases.html), defines a group of hosts or networks. Aliases can be referenced by firewall rules, port forwards, outbound NAT rules, and other places in the firewall.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Name of alias.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "For administrative reference (not parsed).",
Optional: true,
},
"type": schema.StringAttribute{
Description: "Type of alias.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"apply": schema.BoolAttribute{
Description: "Apply change, defaults to 'true'.",
MarkdownDescription: "Apply change, defaults to `true`.",
Computed: true,
Optional: true,
Default: booldefault.StaticBool(true),
},
"entries": schema.ListNestedAttribute{
Description: "Host(s) or network(s).",
Computed: true,
Optional: true,
Default: listdefault.StaticValue(types.ListValueMust(FirewallIPAliasEntryResourceModel{}.GetAttrType(), []attr.Value{})),
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"address": schema.StringAttribute{
Description: "Hosts must be specified by their IP address or fully qualified domain name (FQDN). Networks are specified in CIDR format.",
Required: true,
},
"description": schema.StringAttribute{
Description: "For administrative reference (not parsed).",
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
},
},
}
}
func (r *FirewallIPAliasResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
client, ok := configureResourceClient(req, resp)
if !ok {
return
}
r.client = client
}
func (r *FirewallIPAliasResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *FirewallIPAliasResourceModel
var diags diag.Diagnostics
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
ipAliasReq, d := data.Value(ctx)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
ipAlias, err := r.client.CreateFirewallIPAlias(ctx, *ipAliasReq)
if addError(&resp.Diagnostics, "Error creating IP alias", err) {
return
}
diags = data.SetFromValue(ctx, ipAlias)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if data.Apply.ValueBool() {
err = r.client.ReloadFirewallFilter(ctx)
if addError(&resp.Diagnostics, "Error applying IP alias", err) {
return
}
}
}
func (r *FirewallIPAliasResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *FirewallIPAliasResourceModel
var diags diag.Diagnostics
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
ipAlias, err := r.client.GetFirewallIPAlias(ctx, data.Name.ValueString())
if addError(&resp.Diagnostics, "Error reading IP alias", err) {
return
}
diags = data.SetFromValue(ctx, ipAlias)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *FirewallIPAliasResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *FirewallIPAliasResourceModel
var diags diag.Diagnostics
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
ipAliasReq, d := data.Value(ctx)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
if resp.Diagnostics.HasError() {
return
}
ipAlias, err := r.client.UpdateFirewallIPAlias(ctx, *ipAliasReq)
if addError(&resp.Diagnostics, "Error updating IP alias", err) {
return
}
diags = data.SetFromValue(ctx, ipAlias)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if data.Apply.ValueBool() {
err = r.client.ReloadFirewallFilter(ctx)
if addError(&resp.Diagnostics, "Error applying IP alias", err) {
return
}
}
}
func (r *FirewallIPAliasResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *FirewallIPAliasResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
err := r.client.DeleteFirewallIPAlias(ctx, data.Name.ValueString())
if addError(&resp.Diagnostics, "Error deleting IP alias", err) {
return
}
resp.State.RemoveResource(ctx)
if data.Apply.ValueBool() {
err = r.client.ReloadFirewallFilter(ctx)
if addError(&resp.Diagnostics, "Error applying IP alias", err) {
return
}
}
}
func (r *FirewallIPAliasResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}