-
Notifications
You must be signed in to change notification settings - Fork 645
/
resource_ibm_is_vpc_routing_table_route.go
371 lines (336 loc) · 11.5 KB
/
resource_ibm_is_vpc_routing_table_route.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
363
364
365
366
367
368
369
370
371
// Copyright IBM Corp. 2017, 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"net"
"strings"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
rID = "route_id"
rDestination = "destination"
rAction = "action"
rNextHop = "next_hop"
rName = "name"
rZone = "zone"
)
func ResourceIBMISVPCRoutingTableRoute() *schema.Resource {
return &schema.Resource{
Create: resourceIBMISVPCRoutingTableRouteCreate,
Read: resourceIBMISVPCRoutingTableRouteRead,
Update: resourceIBMISVPCRoutingTableRouteUpdate,
Delete: resourceIBMISVPCRoutingTableRouteDelete,
Exists: resourceIBMISVPCRoutingTableRouteExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
rtID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The routing table identifier.",
},
rtVpcID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPC identifier.",
},
rDestination: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The destination of the route.",
},
rZone: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The zone to apply the route to. Traffic from subnets in this zone will be subject to this route.",
},
rNextHop: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "If action is deliver, the next hop that packets will be delivered to. For other action values, its address will be 0.0.0.0.",
},
rAction: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "deliver",
Description: "The action to perform with a packet matching the route.",
ValidateFunc: validate.InvokeValidator("ibm_is_vpc_routing_table_route", rAction),
},
rName: {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
Description: "The user-defined name for this route.",
ValidateFunc: validate.InvokeValidator("ibm_is_vpc_routing_table_route", rName),
},
rID: {
Type: schema.TypeString,
Computed: true,
Description: "The routing table route identifier.",
},
rtHref: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table route Href",
},
rtCreateAt: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table route Created At",
},
"creator": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, the resource that created the route. Routes with this property present cannot bedirectly deleted. All routes with an `origin` of `learned` or `service` will have thisproperty set, and future `origin` values may also have this property set.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The VPN gateway's CRN.",
},
"deleted": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The VPN gateway's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The unique identifier for this VPN gateway.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The user-defined name for this VPN gateway.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The resource type.",
},
},
},
},
rtLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table route Lifecycle State",
},
rtOrigin: {
Type: schema.TypeString,
Computed: true,
Description: "The origin of this route.",
},
},
}
}
func ResourceIBMISVPCRoutingTableRouteValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
actionAllowedValues := "delegate, delegate_vpc, deliver, drop"
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: rtName,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: false,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: rAction,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: false,
AllowedValues: actionAllowedValues})
ibmVPCRoutingTableRouteValidator := validate.ResourceValidator{ResourceName: "ibm_is_vpc_routing_table_route", Schema: validateSchema}
return &ibmVPCRoutingTableRouteValidator
}
func resourceIBMISVPCRoutingTableRouteCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
vpcID := d.Get(rtVpcID).(string)
tableID := d.Get(rtID).(string)
destination := d.Get(rDestination).(string)
zone := d.Get(rZone).(string)
z := &vpcv1.ZoneIdentityByName{
Name: core.StringPtr(zone),
}
createVpcRoutingTableRouteOptions := sess.NewCreateVPCRoutingTableRouteOptions(vpcID, tableID, destination, z)
createVpcRoutingTableRouteOptions.SetZone(z)
createVpcRoutingTableRouteOptions.SetDestination(destination)
if add, ok := d.GetOk(rNextHop); ok {
item := add.(string)
if net.ParseIP(item) == nil {
nhConnectionID := &vpcv1.RoutePrototypeNextHopRouteNextHopPrototypeVPNGatewayConnectionIdentity{
ID: core.StringPtr(item),
}
createVpcRoutingTableRouteOptions.SetNextHop(nhConnectionID)
} else {
nh := &vpcv1.RoutePrototypeNextHopRouteNextHopPrototypeRouteNextHopIP{
Address: core.StringPtr(item),
}
createVpcRoutingTableRouteOptions.SetNextHop(nh)
}
}
if action, ok := d.GetOk(rAction); ok {
routeAction := action.(string)
createVpcRoutingTableRouteOptions.SetAction(routeAction)
}
if name, ok := d.GetOk(rName); ok {
routeName := name.(string)
createVpcRoutingTableRouteOptions.SetName(routeName)
}
route, response, err := sess.CreateVPCRoutingTableRoute(createVpcRoutingTableRouteOptions)
if err != nil {
log.Printf("[DEBUG] Create VPC Routing table route err %s\n%s", err, response)
return err
}
d.SetId(fmt.Sprintf("%s/%s/%s", vpcID, tableID, *route.ID))
d.Set(rID, *route.ID)
return resourceIBMISVPCRoutingTableRouteRead(d, meta)
}
func resourceIBMISVPCRoutingTableRouteRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
idSet := strings.Split(d.Id(), "/")
getVpcRoutingTableRouteOptions := sess.NewGetVPCRoutingTableRouteOptions(idSet[0], idSet[1], idSet[2])
route, response, err := sess.GetVPCRoutingTableRoute(getVpcRoutingTableRouteOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error Getting VPC Routing table route: %s\n%s", err, response)
}
d.Set(rID, *route.ID)
d.Set(rName, *route.Name)
d.Set(rDestination, *route.Destination)
if route.NextHop != nil {
nexthop := route.NextHop.(*vpcv1.RouteNextHop)
if nexthop.Address != nil {
d.Set(rNextHop, *nexthop.Address)
}
if nexthop.ID != nil {
d.Set(rNextHop, *nexthop.ID)
}
}
if err = d.Set("origin", route.Origin); err != nil {
return fmt.Errorf("[ERROR] Error setting origin %s", err)
}
if route.Zone != nil {
d.Set(rZone, *route.Zone.Name)
}
d.Set(rtHref, route.Href)
d.Set(rtLifecycleState, route.LifecycleState)
d.Set(rtCreateAt, route.CreatedAt.String())
creator := []map[string]interface{}{}
if route.Creator != nil {
mm, err := dataSourceIBMIsRouteCreatorToMap(route.Creator)
if err != nil {
log.Printf("Error reading VPC Routing Table Routes' creator:%s", err)
return err
}
creator = append(creator, mm)
d.Set("creator", creator)
}
return nil
}
func resourceIBMISVPCRoutingTableRouteUpdate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
idSet := strings.Split(d.Id(), "/")
if d.HasChange(rName) {
routePatch := make(map[string]interface{})
updateVpcRoutingTableRouteOptions := sess.NewUpdateVPCRoutingTableRouteOptions(idSet[0], idSet[1], idSet[2], routePatch)
// Construct an instance of the RoutePatch model
routePatchModel := new(vpcv1.RoutePatch)
name := d.Get(rName).(string)
routePatchModel.Name = &name
routePatchModelAsPatch, patchErr := routePatchModel.AsPatch()
if patchErr != nil {
return fmt.Errorf("[ERROR] Error calling asPatch for VPC Routing Table Route Patch: %s", patchErr)
}
updateVpcRoutingTableRouteOptions.RoutePatch = routePatchModelAsPatch
_, response, err := sess.UpdateVPCRoutingTableRoute(updateVpcRoutingTableRouteOptions)
if err != nil {
log.Printf("[DEBUG] Update VPC Routing table route err %s\n%s", err, response)
return err
}
}
return resourceIBMISVPCRoutingTableRouteRead(d, meta)
}
func resourceIBMISVPCRoutingTableRouteDelete(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
idSet := strings.Split(d.Id(), "/")
deleteVpcRoutingTableRouteOptions := sess.NewDeleteVPCRoutingTableRouteOptions(idSet[0], idSet[1], idSet[2])
response, err := sess.DeleteVPCRoutingTableRoute(deleteVpcRoutingTableRouteOptions)
if err != nil && response.StatusCode != 404 {
log.Printf("Error deleting VPC Routing table route : %s", response)
return err
}
d.SetId("")
return nil
}
func resourceIBMISVPCRoutingTableRouteExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess, err := vpcClient(meta)
if err != nil {
return false, err
}
idSet := strings.Split(d.Id(), "/")
if len(idSet) != 3 {
return false, fmt.Errorf("[ERROR] Incorrect ID %s: ID should be a combination of vpcID/routingTableID/routeID", d.Id())
}
getVpcRoutingTableRouteOptions := sess.NewGetVPCRoutingTableRouteOptions(idSet[0], idSet[1], idSet[2])
_, response, err := sess.GetVPCRoutingTableRoute(getVpcRoutingTableRouteOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return false, nil
}
return false, fmt.Errorf("[ERROR] Error Getting VPC Routing table route : %s\n%s", err, response)
}
return true, nil
}