-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_vpcs.go
568 lines (502 loc) · 15.9 KB
/
data_source_ibm_is_vpcs.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
//"encoding/json"
"context"
"fmt"
"log"
"reflect"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isVPCs = "vpcs"
isVPCID = "id"
)
func DataSourceIBMISVPCs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISVPCListRead,
Schema: map[string]*schema.Schema{
isVPCs: {
Type: schema.TypeList,
Description: "Collection of VPCs",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVPCDefaultNetworkACL: {
Type: schema.TypeString,
Computed: true,
},
isVPCClassicAccess: {
Type: schema.TypeBool,
Computed: true,
},
isVPCDefaultRoutingTable: {
Type: schema.TypeString,
Computed: true,
Description: "Default routing table associated with VPC",
},
isVPCName: {
Type: schema.TypeString,
Computed: true,
Description: "VPC name",
},
isVPCID: {
Type: schema.TypeString,
Computed: true,
Description: "VPC id",
},
isVPCDefaultNetworkACLName: {
Type: schema.TypeString,
Computed: true,
Description: "Default Network ACL name",
},
isVPCDefaultSecurityGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "Default security group name",
},
isVPCDefaultSecurityGroupCRN: {
Type: schema.TypeString,
Computed: true,
Description: "Default security group CRN",
},
isVPCDefaultNetworkACLCRN: {
Type: schema.TypeString,
Computed: true,
Description: "Default Network ACL CRN",
},
isVPCDefaultRoutingTableName: {
Type: schema.TypeString,
Computed: true,
Description: "Default routing table name",
},
isVPCResourceGroup: {
Type: schema.TypeString,
Computed: true,
},
isVPCStatus: {
Type: schema.TypeString,
Computed: true,
},
isVPCDefaultSecurityGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Security group associated with VPC",
},
isVPCTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
},
isVPCCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
flex.ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},
flex.ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},
flex.ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
flex.ResourceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the resource",
},
flex.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
cseSourceAddresses: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Computed: true,
Description: "Cloud service endpoint IP Address",
},
"zone_name": {
Type: schema.TypeString,
Computed: true,
Description: "Location info of CSE Address",
},
},
},
},
isVPCSecurityGroupList: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVPCSecurityGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "Security group name",
},
isVPCSecurityGroupID: {
Type: schema.TypeString,
Computed: true,
Description: "Security group id",
ForceNew: true,
},
isSecurityGroupRules: {
Type: schema.TypeList,
Computed: true,
Description: "Security Rules",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVPCSecurityGroupRuleID: {
Type: schema.TypeString,
Computed: true,
Description: "Rule ID",
},
isVPCSecurityGroupRuleDirection: {
Type: schema.TypeString,
Computed: true,
Description: "Direction of traffic to enforce, either inbound or outbound",
},
isVPCSecurityGroupRuleIPVersion: {
Type: schema.TypeString,
Computed: true,
Description: "IP version: ipv4 or ipv6",
},
isVPCSecurityGroupRuleRemote: {
Type: schema.TypeString,
Computed: true,
Description: "Security group id: an IP address, a CIDR block, or a single security group identifier",
},
isVPCSecurityGroupRuleType: {
Type: schema.TypeInt,
Computed: true,
},
isVPCSecurityGroupRuleCode: {
Type: schema.TypeInt,
Computed: true,
},
isVPCSecurityGroupRulePortMin: {
Type: schema.TypeInt,
Computed: true,
},
isVPCSecurityGroupRulePortMax: {
Type: schema.TypeInt,
Computed: true,
},
isVPCSecurityGroupRuleProtocol: {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
subnetsList: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "subent name",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "subnet ID",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "subnet status",
},
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "subnet location",
},
totalIPV4AddressCount: {
Type: schema.TypeInt,
Computed: true,
Description: "Total IPv4 address count in the subnet",
},
availableIPV4AddressCount: {
Type: schema.TypeInt,
Computed: true,
Description: "Available IPv4 address count in the subnet",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMISVPCListRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
start := ""
allrecs := []vpcv1.VPC{}
for {
listOptions := &vpcv1.ListVpcsOptions{}
if start != "" {
listOptions.Start = &start
}
result, detail, err := sess.ListVpcsWithContext(context, listOptions)
if err != nil {
log.Printf("Error reading list of VPCs:%s\n%s", err, detail)
return diag.FromErr(err)
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.Vpcs...)
if start == "" {
break
}
}
vpcs := make([]map[string]interface{}, 0)
for _, vpc := range allrecs {
l := map[string]interface{}{
isVPCID: *vpc.ID,
isVPCName: *vpc.Name,
isVPCClassicAccess: *vpc.ClassicAccess,
isVPCStatus: *vpc.Status,
isVPCCRN: *vpc.CRN,
flex.ResourceName: *vpc.Name,
flex.ResourceCRN: *vpc.CRN,
flex.ResourceStatus: *vpc.Status,
isVPCResourceGroup: *vpc.ResourceGroup.ID,
}
if vpc.DefaultNetworkACL != nil {
l[isVPCDefaultNetworkACL] = *vpc.DefaultNetworkACL.ID
l[isVPCDefaultNetworkACLName] = *vpc.DefaultNetworkACL.Name
l[isVPCDefaultNetworkACLCRN] = vpc.DefaultNetworkACL.CRN
}
if vpc.DefaultRoutingTable != nil {
l[isVPCDefaultRoutingTable] = *vpc.DefaultRoutingTable.ID
l[isVPCDefaultRoutingTableName] = *vpc.DefaultRoutingTable.Name
}
if vpc.DefaultSecurityGroup != nil {
l[isVPCDefaultSecurityGroup] = *vpc.DefaultSecurityGroup.ID
l[isVPCDefaultSecurityGroupName] = *vpc.DefaultSecurityGroup.Name
l[isVPCDefaultSecurityGroupCRN] = vpc.DefaultSecurityGroup.CRN
}
tags, err := flex.GetTagsUsingCRN(meta, *vpc.CRN)
if err != nil {
log.Printf(
"An error occured during reading of vpc (%s) tags : %s", d.Id(), err)
}
l[isVPCTags] = tags
controller, err := flex.GetBaseController(meta)
if err != nil {
return diag.FromErr(err)
}
l[flex.ResourceControllerURL] = controller + "/vpc-ext/network/vpcs"
if vpc.ResourceGroup != nil {
l[flex.ResourceGroupName] = *vpc.ResourceGroup.Name
}
//set the cse ip addresses info
if vpc.CseSourceIps != nil {
cseSourceIpsList := make([]map[string]interface{}, 0)
for _, sourceIP := range vpc.CseSourceIps {
currentCseSourceIp := map[string]interface{}{}
if sourceIP.IP != nil {
currentCseSourceIp["address"] = *sourceIP.IP.Address
currentCseSourceIp["zone_name"] = *sourceIP.Zone.Name
cseSourceIpsList = append(cseSourceIpsList, currentCseSourceIp)
}
}
l[cseSourceAddresses] = cseSourceIpsList
}
// adding pagination support for subnets inside vpc
startSub := ""
allrecsSub := []vpcv1.Subnet{}
options := &vpcv1.ListSubnetsOptions{}
for {
if startSub != "" {
options.Start = &startSub
}
s, response, err := sess.ListSubnetsWithContext(context, options)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching subnets %s\n%s", err, response))
}
startSub = flex.GetNext(s.Next)
allrecsSub = append(allrecsSub, s.Subnets...)
if startSub == "" {
break
}
}
if err == nil {
subnetsInfo := make([]map[string]interface{}, 0)
for _, subnet := range allrecsSub {
if *subnet.VPC.ID == *vpc.ID {
l := map[string]interface{}{
"name": *subnet.Name,
"id": *subnet.ID,
"status": *subnet.Status,
"zone": *subnet.Zone.Name,
totalIPV4AddressCount: *subnet.TotalIpv4AddressCount,
availableIPV4AddressCount: *subnet.AvailableIpv4AddressCount,
}
subnetsInfo = append(subnetsInfo, l)
}
}
l[subnetsList] = subnetsInfo
}
// adding pagination support for sg inside vpc
startSg := ""
allrecsSg := []vpcv1.SecurityGroup{}
for {
vpcId := vpc.ID
listSgOptions := &vpcv1.ListSecurityGroupsOptions{
VPCID: vpcId,
}
if startSg != "" {
listSgOptions.Start = &start
}
sgs, response, err := sess.ListSecurityGroupsWithContext(context, listSgOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching Security Groups %s\n%s", err, response))
}
if *sgs.TotalCount == int64(0) {
break
}
start = flex.GetNext(sgs.Next)
allrecsSg = append(allrecsSg, sgs.SecurityGroups...)
if startSg == "" {
break
}
}
securityGroupList := make([]map[string]interface{}, 0)
for _, group := range allrecsSg {
if *group.VPC.ID == *vpc.ID {
g := make(map[string]interface{})
g[isVPCSecurityGroupName] = *group.Name
g[isVPCSecurityGroupID] = *group.ID
rules := make([]map[string]interface{}, 0)
for _, sgrule := range group.Rules {
switch reflect.TypeOf(sgrule).String() {
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp":
{
rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp)
r := make(map[string]interface{})
if rule.Code != nil {
r[isVPCSecurityGroupRuleCode] = int(*rule.Code)
}
if rule.Type != nil {
r[isVPCSecurityGroupRuleType] = int(*rule.Type)
}
r[isVPCSecurityGroupRuleDirection] = *rule.Direction
r[isVPCSecurityGroupRuleIPVersion] = *rule.IPVersion
if rule.Protocol != nil {
r[isVPCSecurityGroupRuleProtocol] = *rule.Protocol
}
r[isVPCSecurityGroupRuleID] = *rule.ID
remote, ok := rule.Remote.(*vpcv1.SecurityGroupRuleRemote)
if ok {
if remote != nil && reflect.ValueOf(remote).IsNil() == false {
if remote.ID != nil {
r[isVPCSecurityGroupRuleRemote] = remote.ID
} else if remote.Address != nil {
r[isVPCSecurityGroupRuleRemote] = remote.Address
} else if remote.CIDRBlock != nil {
r[isVPCSecurityGroupRuleRemote] = remote.CIDRBlock
}
}
}
rules = append(rules, r)
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll":
{
rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll)
r := make(map[string]interface{})
r[isVPCSecurityGroupRuleDirection] = *rule.Direction
r[isVPCSecurityGroupRuleIPVersion] = *rule.IPVersion
if rule.Protocol != nil {
r[isVPCSecurityGroupRuleProtocol] = *rule.Protocol
}
r[isVPCSecurityGroupRuleID] = *rule.ID
remote, ok := rule.Remote.(*vpcv1.SecurityGroupRuleRemote)
if ok {
if remote != nil && reflect.ValueOf(remote).IsNil() == false {
if remote.ID != nil {
r[isVPCSecurityGroupRuleRemote] = remote.ID
} else if remote.Address != nil {
r[isVPCSecurityGroupRuleRemote] = remote.Address
} else if remote.CIDRBlock != nil {
r[isVPCSecurityGroupRuleRemote] = remote.CIDRBlock
}
}
}
rules = append(rules, r)
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp":
{
rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp)
r := make(map[string]interface{})
r[isVPCSecurityGroupRuleDirection] = *rule.Direction
r[isVPCSecurityGroupRuleIPVersion] = *rule.IPVersion
if rule.PortMin != nil {
r[isVPCSecurityGroupRulePortMin] = int(*rule.PortMin)
}
if rule.PortMax != nil {
r[isVPCSecurityGroupRulePortMax] = int(*rule.PortMax)
}
if rule.Protocol != nil {
r[isVPCSecurityGroupRuleProtocol] = *rule.Protocol
}
remote, ok := rule.Remote.(*vpcv1.SecurityGroupRuleRemote)
if ok {
if remote != nil && reflect.ValueOf(remote).IsNil() == false {
if remote.ID != nil {
r[isVPCSecurityGroupRuleRemote] = remote.ID
} else if remote.Address != nil {
r[isVPCSecurityGroupRuleRemote] = remote.Address
} else if remote.CIDRBlock != nil {
r[isVPCSecurityGroupRuleRemote] = remote.CIDRBlock
}
}
}
rules = append(rules, r)
}
}
}
g[isVPCSgRules] = rules
securityGroupList = append(securityGroupList, g)
}
}
l[isVPCSecurityGroupList] = securityGroupList
vpcs = append(vpcs, l)
}
d.SetId(dataSourceIBMISVPCsID(d))
d.Set(isVPCs, vpcs)
return nil
}
// dataSourceIBMISVPCsID returns a reasonable ID for vpc list.
func dataSourceIBMISVPCsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}