-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_is_subnet_reserved_ip.go
408 lines (372 loc) · 12.1 KB
/
resource_ibm_is_subnet_reserved_ip.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"reflect"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"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/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isReservedIPProvisioning = "provisioning"
isReservedIPProvisioningDone = "done"
isReservedIP = "reserved_ip"
isReservedIPTarget = "target"
isReservedIPTargetCrn = "target_crn"
isReservedIPLifecycleState = "lifecycle_state"
)
func ResourceIBMISReservedIP() *schema.Resource {
return &schema.Resource{
Create: resourceIBMISReservedIPCreate,
Read: resourceIBMISReservedIPRead,
Update: resourceIBMISReservedIPUpdate,
Delete: resourceIBMISReservedIPDelete,
Exists: resourceIBMISReservedIPExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
/*
Request Parameters
==================
These are mandatory req parameters
*/
isSubNetID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The subnet identifier.",
},
isReservedIPAutoDelete: {
Type: schema.TypeBool,
Default: nil,
Computed: true,
Optional: true,
Description: "If set to true, this reserved IP will be automatically deleted",
},
isReservedIPName: {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_is_subnet_reserved_ip", isReservedIPName),
Description: "The user-defined or system-provided name for this reserved IP.",
},
isReservedIPTarget: {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The unique identifier for target.",
},
isReservedIPTargetCrn: {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The crn for target.",
},
isReservedIPLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the reserved IP",
},
/*
Response Parameters
===================
All of these are computed and an user doesn't need to provide
these from outside.
*/
isReservedIPAddress: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The address for this reserved IP.",
},
isReservedIP: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the reserved IP.",
},
isReservedIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the reserved IP was created.",
},
isReservedIPhref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP.",
},
isReservedIPOwner: {
Type: schema.TypeString,
Computed: true,
Description: "The owner of a reserved IP, defining whether it is managed by the user or the provider.",
},
isReservedIPType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
}
}
func ResourceIBMISSubnetReservedIPValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isReservedIPName,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63})
ibmISSubnetReservedIPCResourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_subnet_reserved_ip", Schema: validateSchema}
return &ibmISSubnetReservedIPCResourceValidator
}
// resourceIBMISReservedIPCreate Creates a reserved IP given a subnet ID
func resourceIBMISReservedIPCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
subnetID := d.Get(isSubNetID).(string)
options := sess.NewCreateSubnetReservedIPOptions(subnetID)
nameStr := ""
if name, ok := d.GetOk(isReservedIPName); ok {
nameStr = name.(string)
}
if nameStr != "" {
options.Name = &nameStr
}
addStr := ""
if address, ok := d.GetOk(isReservedIPAddress); ok {
addStr = address.(string)
}
if addStr != "" {
options.Address = &addStr
}
autoDeleteBool := d.Get(isReservedIPAutoDelete).(bool)
options.AutoDelete = &autoDeleteBool
if t, ok := d.GetOk(isReservedIPTarget); ok {
targetId := t.(string)
options.Target = &vpcv1.ReservedIPTargetPrototype{
ID: &targetId,
}
}
rip, response, err := sess.CreateSubnetReservedIP(options)
if err != nil || response == nil || rip == nil {
return fmt.Errorf("[ERROR] Error creating the reserved IP: %s\n%s", err, response)
}
// Set id for the reserved IP as combination of subnet ID and reserved IP ID
d.SetId(fmt.Sprintf("%s/%s", subnetID, *rip.ID))
_, err = isWaitForReservedIpAvailable(sess, subnetID, *rip.ID, d.Timeout(schema.TimeoutCreate), d)
if err != nil {
return fmt.Errorf("[ERROR] Error waiting for the reserved IP to be available: %s", err)
}
return resourceIBMISReservedIPRead(d, meta)
}
func resourceIBMISReservedIPRead(d *schema.ResourceData, meta interface{}) error {
rip, err := get(d, meta)
if err != nil {
return err
}
allIDs, err := flex.IdParts(d.Id())
if err != nil {
return fmt.Errorf("[ERROR] The ID can not be split into subnet ID and reserved IP ID. %s", err)
}
subnetID := allIDs[0]
if rip != nil {
d.Set(isReservedIPAddress, *rip.Address)
d.Set(isReservedIP, *rip.ID)
d.Set(isSubNetID, subnetID)
if rip.LifecycleState != nil {
d.Set(isReservedIPLifecycleState, *rip.LifecycleState)
}
d.Set(isReservedIPAutoDelete, *rip.AutoDelete)
d.Set(isReservedIPCreatedAt, (*rip.CreatedAt).String())
d.Set(isReservedIPhref, *rip.Href)
d.Set(isReservedIPName, *rip.Name)
d.Set(isReservedIPOwner, *rip.Owner)
d.Set(isReservedIPType, *rip.ResourceType)
if rip.Target != nil {
targetIntf := rip.Target
switch reflect.TypeOf(targetIntf).String() {
case "*vpcv1.ReservedIPTargetEndpointGatewayReference":
{
target := targetIntf.(*vpcv1.ReservedIPTargetEndpointGatewayReference)
d.Set(isReservedIPTarget, target.ID)
d.Set(isReservedIPTargetCrn, target.CRN)
}
case "*vpcv1.ReservedIPTargetGenericResourceReference":
{
target := targetIntf.(*vpcv1.ReservedIPTargetGenericResourceReference)
d.Set(isReservedIPTargetCrn, target.CRN)
}
case "*vpcv1.ReservedIPTargetNetworkInterfaceReferenceTargetContext":
{
target := targetIntf.(*vpcv1.ReservedIPTargetNetworkInterfaceReferenceTargetContext)
d.Set(isReservedIPTarget, target.ID)
}
case "*vpcv1.ReservedIPTargetLoadBalancerReference":
{
target := targetIntf.(*vpcv1.ReservedIPTargetLoadBalancerReference)
d.Set(isReservedIPTarget, target.ID)
d.Set(isReservedIPTargetCrn, target.CRN)
}
case "*vpcv1.ReservedIPTargetVPNGatewayReference":
{
target := targetIntf.(*vpcv1.ReservedIPTargetVPNGatewayReference)
d.Set(isReservedIPTarget, target.ID)
d.Set(isReservedIPTargetCrn, target.CRN)
}
case "*vpcv1.ReservedIPTarget":
{
target := targetIntf.(*vpcv1.ReservedIPTarget)
d.Set(isReservedIPTarget, target.ID)
d.Set(isReservedIPTargetCrn, target.CRN)
}
}
}
}
return nil
}
func resourceIBMISReservedIPUpdate(d *schema.ResourceData, meta interface{}) error {
// For updating the name
nameChanged := d.HasChange(isReservedIPName)
autoDeleteChanged := d.HasChange(isReservedIPAutoDelete)
if nameChanged || autoDeleteChanged {
sess, err := vpcClient(meta)
if err != nil {
return err
}
allIDs, err := flex.IdParts(d.Id())
if err != nil {
return err
}
subnetID := allIDs[0]
reservedIPID := allIDs[1]
options := &vpcv1.UpdateSubnetReservedIPOptions{
SubnetID: &subnetID,
ID: &reservedIPID,
}
patch := new(vpcv1.ReservedIPPatch)
if nameChanged {
name := d.Get(isReservedIPName).(string)
patch.Name = core.StringPtr(name)
}
if autoDeleteChanged {
autoDelete := d.Get(isReservedIPAutoDelete).(bool)
patch.AutoDelete = core.BoolPtr(autoDelete)
}
reservedIPPatch, err := patch.AsPatch()
if err != nil {
return fmt.Errorf("[ERROR] Error updating the reserved IP %s", err)
}
options.ReservedIPPatch = reservedIPPatch
_, response, err := sess.UpdateSubnetReservedIP(options)
if err != nil {
return fmt.Errorf("[ERROR] Error updating the reserved IP %s\n%s", err, response)
}
}
return resourceIBMISReservedIPRead(d, meta)
}
func resourceIBMISReservedIPDelete(d *schema.ResourceData, meta interface{}) error {
rip, err := get(d, meta)
if err != nil {
return err
}
if err == nil && rip == nil {
// If there is no such reserved IP, it can not be deleted
return nil
}
sess, err := vpcClient(meta)
if err != nil {
return err
}
allIDs, err := flex.IdParts(d.Id())
if err != nil {
return err
}
subnetID := allIDs[0]
reservedIPID := allIDs[1]
deleteOptions := sess.NewDeleteSubnetReservedIPOptions(subnetID, reservedIPID)
response, err := sess.DeleteSubnetReservedIP(deleteOptions)
if err != nil || response == nil {
return fmt.Errorf("[ERROR] Error deleting the reserverd ip %s in subnet %s, %s\n%s", reservedIPID, subnetID, err, response)
}
d.SetId("")
return nil
}
func resourceIBMISReservedIPExists(d *schema.ResourceData, meta interface{}) (bool, error) {
rip, err := get(d, meta)
if err != nil {
return false, err
}
if err == nil && rip == nil {
return false, nil
}
return true, nil
}
// get is a generic function that gets the reserved ip given subnet id and reserved ip
func get(d *schema.ResourceData, meta interface{}) (*vpcv1.ReservedIP, error) {
sess, err := vpcClient(meta)
if err != nil {
return nil, err
}
allIDs, err := flex.IdParts(d.Id())
subnetID := allIDs[0]
reservedIPID := allIDs[1]
options := sess.NewGetSubnetReservedIPOptions(subnetID, reservedIPID)
rip, response, err := sess.GetSubnetReservedIP(options)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil, nil
}
return nil, fmt.Errorf("[ERROR] Error Getting Reserved IP : %s\n%s", err, response)
}
return rip, nil
}
func isWaitForReservedIpAvailable(sess *vpcv1.VpcV1, subnetid, id string, timeout time.Duration, d *schema.ResourceData) (interface{}, error) {
log.Printf("Waiting for reseved ip (%s/%s) to be available.", subnetid, id)
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"done", "failed", ""},
Refresh: isReserveIpRefreshFunc(sess, subnetid, id, d),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForState()
}
func isReserveIpRefreshFunc(sess *vpcv1.VpcV1, subnetid, id string, d *schema.ResourceData) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
getreservedipOptions := &vpcv1.GetSubnetReservedIPOptions{
ID: &id,
SubnetID: &subnetid,
}
rsip, response, err := sess.GetSubnetReservedIP(getreservedipOptions)
if err != nil {
return nil, "", fmt.Errorf("[ERROR] Error Getting reserved ip(%s/%s) : %s\n%s", subnetid, id, err, response)
}
if rsip.LifecycleState != nil {
d.Set(isReservedIPLifecycleState, *rsip.LifecycleState)
}
d.Set(isReservedIPAddress, *rsip.Address)
if rsip.LifecycleState != nil && *rsip.LifecycleState == "failed" {
return rsip, "failed", fmt.Errorf("[ERROR] Error Reserved ip(%s/%s) creation failed : %s\n%s", subnetid, id, err, response)
}
if rsip.LifecycleState != nil && *rsip.LifecycleState == "stable" {
return rsip, "done", nil
}
return rsip, "pending", nil
}
}