Skip to content

Commit

Permalink
rum: Migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Apr 17, 2024
1 parent 9198fc2 commit 26cb588
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 169 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.21.4
github.com/aws/aws-sdk-go-v2/service/rolesanywhere v1.10.0
github.com/aws/aws-sdk-go-v2/service/route53domains v1.23.4
github.com/aws/aws-sdk-go-v2/service/rum v1.17.5
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/aws/aws-sdk-go-v2/service/s3control v1.44.5
github.com/aws/aws-sdk-go-v2/service/scheduler v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ github.com/aws/aws-sdk-go-v2/service/rolesanywhere v1.10.0 h1:3RsRnhgYzD05lB9md8
github.com/aws/aws-sdk-go-v2/service/rolesanywhere v1.10.0/go.mod h1:RHFmC9Lds1jS1zBvnFq1GDh3yxFXH++n+2sI9TE53Cc=
github.com/aws/aws-sdk-go-v2/service/route53domains v1.23.4 h1:Qb7EiHvGJZGU43aCMahEJrP5sJjV62gGXm4y9x/syRQ=
github.com/aws/aws-sdk-go-v2/service/route53domains v1.23.4/go.mod h1:8wjITSWOCR+G7DhS2WraZnZ/geFYxXLLP0KKTfZtRGQ=
github.com/aws/aws-sdk-go-v2/service/rum v1.17.5 h1:yTDh3ERn4AuSDTpvllwQheAaE6ftpZlQ5B7mHl1FTdM=
github.com/aws/aws-sdk-go-v2/service/rum v1.17.5/go.mod h1:fMEu5ZagfMbhl64YhiY+zfL7wOkY0q2W4rZT43f+MIw=
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1 h1:6cnno47Me9bRykw9AEv9zkXE+5or7jz8TsskTTccbgc=
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1/go.mod h1:qmdkIIAC+GCLASF7R2whgNrJADz0QZPX+Seiw/i4S3o=
github.com/aws/aws-sdk-go-v2/service/s3control v1.44.5 h1:5IGyIMqxk5otMwIj+vYWyyegacuC/Y7NxoRzjfQOzCk=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

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

98 changes: 48 additions & 50 deletions internal/service/rum/app_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/rum"
awstypes "github.com/aws/aws-sdk-go-v2/service/rum/types"
"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/retry"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -90,8 +92,8 @@ func ResourceAppMonitor() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(cloudwatchrum.Telemetry_Values(), false),
Type: schema.TypeString,
ValidateDiagFunc: enum.Validate[awstypes.Telemetry](),
},
},
},
Expand All @@ -113,10 +115,10 @@ func ResourceAppMonitor() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Optional: true,
Default: cloudwatchrum.CustomEventsStatusDisabled,
ValidateFunc: validation.StringInSlice(cloudwatchrum.CustomEventsStatus_Values(), false),
Type: schema.TypeString,
Optional: true,
Default: awstypes.CustomEventsStatusDisabled,
ValidateDiagFunc: enum.Validate[awstypes.CustomEventsStatus](),
},
},
},
Expand Down Expand Up @@ -149,10 +151,10 @@ func ResourceAppMonitor() *schema.Resource {
}

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

name := d.Get("name").(string)
input := &cloudwatchrum.CreateAppMonitorInput{
input := &rum.CreateAppMonitorInput{
Name: aws.String(name),
CwLogEnabled: aws.Bool(d.Get("cw_log_enabled").(bool)),
Domain: aws.String(d.Get("domain").(string)),
Expand All @@ -167,7 +169,7 @@ func resourceAppMonitorCreate(ctx context.Context, d *schema.ResourceData, meta
input.CustomEvents = expandCustomEvents(v.([]interface{})[0].(map[string]interface{}))
}

_, err := conn.CreateAppMonitorWithContext(ctx, input)
_, err := conn.CreateAppMonitor(ctx, input)

if err != nil {
return diag.Errorf("creating CloudWatch RUM App Monitor (%s): %s", name, err)
Expand All @@ -179,7 +181,7 @@ func resourceAppMonitorCreate(ctx context.Context, d *schema.ResourceData, meta
}

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

appMon, err := FindAppMonitorByName(ctx, conn, d.Id())

Expand All @@ -206,7 +208,7 @@ func resourceAppMonitorRead(ctx context.Context, d *schema.ResourceData, meta in
AccountID: meta.(*conns.AWSClient).AccountID,
Partition: meta.(*conns.AWSClient).Partition,
Region: meta.(*conns.AWSClient).Region,
Resource: fmt.Sprintf("appmonitor/%s", aws.StringValue(appMon.Name)),
Resource: fmt.Sprintf("appmonitor/%s", aws.ToString(appMon.Name)),
Service: "rum",
}.String()
d.Set("arn", arn)
Expand All @@ -221,10 +223,10 @@ func resourceAppMonitorRead(ctx context.Context, d *schema.ResourceData, meta in
}

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

if d.HasChangesExcept("tags", "tags_all") {
input := &cloudwatchrum.UpdateAppMonitorInput{
input := &rum.UpdateAppMonitorInput{
Name: aws.String(d.Id()),
}

Expand All @@ -244,7 +246,7 @@ func resourceAppMonitorUpdate(ctx context.Context, d *schema.ResourceData, meta
input.Domain = aws.String(d.Get("domain").(string))
}

_, err := conn.UpdateAppMonitorWithContext(ctx, input)
_, err := conn.UpdateAppMonitor(ctx, input)

if err != nil {
return diag.Errorf("updating CloudWatch RUM App Monitor (%s): %s", d.Id(), err)
Expand All @@ -255,14 +257,14 @@ func resourceAppMonitorUpdate(ctx context.Context, d *schema.ResourceData, meta
}

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

log.Printf("[DEBUG] Deleting CloudWatch RUM App Monitor: %s", d.Id())
_, err := conn.DeleteAppMonitorWithContext(ctx, &cloudwatchrum.DeleteAppMonitorInput{
_, err := conn.DeleteAppMonitor(ctx, &rum.DeleteAppMonitorInput{
Name: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil
}

Expand All @@ -273,14 +275,14 @@ func resourceAppMonitorDelete(ctx context.Context, d *schema.ResourceData, meta
return nil
}

func FindAppMonitorByName(ctx context.Context, conn *cloudwatchrum.CloudWatchRUM, name string) (*cloudwatchrum.AppMonitor, error) {
input := &cloudwatchrum.GetAppMonitorInput{
func FindAppMonitorByName(ctx context.Context, conn *rum.Client, name string) (*awstypes.AppMonitor, error) {
input := &rum.GetAppMonitorInput{
Name: aws.String(name),
}

output, err := conn.GetAppMonitorWithContext(ctx, input)
output, err := conn.GetAppMonitor(ctx, input)

if tfawserr.ErrCodeEquals(err, cloudwatchrum.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand All @@ -298,12 +300,12 @@ func FindAppMonitorByName(ctx context.Context, conn *cloudwatchrum.CloudWatchRUM
return output.AppMonitor, nil
}

func expandAppMonitorConfiguration(tfMap map[string]interface{}) *cloudwatchrum.AppMonitorConfiguration {
func expandAppMonitorConfiguration(tfMap map[string]interface{}) *awstypes.AppMonitorConfiguration {
if tfMap == nil {
return nil
}

config := &cloudwatchrum.AppMonitorConfiguration{}
config := &awstypes.AppMonitorConfiguration{}

if v, ok := tfMap["guest_role_arn"].(string); ok && v != "" {
config.GuestRoleArn = aws.String(v)
Expand All @@ -314,7 +316,7 @@ func expandAppMonitorConfiguration(tfMap map[string]interface{}) *cloudwatchrum.
}

if v, ok := tfMap["session_sample_rate"].(float64); ok {
config.SessionSampleRate = aws.Float64(v)
config.SessionSampleRate = v
}

if v, ok := tfMap["allow_cookies"].(bool); ok {
Expand All @@ -326,94 +328,90 @@ func expandAppMonitorConfiguration(tfMap map[string]interface{}) *cloudwatchrum.
}

if v, ok := tfMap["excluded_pages"].(*schema.Set); ok && v.Len() > 0 {
config.ExcludedPages = flex.ExpandStringSet(v)
config.ExcludedPages = flex.ExpandStringValueSet(v)
}

if v, ok := tfMap["favorite_pages"].(*schema.Set); ok && v.Len() > 0 {
config.FavoritePages = flex.ExpandStringSet(v)
config.FavoritePages = flex.ExpandStringValueSet(v)
}

if v, ok := tfMap["included_pages"].(*schema.Set); ok && v.Len() > 0 {
config.IncludedPages = flex.ExpandStringSet(v)
config.IncludedPages = flex.ExpandStringValueSet(v)
}

if v, ok := tfMap["telemetries"].(*schema.Set); ok && v.Len() > 0 {
config.Telemetries = flex.ExpandStringSet(v)
config.Telemetries = flex.ExpandStringyValueSet[awstypes.Telemetry](v)
}

return config
}

func flattenAppMonitorConfiguration(apiObject *cloudwatchrum.AppMonitorConfiguration) map[string]interface{} {
func flattenAppMonitorConfiguration(apiObject *awstypes.AppMonitorConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.GuestRoleArn; v != nil {
tfMap["guest_role_arn"] = aws.StringValue(v)
tfMap["guest_role_arn"] = aws.ToString(v)
}

if v := apiObject.IdentityPoolId; v != nil {
tfMap["identity_pool_id"] = aws.StringValue(v)
tfMap["identity_pool_id"] = aws.ToString(v)
}

if v := apiObject.SessionSampleRate; v != nil {
tfMap["session_sample_rate"] = aws.Float64Value(v)
}
tfMap["session_sample_rate"] = apiObject.SessionSampleRate

if v := apiObject.AllowCookies; v != nil {
tfMap["allow_cookies"] = aws.BoolValue(v)
tfMap["allow_cookies"] = aws.ToBool(v)
}

if v := apiObject.EnableXRay; v != nil {
tfMap["enable_xray"] = aws.BoolValue(v)
tfMap["enable_xray"] = aws.ToBool(v)
}

if v := apiObject.Telemetries; v != nil {
tfMap["telemetries"] = flex.FlattenStringSet(v)
tfMap["telemetries"] = flex.FlattenStringyValueSet[awstypes.Telemetry](v)
}

if v := apiObject.IncludedPages; v != nil {
tfMap["included_pages"] = flex.FlattenStringSet(v)
tfMap["included_pages"] = v
}

if v := apiObject.FavoritePages; v != nil {
tfMap["favorite_pages"] = flex.FlattenStringSet(v)
tfMap["favorite_pages"] = v
}

if v := apiObject.ExcludedPages; v != nil {
tfMap["excluded_pages"] = flex.FlattenStringSet(v)
tfMap["excluded_pages"] = v
}

return tfMap
}

func expandCustomEvents(tfMap map[string]interface{}) *cloudwatchrum.CustomEvents {
func expandCustomEvents(tfMap map[string]interface{}) *awstypes.CustomEvents {
if tfMap == nil {
return nil
}

config := &cloudwatchrum.CustomEvents{}
config := &awstypes.CustomEvents{}

if v, ok := tfMap["status"].(string); ok && v != "" {
config.Status = aws.String(v)
config.Status = awstypes.CustomEventsStatus(v)
}

return config
}

func flattenCustomEvents(apiObject *cloudwatchrum.CustomEvents) map[string]interface{} {
func flattenCustomEvents(apiObject *awstypes.CustomEvents) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.Status; v != nil {
tfMap["status"] = aws.StringValue(v)
}
tfMap["status"] = string(apiObject.Status)

return tfMap
}
16 changes: 8 additions & 8 deletions internal/service/rum/app_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/cloudwatchrum"
awstypes "github.com/aws/aws-sdk-go-v2/service/rum/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand All @@ -21,7 +21,7 @@ import (

func TestAccRUMAppMonitor_basic(t *testing.T) {
ctx := acctest.Context(t)
var appMon cloudwatchrum.AppMonitor
var appMon awstypes.AppMonitor
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rum_app_monitor.test"

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestAccRUMAppMonitor_basic(t *testing.T) {

func TestAccRUMAppMonitor_customEvents(t *testing.T) {
ctx := acctest.Context(t)
var appMon cloudwatchrum.AppMonitor
var appMon awstypes.AppMonitor
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rum_app_monitor.test"

Expand Down Expand Up @@ -118,7 +118,7 @@ func TestAccRUMAppMonitor_customEvents(t *testing.T) {

func TestAccRUMAppMonitor_tags(t *testing.T) {
ctx := acctest.Context(t)
var appMon cloudwatchrum.AppMonitor
var appMon awstypes.AppMonitor
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rum_app_monitor.test"

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestAccRUMAppMonitor_tags(t *testing.T) {

func TestAccRUMAppMonitor_disappears(t *testing.T) {
ctx := acctest.Context(t)
var appMon cloudwatchrum.AppMonitor
var appMon awstypes.AppMonitor
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rum_app_monitor.test"

Expand All @@ -189,7 +189,7 @@ func TestAccRUMAppMonitor_disappears(t *testing.T) {

func testAccCheckAppMonitorDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).RUMConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).RUMClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_rum_app_monitor" {
Expand All @@ -213,7 +213,7 @@ func testAccCheckAppMonitorDestroy(ctx context.Context) resource.TestCheckFunc {
}
}

func testAccCheckAppMonitorExists(ctx context.Context, n string, v *cloudwatchrum.AppMonitor) resource.TestCheckFunc {
func testAccCheckAppMonitorExists(ctx context.Context, n string, v *awstypes.AppMonitor) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -223,7 +223,7 @@ func testAccCheckAppMonitorExists(ctx context.Context, n string, v *cloudwatchru
return fmt.Errorf("No CloudWatch RUM App Monitor ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).RUMConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).RUMClient(ctx)

output, err := tfcloudwatchrum.FindAppMonitorByName(ctx, conn, rs.Primary.ID)

Expand Down
Loading

0 comments on commit 26cb588

Please sign in to comment.