-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_network_acl_rule.go
303 lines (289 loc) · 9.81 KB
/
data_source_ibm_is_network_acl_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
301
302
303
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"reflect"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isNetworkACLRuleHref = "href"
)
func DataSourceIBMISNetworkACLRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISNetworkACLRuleRead,
Schema: map[string]*schema.Schema{
isNwACLID: {
Type: schema.TypeString,
Required: true,
Description: "Network ACL id",
},
isNwACLRuleId: {
Type: schema.TypeString,
Computed: true,
Description: "Network ACL rule id",
},
isNwACLRuleBefore: {
Type: schema.TypeString,
Computed: true,
Description: "The rule that this rule is immediately before. If absent, this is the last rule.",
},
isNetworkACLRuleName: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_is_network_acl", isNetworkACLRuleName),
Description: "The user-defined name for this rule",
},
isNetworkACLRuleProtocol: {
Type: schema.TypeString,
Computed: true,
Description: "The protocol to enforce.",
},
isNetworkACLRuleHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this network ACL rule",
},
isNetworkACLRuleAction: {
Type: schema.TypeString,
Computed: true,
Description: "Whether to allow or deny matching traffic.",
},
isNetworkACLRuleIPVersion: {
Type: schema.TypeString,
Computed: true,
Description: "The IP version for this rule.",
},
isNetworkACLRuleSource: {
Type: schema.TypeString,
Computed: true,
Description: "The source IP address or CIDR block.",
},
isNetworkACLRuleDestination: {
Type: schema.TypeString,
Computed: true,
Description: "The destination IP address or CIDR block.",
},
isNetworkACLRuleDirection: {
Type: schema.TypeString,
Computed: true,
Description: "Whether the traffic to be matched is inbound or outbound.",
},
isNetworkACLRuleICMP: {
Type: schema.TypeList,
Computed: true,
Description: "The protocol ICMP",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRuleICMPCode: {
Type: schema.TypeInt,
Computed: true,
Description: "The ICMP traffic code to allow. Valid values from 0 to 255.",
},
isNetworkACLRuleICMPType: {
Type: schema.TypeInt,
Computed: true,
Description: "The ICMP traffic type to allow. Valid values from 0 to 254.",
},
},
},
},
isNetworkACLRuleTCP: {
Type: schema.TypeList,
Computed: true,
Description: "TCP protocol",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRulePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The highest port in the range of ports to be matched",
},
isNetworkACLRulePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The lowest port in the range of ports to be matched",
},
isNetworkACLRuleSourcePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The highest port in the range of ports to be matched",
},
isNetworkACLRuleSourcePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The lowest port in the range of ports to be matched",
},
},
},
},
isNetworkACLRuleUDP: {
Type: schema.TypeList,
Computed: true,
Description: "UDP protocol",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRulePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The highest port in the range of ports to be matched",
},
isNetworkACLRulePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The lowest port in the range of ports to be matched",
},
isNetworkACLRuleSourcePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The highest port in the range of ports to be matched",
},
isNetworkACLRuleSourcePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The lowest port in the range of ports to be matched",
},
},
},
},
},
}
}
func dataSourceIBMISNetworkACLRuleRead(d *schema.ResourceData, meta interface{}) error {
nwACLID := d.Get(isNwACLID).(string)
name := d.Get(isNetworkACLRuleName).(string)
err := nawaclRuleDataGet(d, meta, name, nwACLID)
if err != nil {
return err
}
return nil
}
func nawaclRuleDataGet(d *schema.ResourceData, meta interface{}, name, nwACLID string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.NetworkACLRuleItemIntf{}
for {
listNetworkACLRulesOptions := &vpcv1.ListNetworkACLRulesOptions{
NetworkACLID: &nwACLID,
}
if start != "" {
listNetworkACLRulesOptions.Start = &start
}
ruleList, response, err := sess.ListNetworkACLRules(listNetworkACLRulesOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching network acl ruless %s\n%s", err, response)
}
start = flex.GetNext(ruleList.Next)
allrecs = append(allrecs, ruleList.Rules...)
if start == "" {
break
}
}
for _, rule := range allrecs {
switch reflect.TypeOf(rule).String() {
case "*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolIcmp":
{
rulex := rule.(*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolIcmp)
if *rulex.Name == name {
d.SetId(makeTerraformACLRuleID(nwACLID, *rulex.ID))
d.Set(isNwACLRuleId, *rulex.ID)
if rulex.Before != nil {
d.Set(isNwACLRuleBefore, *rulex.Before.ID)
}
d.Set(isNetworkACLRuleHref, *rulex.Href)
d.Set(isNetworkACLRuleProtocol, *rulex.Protocol)
d.Set(isNetworkACLRuleName, *rulex.Name)
d.Set(isNetworkACLRuleAction, *rulex.Action)
d.Set(isNetworkACLRuleIPVersion, *rulex.IPVersion)
d.Set(isNetworkACLRuleSource, *rulex.Source)
d.Set(isNetworkACLRuleDestination, *rulex.Destination)
d.Set(isNetworkACLRuleDirection, *rulex.Direction)
d.Set(isNetworkACLRuleTCP, make([]map[string]int, 0, 0))
d.Set(isNetworkACLRuleUDP, make([]map[string]int, 0, 0))
icmp := make([]map[string]int, 1, 1)
if rulex.Code != nil && rulex.Type != nil {
icmp[0] = map[string]int{
isNetworkACLRuleICMPCode: int(*rulex.Code),
isNetworkACLRuleICMPType: int(*rulex.Type),
}
}
d.Set(isNetworkACLRuleICMP, icmp)
break
}
}
case "*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolTcpudp":
{
rulex := rule.(*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolTcpudp)
if *rulex.Name == name {
d.SetId(makeTerraformACLRuleID(nwACLID, *rulex.ID))
d.Set(isNwACLRuleId, *rulex.ID)
if rulex.Before != nil {
d.Set(isNwACLRuleBefore, *rulex.Before.ID)
}
d.Set(isNetworkACLRuleHref, *rulex.Href)
d.Set(isNetworkACLRuleProtocol, *rulex.Protocol)
d.Set(isNetworkACLRuleName, *rulex.Name)
d.Set(isNetworkACLRuleAction, *rulex.Action)
d.Set(isNetworkACLRuleIPVersion, *rulex.IPVersion)
d.Set(isNetworkACLRuleSource, *rulex.Source)
d.Set(isNetworkACLRuleDestination, *rulex.Destination)
d.Set(isNetworkACLRuleDirection, *rulex.Direction)
if *rulex.Protocol == "tcp" {
d.Set(isNetworkACLRuleICMP, make([]map[string]int, 0, 0))
d.Set(isNetworkACLRuleUDP, make([]map[string]int, 0, 0))
tcp := make([]map[string]int, 1, 1)
tcp[0] = map[string]int{
isNetworkACLRuleSourcePortMax: checkNetworkACLNil(rulex.SourcePortMax),
isNetworkACLRuleSourcePortMin: checkNetworkACLNil(rulex.SourcePortMin),
}
tcp[0][isNetworkACLRulePortMax] = checkNetworkACLNil(rulex.DestinationPortMax)
tcp[0][isNetworkACLRulePortMin] = checkNetworkACLNil(rulex.DestinationPortMin)
d.Set(isNetworkACLRuleTCP, tcp)
} else if *rulex.Protocol == "udp" {
d.Set(isNetworkACLRuleICMP, make([]map[string]int, 0, 0))
d.Set(isNetworkACLRuleTCP, make([]map[string]int, 0, 0))
udp := make([]map[string]int, 1, 1)
udp[0] = map[string]int{
isNetworkACLRuleSourcePortMax: checkNetworkACLNil(rulex.SourcePortMax),
isNetworkACLRuleSourcePortMin: checkNetworkACLNil(rulex.SourcePortMin),
}
udp[0][isNetworkACLRulePortMax] = checkNetworkACLNil(rulex.DestinationPortMax)
udp[0][isNetworkACLRulePortMin] = checkNetworkACLNil(rulex.DestinationPortMin)
d.Set(isNetworkACLRuleUDP, udp)
break
}
}
}
case "*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolAll":
{
rulex := rule.(*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolAll)
if *rulex.Name == name {
d.SetId(makeTerraformACLRuleID(nwACLID, *rulex.ID))
d.Set(isNwACLRuleId, *rulex.ID)
if rulex.Before != nil {
d.Set(isNwACLRuleBefore, *rulex.Before.ID)
}
d.Set(isNetworkACLRuleHref, *rulex.Href)
d.Set(isNetworkACLRuleProtocol, *rulex.Protocol)
d.Set(isNetworkACLRuleName, *rulex.Name)
d.Set(isNetworkACLRuleAction, *rulex.Action)
d.Set(isNetworkACLRuleIPVersion, *rulex.IPVersion)
d.Set(isNetworkACLRuleSource, *rulex.Source)
d.Set(isNetworkACLRuleDestination, *rulex.Destination)
d.Set(isNetworkACLRuleDirection, *rulex.Direction)
d.Set(isNetworkACLRuleICMP, make([]map[string]int, 0, 0))
d.Set(isNetworkACLRuleTCP, make([]map[string]int, 0, 0))
d.Set(isNetworkACLRuleUDP, make([]map[string]int, 0, 0))
break
}
}
}
}
return nil
}