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_analysis - new resource #23532

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8135461
r/aws_ec2_network_insights_analysis - new resource
george-richardson Mar 5, 2022
a19b37d
Merge branch 'main' into HEAD
ewbankkit Aug 22, 2022
e8621b8
r/aws_ec2_network_insights_analysis: Correct documentation subcategory.
ewbankkit Aug 22, 2022
624c12a
r/aws_ec2_network_insights_analysis: Rename source files.
ewbankkit Aug 22, 2022
8cd1b4e
Use 'acctest.ProtoV5ProviderFactories'.
ewbankkit Aug 22, 2022
2dca1a0
r/aws_ec2_network_insights_path: Tidy up.
ewbankkit Aug 22, 2022
85c54b7
r/aws_ec2_network_insights_analysis: Use 'WithoutTimeout' CRUD handlers.
ewbankkit Aug 22, 2022
57a9ba5
r/aws_ec2_network_insights_analysis: Add and use 'WaitNetworkInsights…
ewbankkit Aug 22, 2022
1e6416e
r/aws_ec2_network_insights_analysis: Tidy up acceptance tests.
ewbankkit Aug 22, 2022
a2bec96
Fix semgrep 'dgryski.semgrep-go.errnilcheck.err-nil-check'.
ewbankkit Aug 22, 2022
5d94871
Fix terrafmt error.
ewbankkit Aug 22, 2022
01ecf4a
r/aws_ec2_network_insights_analysis: Ensure 'filter_in_arns' elements…
ewbankkit Aug 23, 2022
1037c98
r/aws_ec2_network_insights_analysis: Add 'alternate_path_hints' attri…
ewbankkit Aug 23, 2022
8ce9928
r/aws_ec2_network_insights_analysis: Add 'explanations' attribute.
ewbankkit Aug 23, 2022
6510a08
r/aws_ec2_network_insights_analysis: Add 'forward_path_components' at…
ewbankkit Aug 23, 2022
cdd7abe
r/aws_ec2_network_insights_analysis: Add 'return_path_components' att…
ewbankkit Aug 23, 2022
6c276b6
% make testacc TESTARGS='-run=TestAccVPCNetworkInsightsPath_' PKG=ec2…
ewbankkit Aug 23, 2022
d8e4ce2
Add 'networkInsightsAnalysisPathComponentsSchema'.
ewbankkit Aug 23, 2022
3a82bd9
Add 'networkInsightsAnalysisExplanationsSchema'.
ewbankkit Aug 23, 2022
5fb0945
d/aws_ec2_network_insights_analysis: New data source.
ewbankkit Aug 23, 2022
97744d9
d/aws_ec2_network_insights_analysis: New data source.
ewbankkit Aug 23, 2022
0baad93
Fix semgrep 'ci.caps2-in-func-name'.
ewbankkit Aug 23, 2022
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/23532.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_ec2_network_insights_analysis
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ func New(_ context.Context) (*schema.Provider, error) {
"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_analysis": ec2.ResourceNetworkInsightsAnalysis(),
"aws_ec2_network_insights_path": ec2.ResourceNetworkInsightsPath(),
"aws_ec2_serial_console_access": ec2.ResourceSerialConsoleAccess(),
"aws_ec2_subnet_cidr_reservation": ec2.ResourceSubnetCIDRReservation(),
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 @@ -49,6 +49,7 @@ const (
errCodeInvalidNetworkACLEntryNotFound = "InvalidNetworkAclEntry.NotFound"
errCodeInvalidNetworkACLIDNotFound = "InvalidNetworkAclID.NotFound"
errCodeInvalidNetworkInterfaceIDNotFound = "InvalidNetworkInterfaceID.NotFound"
errCodeInvalidNetworkInsightsAnalysisIdNotFound = "InvalidNetworkInsightsAnalysisId.NotFound"
errCodeInvalidNetworkInsightsPathIdNotFound = "InvalidNetworkInsightsPathId.NotFound"
errCodeInvalidParameter = "InvalidParameter"
errCodeInvalidParameterException = "InvalidParameterException"
Expand Down
82 changes: 76 additions & 6 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,8 +1460,8 @@ func FindNetworkInterfaceSecurityGroup(conn *ec2.EC2, networkInterfaceID string,
}
}

func FindNetworkInsightsPath(conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPathsInput) (*ec2.NetworkInsightsPath, error) {
output, err := FindNetworkInsightsPaths(conn, input)
func FindNetworkInsightsAnalysis(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeNetworkInsightsAnalysesInput) (*ec2.NetworkInsightsAnalysis, error) {
output, err := FindNetworkInsightsAnalyses(ctx, conn, input)

if err != nil {
return nil, err
Expand All @@ -1478,10 +1478,80 @@ func FindNetworkInsightsPath(conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPa
return output[0], nil
}

func FindNetworkInsightsPaths(conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPathsInput) ([]*ec2.NetworkInsightsPath, error) {
func FindNetworkInsightsAnalyses(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeNetworkInsightsAnalysesInput) ([]*ec2.NetworkInsightsAnalysis, error) {
var output []*ec2.NetworkInsightsAnalysis

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

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

return !lastPage
})

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

if err != nil {
return nil, err
}

return output, nil
}

func FindNetworkInsightsAnalysisByID(ctx context.Context, conn *ec2.EC2, id string) (*ec2.NetworkInsightsAnalysis, error) {
input := &ec2.DescribeNetworkInsightsAnalysesInput{
NetworkInsightsAnalysisIds: aws.StringSlice([]string{id}),
}

output, err := FindNetworkInsightsAnalysis(ctx, conn, input)

if err != nil {
return nil, err
}

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

return output, nil
}

func FindNetworkInsightsPath(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPathsInput) (*ec2.NetworkInsightsPath, error) {
output, err := FindNetworkInsightsPaths(ctx, 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(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeNetworkInsightsPathsInput) ([]*ec2.NetworkInsightsPath, error) {
var output []*ec2.NetworkInsightsPath

err := conn.DescribeNetworkInsightsPathsPages(input, func(page *ec2.DescribeNetworkInsightsPathsOutput, lastPage bool) bool {
err := conn.DescribeNetworkInsightsPathsPagesWithContext(ctx, input, func(page *ec2.DescribeNetworkInsightsPathsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
Expand Down Expand Up @@ -1509,12 +1579,12 @@ func FindNetworkInsightsPaths(conn *ec2.EC2, input *ec2.DescribeNetworkInsightsP
return output, nil
}

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

output, err := FindNetworkInsightsPath(conn, input)
output, err := FindNetworkInsightsPath(ctx, conn, input)

if err != nil {
return nil, err
Expand Down
16 changes: 16 additions & 0 deletions internal/service/ec2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,22 @@ func StatusManagedPrefixListState(conn *ec2.EC2, id string) resource.StateRefres
}
}

func StatusNetworkInsightsAnalysis(ctx context.Context, conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindNetworkInsightsAnalysisByID(ctx, conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.Status), nil
}
}

func StatusNetworkInterfaceStatus(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindNetworkInterfaceByID(conn, id)
Expand Down
184 changes: 184 additions & 0 deletions internal/service/ec2/vpc_network_insights_analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package ec2

import (
"context"
"log"
"time"

"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-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
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 ResourceNetworkInsightsAnalysis() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceNetworkInsightsAnalysisCreate,
ReadWithoutTimeout: resourceNetworkInsightsAnalysisRead,
UpdateWithoutTimeout: resourceNetworkInsightsAnalysisUpdate,
DeleteWithoutTimeout: resourceNetworkInsightsAnalysisDelete,

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

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"filter_in_arns": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"network_insights_path_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"path_found": {
Type: schema.TypeBool,
Computed: true,
},
"start_date": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"status_message": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"wait_for_completion": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"warning_message": {
Type: schema.TypeString,
Computed: true,
},
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceNetworkInsightsAnalysisCreate(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.StartNetworkInsightsAnalysisInput{
NetworkInsightsPathId: aws.String(d.Get("network_insights_path_id").(string)),
TagSpecifications: tagSpecificationsFromKeyValueTags(tags, ec2.ResourceTypeNetworkInsightsAnalysis),
}

if v, ok := d.GetOk("filter_in_arns"); ok && v.(*schema.Set).Len() > 0 {
input.FilterInArns = flex.ExpandStringSet(v.(*schema.Set))
}

log.Printf("[DEBUG] Creating EC2 Network Insights Analysis: %s", input)
response, err := conn.StartNetworkInsightsAnalysisWithContext(ctx, input)

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

d.SetId(aws.StringValue(response.NetworkInsightsAnalysis.NetworkInsightsAnalysisId))

if d.Get("wait_for_completion").(bool) {
if _, err := WaitNetworkInsightsAnalysisCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("error waiting for EC2 Network Insights Analysis (%s) create: %s", d.Id(), err)
}
}

return resourceNetworkInsightsAnalysisRead(ctx, d, meta)
}

func resourceNetworkInsightsAnalysisRead(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

nia, err := FindNetworkInsightsAnalysisByID(ctx, conn, d.Id())

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

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

d.Set("arn", nia.NetworkInsightsAnalysisArn)
d.Set("filter_in_arns", aws.StringValueSlice(nia.FilterInArns))
d.Set("network_insights_path_id", nia.NetworkInsightsPathId)
d.Set("path_found", nia.NetworkPathFound)
d.Set("start_date", nia.StartDate.Format(time.RFC3339))
d.Set("status", nia.Status)
d.Set("status_message", nia.StatusMessage)
d.Set("warning_message", nia.WarningMessage)

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

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

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

return nil
}

func resourceNetworkInsightsAnalysisUpdate(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 := UpdateTagsWithContext(ctx, conn, d.Id(), o, n); err != nil {
return diag.Errorf("updating EC2 Network Insights Analysis (%s) tags: %s", d.Id(), err)
}
}

return resourceNetworkInsightsAnalysisRead(ctx, d, meta)
}

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

log.Printf("[DEBUG] Deleting EC2 Network Insights Analysis: %s", d.Id())
_, err := conn.DeleteNetworkInsightsAnalysisWithContext(ctx, &ec2.DeleteNetworkInsightsAnalysisInput{
NetworkInsightsAnalysisId: aws.String(d.Id()),
})

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

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

return nil
}
Loading