Skip to content

Commit

Permalink
Merge pull request #23532 from george-richardson/f-aws_ec2_network_in…
Browse files Browse the repository at this point in the history
…sights_analysis-new-resource

r/aws_ec2_network_insights_analysis - new resource
  • Loading branch information
ewbankkit committed Aug 23, 2022
2 parents b67c387 + 0baad93 commit ba986c9
Show file tree
Hide file tree
Showing 17 changed files with 3,224 additions and 178 deletions.
11 changes: 11 additions & 0 deletions .changelog/23532.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:new-resource
aws_ec2_network_insights_analysis
```

```release-note:new-data-source
aws_ec2_network_insights_path
```

```release-note:new-data-source
aws_ec2_network_insights_analysis
```
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_ec2_local_gateway": ec2.DataSourceLocalGateway(),
"aws_ec2_local_gateways": ec2.DataSourceLocalGateways(),
"aws_ec2_managed_prefix_list": ec2.DataSourceManagedPrefixList(),
"aws_ec2_network_insights_analysis": ec2.DataSourceNetworkInsightsAnalysis(),
"aws_ec2_network_insights_path": ec2.DataSourceNetworkInsightsPath(),
"aws_ec2_serial_console_access": ec2.DataSourceSerialConsoleAccess(),
"aws_ec2_spot_price": ec2.DataSourceSpotPrice(),
"aws_ec2_transit_gateway": ec2.DataSourceTransitGateway(),
Expand Down Expand Up @@ -1324,6 +1326,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
Loading

0 comments on commit ba986c9

Please sign in to comment.