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

r/aws_ec2_network_insights_path - new resource #23330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .changelog/23330.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_ec2_network_insights_path
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ func Provider() *schema.Provider {
"aws_ec2_local_gateway_route_table_vpc_association": ec2.ResourceLocalGatewayRouteTableVPCAssociation(),
"aws_ec2_managed_prefix_list": ec2.ResourceManagedPrefixList(),
"aws_ec2_managed_prefix_list_entry": ec2.ResourceManagedPrefixListEntry(),
"aws_ec2_network_insights_path": ec2.ResourceNetworkInsightsPath(),
"aws_ec2_subnet_cidr_reservation": ec2.ResourceSubnetCIDRReservation(),
"aws_ec2_tag": ec2.ResourceTag(),
"aws_ec2_traffic_mirror_filter": ec2.ResourceTrafficMirrorFilter(),
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
ErrCodeInvalidNetworkAclEntryNotFound = "InvalidNetworkAclEntry.NotFound"
ErrCodeInvalidNetworkAclIDNotFound = "InvalidNetworkAclID.NotFound"
ErrCodeInvalidNetworkInterfaceIDNotFound = "InvalidNetworkInterfaceID.NotFound"
ErrCodeInvalidNetworkInsightsPathIdNotFound = "InvalidNetworkInsightsPathId.NotFound"
ErrCodeInvalidParameter = "InvalidParameter"
ErrCodeInvalidParameterException = "InvalidParameterException"
ErrCodeInvalidParameterValue = "InvalidParameterValue"
Expand Down
70 changes: 70 additions & 0 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,76 @@ func FindNetworkInterfaceSecurityGroup(conn *ec2.EC2, networkInterfaceID string,
}
}

func FindNetworkInsightsPath(conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPathsInput) (*ec2.NetworkInsightsPath, error) {
output, err := FindNetworkInsightsPaths(conn, input)

if err != nil {
return nil, err
}

if len(output) == 0 || output[0] == nil {
return nil, tfresource.NewEmptyResultError(input)
}

if count := len(output); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
}

return output[0], nil
}

func FindNetworkInsightsPaths(conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPathsInput) ([]*ec2.NetworkInsightsPath, error) {
var output []*ec2.NetworkInsightsPath

err := conn.DescribeNetworkInsightsPathsPages(input, func(page *ec2.DescribeNetworkInsightsPathsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.NetworkInsightsPaths {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, ErrCodeInvalidNetworkInsightsPathIdNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
}

func FindNetworkInsightsPathByID(conn *ec2.EC2, id string) (*ec2.NetworkInsightsPath, error) {
input := &ec2.DescribeNetworkInsightsPathsInput{
NetworkInsightsPathIds: aws.StringSlice([]string{id}),
}

output, err := FindNetworkInsightsPath(conn, input)

if err != nil {
return nil, err
}

// Eventual consistency check.
if aws.StringValue(output.NetworkInsightsPathId) != id {
return nil, &resource.NotFoundError{
LastRequest: input,
}
}

return output, nil
}

// FindMainRouteTableAssociationByID returns the main route table association corresponding to the specified identifier.
// Returns NotFoundError if no route table association is found.
func FindMainRouteTableAssociationByID(conn *ec2.EC2, associationID string) (*ec2.RouteTableAssociation, error) {
Expand Down
180 changes: 180 additions & 0 deletions internal/service/ec2/network_insights_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package ec2

import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceNetworkInsightsPath() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceNetworkInsightsPathCreate,
ReadWithoutTimeout: resourceNetworkInsightsPathRead,
UpdateWithoutTimeout: resourceNetworkInsightsPathUpdate,
DeleteWithoutTimeout: resourceNetworkInsightsPathDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"destination": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_ip": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"destination_port": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ec2.Protocol_Values(), false),
},
"source": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"source_ip": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceNetworkInsightsPathCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EC2Conn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

input := &ec2.CreateNetworkInsightsPathInput{
Destination: aws.String(d.Get("destination").(string)),
Protocol: aws.String(d.Get("protocol").(string)),
Source: aws.String(d.Get("source").(string)),
TagSpecifications: ec2TagSpecificationsFromKeyValueTags(tags, ec2.ResourceTypeNetworkInsightsPath),
}

if v, ok := d.GetOk("destination_ip"); ok {
input.DestinationIp = aws.String(v.(string))
}

if v, ok := d.GetOk("destination_port"); ok {
input.DestinationPort = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("source_ip"); ok {
input.SourceIp = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating EC2 Network Insights Path: %s", input)
output, err := conn.CreateNetworkInsightsPathWithContext(ctx, input)

if err != nil {
return diag.Errorf("error creating EC2 Network Insights Path: %s", err)
}

d.SetId(aws.StringValue(output.NetworkInsightsPath.NetworkInsightsPathId))

return resourceNetworkInsightsPathRead(ctx, d, meta)
}

func resourceNetworkInsightsPathRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EC2Conn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

nip, err := FindNetworkInsightsPathByID(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EC2 Network Insights Path %s not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.Errorf("error reading EC2 Network Insights Path (%s): %s", d.Id(), err)
}

d.Set("arn", nip.NetworkInsightsPathArn)
d.Set("destination", nip.Destination)
d.Set("destination_ip", nip.DestinationIp)
d.Set("destination_port", nip.DestinationPort)
d.Set("protocol", nip.Protocol)
d.Set("source", nip.Source)
d.Set("source_ip", nip.SourceIp)

tags := KeyValueTags(nip.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("error setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("error setting tags_all: %s", err)
}

return nil
}

func resourceNetworkInsightsPathUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EC2Conn

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

if err := UpdateTags(conn, d.Id(), o, n); err != nil {
return diag.Errorf("error updating EC2 Network Insights Path (%s) tags: %s", d.Id(), err)
}
}

return resourceNetworkInsightsPathRead(ctx, d, meta)
}

func resourceNetworkInsightsPathDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EC2Conn

log.Printf("[DEBUG] Deleting EC2 Network Insights Path: %s", d.Id())
_, err := conn.DeleteNetworkInsightsPathWithContext(ctx, &ec2.DeleteNetworkInsightsPathInput{
NetworkInsightsPathId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, ErrCodeInvalidNetworkInsightsPathIdNotFound) {
return nil
}

if err != nil {
return diag.Errorf("error deleting EC2 Network Insights Path (%s): %s", d.Id(), err)
}

return nil
}
Loading