-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_is_subnet_network_acl_attachment.go
446 lines (407 loc) · 14.9 KB
/
resource_ibm_is_subnet_network_acl_attachment.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"reflect"
"time"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isSubnetID = "subnet"
isNetworkACLID = "network_acl"
)
func ResourceIBMISSubnetNetworkACLAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceIBMISSubnetNetworkACLAttachmentCreate,
Read: resourceIBMISSubnetNetworkACLAttachmentRead,
Update: resourceIBMISSubnetNetworkACLAttachmentUpdate,
Delete: resourceIBMISSubnetNetworkACLAttachmentDelete,
Exists: resourceIBMISSubnetNetworkACLAttachmentExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
isSubnetID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The subnet identifier",
},
isNetworkACLID: {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of network ACL",
},
isNetworkACLName: {
Type: schema.TypeString,
Computed: true,
Description: "Network ACL name",
},
isNetworkACLCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn for this Network ACL",
},
isNetworkACLVPC: {
Type: schema.TypeString,
Computed: true,
Description: "Network ACL VPC",
},
isNetworkACLResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Resource group ID for the network ACL",
},
isNetworkACLRules: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRuleID: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this Network ACL rule",
},
isNetworkACLRuleName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this 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 CIDR block",
},
isNetworkACLRuleDestination: {
Type: schema.TypeString,
Computed: true,
Description: "The destination CIDR block",
},
isNetworkACLRuleDirection: {
Type: schema.TypeString,
Computed: true,
Description: "Direction of traffic to enforce, either inbound or outbound",
},
isNetworkACLRuleICMP: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRuleICMPCode: {
Type: schema.TypeInt,
Computed: true,
Description: "The ICMP traffic code to allow",
},
isNetworkACLRuleICMPType: {
Type: schema.TypeInt,
Computed: true,
Description: "The ICMP traffic type to allow",
},
},
},
},
isNetworkACLRuleTCP: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRulePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive upper bound of TCP destination port range",
},
isNetworkACLRulePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive lower bound of TCP destination port range",
},
isNetworkACLRuleSourcePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive upper bound of TCP source port range",
},
isNetworkACLRuleSourcePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive lower bound of TCP source port range",
},
},
},
},
isNetworkACLRuleUDP: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isNetworkACLRulePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive upper bound of UDP destination port range",
},
isNetworkACLRulePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive lower bound of UDP destination port range",
},
isNetworkACLRuleSourcePortMax: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive upper bound of UDP source port range",
},
isNetworkACLRuleSourcePortMin: {
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive lower bound of UDP source port range",
},
},
},
},
},
},
},
},
}
}
func resourceIBMISSubnetNetworkACLAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
subnet := d.Get(isSubnetID).(string)
networkACL := d.Get(isNetworkACLID).(string)
// Construct an instance of the NetworkACLIdentityByID model
networkACLIdentityModel := new(vpcv1.NetworkACLIdentityByID)
networkACLIdentityModel.ID = &networkACL
// Construct an instance of the ReplaceSubnetNetworkACLOptions model
replaceSubnetNetworkACLOptionsModel := new(vpcv1.ReplaceSubnetNetworkACLOptions)
replaceSubnetNetworkACLOptionsModel.ID = &subnet
replaceSubnetNetworkACLOptionsModel.NetworkACLIdentity = networkACLIdentityModel
resultACL, response, err := sess.ReplaceSubnetNetworkACL(replaceSubnetNetworkACLOptionsModel)
if err != nil {
log.Printf("[DEBUG] Error while attaching a network ACL to a subnet %s\n%s", err, response)
return fmt.Errorf("[ERROR] Error while attaching a network ACL to a subnet %s\n%s", err, response)
}
d.SetId(subnet)
log.Printf("[INFO] Network ACL : %s", *resultACL.ID)
log.Printf("[INFO] Subnet ID : %s", subnet)
return resourceIBMISSubnetNetworkACLAttachmentRead(d, meta)
}
func resourceIBMISSubnetNetworkACLAttachmentRead(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
sess, err := vpcClient(meta)
if err != nil {
return err
}
getSubnetNetworkACLOptionsModel := &vpcv1.GetSubnetNetworkACLOptions{
ID: &id,
}
nwacl, response, err := sess.GetSubnetNetworkACL(getSubnetNetworkACLOptionsModel)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error getting subnet's (%s) attached network ACL: %s\n%s", id, err, response)
}
d.Set(isNetworkACLName, *nwacl.Name)
d.Set(isNetworkACLCRN, *nwacl.CRN)
d.Set(isNetworkACLVPC, *nwacl.VPC.ID)
d.Set(isNetworkACLID, *nwacl.ID)
if nwacl.ResourceGroup != nil {
d.Set(isNetworkACLResourceGroup, *nwacl.ResourceGroup.ID)
}
rules := make([]interface{}, 0)
if len(nwacl.Rules) > 0 {
for _, rulex := range nwacl.Rules {
log.Println("[DEBUG] Type of the Rule", reflect.TypeOf(rulex))
rule := make(map[string]interface{})
switch reflect.TypeOf(rulex).String() {
case "*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolIcmp":
{
rulex := rulex.(*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolIcmp)
rule[isNetworkACLRuleID] = *rulex.ID
rule[isNetworkACLRuleName] = *rulex.Name
rule[isNetworkACLRuleAction] = *rulex.Action
rule[isNetworkACLRuleIPVersion] = *rulex.IPVersion
rule[isNetworkACLRuleSource] = *rulex.Source
rule[isNetworkACLRuleDestination] = *rulex.Destination
rule[isNetworkACLRuleDirection] = *rulex.Direction
rule[isNetworkACLRuleTCP] = make([]map[string]int, 0, 0)
rule[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.Code),
}
}
rule[isNetworkACLRuleICMP] = icmp
}
case "*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolTcpudp":
{
rulex := rulex.(*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolTcpudp)
rule[isNetworkACLRuleID] = *rulex.ID
rule[isNetworkACLRuleName] = *rulex.Name
rule[isNetworkACLRuleAction] = *rulex.Action
rule[isNetworkACLRuleIPVersion] = *rulex.IPVersion
rule[isNetworkACLRuleSource] = *rulex.Source
rule[isNetworkACLRuleDestination] = *rulex.Destination
rule[isNetworkACLRuleDirection] = *rulex.Direction
if *rulex.Protocol == "tcp" {
rule[isNetworkACLRuleICMP] = make([]map[string]int, 0, 0)
rule[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)
rule[isNetworkACLRuleTCP] = tcp
} else if *rulex.Protocol == "udp" {
rule[isNetworkACLRuleICMP] = make([]map[string]int, 0, 0)
rule[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)
rule[isNetworkACLRuleUDP] = udp
}
}
case "*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolAll":
{
rulex := rulex.(*vpcv1.NetworkACLRuleItemNetworkACLRuleProtocolAll)
rule[isNetworkACLRuleID] = *rulex.ID
rule[isNetworkACLRuleName] = *rulex.Name
rule[isNetworkACLRuleAction] = *rulex.Action
rule[isNetworkACLRuleIPVersion] = *rulex.IPVersion
rule[isNetworkACLRuleSource] = *rulex.Source
rule[isNetworkACLRuleDestination] = *rulex.Destination
rule[isNetworkACLRuleDirection] = *rulex.Direction
rule[isNetworkACLRuleICMP] = make([]map[string]int, 0, 0)
rule[isNetworkACLRuleTCP] = make([]map[string]int, 0, 0)
rule[isNetworkACLRuleUDP] = make([]map[string]int, 0, 0)
}
}
rules = append(rules, rule)
}
}
d.Set(isNetworkACLRules, rules)
return nil
}
func resourceIBMISSubnetNetworkACLAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
if d.HasChange(isNetworkACLID) {
subnet := d.Get(isSubnetID).(string)
networkACL := d.Get(isNetworkACLID).(string)
// Construct an instance of the NetworkACLIdentityByID model
networkACLIdentityModel := new(vpcv1.NetworkACLIdentityByID)
networkACLIdentityModel.ID = &networkACL
// Construct an instance of the ReplaceSubnetNetworkACLOptions model
replaceSubnetNetworkACLOptionsModel := new(vpcv1.ReplaceSubnetNetworkACLOptions)
replaceSubnetNetworkACLOptionsModel.ID = &subnet
replaceSubnetNetworkACLOptionsModel.NetworkACLIdentity = networkACLIdentityModel
resultACL, response, err := sess.ReplaceSubnetNetworkACL(replaceSubnetNetworkACLOptionsModel)
if err != nil {
log.Printf("[DEBUG] Error while attaching a network ACL to a subnet %s\n%s", err, response)
return fmt.Errorf("[ERROR] Error while attaching a network ACL to a subnet %s\n%s", err, response)
}
log.Printf("[INFO] Updated subnet %s with Network ACL : %s", subnet, *resultACL.ID)
d.SetId(subnet)
return resourceIBMISSubnetNetworkACLAttachmentRead(d, meta)
}
return resourceIBMISSubnetNetworkACLAttachmentRead(d, meta)
}
func resourceIBMISSubnetNetworkACLAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
sess, err := vpcClient(meta)
if err != nil {
return err
}
// Set the subnet with VPC default network ACL
getSubnetOptions := &vpcv1.GetSubnetOptions{
ID: &id,
}
subnet, response, err := sess.GetSubnet(getSubnetOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error Getting Subnet (%s): %s\n%s", id, err, response)
}
// Fetch VPC
vpcID := *subnet.VPC.ID
getvpcOptions := &vpcv1.GetVPCOptions{
ID: &vpcID,
}
vpc, response, err := sess.GetVPC(getvpcOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error getting VPC : %s\n%s", err, response)
}
// Fetch default network ACL
if vpc.DefaultNetworkACL != nil {
log.Printf("[DEBUG] vpc default network acl is not null :%s", *vpc.DefaultNetworkACL.ID)
// Construct an instance of the NetworkACLIdentityByID model
networkACLIdentityModel := new(vpcv1.NetworkACLIdentityByID)
networkACLIdentityModel.ID = vpc.DefaultNetworkACL.ID
// Construct an instance of the ReplaceSubnetNetworkACLOptions model
replaceSubnetNetworkACLOptionsModel := new(vpcv1.ReplaceSubnetNetworkACLOptions)
replaceSubnetNetworkACLOptionsModel.ID = &id
replaceSubnetNetworkACLOptionsModel.NetworkACLIdentity = networkACLIdentityModel
resultACL, response, err := sess.ReplaceSubnetNetworkACL(replaceSubnetNetworkACLOptionsModel)
if err != nil {
log.Printf("[DEBUG] Error while attaching a network ACL to a subnet %s\n%s", err, response)
return fmt.Errorf("[ERROR] Error while attaching a network ACL to a subnet %s\n%s", err, response)
}
log.Printf("[INFO] Updated subnet %s with VPC default Network ACL : %s", id, *resultACL.ID)
} else {
log.Printf("[DEBUG] vpc default network acl is null")
}
d.SetId("")
return nil
}
func resourceIBMISSubnetNetworkACLAttachmentExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id := d.Id()
sess, err := vpcClient(meta)
if err != nil {
return false, err
}
getSubnetNetworkACLOptionsModel := &vpcv1.GetSubnetNetworkACLOptions{
ID: &id,
}
_, response, err := sess.GetSubnetNetworkACL(getSubnetNetworkACLOptionsModel)
if err != nil {
if response != nil && response.StatusCode == 404 {
return false, nil
}
return false, fmt.Errorf("[ERROR] Error getting subnet's attached network ACL: %s\n%s", err, response)
}
return true, nil
}