-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_security_group.go
323 lines (282 loc) · 8.21 KB
/
data_source_ibm_is_security_group.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"reflect"
"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"
)
const (
isSgName = "name"
isSgRules = "rules"
isSgRuleID = "rule_id"
isSgRuleDirection = "direction"
isSgRuleIPVersion = "ip_version"
isSgRuleRemote = "remote"
isSgRuleType = "type"
isSgRuleCode = "code"
isSgRulePortMax = "port_max"
isSgRulePortMin = "port_min"
isSgRuleProtocol = "protocol"
isSgVPC = "vpc"
isSgTags = "tags"
isSgCRN = "crn"
)
func DataSourceIBMISSecurityGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSecurityGroupRuleRead,
Schema: map[string]*schema.Schema{
isSgName: {
Type: schema.TypeString,
Required: true,
Description: "Security group name",
},
isSecurityGroupVPC: {
Type: schema.TypeString,
Computed: true,
Description: "Security group's resource group id",
ForceNew: true,
},
isSgRules: {
Type: schema.TypeList,
Computed: true,
Description: "Security Rules",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isSgRuleID: {
Type: schema.TypeString,
Computed: true,
Description: "Rule id",
},
isSgRuleDirection: {
Type: schema.TypeString,
Computed: true,
Description: "Direction of traffic to enforce, either inbound or outbound",
},
isSgRuleIPVersion: {
Type: schema.TypeString,
Computed: true,
Description: "IP version: ipv4",
},
isSgRuleRemote: {
Type: schema.TypeString,
Computed: true,
Description: "Security group id: an IP address, a CIDR block, or a single security group identifier",
},
isSgRuleType: {
Type: schema.TypeInt,
Computed: true,
},
isSgRuleCode: {
Type: schema.TypeInt,
Computed: true,
},
isSgRulePortMin: {
Type: schema.TypeInt,
Computed: true,
},
isSgRulePortMax: {
Type: schema.TypeInt,
Computed: true,
},
isSgRuleProtocol: {
Type: schema.TypeString,
Computed: true,
},
},
},
},
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.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
isSgTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of tags",
},
isSgCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
},
}
}
func dataSourceIBMISSecurityGroupRuleRead(d *schema.ResourceData, meta interface{}) error {
sgName := d.Get(isSgName).(string)
err := securityGroupGet(d, meta, sgName)
if err != nil {
return err
}
return nil
}
func securityGroupGet(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
// Support for pagination
start := ""
allrecs := []vpcv1.SecurityGroup{}
for {
listSgOptions := &vpcv1.ListSecurityGroupsOptions{}
if start != "" {
listSgOptions.Start = &start
}
sgs, response, err := sess.ListSecurityGroups(listSgOptions)
if err != nil || sgs == nil {
return fmt.Errorf("[ERROR] Error Getting Security Groups %s\n%s", err, response)
}
if *sgs.TotalCount == int64(0) {
break
}
start = flex.GetNext(sgs.Next)
allrecs = append(allrecs, sgs.SecurityGroups...)
if start == "" {
break
}
}
for _, group := range allrecs {
if *group.Name == name {
d.Set(isSgName, *group.Name)
d.Set(isSgVPC, *group.VPC.ID)
d.Set(isSgCRN, *group.CRN)
tags, err := flex.GetTagsUsingCRN(meta, *group.CRN)
if err != nil {
log.Printf(
"An error occured during reading of security group (%s) tags : %s", *group.ID, err)
}
d.Set(isSgTags, tags)
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[isSgRuleCode] = int(*rule.Code)
}
if rule.Type != nil {
r[isSgRuleType] = int(*rule.Type)
}
r[isSgRuleDirection] = *rule.Direction
r[isSgRuleIPVersion] = *rule.IPVersion
if rule.Protocol != nil {
r[isSgRuleProtocol] = *rule.Protocol
}
r[isSgRuleID] = *rule.ID
remote, ok := rule.Remote.(*vpcv1.SecurityGroupRuleRemote)
if ok {
if remote != nil && reflect.ValueOf(remote).IsNil() == false {
if remote.ID != nil {
r[isSgRuleRemote] = remote.ID
} else if remote.Address != nil {
r[isSgRuleRemote] = remote.Address
} else if remote.CIDRBlock != nil {
r[isSgRuleRemote] = remote.CIDRBlock
}
}
}
rules = append(rules, r)
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll":
{
rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll)
r := make(map[string]interface{})
r[isSgRuleDirection] = *rule.Direction
r[isSgRuleIPVersion] = *rule.IPVersion
if rule.Protocol != nil {
r[isSgRuleProtocol] = *rule.Protocol
}
r[isSgRuleID] = *rule.ID
remote, ok := rule.Remote.(*vpcv1.SecurityGroupRuleRemote)
if ok {
if remote != nil && reflect.ValueOf(remote).IsNil() == false {
if remote.ID != nil {
r[isSgRuleRemote] = remote.ID
} else if remote.Address != nil {
r[isSgRuleRemote] = remote.Address
} else if remote.CIDRBlock != nil {
r[isSgRuleRemote] = remote.CIDRBlock
}
}
}
rules = append(rules, r)
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp":
{
rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp)
r := make(map[string]interface{})
if rule.PortMin != nil {
r[isSgRulePortMin] = int(*rule.PortMin)
}
if rule.PortMax != nil {
r[isSgRulePortMax] = int(*rule.PortMax)
}
r[isSgRuleDirection] = *rule.Direction
r[isSgRuleIPVersion] = *rule.IPVersion
if rule.Protocol != nil {
r[isSgRuleProtocol] = *rule.Protocol
}
remote, ok := rule.Remote.(*vpcv1.SecurityGroupRuleRemote)
if ok {
if remote != nil && reflect.ValueOf(remote).IsNil() == false {
if remote.ID != nil {
r[isSgRuleRemote] = remote.ID
} else if remote.Address != nil {
r[isSgRuleRemote] = remote.Address
} else if remote.CIDRBlock != nil {
r[isSgRuleRemote] = remote.CIDRBlock
}
}
}
rules = append(rules, r)
}
}
}
d.Set(isSgRules, rules)
d.SetId(*group.ID)
if group.ResourceGroup != nil {
if group.ResourceGroup.Name != nil {
d.Set(flex.ResourceGroupName, *group.ResourceGroup.Name)
}
}
controller, err := flex.GetBaseController(meta)
if err != nil {
return err
}
d.Set(flex.ResourceControllerURL, controller+"/vpc/network/securityGroups")
if group.Name != nil {
d.Set(flex.ResourceName, *group.Name)
}
if group.CRN != nil {
d.Set(flex.ResourceCRN, *group.CRN)
}
return nil
}
}
return fmt.Errorf("[ERROR] No Security Group found with name %s", name)
}