-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_security_group_rules.go
230 lines (216 loc) · 8.39 KB
/
data_source_ibm_is_security_group_rules.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"reflect"
"time"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMIsSecurityGroupRules() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMIsSecurityGroupRulesRead,
Schema: map[string]*schema.Schema{
"security_group": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The security group identifier.",
},
"rules": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Array of rules.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"direction": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The direction of traffic to enforce, either `inbound` or `outbound`.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this security group rule.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this security group rule.",
},
"ip_version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP version to enforce. The format of `remote.address` or `remote.cidr_block` must match this property, if they are used. Alternatively, if `remote` references a security group, then this rule only applies to IP addresses (network interfaces) in that group matching this IP version.",
},
"protocol": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The protocol to enforce.",
},
"remote": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The IP addresses or security groups from which this rule allows traffic (or to which,for outbound rules). Can be specified as an IP address, a CIDR block, or a securitygroup. A CIDR block of `0.0.0.0/0` allows traffic from any source (or to any source,for outbound rules).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP address.This property may add support for IPv6 addresses in the future. When processing a value in this property, verify that the address is in an expected format. If it is not, log an error. Optionally halt processing and surface the error, or bypass the resource on which the unexpected IP address format was encountered.",
},
"cidr_block": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CIDR block. This property may add support for IPv6 CIDR blocks in the future. When processing a value in this property, verify that the CIDR block is in an expected format. If it is not, log an error. Optionally halt processing and surface the error, or bypass the resource on which the unexpected CIDR block format was encountered.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The security group's CRN.",
},
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The security group's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this security group.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this security group. Names must be unique within the VPC the security group resides in.",
},
},
},
},
"code": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The ICMP traffic code to allow.",
},
"type": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The ICMP traffic type to allow.",
},
"port_max": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive upper bound of TCP/UDP port range.",
},
"port_min": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive lower bound of TCP/UDP port range.",
},
},
},
},
},
}
}
func dataSourceIBMIsSecurityGroupRulesRead(d *schema.ResourceData, meta interface{}) error {
secGrpId := d.Get("security_group").(string)
sess, err := vpcClient(meta)
if err != nil {
return err
}
listSecurityGroupRuleOptions := vpcv1.ListSecurityGroupRulesOptions{
SecurityGroupID: &secGrpId,
}
ruleList, response, err := sess.ListSecurityGroupRules(&listSecurityGroupRuleOptions)
if err != nil {
return fmt.Errorf("Error fetching security group rules %s\n%s", err, response)
}
rulesInfo := make([]map[string]interface{}, 0)
for _, rule := range ruleList.Rules {
l := map[string]interface{}{}
switch reflect.TypeOf(rule).String() {
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll":
{
rulex := rule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll)
l["direction"] = *rulex.Direction
l["href"] = *rulex.Href
l["id"] = *rulex.ID
l["ip_version"] = *rulex.IPVersion
l["protocol"] = *rulex.Protocol
// nested map for remote.
if rulex.Remote != nil {
remoteList := []map[string]interface{}{}
remoteMap := dataSourceSecurityGroupRuleRemoteToMap(rulex.Remote.(*vpcv1.SecurityGroupRuleRemote))
remoteList = append(remoteList, remoteMap)
l["remote"] = remoteList
}
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp":
{
rulex := rule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp)
l["direction"] = *rulex.Direction
l["href"] = *rulex.Href
l["id"] = *rulex.ID
l["ip_version"] = *rulex.IPVersion
l["code"] = *rulex.Code
l["protocol"] = *rulex.Protocol
l["type"] = *rulex.Type
// remote
if rulex.Remote != nil {
remoteList := []map[string]interface{}{}
remoteMap := dataSourceSecurityGroupRuleRemoteToMap(rulex.Remote.(*vpcv1.SecurityGroupRuleRemote))
remoteList = append(remoteList, remoteMap)
l["remote"] = remoteList
}
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp":
{
rulex := rule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp)
l["direction"] = *rulex.Direction
l["href"] = *rulex.Href
l["id"] = *rulex.ID
l["ip_version"] = *rulex.IPVersion
l["protocol"] = *rulex.Protocol
l["port_max"] = *rulex.PortMax
l["port_min"] = *rulex.PortMin
// remote
if rulex.Remote != nil {
remoteList := []map[string]interface{}{}
remoteMap := dataSourceSecurityGroupRuleRemoteToMap(rulex.Remote.(*vpcv1.SecurityGroupRuleRemote))
remoteList = append(remoteList, remoteMap)
l["remote"] = remoteList
}
}
}
rulesInfo = append(rulesInfo, l)
}
d.SetId(dataSourceIBMIsSecurityGroupRulesID(d))
d.Set("rules", rulesInfo)
return nil
}
// dataSourceIBMIsSecurityGroupRulesID returns a reasonable ID for the list.
func dataSourceIBMIsSecurityGroupRulesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceSecurityGroupRuleCollectionRemoteDeletedToMap(deletedItem *vpcv1.SecurityGroupReferenceDeleted) (resultMap map[string]interface{}) {
resultMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
resultMap["more_info"] = *deletedItem.MoreInfo
}
return resultMap
}