Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New resources: Traffic mirroring #9372

Merged
merged 27 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d71e53f
initial commit
johnthedev97 Jul 15, 2019
294f89a
added network service option and update capability to filter and tested
johnthedev97 Jul 15, 2019
aacd9f3
filter and filter rule working
johnthedev97 Jul 16, 2019
5efbfb3
Added mirror target, fixed a typo in filter rule
johnthedev97 Jul 16, 2019
3b6b498
Add mirror session
crjohn Jul 17, 2019
fcf9f8b
First round of completion - functionality, tests and docs
johnthedev97 Jul 21, 2019
f7c1fdb
fix a lint error
johnthedev97 Jul 21, 2019
89987c4
refactor the filter rule to avoid composite ID at the cost of looping…
johnthedev97 Jul 21, 2019
306f82b
Merge branch 'master' into feature/traffic-mirroring
johnthedev97 Jul 28, 2019
ad58bef
restructure the documentation according to new structure
johnthedev97 Jul 28, 2019
b54f538
Merge branch 'master' of https://github.com/terraform-providers/terra…
crjohn Oct 8, 2019
3b01491
Merge branch 'feature/traffic-mirroring' of github.com:johnthedev97/t…
crjohn Nov 19, 2019
6cd9db0
refactored for terraform-plugin-sdk
crjohn Nov 19, 2019
9cf8e98
Merge branch 'master' into feature/traffic-mirroring
crjohn Nov 21, 2019
2100c1c
add subcategory to docs
crjohn Nov 21, 2019
fdd7f92
renaming files and resources
johnthedev97 Feb 17, 2020
96879ec
fix subcategory
johnthedev97 Feb 17, 2020
2fab962
Documentation fixes
johnthedev97 Feb 17, 2020
8e5ba61
Merge branch 'master' into feature/traffic-mirroring
johnthedev97 Feb 17, 2020
5a702cf
fix website lint error
johnthedev97 Feb 17, 2020
0eecb1c
Review fixes for aws_ec2_traffic_mirror_filter
johnthedev97 Feb 17, 2020
1069f81
Update aws/resource_aws_ec2_traffic_mirror_target.go
johnthedev97 Feb 20, 2020
37fd41d
Update aws/resource_aws_ec2_traffic_mirror_filter_rule.go
johnthedev97 Feb 20, 2020
f1211eb
Update aws/resource_aws_ec2_traffic_mirror_session.go
johnthedev97 Feb 20, 2020
445055e
review fixes
johnthedev97 Feb 20, 2020
861b78f
fix spelling mistake
johnthedev97 Feb 20, 2020
9d59e2a
service/ec2: Apply suggestions from #9372 code review
bflad Feb 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ func Provider() terraform.ResourceProvider {
"aws_ec2_client_vpn_endpoint": resourceAwsEc2ClientVpnEndpoint(),
"aws_ec2_client_vpn_network_association": resourceAwsEc2ClientVpnNetworkAssociation(),
"aws_ec2_fleet": resourceAwsEc2Fleet(),
"aws_ec2_traffic_mirror_filter": resourceAwsEc2TrafficMirrorFilter(),
"aws_ec2_traffic_mirror_filter_rule": resourceAwsEc2TrafficMirrorFilterRule(),
"aws_ec2_traffic_mirror_target": resourceAwsEc2TrafficMirrorTarget(),
"aws_ec2_traffic_mirror_session": resourceAwsEc2TrafficMirrorSession(),
"aws_ec2_transit_gateway": resourceAwsEc2TransitGateway(),
"aws_ec2_transit_gateway_route": resourceAwsEc2TransitGatewayRoute(),
"aws_ec2_transit_gateway_route_table": resourceAwsEc2TransitGatewayRouteTable(),
Expand Down
131 changes: 131 additions & 0 deletions aws/resource_aws_ec2_traffic_mirror_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package aws

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceAwsEc2TrafficMirrorFilter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEc2TrafficMirrorinFilterCreate,
Read: resourceAwsEc2TrafficMirrorFilterRead,
Update: resourceAwsEc2TrafficMirrorFilterUpdate,
Delete: resourceAwsEc2TrafficMirrorFilterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"network_services": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"amazon-dns",
}, false),
},
},
},
}
}

func resourceAwsEc2TrafficMirrorinFilterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

input := &ec2.CreateTrafficMirrorFilterInput{}

if description, ok := d.GetOk("description"); ok {
input.Description = aws.String(description.(string))
}

out, err := conn.CreateTrafficMirrorFilter(input)
if err != nil {
return fmt.Errorf("Error while creating traffic filter %s", err)
}

d.SetId(*out.TrafficMirrorFilter.TrafficMirrorFilterId)

return resourceAwsEc2TrafficMirrorFilterUpdate(d, meta)
}

func resourceAwsEc2TrafficMirrorFilterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

if d.HasChange("network_services") {
input := &ec2.ModifyTrafficMirrorFilterNetworkServicesInput{
TrafficMirrorFilterId: aws.String(d.Id()),
}

o, n := d.GetChange("network_services")
newServices := n.(*schema.Set).Difference(o.(*schema.Set)).List()
if len(newServices) > 0 {
input.AddNetworkServices = expandStringList(newServices)
}

removeServices := o.(*schema.Set).Difference(n.(*schema.Set)).List()
if len(removeServices) > 0 {
input.RemoveNetworkServices = expandStringList(removeServices)
}

_, err := conn.ModifyTrafficMirrorFilterNetworkServices(input)
if err != nil {
return fmt.Errorf("Error modifying network services for traffic mirror filter %v", d.Id())
bflad marked this conversation as resolved.
Show resolved Hide resolved
}
}

return resourceAwsEc2TrafficMirrorFilterRead(d, meta)
}

func resourceAwsEc2TrafficMirrorFilterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

input := &ec2.DescribeTrafficMirrorFiltersInput{
TrafficMirrorFilterIds: aws.StringSlice([]string{d.Id()}),
}

out, err := conn.DescribeTrafficMirrorFilters(input)
if err != nil {
return fmt.Errorf("Error describing traffic mirror filter %v: %v", d.Id(), err)
}

if len(out.TrafficMirrorFilters) == 0 {
log.Printf("[WARN] EC2 Traffic Mirror Filter (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.SetId(*out.TrafficMirrorFilters[0].TrafficMirrorFilterId)
d.Set("description", out.TrafficMirrorFilters[0].Description)

if err := d.Set("network_services", aws.StringValueSlice(out.TrafficMirrorFilters[0].NetworkServices)); err != nil {
return fmt.Errorf("error setting network_services for filter %v: %s", d.Id(), err)
}

return nil
}

func resourceAwsEc2TrafficMirrorFilterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

input := &ec2.DeleteTrafficMirrorFilterInput{
TrafficMirrorFilterId: aws.String(d.Id()),
}

_, err := conn.DeleteTrafficMirrorFilter(input)
if err != nil {
return fmt.Errorf("Error deleting traffic mirror filter %v: %v", d.Id(), err)
}

return nil
}
Loading