forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_ibm_is_virtual_endpoint_gateway.go
430 lines (394 loc) · 13.5 KB
/
resource_ibm_is_virtual_endpoint_gateway.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package ibm
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isVirtualEndpointGatewayName = "name"
isVirtualEndpointGatewayResourceType = "resource_type"
isVirtualEndpointGatewayResourceGroupID = "resource_group"
isVirtualEndpointGatewayCreatedAt = "created_at"
isVirtualEndpointGatewayIPs = "ips"
isVirtualEndpointGatewayIPsID = "id"
isVirtualEndpointGatewayIPsAddress = "address"
isVirtualEndpointGatewayIPsName = "name"
isVirtualEndpointGatewayIPsSubnet = "subnet"
isVirtualEndpointGatewayIPsResourceType = "resource_type"
isVirtualEndpointGatewayHealthState = "health_state"
isVirtualEndpointGatewayLifecycleState = "lifecycle_state"
isVirtualEndpointGatewayTarget = "target"
isVirtualEndpointGatewayTargetName = "name"
isVirtualEndpointGatewayTargetCRN = "crn"
isVirtualEndpointGatewayTargetResourceType = "resource_type"
isVirtualEndpointGatewayVpcID = "vpc"
isVirtualEndpointGatewayTags = "tags"
)
func resourceIBMISEndpointGateway() *schema.Resource {
targetNameFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetName)
targetCRNFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetCRN)
return &schema.Resource{
Create: resourceIBMisVirtualEndpointGatewayCreate,
Read: resourceIBMisVirtualEndpointGatewayRead,
Update: resourceIBMisVirtualEndpointGatewayUpdate,
Delete: resourceIBMisVirtualEndpointGatewayDelete,
Exists: resourceIBMisVirtualEndpointGatewayExists,
Importer: &schema.ResourceImporter{},
CustomizeDiff: customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return resourceTagsCustomizeDiff(diff)
},
),
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateISName,
Description: "Endpoint gateway name",
},
isVirtualEndpointGatewayResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway resource type",
},
isVirtualEndpointGatewayResourceGroupID: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The resource group id",
},
isVirtualEndpointGatewayCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway created date and time",
},
isVirtualEndpointGatewayHealthState: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway health state",
},
isVirtualEndpointGatewayLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway lifecycle state",
},
isVirtualEndpointGatewayIPs: {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Endpoint gateway IPs",
DiffSuppressFunc: applyOnce,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayIPsID: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The IPs id",
},
isVirtualEndpointGatewayIPsName: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The IPs name",
},
isVirtualEndpointGatewayIPsSubnet: {
Type: schema.TypeString,
Optional: true,
Description: "The Subnet id",
},
isVirtualEndpointGatewayIPsResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The VPE Resource Type",
},
isVirtualEndpointGatewayIPsAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP Address",
},
},
},
},
isVirtualEndpointGatewayTarget: {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Description: "Endpoint gateway target",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayTargetName: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
AtLeastOneOf: []string{
targetNameFmt,
targetCRNFmt,
},
Description: "The target name",
},
isVirtualEndpointGatewayTargetCRN: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
AtLeastOneOf: []string{
targetNameFmt,
targetCRNFmt,
},
Description: "The target crn",
},
isVirtualEndpointGatewayTargetResourceType: {
Type: schema.TypeString,
Required: true,
Description: "The target resource type",
},
},
},
},
isVirtualEndpointGatewayVpcID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPC id",
},
isVirtualEndpointGatewayTags: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: InvokeValidator("ibm_is_virtual_endpoint_gateway", "tag")},
Set: resourceIBMVPCHash,
Description: "List of tags for VPE",
},
},
}
}
func resourceIBMISEndpointGatewayValidator() *ResourceValidator {
validateSchema := make([]ValidateSchema, 1)
validateSchema = append(validateSchema,
ValidateSchema{
Identifier: "tag",
ValidateFunctionIdentifier: ValidateRegexpLen,
Type: TypeString,
Optional: true,
Regexp: `^[A-Za-z0-9:_ .-]+$`,
MinValueLength: 1,
MaxValueLength: 128})
ibmEndpointGatewayResourceValidator := ResourceValidator{ResourceName: "ibm_is_virtual_endpoint_gateway", Schema: validateSchema}
return &ibmEndpointGatewayResourceValidator
}
func resourceIBMisVirtualEndpointGatewayCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
name := d.Get(isVirtualEndpointGatewayName).(string)
// target opiton
targetOpt := &vpcv1.EndpointGatewayTargetPrototype{}
targetNameFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetName)
targetCRNFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetCRN)
targetResourceTypeFmt := fmt.Sprintf("%s.0.%s", isVirtualEndpointGatewayTarget, isVirtualEndpointGatewayTargetResourceType)
targetOpt.ResourceType = core.StringPtr(d.Get(targetResourceTypeFmt).(string))
if v, ok := d.GetOk(targetNameFmt); ok {
targetOpt.Name = core.StringPtr(v.(string))
}
if v, ok := d.GetOk(targetCRNFmt); ok {
targetOpt.CRN = core.StringPtr(v.(string))
}
// vpc option
vpcID := d.Get(isVirtualEndpointGatewayVpcID).(string)
vpcOpt := &vpcv1.VPCIdentity{
ID: core.StringPtr(vpcID),
}
// update option
opt := sess.NewCreateEndpointGatewayOptions(targetOpt, vpcOpt)
opt.SetName(name)
opt.SetTarget(targetOpt)
opt.SetVPC(vpcOpt)
// IPs option
if ips, ok := d.GetOk(isVirtualEndpointGatewayIPs); ok {
opt.SetIps(expandIPs(ips.([]interface{})))
}
// Resource group option
if resourceGroup, ok := d.GetOk(isVirtualEndpointGatewayResourceGroupID); ok {
resourceGroupID := resourceGroup.(string)
resourceGroupOpt := &vpcv1.ResourceGroupIdentity{
ID: core.StringPtr(resourceGroupID),
}
opt.SetResourceGroup(resourceGroupOpt)
}
result, response, err := sess.CreateEndpointGateway(opt)
if err != nil {
log.Printf("Create Endpoint Gateway failed: %v", response)
return err
}
d.SetId(*result.ID)
v := os.Getenv("IC_ENV_TAGS")
if _, ok := d.GetOk(isVirtualEndpointGatewayTags); ok || v != "" {
oldList, newList := d.GetChange(isVirtualEndpointGatewayTags)
err = UpdateTagsUsingCRN(oldList, newList, meta, *result.CRN)
if err != nil {
log.Printf(
"Error on create of VPE (%s) tags: %s", d.Id(), err)
}
}
return resourceIBMisVirtualEndpointGatewayRead(d, meta)
}
func resourceIBMisVirtualEndpointGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
if d.HasChange(isVirtualEndpointGatewayName) {
name := d.Get(isVirtualEndpointGatewayName).(string)
// create option
endpointGatewayPatchModel := new(vpcv1.EndpointGatewayPatch)
endpointGatewayPatchModel.Name = core.StringPtr(name)
endpointGatewayPatchModelAsPatch, _ := endpointGatewayPatchModel.AsPatch()
opt := sess.NewUpdateEndpointGatewayOptions(d.Id(), endpointGatewayPatchModelAsPatch)
_, response, err := sess.UpdateEndpointGateway(opt)
if err != nil {
log.Printf("Update Endpoint Gateway failed: %v", response)
return err
}
}
if d.HasChange(isVirtualEndpointGatewayTags) {
opt := sess.NewGetEndpointGatewayOptions(d.Id())
result, response, err := sess.GetEndpointGateway(opt)
if err != nil {
return fmt.Errorf("Error getting VPE: %s\n%s", err, response)
}
oldList, newList := d.GetChange(isVirtualEndpointGatewayTags)
err = UpdateTagsUsingCRN(oldList, newList, meta, *result.CRN)
if err != nil {
log.Printf(
"Error on update of VPE (%s) tags: %s", d.Id(), err)
}
}
return resourceIBMisVirtualEndpointGatewayRead(d, meta)
}
func resourceIBMisVirtualEndpointGatewayRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
// read option
opt := sess.NewGetEndpointGatewayOptions(d.Id())
result, response, err := sess.GetEndpointGateway(opt)
if err != nil {
log.Printf("Get Endpoint Gateway failed: %v", response)
return err
}
d.Set(isVirtualEndpointGatewayName, result.Name)
d.Set(isVirtualEndpointGatewayHealthState, result.HealthState)
d.Set(isVirtualEndpointGatewayCreatedAt, result.CreatedAt.String())
d.Set(isVirtualEndpointGatewayLifecycleState, result.LifecycleState)
d.Set(isVirtualEndpointGatewayResourceType, result.ResourceType)
d.Set(isVirtualEndpointGatewayIPs, flattenIPs(result.Ips))
d.Set(isVirtualEndpointGatewayResourceGroupID, result.ResourceGroup.ID)
d.Set(isVirtualEndpointGatewayTarget,
flattenEndpointGatewayTarget(result.Target.(*vpcv1.EndpointGatewayTarget)))
d.Set(isVirtualEndpointGatewayVpcID, result.VPC.ID)
tags, err := GetTagsUsingCRN(meta, *result.CRN)
if err != nil {
log.Printf(
"Error on get of VPE (%s) tags: %s", d.Id(), err)
}
d.Set(isVirtualEndpointGatewayTags, tags)
return nil
}
func resourceIBMisVirtualEndpointGatewayDelete(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
opt := sess.NewDeleteEndpointGatewayOptions(d.Id())
response, err := sess.DeleteEndpointGateway(opt)
if err != nil {
log.Printf("Delete Endpoint Gateway failed: %v", response)
}
return nil
}
func resourceIBMisVirtualEndpointGatewayExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess, err := vpcClient(meta)
if err != nil {
return false, err
}
opt := sess.NewGetEndpointGatewayOptions(d.Id())
_, response, err := sess.GetEndpointGateway(opt)
if err != nil {
if response != nil && response.StatusCode == 404 {
log.Printf("Endpoint Gateway does not exist.")
return false, nil
}
log.Printf("Error : %s", response)
return false, err
}
return true, nil
}
func expandIPs(ipsSet []interface{}) (ipsOptions []vpcv1.EndpointGatewayReservedIPIntf) {
ipsList := ipsSet
for _, item := range ipsList {
ips := item.(map[string]interface{})
// IPs option
ipsID := ips[isVirtualEndpointGatewayIPsID].(string)
ipsName := ips[isVirtualEndpointGatewayIPsName].(string)
// IPs subnet option
ipsSubnetID := ips[isVirtualEndpointGatewayIPsSubnet].(string)
ipsSubnetOpt := &vpcv1.SubnetIdentity{
ID: &ipsSubnetID,
}
ipsOpt := &vpcv1.EndpointGatewayReservedIP{
ID: core.StringPtr(ipsID),
Name: core.StringPtr(ipsName),
Subnet: ipsSubnetOpt,
}
ipsOptions = append(ipsOptions, ipsOpt)
}
return ipsOptions
}
func flattenIPs(ipsList []vpcv1.ReservedIPReference) interface{} {
ipsListOutput := make([]interface{}, 0)
for _, item := range ipsList {
ips := make(map[string]interface{}, 0)
ips[isVirtualEndpointGatewayIPsID] = *item.ID
ips[isVirtualEndpointGatewayIPsName] = *item.Name
ips[isVirtualEndpointGatewayIPsResourceType] = *item.ResourceType
ips[isVirtualEndpointGatewayIPsAddress] = *item.Address
ipsListOutput = append(ipsListOutput, ips)
}
return ipsListOutput
}
func flattenEndpointGatewayTarget(target *vpcv1.EndpointGatewayTarget) interface{} {
targetSlice := []interface{}{}
targetOutput := map[string]string{}
if target == nil {
return targetOutput
}
if target.Name != nil {
targetOutput[isVirtualEndpointGatewayTargetName] = *target.Name
}
if target.CRN != nil {
targetOutput[isVirtualEndpointGatewayTargetCRN] = *target.CRN
}
if target.ResourceType != nil {
targetOutput[isVirtualEndpointGatewayTargetResourceType] = *target.ResourceType
}
targetSlice = append(targetSlice, targetOutput)
return targetSlice
}