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_vpc_network_performance_metric_subscription: New resource #28150

Merged
merged 9 commits into from
Dec 5, 2022
3 changes: 3 additions & 0 deletions .changelog/28150.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_vpc_network_performance_metric_subscription
```
1 change: 1 addition & 0 deletions .github/labeler-pr-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ service/vpc:
- 'website/**/vpc_dhcp_*'
- 'website/**/vpc_endpoint*'
- 'website/**/vpc_ipv*'
- 'website/**/vpc_network_performance*'
- 'website/**/vpc_peering_*'
- 'website/**/vpc\.*'
- 'website/**/vpcs\.*'
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.17.0
github.com/aws/aws-sdk-go-v2/service/comprehend v1.20.0
github.com/aws/aws-sdk-go-v2/service/computeoptimizer v1.18.0
github.com/aws/aws-sdk-go-v2/service/ec2 v1.74.0
github.com/aws/aws-sdk-go-v2/service/fis v1.13.3
github.com/aws/aws-sdk-go-v2/service/identitystore v1.15.7
github.com/aws/aws-sdk-go-v2/service/inspector2 v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/aws/aws-sdk-go-v2/service/comprehend v1.20.0 h1:EM659shxckJi2/Z1t1oWw
github.com/aws/aws-sdk-go-v2/service/comprehend v1.20.0/go.mod h1:TF15nEFgTfsELGXN/OvQHFa3dIueBisgKGHEwKVityA=
github.com/aws/aws-sdk-go-v2/service/computeoptimizer v1.18.0 h1:son0BFo2SXZ/yppiJ5KO8ZK2k3p1CDgD5S7LdEF0Pog=
github.com/aws/aws-sdk-go-v2/service/computeoptimizer v1.18.0/go.mod h1:/8UBfpJBoKhDY0+fWZOjGG2ZMt9G1xjdmz9mMZi7K6o=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.74.0 h1:5MCRd9q1yrGoRdYZDxK6y048VNmQ6gKLdCFr+TZsvTY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.74.0/go.mod h1:zul71QqzR4D1a90/5FloZiAnZ1CtuIjVH7R9MP997+A=
github.com/aws/aws-sdk-go-v2/service/fis v1.13.3 h1:9QBGOOqrzobQ8iFRRWN1cK9gmuUHW07DE0Zccspk/ik=
github.com/aws/aws-sdk-go-v2/service/fis v1.13.3/go.mod h1:02kLaeU5zFXqEqRIoLtES7JggFsLdbigOewpUALlIkA=
github.com/aws/aws-sdk-go-v2/service/iam v1.18.4 h1:E41guA79mjEbwJdh0zXz1d8+Zt4zxRr+b1ipiVbKXzs=
Expand Down
6 changes: 6 additions & 0 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/conns/config_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_vpc_ipam_scope": ec2.ResourceIPAMScope(),
"aws_vpc_ipv4_cidr_block_association": ec2.ResourceVPCIPv4CIDRBlockAssociation(),
"aws_vpc_ipv6_cidr_block_association": ec2.ResourceVPCIPv6CIDRBlockAssociation(),
"aws_vpc_network_performance_metric_subscription": ec2.ResourceNetworkPerformanceMetricSubscription(),
"aws_vpc_peering_connection": ec2.ResourceVPCPeeringConnection(),
"aws_vpc_peering_connection_accepter": ec2.ResourceVPCPeeringConnectionAccepter(),
"aws_vpc_peering_connection_options": ec2.ResourceVPCPeeringConnectionOptions(),
Expand Down
40 changes: 40 additions & 0 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"strconv"

aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws"
ec2_sdkv2 "github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"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"
Expand Down Expand Up @@ -6475,3 +6478,40 @@ func FindSnapshotTierStatusBySnapshotID(conn *ec2.EC2, id string) (*ec2.Snapshot

return output, nil
}

func FindNetworkPerformanceMetricSubscriptions(ctx context.Context, conn *ec2_sdkv2.Client, input *ec2_sdkv2.DescribeAwsNetworkPerformanceMetricSubscriptionsInput) ([]types.Subscription, error) {
var output []types.Subscription
paginator := ec2_sdkv2.NewDescribeAwsNetworkPerformanceMetricSubscriptionsPaginator(conn, input, func(o *ec2_sdkv2.DescribeAwsNetworkPerformanceMetricSubscriptionsPaginatorOptions) {
o.Limit = 100
})

for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)

if err != nil {
return nil, err
}

output = append(output, page.Subscriptions...)
}

return output, nil
}

func FindNetworkPerformanceMetricSubscriptionByFourPartKey(ctx context.Context, conn *ec2_sdkv2.Client, source, destination, metric, statistic string) (*types.Subscription, error) {
input := &ec2_sdkv2.DescribeAwsNetworkPerformanceMetricSubscriptionsInput{}

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

if err != nil {
return nil, err
}

for _, v := range output {
if aws_sdkv2.ToString(v.Source) == source && aws_sdkv2.ToString(v.Destination) == destination && string(v.Metric) == metric && string(v.Statistic) == statistic {
return &v, nil
}
}

return nil, &resource.NotFoundError{}
}
50 changes: 50 additions & 0 deletions internal/service/ec2/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ func init() {
Name: "aws_ami",
F: sweepAMIs,
})

// aws_vpc_network_performance_metric_subscription
resource.AddTestSweepers("aws_vpc_network_performance_metric_subscription", &resource.Sweeper{
Name: "aws_vpc_network_performance_metric_subscription",
F: sweepNetworkPerformanceMetricSubscriptions,
})
}

func sweepCapacityReservations(region string) error {
Expand Down Expand Up @@ -2451,3 +2457,47 @@ func sweepAMIs(region string) error {

return nil
}

func sweepNetworkPerformanceMetricSubscriptions(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*conns.AWSClient).EC2Conn
input := &ec2.DescribeAwsNetworkPerformanceMetricSubscriptionsInput{}
sweepResources := make([]sweep.Sweepable, 0)

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

for _, v := range page.Subscriptions {
r := ResourceNetworkPerformanceMetricSubscription()
id := NetworkPerformanceMetricSubscriptionCreateResourceID(aws.StringValue(v.Source), aws.StringValue(v.Destination), aws.StringValue(v.Metric), aws.StringValue(v.Statistic))
d := r.Data(nil)
d.SetId(id)

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping EC2 AWS Network Performance Metric Subscription sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing EC2 AWS Network Performance Metric Subscriptions (%s): %w", region, err)
}

err = sweep.SweepOrchestrator(sweepResources)

if err != nil {
return fmt.Errorf("error sweeping EC2 AWS Network Performance Metric Subscriptions (%s): %w", region, err)
}

return nil
}
155 changes: 155 additions & 0 deletions internal/service/ec2/vpc_network_performance_metric_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package ec2

import (
"context"
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"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/enum"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func ResourceNetworkPerformanceMetricSubscription() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceNetworkPerformanceMetricSubscriptionCreate,
ReadWithoutTimeout: resourceNetworkPerformanceMetricSubscriptionRead,
DeleteWithoutTimeout: resourceNetworkPerformanceMetricSubscriptionDelete,

Schema: map[string]*schema.Schema{
"destination": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"metric": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: types.MetricTypeAggregateLatency,
ValidateDiagFunc: enum.Validate[types.MetricType](),
},
"period": {
Type: schema.TypeString,
Computed: true,
},
"source": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"statistic": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: types.StatisticTypeP50,
ValidateDiagFunc: enum.Validate[types.StatisticType](),
},
},
}
}

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

source := d.Get("source").(string)
destination := d.Get("destination").(string)
metric := d.Get("metric").(string)
statistic := d.Get("statistic").(string)
id := NetworkPerformanceMetricSubscriptionCreateResourceID(source, destination, metric, statistic)
input := &ec2.EnableAwsNetworkPerformanceMetricSubscriptionInput{
Destination: aws.String(destination),
Metric: types.MetricType(metric),
Source: aws.String(source),
Statistic: types.StatisticType(statistic),
}

_, err := conn.EnableAwsNetworkPerformanceMetricSubscription(ctx, input)

if err != nil {
return diag.Errorf("enabling EC2 AWS Network Performance Metric Subscription (%s): %s", id, err)
}

d.SetId(id)

return resourceNetworkPerformanceMetricSubscriptionRead(ctx, d, meta)
}

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

source, destination, metric, statistic, err := NetworkPerformanceMetricSubscriptionResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
}

subscription, err := FindNetworkPerformanceMetricSubscriptionByFourPartKey(ctx, conn, source, destination, metric, statistic)

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

if err != nil {
return diag.Errorf("reading EC2 AWS Network Performance Metric Subscription (%s): %s", d.Id(), err)
}

d.Set("destination", subscription.Destination)
d.Set("metric", subscription.Metric)
d.Set("period", subscription.Period)
d.Set("source", subscription.Source)
d.Set("statistic", subscription.Statistic)

return nil
}

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

source, destination, metric, statistic, err := NetworkPerformanceMetricSubscriptionResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] Deleting EC2 AWS Network Performance Metric Subscriptione: %s", d.Id())
_, err = conn.DisableAwsNetworkPerformanceMetricSubscription(ctx, &ec2.DisableAwsNetworkPerformanceMetricSubscriptionInput{
Destination: aws.String(destination),
Metric: types.MetricType(metric),
Source: aws.String(source),
Statistic: types.StatisticType(statistic),
})

if err != nil {
return diag.Errorf("disabling EC2 AWS Network Performance Metric Subscription (%s): %s", d.Id(), err)
}

return nil
}

const networkPerformanceMetricSubscriptionRuleIDSeparator = "/"

func NetworkPerformanceMetricSubscriptionCreateResourceID(source, destination, metric, statistic string) string {
parts := []string{source, destination, metric, statistic}
id := strings.Join(parts, networkPerformanceMetricSubscriptionRuleIDSeparator)

return id
}

func NetworkPerformanceMetricSubscriptionResourceID(id string) (string, string, string, string, error) {
parts := strings.Split(id, networkPerformanceMetricSubscriptionRuleIDSeparator)

if len(parts) == 4 && parts[0] != "" && parts[1] != "" && parts[2] != "" && parts[3] != "" {
return parts[0], parts[1], parts[2], parts[3], nil
}

return "", "", "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected source%[2]sdestination%[2]smetric%[2]sstatistic", id, networkPerformanceMetricSubscriptionRuleIDSeparator)
}
Loading