-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_security_group_targets.go
125 lines (103 loc) · 3.18 KB
/
data_source_ibm_is_security_group_targets.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
// 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/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMISSecurityGroupTargets() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSecurityGroupTargetsRead,
Schema: map[string]*schema.Schema{
"security_group": {
Type: schema.TypeString,
Required: true,
Description: "Security group id",
},
"targets": {
Type: schema.TypeList,
Description: "List of targets",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target": {
Type: schema.TypeString,
Computed: true,
Description: "security group target identifier",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this target",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Security group target name",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "Resource Type",
},
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources",
},
},
},
},
},
}
}
func dataSourceIBMISSecurityGroupTargetsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
securityGroupID := d.Get("security_group").(string)
// Support for pagination
start := ""
allrecs := []vpcv1.SecurityGroupTargetReferenceIntf{}
for {
listSecurityGroupTargetsOptions := sess.NewListSecurityGroupTargetsOptions(securityGroupID)
if start != "" {
listSecurityGroupTargetsOptions.Start = &start
}
groups, response, err := sess.ListSecurityGroupTargets(listSecurityGroupTargetsOptions)
if err != nil || groups == nil {
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Managers %s\n%s", err, response)
}
if *groups.TotalCount == int64(0) {
break
}
start = flex.GetNext(groups.Next)
allrecs = append(allrecs, groups.Targets...)
if start == "" {
break
}
}
targets := make([]map[string]interface{}, 0)
for _, securityGroupTargetReferenceIntf := range allrecs {
securityGroupTargetReference := securityGroupTargetReferenceIntf.(*vpcv1.SecurityGroupTargetReference)
tr := map[string]interface{}{
"name": *securityGroupTargetReference.Name,
"target": *securityGroupTargetReference.ID,
"crn": securityGroupTargetReference.CRN,
// "resource_type": *securityGroupTargetReference.ResourceType,
}
if securityGroupTargetReference.Deleted != nil {
tr["more_info"] = *securityGroupTargetReference.Deleted.MoreInfo
}
if securityGroupTargetReference != nil && securityGroupTargetReference.ResourceType != nil {
tr["resource_type"] = *securityGroupTargetReference.ResourceType
}
targets = append(targets, tr)
}
d.Set("targets", targets)
d.SetId(securityGroupID)
return nil
}