forked from PaloAltoNetworks/terraform-provider-panos
/
ospf_area_virtual_link.go
362 lines (316 loc) · 8.95 KB
/
ospf_area_virtual_link.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package panos
import (
"strings"
"github.com/PaloAltoNetworks/pango"
"github.com/PaloAltoNetworks/pango/netw/routing/protocol/ospf/area/vlink"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// Data source (listing).
func dataSourceOspfAreaVirtualLinks() *schema.Resource {
s := map[string]*schema.Schema{
"template": templateSchema(true),
"template_stack": templateStackSchema(),
"virtual_router": {
Type: schema.TypeString,
Required: true,
Description: "The virtual router name",
},
"ospf_area": {
Type: schema.TypeString,
Required: true,
Description: "The OSPF area name",
},
}
for key, value := range listingSchema() {
s[key] = value
}
return &schema.Resource{
Read: readDataSourceOspfAreaVirtualLinks,
Schema: s,
}
}
func readDataSourceOspfAreaVirtualLinks(d *schema.ResourceData, meta interface{}) error {
var err error
var listing []string
var id string
vr := d.Get("virtual_router").(string)
area := d.Get("ospf_area").(string)
switch con := meta.(type) {
case *pango.Firewall:
id = base64Encode([]string{
vr, area,
})
listing, err = con.Network.OspfAreaVirtualLink.GetList(vr, area)
case *pango.Panorama:
tmpl := d.Get("template").(string)
ts := d.Get("template_stack").(string)
id = base64Encode([]string{
tmpl, ts, vr, area,
})
listing, err = con.Network.OspfAreaVirtualLink.GetList(tmpl, ts, vr, area)
}
if err != nil {
return err
}
d.SetId(id)
saveListing(d, listing)
return nil
}
// Data source.
func dataSourceOspfAreaVirtualLink() *schema.Resource {
return &schema.Resource{
Read: readDataSourceOspfAreaVirtualLink,
Schema: ospfAreaVirtualLinkSchema(false),
}
}
func readDataSourceOspfAreaVirtualLink(d *schema.ResourceData, meta interface{}) error {
var err error
var o vlink.Entry
var id string
vr := d.Get("virtual_router").(string)
area := d.Get("ospf_area").(string)
name := d.Get("name").(string)
switch con := meta.(type) {
case *pango.Firewall:
id = buildFirewallOspfAreaVirtualLinkId(vr, area, name)
o, err = con.Network.OspfAreaVirtualLink.Get(vr, area, name)
case *pango.Panorama:
tmpl := d.Get("template").(string)
ts := d.Get("template_stack").(string)
id = buildPanoramaOspfAreaVirtualLinkId(tmpl, ts, vr, area, name)
o, err = con.Network.OspfAreaVirtualLink.Get(tmpl, ts, vr, area, name)
}
if err != nil {
if isObjectNotFound(err) {
d.SetId("")
return nil
}
return err
}
d.SetId(id)
saveOspfAreaVirtualLink(d, o)
return nil
}
// Resource.
func resourceOspfAreaVirtualLink() *schema.Resource {
return &schema.Resource{
Create: createOspfAreaVirtualLink,
Read: readOspfAreaVirtualLink,
Update: updateOspfAreaVirtualLink,
Delete: deleteOspfAreaVirtualLink,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: ospfAreaVirtualLinkSchema(true),
}
}
func createOspfAreaVirtualLink(d *schema.ResourceData, meta interface{}) error {
var err error
var id string
vr := d.Get("virtual_router").(string)
area := d.Get("ospf_area").(string)
o := loadOspfAreaVirtualLink(d)
switch con := meta.(type) {
case *pango.Firewall:
id = buildFirewallOspfAreaVirtualLinkId(vr, area, o.Name)
err = con.Network.OspfAreaVirtualLink.Set(vr, area, o)
case *pango.Panorama:
tmpl := d.Get("template").(string)
ts := d.Get("template_stack").(string)
id = buildPanoramaOspfAreaVirtualLinkId(tmpl, ts, vr, area, o.Name)
err = con.Network.OspfAreaVirtualLink.Set(tmpl, ts, vr, area, o)
}
if err != nil {
return err
}
d.SetId(id)
return readOspfAreaVirtualLink(d, meta)
}
func readOspfAreaVirtualLink(d *schema.ResourceData, meta interface{}) error {
var err error
var o vlink.Entry
switch con := meta.(type) {
case *pango.Firewall:
vr, area, name := parseFirewallOspfAreaVirtualLinkId(d.Id())
o, err = con.Network.OspfAreaVirtualLink.Get(vr, area, name)
case *pango.Panorama:
tmpl, ts, vr, area, name := parsePanoramaOspfAreaVirtualLinkId(d.Id())
o, err = con.Network.OspfAreaVirtualLink.Get(tmpl, ts, vr, area, name)
}
if err != nil {
if isObjectNotFound(err) {
d.SetId("")
return nil
}
return err
}
saveOspfAreaVirtualLink(d, o)
return nil
}
func updateOspfAreaVirtualLink(d *schema.ResourceData, meta interface{}) error {
o := loadOspfAreaVirtualLink(d)
switch con := meta.(type) {
case *pango.Firewall:
vr, area, name := parseFirewallOspfAreaVirtualLinkId(d.Id())
lo, err := con.Network.OspfAreaVirtualLink.Get(vr, area, name)
if err != nil {
return err
}
lo.Copy(o)
if err = con.Network.OspfAreaVirtualLink.Edit(vr, area, o); err != nil {
return err
}
case *pango.Panorama:
tmpl, ts, vr, area, name := parsePanoramaOspfAreaVirtualLinkId(d.Id())
lo, err := con.Network.OspfAreaVirtualLink.Get(tmpl, ts, vr, area, name)
if err != nil {
return err
}
lo.Copy(o)
if err = con.Network.OspfAreaVirtualLink.Edit(tmpl, ts, vr, area, o); err != nil {
return err
}
}
return readOspfAreaVirtualLink(d, meta)
}
func deleteOspfAreaVirtualLink(d *schema.ResourceData, meta interface{}) error {
var err error
switch con := meta.(type) {
case *pango.Firewall:
vr, area, name := parseFirewallOspfAreaVirtualLinkId(d.Id())
err = con.Network.OspfAreaVirtualLink.Delete(vr, area, name)
case *pango.Panorama:
tmpl, ts, vr, area, name := parsePanoramaOspfAreaVirtualLinkId(d.Id())
err = con.Network.OspfAreaVirtualLink.Delete(tmpl, ts, vr, area, name)
}
if err != nil {
if !isObjectNotFound(err) {
return err
}
}
d.SetId("")
return nil
}
// Schema handling.
func ospfAreaVirtualLinkSchema(isResource bool) map[string]*schema.Schema {
ans := map[string]*schema.Schema{
"template": templateSchema(true),
"template_stack": templateStackSchema(),
"virtual_router": {
Type: schema.TypeString,
Required: true,
Description: "The virtual router",
ForceNew: true,
},
"ospf_area": {
Type: schema.TypeString,
Required: true,
Description: "The OSPF area name",
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name",
ForceNew: true,
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable",
Default: true,
},
"neighbor_id": {
Type: schema.TypeString,
Required: true,
Description: "Neighbor ID",
},
"transit_area_id": {
Type: schema.TypeString,
Required: true,
Description: "Transit area ID",
},
"hello_interval": {
Type: schema.TypeInt,
Optional: true,
Description: "Hello interval in seconds",
Default: 10,
},
"dead_counts": {
Type: schema.TypeInt,
Optional: true,
Description: "Dead counts",
Default: 4,
},
"retransmit_interval": {
Type: schema.TypeInt,
Optional: true,
Description: "Retransmit interval in seconds",
Default: 5,
},
"transit_delay": {
Type: schema.TypeInt,
Optional: true,
Description: "Transit delay in seconds",
Default: 1,
},
"auth_profile": {
Type: schema.TypeString,
Optional: true,
Description: "Auth profile",
},
"bfd_profile": {
Type: schema.TypeString,
Optional: true,
Description: "BFD profile",
},
}
if !isResource {
computed(ans, "", []string{
"template", "template_stack",
"virtual_router", "ospf_area", "name",
})
}
return ans
}
func loadOspfAreaVirtualLink(d *schema.ResourceData) vlink.Entry {
return vlink.Entry{
Name: d.Get("name").(string),
Enable: d.Get("enable").(bool),
NeighborId: d.Get("neighbor_id").(string),
TransitAreaId: d.Get("transit_area_id").(string),
HelloInterval: d.Get("hello_interval").(int),
DeadCounts: d.Get("dead_counts").(int),
RetransmitInterval: d.Get("retransmit_interval").(int),
TransitDelay: d.Get("transit_delay").(int),
AuthProfile: d.Get("auth_profile").(string),
BfdProfile: d.Get("bfd_profile").(string),
}
}
func saveOspfAreaVirtualLink(d *schema.ResourceData, o vlink.Entry) {
d.Set("name", o.Name)
d.Set("enable", o.Enable)
d.Set("neighbor_id", o.NeighborId)
d.Set("transit_area_id", o.TransitAreaId)
d.Set("hello_interval", o.HelloInterval)
d.Set("dead_counts", o.DeadCounts)
d.Set("retransmit_interval", o.RetransmitInterval)
d.Set("transit_delay", o.TransitDelay)
d.Set("auth_profile", o.AuthProfile)
d.Set("bfd_profile", o.BfdProfile)
}
// Id functions.
func parseFirewallOspfAreaVirtualLinkId(v string) (string, string, string) {
t := strings.Split(v, IdSeparator)
return t[0], t[1], t[2]
}
func parsePanoramaOspfAreaVirtualLinkId(v string) (string, string, string, string, string) {
t := strings.Split(v, IdSeparator)
return t[0], t[1], t[2], t[3], t[4]
}
func buildFirewallOspfAreaVirtualLinkId(a, b, c string) string {
return strings.Join([]string{a, b, c}, IdSeparator)
}
func buildPanoramaOspfAreaVirtualLinkId(a, b, c, d, e string) string {
return strings.Join([]string{a, b, c, d, e}, IdSeparator)
}