/
resource_bgp_instance.go
213 lines (196 loc) · 8.72 KB
/
resource_bgp_instance.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
package mikrotik
import (
"context"
"github.com/ddelnano/terraform-provider-mikrotik/client"
"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/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
tftypes "github.com/hashicorp/terraform-plugin-framework/types"
)
type bgpInstance struct {
client *client.Mikrotik
}
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &bgpInstance{}
_ resource.ResourceWithConfigure = &bgpInstance{}
_ resource.ResourceWithImportState = &bgpInstance{}
)
// NewBgpInstanceResource is a helper function to simplify the provider implementation.
func NewBgpInstanceResource() resource.Resource {
return &bgpInstance{}
}
func (r *bgpInstance) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(*client.Mikrotik)
}
// Metadata returns the resource type name.
func (r *bgpInstance) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_bgp_instance"
}
// Schema defines the schema for the resource.
func (s *bgpInstance) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Creates a Mikrotik BGP Instance.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "ID of this resource.",
},
"name": schema.StringAttribute{
Required: true,
Description: "The name of the BGP instance.",
},
"as": schema.Int64Attribute{
Required: true,
Description: "The 32-bit BGP autonomous system number. Must be a value within 0 to 4294967295.",
},
"client_to_client_reflection": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
Description: "In case this instance is a route reflector: whether to redistribute routes learned from one routing reflection client to other clients.",
},
"comment": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Description: "The comment of the BGP instance to be created.",
},
"confederation_peers": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Description: "List of AS numbers internal to the [local] confederation. For example: `10,20,30-50`.",
},
"disabled": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "Whether instance is disabled.",
},
"ignore_as_path_len": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "Whether to ignore AS_PATH attribute in BGP route selection algorithm.",
},
"out_filter": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Description: "Output routing filter chain used by all BGP peers belonging to this instance.",
},
"redistribute_connected": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "If enabled, this BGP instance will redistribute the information about connected routes.",
},
"redistribute_ospf": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "If enabled, this BGP instance will redistribute the information about routes learned by OSPF.",
},
"redistribute_other_bgp": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "If enabled, this BGP instance will redistribute the information about routes learned by other BGP instances.",
},
"redistribute_rip": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "If enabled, this BGP instance will redistribute the information about routes learned by RIP.",
},
"redistribute_static": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "If enabled, the router will redistribute the information about static routes added to its routing database.",
},
"router_id": schema.StringAttribute{
Required: true,
Description: "BGP Router ID (for this instance). If set to 0.0.0.0, BGP will use one of router's IP addresses.",
},
"routing_table": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Description: "Name of routing table this BGP instance operates on. ",
},
"cluster_id": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Description: "In case this instance is a route reflector: cluster ID of the router reflector cluster this instance belongs to.",
},
"confederation": schema.Int64Attribute{
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
Description: "In case of BGP confederations: autonomous system number that identifies the [local] confederation as a whole.",
},
},
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *bgpInstance) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var terraformModel bgpInstanceModel
var mikrotikModel client.BgpInstance
GenericCreateResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}
// Read refreshes the Terraform state with the latest data.
func (r *bgpInstance) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var terraformModel bgpInstanceModel
var mikrotikModel client.BgpInstance
GenericReadResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *bgpInstance) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var terraformModel bgpInstanceModel
var mikrotikModel client.BgpInstance
GenericUpdateResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}
// Delete deletes the resource and removes the Terraform state on success.
func (r *bgpInstance) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var terraformModel bgpInstanceModel
var mikrotikModel client.BgpInstance
GenericDeleteResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}
func (r *bgpInstance) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
type bgpInstanceModel struct {
Id tftypes.String `tfsdk:"id"`
Name tftypes.String `tfsdk:"name"`
As tftypes.Int64 `tfsdk:"as"`
ClientToClientReflection tftypes.Bool `tfsdk:"client_to_client_reflection"`
Comment tftypes.String `tfsdk:"comment"`
ConfederationPeers tftypes.String `tfsdk:"confederation_peers"`
Disabled tftypes.Bool `tfsdk:"disabled"`
IgnoreAsPathLen tftypes.Bool `tfsdk:"ignore_as_path_len"`
OutFilter tftypes.String `tfsdk:"out_filter"`
RedistributeConnected tftypes.Bool `tfsdk:"redistribute_connected"`
RedistributeOspf tftypes.Bool `tfsdk:"redistribute_ospf"`
RedistributeOtherBgp tftypes.Bool `tfsdk:"redistribute_other_bgp"`
RedistributeRip tftypes.Bool `tfsdk:"redistribute_rip"`
RedistributeStatic tftypes.Bool `tfsdk:"redistribute_static"`
RouterID tftypes.String `tfsdk:"router_id"`
RoutingTable tftypes.String `tfsdk:"routing_table"`
ClusterID tftypes.String `tfsdk:"cluster_id"`
Confederation tftypes.Int64 `tfsdk:"confederation"`
}