Skip to content

Commit 62ec69a

Browse files
authored
Merge pull request hashicorp#7669 from kwilczynski/fix/aws-network-acl-rule
Fix icmp_type and icmp_code in aws_network_acl_rule.
2 parents a445731 + 96b6a3d commit 62ec69a

3 files changed

Lines changed: 60 additions & 17 deletions

File tree

builtin/providers/aws/resource_aws_network_acl_rule.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ func resourceAwsNetworkAclRule() *schema.Resource {
6363
ForceNew: true,
6464
},
6565
"icmp_type": &schema.Schema{
66-
Type: schema.TypeInt,
67-
Optional: true,
68-
ForceNew: true,
66+
Type: schema.TypeString,
67+
Optional: true,
68+
ForceNew: true,
69+
ValidateFunc: validateICMPArgumentValue,
6970
},
7071
"icmp_code": &schema.Schema{
71-
Type: schema.TypeInt,
72-
Optional: true,
73-
ForceNew: true,
72+
Type: schema.TypeString,
73+
Optional: true,
74+
ForceNew: true,
75+
ValidateFunc: validateICMPArgumentValue,
7476
},
7577
},
7678
}
@@ -85,7 +87,7 @@ func resourceAwsNetworkAclRuleCreate(d *schema.ResourceData, meta interface{}) e
8587
var ok bool
8688
p, ok = protocolIntegers()[protocol]
8789
if !ok {
88-
return fmt.Errorf("Invalid Protocol %s for rule %#v", protocol, d.Get("rule_number").(int))
90+
return fmt.Errorf("Invalid Protocol %s for rule %d", protocol, d.Get("rule_number").(int))
8991
}
9092
}
9193
log.Printf("[INFO] Transformed Protocol %s into %d", protocol, p)
@@ -103,14 +105,25 @@ func resourceAwsNetworkAclRuleCreate(d *schema.ResourceData, meta interface{}) e
103105
},
104106
}
105107

106-
// Specify additional required fields for ICMP
108+
// Specify additional required fields for ICMP. For the list
109+
// of ICMP codes and types, see: http://www.nthelp.com/icmp.html
107110
if p == 1 {
108111
params.IcmpTypeCode = &ec2.IcmpTypeCode{}
109-
if v, ok := d.GetOk("icmp_code"); ok {
110-
params.IcmpTypeCode.Code = aws.Int64(int64(v.(int)))
111-
}
112112
if v, ok := d.GetOk("icmp_type"); ok {
113-
params.IcmpTypeCode.Type = aws.Int64(int64(v.(int)))
113+
icmpType, err := strconv.Atoi(v.(string))
114+
if err != nil {
115+
return fmt.Errorf("Unable to parse ICMP type %s for rule %d", v, d.Get("rule_number").(int))
116+
}
117+
params.IcmpTypeCode.Type = aws.Int64(int64(icmpType))
118+
log.Printf("[DEBUG] Got ICMP type %d for rule %d", icmpType, d.Get("rule_number").(int))
119+
}
120+
if v, ok := d.GetOk("icmp_code"); ok {
121+
icmpCode, err := strconv.Atoi(v.(string))
122+
if err != nil {
123+
return fmt.Errorf("Unable to parse ICMP code %s for rule %d", v, d.Get("rule_number").(int))
124+
}
125+
params.IcmpTypeCode.Code = aws.Int64(int64(icmpCode))
126+
log.Printf("[DEBUG] Got ICMP code %d for rule %d", icmpCode, d.Get("rule_number").(int))
114127
}
115128
}
116129

@@ -165,7 +178,7 @@ func resourceAwsNetworkAclRuleRead(d *schema.ResourceData, meta interface{}) err
165178
var ok bool
166179
protocol, ok := protocolStrings(protocolIntegers())[p]
167180
if !ok {
168-
return fmt.Errorf("Invalid Protocol %s for rule %#v", *resp.Protocol, d.Get("rule_number").(int))
181+
return fmt.Errorf("Invalid Protocol %s for rule %d", *resp.Protocol, d.Get("rule_number").(int))
169182
}
170183
log.Printf("[INFO] Transformed Protocol %s back into %s", *resp.Protocol, protocol)
171184
d.Set("protocol", protocol)
@@ -198,7 +211,7 @@ func findNetworkAclRule(d *schema.ResourceData, meta interface{}) (*ec2.NetworkA
198211
filters := make([]*ec2.Filter, 0, 2)
199212
ruleNumberFilter := &ec2.Filter{
200213
Name: aws.String("entry.rule-number"),
201-
Values: []*string{aws.String(fmt.Sprintf("%v", d.Get("rule_number").(int)))},
214+
Values: []*string{aws.String(fmt.Sprintf("%d", d.Get("rule_number").(int)))},
202215
}
203216
filters = append(filters, ruleNumberFilter)
204217
egressFilter := &ec2.Filter{
@@ -245,3 +258,12 @@ func networkAclIdRuleNumberEgressHash(networkAclId string, ruleNumber int, egres
245258
buf.WriteString(fmt.Sprintf("%s-", protocol))
246259
return fmt.Sprintf("nacl-%d", hashcode.String(buf.String()))
247260
}
261+
262+
func validateICMPArgumentValue(v interface{}, k string) (ws []string, errors []error) {
263+
value := v.(string)
264+
_, err := strconv.Atoi(value)
265+
if len(value) == 0 || err != nil {
266+
errors = append(errors, fmt.Errorf("%q must be an integer value: %q", k, value))
267+
}
268+
return
269+
}

builtin/providers/aws/resource_aws_network_acl_rule_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ func TestAccAWSNetworkAclRule_basic(t *testing.T) {
2323
resource.TestStep{
2424
Config: testAccAWSNetworkAclRuleBasicConfig,
2525
Check: resource.ComposeTestCheckFunc(
26-
testAccCheckAWSNetworkAclRuleExists("aws_network_acl_rule.bar", &networkAcl),
26+
testAccCheckAWSNetworkAclRuleExists("aws_network_acl_rule.baz", &networkAcl),
27+
testAccCheckAWSNetworkAclRuleExists("aws_network_acl_rule.qux", &networkAcl),
28+
testAccCheckAWSNetworkAclRuleExists("aws_network_acl_rule.wibble", &networkAcl),
2729
),
2830
},
2931
},
@@ -112,7 +114,7 @@ resource "aws_vpc" "foo" {
112114
resource "aws_network_acl" "bar" {
113115
vpc_id = "${aws_vpc.foo.id}"
114116
}
115-
resource "aws_network_acl_rule" "bar" {
117+
resource "aws_network_acl_rule" "baz" {
116118
network_acl_id = "${aws_network_acl.bar.id}"
117119
rule_number = 200
118120
egress = false
@@ -122,4 +124,22 @@ resource "aws_network_acl_rule" "bar" {
122124
from_port = 22
123125
to_port = 22
124126
}
127+
resource "aws_network_acl_rule" "qux" {
128+
network_acl_id = "${aws_network_acl.bar.id}"
129+
rule_number = 300
130+
protocol = "icmp"
131+
rule_action = "allow"
132+
cidr_block = "0.0.0.0/0"
133+
icmp_type = 0
134+
icmp_code = -1
135+
}
136+
resource "aws_network_acl_rule" "wibble" {
137+
network_acl_id = "${aws_network_acl.bar.id}"
138+
rule_number = 400
139+
protocol = "icmp"
140+
rule_action = "allow"
141+
cidr_block = "0.0.0.0/0"
142+
icmp_type = -1
143+
icmp_code = -1
144+
}
125145
`

website/source/docs/providers/aws/r/network_acl_rule.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ The following arguments are supported:
4545

4646
~> **NOTE:** If the value of `protocol` is `-1` or `all`, the `from_port` and `to_port` values will be ignored and the rule will apply to all ports.
4747

48+
~> **NOTE:** If the value of `icmp_type` is `-1` (which results in a wildcard ICMP type), the `icmp_code` must also be set to `-1` (wildcard ICMP code).
49+
4850
~> Note: For more information on ICMP types and codes, see here: http://www.nthelp.com/icmp.html
4951

5052
## Attributes Reference
5153

5254
The following attributes are exported:
5355

5456
* `id` - The ID of the network ACL Rule
55-

0 commit comments

Comments
 (0)