Skip to content

Commit cd8b30c

Browse files
committedDec 8, 2022
r/aws_rum_metrics_destination: Switch to 'WithoutTimeout' CRUD handlers (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
1 parent 6e08655 commit cd8b30c

File tree

3 files changed

+71
-65
lines changed

3 files changed

+71
-65
lines changed
 

‎internal/service/rum/find.go

-38
This file was deleted.

‎internal/service/rum/metrics_destination.go

+60-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package rum
22

33
import (
4-
"fmt"
4+
"context"
55
"log"
66

77
"github.com/aws/aws-sdk-go/aws"
88
"github.com/aws/aws-sdk-go/service/cloudwatchrum"
99
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -16,10 +18,11 @@ import (
1618

1719
func ResourceMetricsDestination() *schema.Resource {
1820
return &schema.Resource{
19-
Create: resourceMetricsDestinationPut,
20-
Read: resourceMetricsDestinationRead,
21-
Update: resourceMetricsDestinationPut,
22-
Delete: resourceMetricsDestinationDelete,
21+
CreateWithoutTimeout: resourceMetricsDestinationPut,
22+
ReadWithoutTimeout: resourceMetricsDestinationRead,
23+
UpdateWithoutTimeout: resourceMetricsDestinationPut,
24+
DeleteWithoutTimeout: resourceMetricsDestinationDelete,
25+
2326
Importer: &schema.ResourceImporter{
2427
State: schema.ImportStatePassthrough,
2528
},
@@ -48,7 +51,7 @@ func ResourceMetricsDestination() *schema.Resource {
4851
}
4952
}
5053

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

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

68-
_, err := conn.PutRumMetricsDestination(input)
71+
_, err := conn.PutRumMetricsDestinationWithContext(ctx, input)
6972

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

74-
d.SetId(name)
77+
if d.IsNewResource() {
78+
d.SetId(name)
79+
}
7580

76-
return resourceMetricsDestinationRead(d, meta)
81+
return resourceMetricsDestinationRead(ctx, d, meta)
7782
}
7883

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

82-
dest, err := FindMetricsDestinationByName(conn, d.Id())
87+
dest, err := FindMetricsDestinationByName(ctx, conn, d.Id())
88+
8389
if !d.IsNewResource() && tfresource.NotFound(err) {
84-
log.Printf("[WARN] Unable to find CloudWatch RUM Metric Destination (%s); removing from state", d.Id())
90+
log.Printf("[WARN] CloudWatch RUM Metrics Destination %s not found, removing from state", d.Id())
8591
d.SetId("")
8692
return nil
8793
}
8894

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

93-
d.Set("destination", dest.Destination)
9499
d.Set("app_monitor_name", d.Id())
100+
d.Set("destination", dest.Destination)
95101
d.Set("destination_arn", dest.DestinationArn)
96102
d.Set("iam_role_arn", dest.IamRoleArn)
97103

98104
return nil
99105
}
100106

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

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

113-
if _, err := conn.DeleteRumMetricsDestination(input); err != nil {
114-
if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) {
115-
return nil
116-
}
117-
return fmt.Errorf("error deleting CloudWatch RUM Metric Destination (%s): %w", d.Id(), err)
119+
log.Printf("[DEBUG] Deleting CloudWatch RUM Metrics Destination: %s", d.Id())
120+
_, err := conn.DeleteRumMetricsDestinationWithContext(ctx, input)
121+
122+
if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) {
123+
return nil
124+
}
125+
126+
if err != nil {
127+
return diag.Errorf("deleting CloudWatch RUM Metrics Destination (%s): %s", d.Id(), err)
118128
}
119129

120130
return nil
121131
}
132+
133+
func FindMetricsDestinationByName(ctx context.Context, conn *cloudwatchrum.CloudWatchRUM, name string) (*cloudwatchrum.MetricDestinationSummary, error) {
134+
input := cloudwatchrum.ListRumMetricsDestinationsInput{
135+
AppMonitorName: aws.String(name),
136+
}
137+
138+
output, err := conn.ListRumMetricsDestinations(&input)
139+
140+
if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) {
141+
return nil, &resource.NotFoundError{
142+
LastError: err,
143+
LastRequest: input,
144+
}
145+
}
146+
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
if output == nil || len(output.Destinations) == 0 || output.Destinations[0] == nil {
152+
return nil, tfresource.NewEmptyResultError(input)
153+
}
154+
155+
if count := len(output.Destinations); count > 1 {
156+
return nil, tfresource.NewTooManyResultsError(count, input)
157+
}
158+
159+
return output.Destinations[0], nil
160+
}

‎internal/service/rum/metrics_destination_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rum_test
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67

@@ -98,37 +99,41 @@ func testAccCheckMetricsDestinationDestroy(s *terraform.State) error {
9899
continue
99100
}
100101

101-
_, err := tfcloudwatchrum.FindMetricsDestinationByName(conn, rs.Primary.ID)
102+
_, err := tfcloudwatchrum.FindMetricsDestinationByName(context.Background(), conn, rs.Primary.ID)
103+
102104
if tfresource.NotFound(err) {
103105
continue
104106
}
105107

106108
if err != nil {
107109
return err
108110
}
111+
112+
return fmt.Errorf("CloudWatch RUM Metrics Destination %s still exists", rs.Primary.ID)
109113
}
110114

111115
return nil
112116
}
113117

114-
func testAccCheckMetricsDestinationExists(n string, dest *cloudwatchrum.MetricDestinationSummary) resource.TestCheckFunc {
118+
func testAccCheckMetricsDestinationExists(n string, v *cloudwatchrum.MetricDestinationSummary) resource.TestCheckFunc {
115119
return func(s *terraform.State) error {
116120
rs, ok := s.RootModule().Resources[n]
117121
if !ok {
118122
return fmt.Errorf("Not found: %s", n)
119123
}
120-
121124
if rs.Primary.ID == "" {
122-
return fmt.Errorf("No cloudwatchrum Metrics Destination ID is set")
125+
return fmt.Errorf("No CloudWatch RUM Metrics Destination ID is set")
123126
}
124127

125128
conn := acctest.Provider.Meta().(*conns.AWSClient).RUMConn
126-
resp, err := tfcloudwatchrum.FindMetricsDestinationByName(conn, rs.Primary.ID)
129+
130+
output, err := tfcloudwatchrum.FindMetricsDestinationByName(context.Background(), conn, rs.Primary.ID)
131+
127132
if err != nil {
128133
return err
129134
}
130135

131-
*dest = *resp
136+
*v = *output
132137

133138
return nil
134139
}

0 commit comments

Comments
 (0)