-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_is_vpc_address_prefix.go
377 lines (332 loc) · 11.1 KB
/
resource_ibm_is_vpc_address_prefix.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isVPCAddressPrefixPrefixName = "name"
isVPCAddressPrefixZoneName = "zone"
isVPCAddressPrefixCIDR = "cidr"
isVPCAddressPrefixVPCID = "vpc"
isVPCAddressPrefixHasSubnets = "has_subnets"
isVPCAddressPrefixDefault = "is_default"
isAddressPrefix = "address_prefix"
)
func ResourceIBMISVpcAddressPrefix() *schema.Resource {
return &schema.Resource{
Create: resourceIBMISVpcAddressPrefixCreate,
Read: resourceIBMISVpcAddressPrefixRead,
Update: resourceIBMISVpcAddressPrefixUpdate,
Delete: resourceIBMISVpcAddressPrefixDelete,
Exists: resourceIBMISVpcAddressPrefixExists,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
isVPCAddressPrefixPrefixName: {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validate.InvokeValidator("ibm_is_address_prefix", isVPCAddressPrefixPrefixName),
Description: "Name",
},
isVPCAddressPrefixZoneName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Zone name",
},
isVPCAddressPrefixCIDR: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_is_address_prefix", isVPCAddressPrefixCIDR),
Description: "CIDIR address prefix",
},
isVPCAddressPrefixDefault: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Is default prefix for this zone in this VPC",
},
isVPCAddressPrefixVPCID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "VPC id",
},
isVPCAddressPrefixHasSubnets: {
Type: schema.TypeBool,
Computed: true,
Description: "Boolean value, set to true if VPC instance have subnets",
},
flex.RelatedCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the VPC resource",
},
isAddressPrefix: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the address prefix",
},
},
}
}
func ResourceIBMISAddressPrefixValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isVPCAddressPrefixPrefixName,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isVPCRouteDestinationCIDR,
ValidateFunctionIdentifier: validate.ValidateCIDRAddress,
Type: validate.TypeString,
ForceNew: true,
Required: true})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isVPCAddressPrefixCIDR,
ValidateFunctionIdentifier: validate.ValidateOverlappingAddress,
Type: validate.TypeString,
ForceNew: true,
Required: true})
ibmISAddressPrefixResourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_address_prefix", Schema: validateSchema}
return &ibmISAddressPrefixResourceValidator
}
func resourceIBMISVpcAddressPrefixCreate(d *schema.ResourceData, meta interface{}) error {
isDefault := false
prefixName := d.Get(isVPCAddressPrefixPrefixName).(string)
zoneName := d.Get(isVPCAddressPrefixZoneName).(string)
cidr := d.Get(isVPCAddressPrefixCIDR).(string)
vpcID := d.Get(isVPCAddressPrefixVPCID).(string)
if isDefaultPrefix, ok := d.GetOk(isVPCAddressPrefixDefault); ok {
isDefault = isDefaultPrefix.(bool)
}
isVPCAddressPrefixKey := "vpc_address_prefix_key_" + vpcID
conns.IbmMutexKV.Lock(isVPCAddressPrefixKey)
defer conns.IbmMutexKV.Unlock(isVPCAddressPrefixKey)
err := vpcAddressPrefixCreate(d, meta, prefixName, zoneName, cidr, vpcID, isDefault)
if err != nil {
return err
}
return resourceIBMISVpcAddressPrefixRead(d, meta)
}
func vpcAddressPrefixCreate(d *schema.ResourceData, meta interface{}, name, zone, cidr, vpcID string, isDefault bool) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
options := &vpcv1.CreateVPCAddressPrefixOptions{
Name: &name,
VPCID: &vpcID,
CIDR: &cidr,
IsDefault: &isDefault,
Zone: &vpcv1.ZoneIdentity{
Name: &zone,
},
}
addrPrefix, response, err := sess.CreateVPCAddressPrefix(options)
if err != nil {
return fmt.Errorf("[ERROR] Error while creating VPC Address Prefix %s\n%s", err, response)
}
addrPrefixID := *addrPrefix.ID
d.SetId(fmt.Sprintf("%s/%s", vpcID, addrPrefixID))
d.Set(isAddressPrefix, addrPrefixID)
return nil
}
func resourceIBMISVpcAddressPrefixRead(d *schema.ResourceData, meta interface{}) error {
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
vpcID := parts[0]
addrPrefixID := parts[1]
error := vpcAddressPrefixGet(d, meta, vpcID, addrPrefixID)
if error != nil {
return error
}
return nil
}
func vpcAddressPrefixGet(d *schema.ResourceData, meta interface{}, vpcID, addrPrefixID string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getvpcAddressPrefixOptions := &vpcv1.GetVPCAddressPrefixOptions{
VPCID: &vpcID,
ID: &addrPrefixID,
}
addrPrefix, response, err := sess.GetVPCAddressPrefix(getvpcAddressPrefixOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error Getting VPC Address Prefix (%s): %s\n%s", addrPrefixID, err, response)
}
d.Set(isVPCAddressPrefixVPCID, vpcID)
d.Set(isVPCAddressPrefixDefault, *addrPrefix.IsDefault)
d.Set(isVPCAddressPrefixPrefixName, *addrPrefix.Name)
if addrPrefix.Zone != nil {
d.Set(isVPCAddressPrefixZoneName, *addrPrefix.Zone.Name)
}
d.Set(isVPCAddressPrefixCIDR, *addrPrefix.CIDR)
d.Set(isVPCAddressPrefixHasSubnets, *addrPrefix.HasSubnets)
d.Set(isAddressPrefix, addrPrefixID)
getVPCOptions := &vpcv1.GetVPCOptions{
ID: &vpcID,
}
vpc, response, err := sess.GetVPC(getVPCOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting VPC : %s\n%s", err, response)
}
d.Set(flex.RelatedCRN, *vpc.CRN)
return nil
}
func resourceIBMISVpcAddressPrefixUpdate(d *schema.ResourceData, meta interface{}) error {
name := ""
isDefault := false
hasNameChanged := false
hasIsDefaultChanged := false
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
vpcID := parts[0]
addrPrefixID := parts[1]
isVPCAddressPrefixKey := "vpc_address_prefix_key_" + vpcID
conns.IbmMutexKV.Lock(isVPCAddressPrefixKey)
defer conns.IbmMutexKV.Unlock(isVPCAddressPrefixKey)
if d.HasChange(isVPCAddressPrefixPrefixName) {
name = d.Get(isVPCAddressPrefixPrefixName).(string)
hasNameChanged = true
}
if d.HasChange(isVPCAddressPrefixDefault) {
isDefault = d.Get(isVPCAddressPrefixDefault).(bool)
hasIsDefaultChanged = true
}
error := vpcAddressPrefixUpdate(d, meta, vpcID, addrPrefixID, name, isDefault, hasNameChanged, hasIsDefaultChanged)
if error != nil {
return error
}
return resourceIBMISVpcAddressPrefixRead(d, meta)
}
func vpcAddressPrefixUpdate(d *schema.ResourceData, meta interface{}, vpcID, addrPrefixID, name string, isDefault, hasNameChanged, hasIsDefaultChanged bool) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
if hasNameChanged || hasIsDefaultChanged {
updatevpcAddressPrefixoptions := &vpcv1.UpdateVPCAddressPrefixOptions{
VPCID: &vpcID,
ID: &addrPrefixID,
}
addressPrefixPatchModel := &vpcv1.AddressPrefixPatch{}
if hasNameChanged {
addressPrefixPatchModel.Name = &name
}
if hasIsDefaultChanged {
addressPrefixPatchModel.IsDefault = &isDefault
}
addressPrefixPatch, err := addressPrefixPatchModel.AsPatch()
if err != nil {
return fmt.Errorf("[ERROR] Error calling asPatch for AddressPrefixPatch: %s", err)
}
updatevpcAddressPrefixoptions.AddressPrefixPatch = addressPrefixPatch
_, response, err := sess.UpdateVPCAddressPrefix(updatevpcAddressPrefixoptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating VPC Address Prefix: %s\n%s", err, response)
}
}
return nil
}
func resourceIBMISVpcAddressPrefixDelete(d *schema.ResourceData, meta interface{}) error {
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
vpcID := parts[0]
addrPrefixID := parts[1]
isVPCAddressPrefixKey := "vpc_address_prefix_key_" + vpcID
conns.IbmMutexKV.Lock(isVPCAddressPrefixKey)
defer conns.IbmMutexKV.Unlock(isVPCAddressPrefixKey)
error := vpcAddressPrefixDelete(d, meta, vpcID, addrPrefixID)
if error != nil {
return error
}
d.SetId("")
return nil
}
func vpcAddressPrefixDelete(d *schema.ResourceData, meta interface{}, vpcID, addrPrefixID string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getvpcAddressPrefixOptions := &vpcv1.GetVPCAddressPrefixOptions{
VPCID: &vpcID,
ID: &addrPrefixID,
}
_, response, err := sess.GetVPCAddressPrefix(getvpcAddressPrefixOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return nil
}
return fmt.Errorf("[ERROR] Error Getting VPC Address Prefix (%s): %s\n%s", addrPrefixID, err, response)
}
deletevpcAddressPrefixOptions := &vpcv1.DeleteVPCAddressPrefixOptions{
VPCID: &vpcID,
ID: &addrPrefixID,
}
response, err = sess.DeleteVPCAddressPrefix(deletevpcAddressPrefixOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return nil
}
return fmt.Errorf("[ERROR] Error Deleting VPC Address Prefix (%s): %s\n%s", addrPrefixID, err, response)
}
d.SetId("")
return nil
}
func resourceIBMISVpcAddressPrefixExists(d *schema.ResourceData, meta interface{}) (bool, error) {
parts, err := flex.IdParts(d.Id())
if len(parts) != 2 {
return false, fmt.Errorf("[ERROR] Incorrect ID %s: ID should be a combination of vpcID/addrPrefixID", d.Id())
}
if err != nil {
return false, err
}
vpcID := parts[0]
addrPrefixID := parts[1]
exists, err := vpcAddressPrefixExists(d, meta, vpcID, addrPrefixID)
return exists, err
}
func vpcAddressPrefixExists(d *schema.ResourceData, meta interface{}, vpcID, addrPrefixID string) (bool, error) {
sess, err := vpcClient(meta)
if err != nil {
return false, err
}
getvpcAddressPrefixOptions := &vpcv1.GetVPCAddressPrefixOptions{
VPCID: &vpcID,
ID: &addrPrefixID,
}
_, response, err := sess.GetVPCAddressPrefix(getvpcAddressPrefixOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return false, nil
}
return false, fmt.Errorf("[ERROR] Error getting VPC Address Prefix: %s\n%s", err, response)
}
return true, nil
}