-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_security_group_rule.go
300 lines (275 loc) · 11.4 KB
/
data_source_ibm_is_security_group_rule.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"reflect"
"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"
)
func DataSourceIBMIsSecurityGroupRule() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsSecurityGroupRuleRead,
Schema: map[string]*schema.Schema{
"security_group": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The security group identifier.",
},
"security_group_rule": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The rule identifier.",
},
"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.",
},
"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 dataSourceIBMIsSecurityGroupRuleRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
getSecurityGroupRuleOptions := &vpcv1.GetSecurityGroupRuleOptions{}
getSecurityGroupRuleOptions.SetSecurityGroupID(d.Get("security_group").(string))
getSecurityGroupRuleOptions.SetID(d.Get("security_group_rule").(string))
securityGroupRuleIntf, response, err := vpcClient.GetSecurityGroupRuleWithContext(context, getSecurityGroupRuleOptions)
if err != nil || securityGroupRuleIntf == nil {
log.Printf("[DEBUG] GetSecurityGroupRuleWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetSecurityGroupRuleWithContext failed %s\n%s", err, response))
}
switch reflect.TypeOf(securityGroupRuleIntf).String() {
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll":
{
securityGroupRule := securityGroupRuleIntf.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll)
d.SetId(*securityGroupRule.ID)
if err = d.Set("direction", securityGroupRule.Direction); err != nil {
return diag.FromErr(fmt.Errorf("Error setting direction: %s", err))
}
if err = d.Set("href", securityGroupRule.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("ip_version", securityGroupRule.IPVersion); err != nil {
return diag.FromErr(fmt.Errorf("Error setting ip_version: %s", err))
}
if err = d.Set("protocol", securityGroupRule.Protocol); err != nil {
return diag.FromErr(fmt.Errorf("Error setting protocol: %s", err))
}
if securityGroupRule.Remote != nil {
securityGroupRuleRemote, err := dataSourceSecurityGroupRuleFlattenRemote(securityGroupRule.Remote)
if err != nil {
return diag.FromErr(fmt.Errorf("Error flattening securityGroupRule.Remote %s", err))
}
err = d.Set("remote", securityGroupRuleRemote)
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting remote %s", err))
}
}
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp":
{
securityGroupRule := securityGroupRuleIntf.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp)
d.SetId(*securityGroupRule.ID)
if err = d.Set("direction", securityGroupRule.Direction); err != nil {
return diag.FromErr(fmt.Errorf("Error setting direction: %s", err))
}
if err = d.Set("href", securityGroupRule.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("ip_version", securityGroupRule.IPVersion); err != nil {
return diag.FromErr(fmt.Errorf("Error setting ip_version: %s", err))
}
if err = d.Set("protocol", securityGroupRule.Protocol); err != nil {
return diag.FromErr(fmt.Errorf("Error setting protocol: %s", err))
}
if securityGroupRule.Remote != nil {
securityGroupRuleRemote, err := dataSourceSecurityGroupRuleFlattenRemote(securityGroupRule.Remote)
if err != nil {
return diag.FromErr(fmt.Errorf("Error flattening securityGroupRule.Remote %s", err))
}
err = d.Set("remote", securityGroupRuleRemote)
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting remote %s", err))
}
}
if err = d.Set("code", flex.IntValue(securityGroupRule.Code)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting code: %s", err))
}
if err = d.Set("type", flex.IntValue(securityGroupRule.Type)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting type: %s", err))
}
}
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp":
{
securityGroupRule := securityGroupRuleIntf.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp)
d.SetId(*securityGroupRule.ID)
if err = d.Set("direction", securityGroupRule.Direction); err != nil {
return diag.FromErr(fmt.Errorf("Error setting direction: %s", err))
}
if err = d.Set("href", securityGroupRule.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("ip_version", securityGroupRule.IPVersion); err != nil {
return diag.FromErr(fmt.Errorf("Error setting ip_version: %s", err))
}
if err = d.Set("protocol", securityGroupRule.Protocol); err != nil {
return diag.FromErr(fmt.Errorf("Error setting protocol: %s", err))
}
if securityGroupRule.Remote != nil {
securityGroupRuleRemote, err := dataSourceSecurityGroupRuleFlattenRemote(securityGroupRule.Remote)
if err != nil {
return diag.FromErr(fmt.Errorf("Error flattening securityGroupRule.Remote %s", err))
}
err = d.Set("remote", securityGroupRuleRemote)
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting remote %s", err))
}
}
if err = d.Set("port_max", flex.IntValue(securityGroupRule.PortMax)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting port_max: %s", err))
}
if err = d.Set("port_min", flex.IntValue(securityGroupRule.PortMin)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting port_min: %s", err))
}
}
}
return nil
}
func dataSourceSecurityGroupRuleFlattenRemote(m vpcv1.SecurityGroupRuleRemoteIntf) ([]map[string]interface{}, error) {
var ruleList []map[string]interface{}
ruleMap := dataSourceSecurityGroupRuleRemoteToMap(m.(*vpcv1.SecurityGroupRuleRemote))
ruleList = append(ruleList, ruleMap)
return ruleList, nil
}
func dataSourceSecurityGroupRuleRemoteToMap(remoteItem *vpcv1.SecurityGroupRuleRemote) (remoteMap map[string]interface{}) {
remoteMap = map[string]interface{}{}
if remoteItem.Address != nil {
remoteMap["address"] = *remoteItem.Address
}
if remoteItem.CIDRBlock != nil {
remoteMap["cidr_block"] = *remoteItem.CIDRBlock
}
if remoteItem.CRN != nil {
remoteMap["crn"] = *remoteItem.CRN
}
if remoteItem.Deleted != nil {
remoteDeletedList := []map[string]interface{}{}
remoteDeletedMap := dataSourceSecurityGroupRuleRemoteDeletedToMap(remoteItem.Deleted)
remoteDeletedList = append(remoteDeletedList, remoteDeletedMap)
remoteMap["deleted"] = remoteDeletedList
}
if remoteItem.Href != nil {
remoteMap["href"] = *remoteItem.Href
}
if remoteItem.ID != nil {
remoteMap["id"] = *remoteItem.ID
}
if remoteItem.Name != nil {
remoteMap["name"] = *remoteItem.Name
}
return remoteMap
}
func dataSourceSecurityGroupRuleRemoteDeletedToMap(deletedItem *vpcv1.SecurityGroupReferenceDeleted) (resultMap map[string]interface{}) {
resultMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
resultMap["more_info"] = deletedItem.MoreInfo
}
return resultMap
}