-
Notifications
You must be signed in to change notification settings - Fork 2
/
prefix_filter.go
212 lines (194 loc) · 9.5 KB
/
prefix_filter.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
package blueprint
import (
"context"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/apstra_validator"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"net"
)
type prefixFilter struct {
Prefix types.String `tfsdk:"prefix"`
GeMask types.Int64 `tfsdk:"ge_mask"`
LeMask types.Int64 `tfsdk:"le_mask"`
Action types.String `tfsdk:"action"`
}
func (o prefixFilter) resourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"prefix": resourceSchema.StringAttribute{
MarkdownDescription: "IPv4 or IPv6 network address specified in the form of network/prefixlen.",
Required: true,
Validators: []validator.String{apstravalidator.ParseCidr(false, false)},
},
"ge_mask": resourceSchema.Int64Attribute{
MarkdownDescription: "Match less-specific prefixes from a parent prefix, up from `ge_mask` to the prefix " +
"length of the route. Range is 0-32 for IPv4, 0-128 for IPv6. If not specified, implies the " +
"prefix-list entry should be an exact match. The option can be optionally be used in combination " +
"with `le_mask`. `ge_mask` must be longer than the subnet prefix length. If `le_mask` and `ge_mask` " +
"are both specified, then `le_mask` must be greater than `ge_mask`.",
Optional: true,
Validators: []validator.Int64{int64validator.AtLeast(1)},
},
"le_mask": resourceSchema.Int64Attribute{
MarkdownDescription: "Match more-specific prefixes from a parent prefix, up until `le_mask` prefix len. " +
"Range is 0-32 for IPv4, 0-128 for IPv6. If not specified, implies the prefix-list entry should be " +
"an exact match. The option can be optionally be used in combination with `ge_mask`. `le_mask` must " +
"be longer than the subnet prefix length. If `le_mask` and `ge_mask` are both specified, then " +
"`le_mask` must be greater than `ge_mask`.",
Optional: true,
Validators: []validator.Int64{int64validator.AtLeast(1)},
},
"action": resourceSchema.StringAttribute{
MarkdownDescription: fmt.Sprintf("If the action is %q, match the route. If the action is %q, do "+
"not match the route. For composing complex policies, all prefix-list items will be processed in the "+
"order specified, top-down. This allows the user to deny a subset of a route that may otherwise be "+
"permitted.", apstra.PrefixFilterActionPermit, apstra.PrefixFilterActionDeny),
Computed: true,
Optional: true,
Default: stringdefault.StaticString(apstra.PrefixFilterActionPermit.String()),
Validators: []validator.String{stringvalidator.OneOf(utils.AllValidPrefixFilterActions()...)},
},
}
}
func (o prefixFilter) dataSourceAttributes() map[string]dataSourceSchema.Attribute {
return map[string]dataSourceSchema.Attribute{
"prefix": dataSourceSchema.StringAttribute{
MarkdownDescription: "IPv4 or IPv6 network address specified in the form of network/prefixlen.",
Computed: true,
},
"ge_mask": dataSourceSchema.Int64Attribute{
MarkdownDescription: "Match less-specific prefixes from a parent prefix, up from `ge_mask` to the prefix " +
"length of the route. Range is 0-32 for IPv4, 0-128 for IPv6. If not specified, implies the " +
"prefix-list entry should be an exact match. The option can be optionally be used in combination " +
"with `le_mask`. `ge_mask` must be longer than the subnet prefix length. If `le_mask` and `ge_mask` " +
"are both specified, then `le_mask` must be greater than `ge_mask`.",
Computed: true,
},
"le_mask": dataSourceSchema.Int64Attribute{
MarkdownDescription: "Match more-specific prefixes from a parent prefix, up until `le_mask` prefix len. " +
"Range is 0-32 for IPv4, 0-128 for IPv6. If not specified, implies the prefix-list entry should be " +
"an exact match. The option can be optionally be used in combination with `ge_mask`. `le_mask` must " +
"be longer than the subnet prefix length. If `le_mask` and `ge_mask` are both specified, then " +
"`le_mask` must be greater than `ge_mask`.",
Computed: true,
},
"action": dataSourceSchema.StringAttribute{
MarkdownDescription: fmt.Sprintf("If the action is %q, match the route. If the action is %q, do "+
"not match the route. For composing complex policies, all prefix-list items will be processed in the "+
"order specified, top-down. This allows the user to deny a subset of a route that may otherwise be "+
"permitted.", apstra.PrefixFilterActionPermit, apstra.PrefixFilterActionDeny),
Computed: true,
},
}
}
func (o prefixFilter) dataSourceAttributesAsFilter() map[string]dataSourceSchema.Attribute {
return map[string]dataSourceSchema.Attribute{
"prefix": dataSourceSchema.StringAttribute{
MarkdownDescription: "IPv4 or IPv6 network address specified in the form of network/prefixlen.",
Optional: true,
Validators: []validator.String{apstravalidator.ParseCidr(false, false)},
},
"ge_mask": dataSourceSchema.Int64Attribute{
MarkdownDescription: "Match less-specific prefixes from a parent prefix, up from `ge_mask` to the prefix " +
"length of the route. Range is 0-32 for IPv4, 0-128 for IPv6. If not specified, implies the " +
"prefix-list entry should be an exact match. The option can be optionally be used in combination " +
"with `le_mask`. `ge_mask` must be longer than the subnet prefix length. If `le_mask` and `ge_mask` " +
"are both specified, then `le_mask` must be greater than `ge_mask`.",
Optional: true,
},
"le_mask": dataSourceSchema.Int64Attribute{
MarkdownDescription: "Match more-specific prefixes from a parent prefix, up until `le_mask` prefix len. " +
"Range is 0-32 for IPv4, 0-128 for IPv6. If not specified, implies the prefix-list entry should be " +
"an exact match. The option can be optionally be used in combination with `ge_mask`. `le_mask` must " +
"be longer than the subnet prefix length. If `le_mask` and `ge_mask` are both specified, then " +
"`le_mask` must be greater than `ge_mask`.",
Optional: true,
},
"action": dataSourceSchema.StringAttribute{
MarkdownDescription: fmt.Sprintf("If the action is %q, match the route. If the action is %q, do "+
"not match the route. For composing complex policies, all prefix-list items will be processed in the "+
"order specified, top-down. This allows the user to deny a subset of a route that may otherwise be "+
"permitted.", apstra.PrefixFilterActionPermit, apstra.PrefixFilterActionDeny),
Optional: true,
},
}
}
func (o prefixFilter) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"prefix": types.StringType,
"ge_mask": types.Int64Type,
"le_mask": types.Int64Type,
"action": types.StringType,
}
}
func (o *prefixFilter) loadApiData(ctx context.Context, in *apstra.PrefixFilter, diags *diag.Diagnostics) {
o.Prefix = types.StringValue(in.Prefix.String())
o.GeMask = utils.Int64ValueOrNull(ctx, in.GeMask, diags)
o.LeMask = utils.Int64ValueOrNull(ctx, in.LeMask, diags)
o.Action = types.StringValue(in.Action.String())
}
func (o *prefixFilter) request(_ context.Context, diags *diag.Diagnostics) *apstra.PrefixFilter {
var action apstra.PrefixFilterAction
err := action.FromString(o.Action.ValueString())
if err != nil {
diags.AddError(fmt.Sprintf("error parsing prefix filter action string %q", o.Action.ValueString()), err.Error())
return nil
}
_, prefix, err := net.ParseCIDR(o.Prefix.ValueString())
if err != nil {
diags.AddError(fmt.Sprintf("error parsing prefix %q", o.Prefix.ValueString()), err.Error())
return nil
}
var geMask, leMask *int
if !o.GeMask.IsNull() && !o.GeMask.IsUnknown() {
m := int(o.GeMask.ValueInt64())
geMask = &m
}
if !o.LeMask.IsNull() && !o.LeMask.IsUnknown() {
m := int(o.LeMask.ValueInt64())
leMask = &m
}
return &apstra.PrefixFilter{
Action: action,
Prefix: *prefix,
GeMask: geMask,
LeMask: leMask,
}
}
func (o *prefixFilter) filterMatch(_ context.Context, candidate *prefixFilter, diags *diag.Diagnostics) bool {
if !o.Prefix.IsNull() {
_, filterPrefix, _ := net.ParseCIDR(o.Prefix.ValueString()) // ignore errors already caught in input validation
candidateIp, candidatePrefix, err := net.ParseCIDR(candidate.Prefix.ValueString())
if err != nil {
diags.AddError(fmt.Sprintf("error parsing filter prefix %s", candidate.Prefix.String()), err.Error())
return false
}
if !candidateIp.Equal(candidatePrefix.IP) {
diags.AddError("unexpected API response", fmt.Sprintf(
"API response for routing policy prefix misaligned with CIDR boundary - %q", candidatePrefix.String()))
return false
}
if filterPrefix.String() != candidatePrefix.String() {
return false
}
}
if !o.GeMask.IsNull() && !o.GeMask.Equal(candidate.GeMask) {
return false
}
if !o.LeMask.IsNull() && !o.LeMask.Equal(candidate.LeMask) {
return false
}
if !o.Action.IsNull() && !o.Action.Equal(candidate.Action) {
return false
}
return true
}