Skip to content

Commit cc59465

Browse files
authored
Merge pull request #13949 from DrFaust92/r/ec2_traffic_mirror_filter_rule
r/ec2_traffic_mirror_filter_rule - add arn attribute
2 parents eeb6b37 + f576ad2 commit cc59465

5 files changed

Lines changed: 91 additions & 33 deletions

.changelog/13949.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```release-note:enhancement
2+
resource/aws_ec2_traffic_mirror_filter_rule: Add arn attribute.
3+
```
4+
5+
```release-note:enhancement
6+
resource/aws_ec2_traffic_mirror_filter_rule: Add plan time validation to `destination_port_range.from_port`,
7+
`destination_port_range.to_port`, `source_port_range.from_port`, and `source_port_range.to_port`.
8+
```

aws/resource_aws_ec2_traffic_mirror_filter_rule.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/aws/arn"
910
"github.com/aws/aws-sdk-go/service/ec2"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -21,6 +22,10 @@ func resourceAwsEc2TrafficMirrorFilterRule() *schema.Resource {
2122
State: resourceAwsEc2TrafficMirrorFilterRuleImport,
2223
},
2324
Schema: map[string]*schema.Schema{
25+
"arn": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
2429
"description": {
2530
Type: schema.TypeString,
2631
Optional: true,
@@ -42,12 +47,14 @@ func resourceAwsEc2TrafficMirrorFilterRule() *schema.Resource {
4247
Elem: &schema.Resource{
4348
Schema: map[string]*schema.Schema{
4449
"from_port": {
45-
Type: schema.TypeInt,
46-
Optional: true,
50+
Type: schema.TypeInt,
51+
Optional: true,
52+
ValidateFunc: validation.IsPortNumberOrZero,
4753
},
4854
"to_port": {
49-
Type: schema.TypeInt,
50-
Optional: true,
55+
Type: schema.TypeInt,
56+
Optional: true,
57+
ValidateFunc: validation.IsPortNumberOrZero,
5158
},
5259
},
5360
},
@@ -80,12 +87,14 @@ func resourceAwsEc2TrafficMirrorFilterRule() *schema.Resource {
8087
Elem: &schema.Resource{
8188
Schema: map[string]*schema.Schema{
8289
"from_port": {
83-
Type: schema.TypeInt,
84-
Optional: true,
90+
Type: schema.TypeInt,
91+
Optional: true,
92+
ValidateFunc: validation.IsPortNumberOrZero,
8593
},
8694
"to_port": {
87-
Type: schema.TypeInt,
88-
Optional: true,
95+
Type: schema.TypeInt,
96+
Optional: true,
97+
ValidateFunc: validation.IsPortNumberOrZero,
8998
},
9099
},
91100
},
@@ -162,7 +171,7 @@ func resourceAwsEc2TrafficMirrorFilterRuleRead(d *schema.ResourceData, meta inte
162171
}
163172

164173
if nil == rule {
165-
log.Printf("[WARN] EC2 Traffic Mirror Filter (%s) not found, removing from state", d.Id())
174+
log.Printf("[WARN] EC2 Traffic Mirror Filter Rule (%s) not found, removing from state", d.Id())
166175
d.SetId("")
167176
return nil
168177
}
@@ -185,29 +194,40 @@ func resourceAwsEc2TrafficMirrorFilterRuleRead(d *schema.ResourceData, meta inte
185194
return fmt.Errorf("error setting source_port_range: %s", err)
186195
}
187196

197+
arn := arn.ARN{
198+
Partition: meta.(*AWSClient).partition,
199+
Service: ec2.ServiceName,
200+
Region: meta.(*AWSClient).region,
201+
AccountID: meta.(*AWSClient).accountid,
202+
Resource: fmt.Sprintf("traffic-mirror-filter-rule/%s", d.Id()),
203+
}.String()
204+
205+
d.Set("arn", arn)
206+
188207
return nil
189208
}
190209

191210
func findEc2TrafficMirrorFilterRule(ruleId string, filters []*ec2.TrafficMirrorFilter) (rule *ec2.TrafficMirrorFilterRule) {
192211
log.Printf("[DEBUG] searching %s in %d filters", ruleId, len(filters))
193212
for _, v := range filters {
194-
log.Printf("[DEBUG]: searching filter %s, ingress rule count = %d, egress rule count = %d", *v.TrafficMirrorFilterId, len(v.IngressFilterRules), len(v.EgressFilterRules))
213+
log.Printf("[DEBUG]: searching filter %s, ingress rule count = %d, egress rule count = %d",
214+
aws.StringValue(v.TrafficMirrorFilterId), len(v.IngressFilterRules), len(v.EgressFilterRules))
195215
for _, r := range v.IngressFilterRules {
196-
if *r.TrafficMirrorFilterRuleId == ruleId {
216+
if aws.StringValue(r.TrafficMirrorFilterRuleId) == ruleId {
197217
rule = r
198218
break
199219
}
200220
}
201221
for _, r := range v.EgressFilterRules {
202-
if *r.TrafficMirrorFilterRuleId == ruleId {
222+
if aws.StringValue(r.TrafficMirrorFilterRuleId) == ruleId {
203223
rule = r
204224
break
205225
}
206226
}
207227
}
208228

209229
if nil != rule {
210-
log.Printf("[DEBUG]: Found %s in %s", ruleId, *rule.TrafficDirection)
230+
log.Printf("[DEBUG]: Found %s in %s", ruleId, aws.StringValue(rule.TrafficDirection))
211231
}
212232

213233
return rule

aws/resource_aws_ec2_traffic_mirror_filter_rule_test.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestAccAWSEc2TrafficMirrorFilterRule_basic(t *testing.T) {
16-
resourceName := "aws_ec2_traffic_mirror_filter_rule.rule"
16+
resourceName := "aws_ec2_traffic_mirror_filter_rule.test"
1717
dstCidr := "10.0.0.0/8"
1818
srcCidr := "0.0.0.0/0"
1919
ruleNum := 1
@@ -39,6 +39,7 @@ func TestAccAWSEc2TrafficMirrorFilterRule_basic(t *testing.T) {
3939
Config: testAccEc2TrafficMirrorFilterRuleConfig(dstCidr, srcCidr, action, direction, ruleNum),
4040
Check: resource.ComposeTestCheckFunc(
4141
testAccCheckAWSEc2TrafficMirrorFilterRuleExists(resourceName),
42+
testAccMatchResourceAttrRegionalARN(resourceName, "arn", ec2.ServiceName, regexp.MustCompile(`traffic-mirror-filter-rule/tmfr-.+`)),
4243
resource.TestMatchResourceAttr(resourceName, "traffic_mirror_filter_id", regexp.MustCompile("tmf-.*")),
4344
resource.TestCheckResourceAttr(resourceName, "destination_cidr_block", dstCidr),
4445
resource.TestCheckResourceAttr(resourceName, "rule_action", action),
@@ -99,6 +100,34 @@ func TestAccAWSEc2TrafficMirrorFilterRule_basic(t *testing.T) {
99100
})
100101
}
101102

103+
func TestAccAWSEc2TrafficMirrorFilterRule_disappears(t *testing.T) {
104+
resourceName := "aws_ec2_traffic_mirror_filter_rule.test"
105+
dstCidr := "10.0.0.0/8"
106+
srcCidr := "0.0.0.0/0"
107+
ruleNum := 1
108+
action := "accept"
109+
direction := "ingress"
110+
111+
resource.ParallelTest(t, resource.TestCase{
112+
PreCheck: func() {
113+
testAccPreCheck(t)
114+
testAccPreCheckAWSEc2TrafficMirrorFilterRule(t)
115+
},
116+
Providers: testAccProviders,
117+
CheckDestroy: testAccCheckAWSEc2TrafficMirrorFilterRuleDestroy,
118+
Steps: []resource.TestStep{
119+
{
120+
Config: testAccEc2TrafficMirrorFilterRuleConfig(dstCidr, srcCidr, action, direction, ruleNum),
121+
Check: resource.ComposeTestCheckFunc(
122+
testAccCheckAWSEc2TrafficMirrorFilterRuleExists(resourceName),
123+
testAccCheckResourceDisappears(testAccProvider, resourceAwsEc2TrafficMirrorFilterRule(), resourceName),
124+
),
125+
ExpectNonEmptyPlan: true,
126+
},
127+
},
128+
})
129+
}
130+
102131
func testAccCheckAWSEc2TrafficMirrorFilterRuleExists(name string) resource.TestCheckFunc {
103132
return func(s *terraform.State) error {
104133
rs, ok := s.RootModule().Resources[name]
@@ -134,7 +163,7 @@ func testAccCheckAWSEc2TrafficMirrorFilterRuleExists(name string) resource.TestC
134163

135164
var exists bool
136165
for _, rule := range ruleList {
137-
if *rule.TrafficMirrorFilterRuleId == ruleId {
166+
if aws.StringValue(rule.TrafficMirrorFilterRuleId) == ruleId {
138167
exists = true
139168
break
140169
}
@@ -150,11 +179,11 @@ func testAccCheckAWSEc2TrafficMirrorFilterRuleExists(name string) resource.TestC
150179

151180
func testAccEc2TrafficMirrorFilterRuleConfig(dstCidr, srcCidr, action, dir string, num int) string {
152181
return fmt.Sprintf(`
153-
resource "aws_ec2_traffic_mirror_filter" "filter" {
182+
resource "aws_ec2_traffic_mirror_filter" "test" {
154183
}
155184
156-
resource "aws_ec2_traffic_mirror_filter_rule" "rule" {
157-
traffic_mirror_filter_id = aws_ec2_traffic_mirror_filter.filter.id
185+
resource "aws_ec2_traffic_mirror_filter_rule" "test" {
186+
traffic_mirror_filter_id = aws_ec2_traffic_mirror_filter.test.id
158187
destination_cidr_block = "%s"
159188
rule_action = "%s"
160189
rule_number = %d
@@ -166,11 +195,10 @@ resource "aws_ec2_traffic_mirror_filter_rule" "rule" {
166195

167196
func testAccEc2TrafficMirrorFilterRuleConfigFull(dstCidr, srcCidr, action, dir, description string, ruleNum, srcPortFrom, srcPortTo, dstPortFrom, dstPortTo, protocol int) string {
168197
return fmt.Sprintf(`
169-
resource "aws_ec2_traffic_mirror_filter" "filter" {
170-
}
198+
resource "aws_ec2_traffic_mirror_filter" "test" {}
171199
172-
resource "aws_ec2_traffic_mirror_filter_rule" "rule" {
173-
traffic_mirror_filter_id = aws_ec2_traffic_mirror_filter.filter.id
200+
resource "aws_ec2_traffic_mirror_filter_rule" "test" {
201+
traffic_mirror_filter_id = aws_ec2_traffic_mirror_filter.test.id
174202
destination_cidr_block = "%s"
175203
rule_action = "%s"
176204
rule_number = %d
@@ -239,7 +267,7 @@ func testAccCheckAWSEc2TrafficMirrorFilterRuleDestroy(s *terraform.State) error
239267
ruleList = append(ruleList, filter.EgressFilterRules...)
240268

241269
for _, rule := range ruleList {
242-
if *rule.TrafficMirrorFilterRuleId == ruleId {
270+
if aws.StringValue(rule.TrafficMirrorFilterRuleId) == ruleId {
243271
return fmt.Errorf("Rule %s still exists in filter %s", ruleId, filterId)
244272
}
245273
}

website/docs/index.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ for more information about connecting to alternate AWS endpoints or AWS compatib
265265
- [`aws_ebs_volume` data source](/docs/providers/aws/d/ebs_volume.html)
266266
- [`aws_ec2_capacity_reservation` resource](/docs/providers/aws/r/ec2_capacity_reservation.html)
267267
- [`aws_ec2_client_vpn_endpoint` resource](/docs/providers/aws/r/ec2_client_vpn_endpoint.html)
268+
- [`aws_ec2_traffic_mirror_filter_rule` resource](/docs/providers/aws/r/ec2_traffic_mirror_filter_rule.html)
268269
- [`aws_ec2_traffic_mirror_session` resource](/docs/providers/aws/r/ec2_traffic_mirror_session.html)
269270
- [`aws_ec2_traffic_mirror_target` resource](/docs/providers/aws/r/ec2_traffic_mirror_target.html)
270271
- [`aws_ec2_transit_gateway_route_table` data source](/docs/providers/aws/d/ec2_transit_gateway_route_table.html)

website/docs/r/ec2_traffic_mirror_filter_rule.html.markdown

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ resource "aws_ec2_traffic_mirror_filter_rule" "rulein" {
5757

5858
The following arguments are supported:
5959

60-
* `description` - (Optional) A description of the traffic mirror filter rule.
60+
* `description` - (Optional) Description of the traffic mirror filter rule.
6161
* `traffic_mirror_filter_id` - (Required) ID of the traffic mirror filter to which this rule should be added
62-
* `destination_cidr_block` - (Required) The destination CIDR block to assign to the Traffic Mirror rule.
63-
* `destination_port_range` - (Optional) The destination port range. Supported only when the protocol is set to TCP(6) or UDP(17). See Traffic mirror port range documented below
64-
* `protocol` - (Optional) The protocol number, for example 17 (UDP), to assign to the Traffic Mirror rule. For information about the protocol value, see [Protocol Numbers](https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml) on the Internet Assigned Numbers Authority (IANA) website.
65-
* `rule_action` - (Required) The action to take (accept | reject) on the filtered traffic. Valid values are `accept` and `reject`
66-
* `rule_number` - (Required) The number of the Traffic Mirror rule. This number must be unique for each Traffic Mirror rule in a given direction. The rules are processed in ascending order by rule number.
67-
* `source_cidr_block` - (Required) The source CIDR block to assign to the Traffic Mirror rule.
68-
* `source_port_range` - (Optional) The source port range. Supported only when the protocol is set to TCP(6) or UDP(17). See Traffic mirror port range documented below
69-
* `traffic_direction` - (Required) The direction of traffic to be captured. Valid values are `ingress` and `egress`
62+
* `destination_cidr_block` - (Required) Destination CIDR block to assign to the Traffic Mirror rule.
63+
* `destination_port_range` - (Optional) Destination port range. Supported only when the protocol is set to TCP(6) or UDP(17). See Traffic mirror port range documented below
64+
* `protocol` - (Optional) Protocol number, for example 17 (UDP), to assign to the Traffic Mirror rule. For information about the protocol value, see [Protocol Numbers](https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml) on the Internet Assigned Numbers Authority (IANA) website.
65+
* `rule_action` - (Required) Action to take (accept | reject) on the filtered traffic. Valid values are `accept` and `reject`
66+
* `rule_number` - (Required) Number of the Traffic Mirror rule. This number must be unique for each Traffic Mirror rule in a given direction. The rules are processed in ascending order by rule number.
67+
* `source_cidr_block` - (Required) Source CIDR block to assign to the Traffic Mirror rule.
68+
* `source_port_range` - (Optional) Source port range. Supported only when the protocol is set to TCP(6) or UDP(17). See Traffic mirror port range documented below
69+
* `traffic_direction` - (Required) Direction of traffic to be captured. Valid values are `ingress` and `egress`
7070

7171
Traffic mirror port range support following attributes:
7272

@@ -77,7 +77,8 @@ Traffic mirror port range support following attributes:
7777

7878
In addition to all arguments above, the following attributes are exported:
7979

80-
* `id` - The name of the traffic mirror filter rule.
80+
* `arn` - ARN of the traffic mirror filter rule.
81+
* `id` - Name of the traffic mirror filter rule.
8182

8283
## Import
8384

0 commit comments

Comments
 (0)