Skip to content

Commit

Permalink
resource/aws_ec2_traffic_mirror_target: Add tags argument and networ…
Browse files Browse the repository at this point in the history
…k_load_balancer_arn plan-time validation (#12135)

Output from acceptance testing:

```
--- PASS: TestAccAWSEc2TrafficMirrorTarget_eni (74.21s)
--- PASS: TestAccAWSEc2TrafficMirrorTarget_tags (377.38s)
--- PASS: TestAccAWSEc2TrafficMirrorTarget_disappears (591.15s)
--- PASS: TestAccAWSEc2TrafficMirrorTarget_nlb (750.29s)
```
  • Loading branch information
DrFaust92 authored Mar 9, 2020
1 parent 6b8cd7f commit dc2e449
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 39 deletions.
26 changes: 26 additions & 0 deletions aws/resource_aws_ec2_traffic_mirror_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsEc2TrafficMirrorTarget() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEc2TrafficMirrorTargetCreate,
Read: resourceAwsEc2TrafficMirrorTargetRead,
Update: resourceAwsEc2TrafficMirrorTargetUpdate,
Delete: resourceAwsEc2TrafficMirrorTargetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -40,7 +42,9 @@ func resourceAwsEc2TrafficMirrorTarget() *schema.Resource {
"network_interface_id",
"network_load_balancer_arn",
},
ValidateFunc: validateArn,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -61,6 +65,10 @@ func resourceAwsEc2TrafficMirrorTargetCreate(d *schema.ResourceData, meta interf
input.NetworkLoadBalancerArn = aws.String(v.(string))
}

if v, ok := d.GetOk("tags"); ok {
input.TagSpecifications = ec2TagSpecificationsFromMap(v.(map[string]interface{}), ec2.ResourceTypeTrafficMirrorTarget)
}

out, err := conn.CreateTrafficMirrorTarget(input)
if err != nil {
return fmt.Errorf("Error creating traffic mirror target %v", err)
Expand All @@ -71,6 +79,20 @@ func resourceAwsEc2TrafficMirrorTargetCreate(d *schema.ResourceData, meta interf
return resourceAwsEc2TrafficMirrorTargetRead(d, meta)
}

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

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.Ec2UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating EC2 Traffic Mirror Target (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsEc2TrafficMirrorTargetRead(d, meta)
}

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

Expand Down Expand Up @@ -101,6 +123,10 @@ func resourceAwsEc2TrafficMirrorTargetRead(d *schema.ResourceData, meta interfac
d.Set("network_interface_id", target.NetworkInterfaceId)
d.Set("network_load_balancer_arn", target.NetworkLoadBalancerArn)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(target.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

Expand Down
Loading

0 comments on commit dc2e449

Please sign in to comment.