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

rum: Migrate to AWS SDK v2 #36966

Merged
merged 9 commits into from
Jun 17, 2024
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/route53 v1.40.10
github.com/aws/aws-sdk-go-v2/service/route53domains v1.23.10
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.0.7
github.com/aws/aws-sdk-go-v2/service/rum v1.17.12
github.com/aws/aws-sdk-go-v2/service/s3 v1.55.1
github.com/aws/aws-sdk-go-v2/service/s3control v1.44.13
github.com/aws/aws-sdk-go-v2/service/scheduler v1.8.10
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ github.com/aws/aws-sdk-go-v2/service/route53domains v1.23.10 h1:R56F/k3CQZHwrd3k
github.com/aws/aws-sdk-go-v2/service/route53domains v1.23.10/go.mod h1:W3+eDMk01Na1U3aQfwGkkEP1Yfe6WUn8hXzyInvGlcU=
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.0.7 h1:32/NRAG4ka8/hwr1k9ZA2xwarcJeWO6djaIFJ42tuFg=
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.0.7/go.mod h1:H9RRL0qQ+s+XlaZO5s5G3Z8cVZpKEoj313hOyglUwj0=
github.com/aws/aws-sdk-go-v2/service/rum v1.17.12 h1:QeR9ldfI/Hf5sNgvMKi/lnyTKqhg8DQ/BUlRBUuF2tA=
github.com/aws/aws-sdk-go-v2/service/rum v1.17.12/go.mod h1:sd4DiDquert2Ds9FJXnRj8v/78A682gIz1dxjiMRy/I=
github.com/aws/aws-sdk-go-v2/service/s3 v1.55.1 h1:UAxBuh0/8sFJk1qOkvOKewP5sWeWaTPDknbQz0ZkDm0=
github.com/aws/aws-sdk-go-v2/service/s3 v1.55.1/go.mod h1:hWjsYGjVuqCgfoveVcVFPXIWgz0aByzwaxKlN1StKcM=
github.com/aws/aws-sdk-go-v2/service/s3control v1.44.13 h1:HhsZlX5gsL/KfEyHyBO5H0ewgmXoiBpjDPAZ3Ggrj8g=
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.

115 changes: 57 additions & 58 deletions internal/service/rum/app_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ package rum

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"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/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/rum"
awstypes "github.com/aws/aws-sdk-go-v2/service/rum/types"
"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/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
Expand All @@ -27,7 +28,7 @@ import (

// @SDKResource("aws_rum_app_monitor", name="App Monitor")
// @Tags(identifierAttribute="arn")
func ResourceAppMonitor() *schema.Resource {
func resourceAppMonitor() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAppMonitorCreate,
ReadWithoutTimeout: resourceAppMonitorRead,
Expand Down Expand Up @@ -91,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 @@ -114,10 +115,10 @@ func ResourceAppMonitor() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrStatus: {
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 @@ -145,16 +146,17 @@ func ResourceAppMonitor() *schema.Resource {
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
}
}

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

name := d.Get(names.AttrName).(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(names.AttrDomain).(string)),
Expand All @@ -169,7 +171,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 sdkdiag.AppendErrorf(diags, "creating CloudWatch RUM App Monitor (%s): %s", name, err)
Expand All @@ -182,9 +184,9 @@ func resourceAppMonitorCreate(ctx context.Context, d *schema.ResourceData, meta

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

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

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] CloudWatch RUM App Monitor %s not found, removing from state", d.Id())
Expand All @@ -205,18 +207,19 @@ func resourceAppMonitorRead(ctx context.Context, d *schema.ResourceData, meta in
}

d.Set("app_monitor_id", appMon.Id)
name := aws.ToString(appMon.Name)
arn := arn.ARN{
AccountID: meta.(*conns.AWSClient).AccountID,
Partition: meta.(*conns.AWSClient).Partition,
Region: meta.(*conns.AWSClient).Region,
Resource: fmt.Sprintf("appmonitor/%s", aws.StringValue(appMon.Name)),
Service: "rum",
Region: meta.(*conns.AWSClient).Region,
AccountID: meta.(*conns.AWSClient).AccountID,
Resource: "appmonitor/" + name,
}.String()
d.Set(names.AttrARN, arn)
d.Set("cw_log_enabled", appMon.DataStorage.CwLog.CwLogEnabled)
d.Set("cw_log_group", appMon.DataStorage.CwLog.CwLogGroup)
d.Set(names.AttrDomain, appMon.Domain)
d.Set(names.AttrName, appMon.Name)
d.Set(names.AttrName, name)

setTagsOut(ctx, appMon.Tags)

Expand All @@ -225,10 +228,10 @@ func resourceAppMonitorRead(ctx context.Context, d *schema.ResourceData, meta in

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

if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) {
input := &cloudwatchrum.UpdateAppMonitorInput{
input := &rum.UpdateAppMonitorInput{
Name: aws.String(d.Id()),
}

Expand All @@ -248,7 +251,7 @@ func resourceAppMonitorUpdate(ctx context.Context, d *schema.ResourceData, meta
input.Domain = aws.String(d.Get(names.AttrDomain).(string))
}

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

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating CloudWatch RUM App Monitor (%s): %s", d.Id(), err)
Expand All @@ -260,14 +263,14 @@ func resourceAppMonitorUpdate(ctx context.Context, d *schema.ResourceData, meta

func resourceAppMonitorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags 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 diags
}

Expand All @@ -278,14 +281,14 @@ func resourceAppMonitorDelete(ctx context.Context, d *schema.ResourceData, meta
return diags
}

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 @@ -303,12 +306,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 @@ -319,7 +322,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 @@ -331,93 +334,89 @@ 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"] = apiObject.Telemetries
}

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

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

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

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[names.AttrStatus].(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[names.AttrStatus] = aws.StringValue(v)
tfMap := map[string]interface{}{
names.AttrStatus: apiObject.Status,
}

return tfMap
Expand Down
Loading
Loading