diff --git a/internal/service/rum/app_monitor.go b/internal/service/rum/app_monitor.go index 7e6d231240a..240491d0100 100644 --- a/internal/service/rum/app_monitor.go +++ b/internal/service/rum/app_monitor.go @@ -1,6 +1,7 @@ package rum import ( + "context" "fmt" "log" @@ -8,6 +9,8 @@ import ( "github.com/aws/aws-sdk-go/aws/arn" "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" @@ -19,10 +22,11 @@ import ( func ResourceAppMonitor() *schema.Resource { return &schema.Resource{ - Create: resourceAppMonitorCreate, - Read: resourceAppMonitorRead, - Update: resourceAppMonitorUpdate, - Delete: resourceAppMonitorDelete, + CreateWithoutTimeout: resourceAppMonitorCreate, + ReadWithoutTimeout: resourceAppMonitorRead, + UpdateWithoutTimeout: resourceAppMonitorUpdate, + DeleteWithoutTimeout: resourceAppMonitorDelete, + Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -122,7 +126,7 @@ func ResourceAppMonitor() *schema.Resource { } } -func resourceAppMonitorCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAppMonitorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).RUMConn defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{}))) @@ -142,35 +146,36 @@ func resourceAppMonitorCreate(d *schema.ResourceData, meta interface{}) error { input.Tags = Tags(tags.IgnoreAWS()) } - _, err := conn.CreateAppMonitor(input) + _, err := conn.CreateAppMonitorWithContext(ctx, input) if err != nil { - return fmt.Errorf("error creating CloudWatch RUM App Monitor %s: %w", name, err) + return diag.Errorf("creating CloudWatch RUM App Monitor (%s): %s", name, err) } d.SetId(name) - return resourceAppMonitorRead(d, meta) + return resourceAppMonitorRead(ctx, d, meta) } -func resourceAppMonitorRead(d *schema.ResourceData, meta interface{}) error { +func resourceAppMonitorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).RUMConn defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - appMon, err := FindAppMonitorByName(conn, d.Id()) + appMon, err := FindAppMonitorByName(ctx, conn, d.Id()) + if !d.IsNewResource() && tfresource.NotFound(err) { - log.Printf("[WARN] Unable to find CloudWatch RUM App Monitor (%s); removing from state", d.Id()) + log.Printf("[WARN] CloudWatch RUM App Monitor %s not found, removing from state", d.Id()) d.SetId("") return nil } if err != nil { - return fmt.Errorf("error reading CloudWatch RUM App Monitor (%s): %w", d.Id(), err) + return diag.Errorf("reading CloudWatch RUM App Monitor (%s): %s", d.Id(), err) } if err := d.Set("app_monitor_configuration", []interface{}{flattenAppMonitorConfiguration(appMon.AppMonitorConfiguration)}); err != nil { - return fmt.Errorf("setting app_monitor_configuration: %w", err) + return diag.Errorf("setting app_monitor_configuration: %s", err) } d.Set("app_monitor_id", appMon.Id) arn := arn.ARN{ @@ -190,17 +195,17 @@ func resourceAppMonitorRead(d *schema.ResourceData, meta interface{}) error { //lintignore:AWSR002 if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { - return fmt.Errorf("error setting tags: %w", err) + return diag.Errorf("setting tags: %s", err) } if err := d.Set("tags_all", tags.Map()); err != nil { - return fmt.Errorf("error setting tags_all: %w", err) + return diag.Errorf("setting tags_all: %s", err) } return nil } -func resourceAppMonitorUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAppMonitorUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).RUMConn if d.HasChangesExcept("tags", "tags_all") { @@ -208,53 +213,80 @@ func resourceAppMonitorUpdate(d *schema.ResourceData, meta interface{}) error { Name: aws.String(d.Id()), } - if d.HasChange("cw_log_enabled") { - input.CwLogEnabled = aws.Bool(d.Get("cw_log_enabled").(bool)) - } - if d.HasChange("app_monitor_configuration") { input.AppMonitorConfiguration = expandAppMonitorConfiguration(d.Get("app_monitor_configuration").([]interface{})[0].(map[string]interface{})) } + if d.HasChange("cw_log_enabled") { + input.CwLogEnabled = aws.Bool(d.Get("cw_log_enabled").(bool)) + } + if d.HasChange("domain") { input.Domain = aws.String(d.Get("domain").(string)) } - log.Printf("[DEBUG] cloudwatchrum AppMonitor update config: %s", input.String()) - _, err := conn.UpdateAppMonitor(input) + _, err := conn.UpdateAppMonitorWithContext(ctx, input) + if err != nil { - return fmt.Errorf("error updating CloudWatch RUM App Monitor: %w", err) + return diag.Errorf("updating CloudWatch RUM App Monitor (%s): %s", d.Id(), err) } } if d.HasChange("tags_all") { o, n := d.GetChange("tags_all") - if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil { - return fmt.Errorf("error updating CloudWatch RUM App Monitor (%s) tags: %w", d.Id(), err) + if err := UpdateTagsWithContext(ctx, conn, d.Get("arn").(string), o, n); err != nil { + return diag.Errorf("updating CloudWatch RUM App Monitor (%s) tags: %s", d.Id(), err) } } - return resourceAppMonitorRead(d, meta) + return resourceAppMonitorRead(ctx, d, meta) } -func resourceAppMonitorDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAppMonitorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).RUMConn - input := &cloudwatchrum.DeleteAppMonitorInput{ + log.Printf("[DEBUG] Deleting CloudWatch RUM App Monitor: %s", d.Id()) + _, err := conn.DeleteAppMonitorWithContext(ctx, &cloudwatchrum.DeleteAppMonitorInput{ Name: aws.String(d.Id()), + }) + + if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) { + return nil } - if _, err := conn.DeleteAppMonitor(input); err != nil { - if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) { - return nil - } - return fmt.Errorf("error deleting CloudWatch RUM App Monitor (%s): %w", d.Id(), err) + if err != nil { + return diag.Errorf("deleting CloudWatch RUM App Monitor (%s): %s", d.Id(), err) } return nil } +func FindAppMonitorByName(ctx context.Context, conn *cloudwatchrum.CloudWatchRUM, name string) (*cloudwatchrum.AppMonitor, error) { + input := &cloudwatchrum.GetAppMonitorInput{ + Name: aws.String(name), + } + + output, err := conn.GetAppMonitorWithContext(ctx, input) + + if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) { + return nil, &resource.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil || output.AppMonitor == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.AppMonitor, nil +} + func expandAppMonitorConfiguration(tfMap map[string]interface{}) *cloudwatchrum.AppMonitorConfiguration { if tfMap == nil { return nil diff --git a/internal/service/rum/app_monitor_test.go b/internal/service/rum/app_monitor_test.go index 9bcfc7387b7..60cedb64d5b 100644 --- a/internal/service/rum/app_monitor_test.go +++ b/internal/service/rum/app_monitor_test.go @@ -1,10 +1,10 @@ package rum_test import ( + "context" "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudwatchrum" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -141,7 +141,8 @@ func testAccCheckAppMonitorDestroy(s *terraform.State) error { continue } - appMon, err := tfcloudwatchrum.FindAppMonitorByName(conn, rs.Primary.ID) + _, err := tfcloudwatchrum.FindAppMonitorByName(context.Background(), conn, rs.Primary.ID) + if tfresource.NotFound(err) { continue } @@ -150,32 +151,31 @@ func testAccCheckAppMonitorDestroy(s *terraform.State) error { return err } - if aws.StringValue(appMon.Name) == rs.Primary.ID { - return fmt.Errorf("cloudwatchrum App Monitor %q still exists", rs.Primary.ID) - } + return fmt.Errorf("CloudWatch RUM App Monitor %s still exists", rs.Primary.ID) } return nil } -func testAccCheckAppMonitorExists(n string, appMon *cloudwatchrum.AppMonitor) resource.TestCheckFunc { +func testAccCheckAppMonitorExists(n string, v *cloudwatchrum.AppMonitor) 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 App Monitor ID is set") + return fmt.Errorf("No CloudWatch RUM App Monitor ID is set") } conn := acctest.Provider.Meta().(*conns.AWSClient).RUMConn - resp, err := tfcloudwatchrum.FindAppMonitorByName(conn, rs.Primary.ID) + + output, err := tfcloudwatchrum.FindAppMonitorByName(context.Background(), conn, rs.Primary.ID) + if err != nil { return err } - *appMon = *resp + *v = *output return nil } diff --git a/internal/service/rum/find.go b/internal/service/rum/find.go index 0fc028f449f..8a1927e3460 100644 --- a/internal/service/rum/find.go +++ b/internal/service/rum/find.go @@ -8,31 +8,6 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindAppMonitorByName(conn *cloudwatchrum.CloudWatchRUM, name string) (*cloudwatchrum.AppMonitor, error) { - input := cloudwatchrum.GetAppMonitorInput{ - Name: aws.String(name), - } - - output, err := conn.GetAppMonitor(&input) - - if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) { - return nil, &resource.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return nil, err - } - - if output == nil || output.AppMonitor == nil { - return nil, tfresource.NewEmptyResultError(input) - } - - return output.AppMonitor, nil -} - func FindMetricsDestinationByName(conn *cloudwatchrum.CloudWatchRUM, name string) (*cloudwatchrum.MetricDestinationSummary, error) { input := cloudwatchrum.ListRumMetricsDestinationsInput{ AppMonitorName: aws.String(name),