Skip to content

Commit

Permalink
r/aws_rum_metrics_destination: Switch to 'WithoutTimeout' CRUD handle…
Browse files Browse the repository at this point in the history
…rs (hashicorp#15090).

Acceptance test output:

% make testacc TESTARGS='-run=TestAccRUMMetricsDestination_' PKG=rum ACCTEST_PARALLELISM=3
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/rum/... -v -count 1 -parallel 3  -run=TestAccRUMMetricsDestination_ -timeout 180m
=== RUN   TestAccRUMMetricsDestination_basic
=== PAUSE TestAccRUMMetricsDestination_basic
=== RUN   TestAccRUMMetricsDestination_disappears
=== PAUSE TestAccRUMMetricsDestination_disappears
=== RUN   TestAccRUMMetricsDestination_disappears_appMonitor
=== PAUSE TestAccRUMMetricsDestination_disappears_appMonitor
=== CONT  TestAccRUMMetricsDestination_basic
=== CONT  TestAccRUMMetricsDestination_disappears_appMonitor
=== CONT  TestAccRUMMetricsDestination_disappears
--- PASS: TestAccRUMMetricsDestination_disappears_appMonitor (16.24s)
--- PASS: TestAccRUMMetricsDestination_disappears (18.22s)
--- PASS: TestAccRUMMetricsDestination_basic (20.54s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/rum	25.636s
  • Loading branch information
ewbankkit committed Dec 8, 2022
1 parent 6e08655 commit cd8b30c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 65 deletions.
38 changes: 0 additions & 38 deletions internal/service/rum/find.go

This file was deleted.

81 changes: 60 additions & 21 deletions internal/service/rum/metrics_destination.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package rum

import (
"fmt"
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchrum"
"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/resource"
"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"
Expand All @@ -16,10 +18,11 @@ import (

func ResourceMetricsDestination() *schema.Resource {
return &schema.Resource{
Create: resourceMetricsDestinationPut,
Read: resourceMetricsDestinationRead,
Update: resourceMetricsDestinationPut,
Delete: resourceMetricsDestinationDelete,
CreateWithoutTimeout: resourceMetricsDestinationPut,
ReadWithoutTimeout: resourceMetricsDestinationRead,
UpdateWithoutTimeout: resourceMetricsDestinationPut,
DeleteWithoutTimeout: resourceMetricsDestinationDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -48,7 +51,7 @@ func ResourceMetricsDestination() *schema.Resource {
}
}

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

name := d.Get("app_monitor_name").(string)
Expand All @@ -65,40 +68,43 @@ func resourceMetricsDestinationPut(d *schema.ResourceData, meta interface{}) err
input.IamRoleArn = aws.String(v.(string))
}

_, err := conn.PutRumMetricsDestination(input)
_, err := conn.PutRumMetricsDestinationWithContext(ctx, input)

if err != nil {
return fmt.Errorf("error creating CloudWatch RUM Metric Destination %s: %w", name, err)
return diag.Errorf("putting CloudWatch RUM Metrics Destination (%s): %s", name, err)
}

d.SetId(name)
if d.IsNewResource() {
d.SetId(name)
}

return resourceMetricsDestinationRead(d, meta)
return resourceMetricsDestinationRead(ctx, d, meta)
}

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

dest, err := FindMetricsDestinationByName(conn, d.Id())
dest, err := FindMetricsDestinationByName(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Unable to find CloudWatch RUM Metric Destination (%s); removing from state", d.Id())
log.Printf("[WARN] CloudWatch RUM Metrics Destination %s not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading CloudWatch RUM Metric Destination (%s): %w", d.Id(), err)
return diag.Errorf("reading CloudWatch RUM Metrics Destination (%s): %s", d.Id(), err)
}

d.Set("destination", dest.Destination)
d.Set("app_monitor_name", d.Id())
d.Set("destination", dest.Destination)
d.Set("destination_arn", dest.DestinationArn)
d.Set("iam_role_arn", dest.IamRoleArn)

return nil
}

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

input := &cloudwatchrum.DeleteRumMetricsDestinationInput{
Expand All @@ -110,12 +116,45 @@ func resourceMetricsDestinationDelete(d *schema.ResourceData, meta interface{})
input.DestinationArn = aws.String(v.(string))
}

if _, err := conn.DeleteRumMetricsDestination(input); err != nil {
if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) {
return nil
}
return fmt.Errorf("error deleting CloudWatch RUM Metric Destination (%s): %w", d.Id(), err)
log.Printf("[DEBUG] Deleting CloudWatch RUM Metrics Destination: %s", d.Id())
_, err := conn.DeleteRumMetricsDestinationWithContext(ctx, input)

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

if err != nil {
return diag.Errorf("deleting CloudWatch RUM Metrics Destination (%s): %s", d.Id(), err)
}

return nil
}

func FindMetricsDestinationByName(ctx context.Context, conn *cloudwatchrum.CloudWatchRUM, name string) (*cloudwatchrum.MetricDestinationSummary, error) {
input := cloudwatchrum.ListRumMetricsDestinationsInput{
AppMonitorName: aws.String(name),
}

output, err := conn.ListRumMetricsDestinations(&input)

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

if err != nil {
return nil, err
}

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

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

return output.Destinations[0], nil
}
17 changes: 11 additions & 6 deletions internal/service/rum/metrics_destination_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rum_test

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -98,37 +99,41 @@ func testAccCheckMetricsDestinationDestroy(s *terraform.State) error {
continue
}

_, err := tfcloudwatchrum.FindMetricsDestinationByName(conn, rs.Primary.ID)
_, err := tfcloudwatchrum.FindMetricsDestinationByName(context.Background(), conn, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}

return fmt.Errorf("CloudWatch RUM Metrics Destination %s still exists", rs.Primary.ID)
}

return nil
}

func testAccCheckMetricsDestinationExists(n string, dest *cloudwatchrum.MetricDestinationSummary) resource.TestCheckFunc {
func testAccCheckMetricsDestinationExists(n string, v *cloudwatchrum.MetricDestinationSummary) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No cloudwatchrum Metrics Destination ID is set")
return fmt.Errorf("No CloudWatch RUM Metrics Destination ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).RUMConn
resp, err := tfcloudwatchrum.FindMetricsDestinationByName(conn, rs.Primary.ID)

output, err := tfcloudwatchrum.FindMetricsDestinationByName(context.Background(), conn, rs.Primary.ID)

if err != nil {
return err
}

*dest = *resp
*v = *output

return nil
}
Expand Down

0 comments on commit cd8b30c

Please sign in to comment.