Skip to content

Commit f35d554

Browse files
committed
aws_emrserverless_application: log_types as Set
1 parent be249ed commit f35d554

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

internal/service/emrserverless/application.go

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"context"
88
"errors"
99
"log"
10-
"regexp"
1110
"strings"
1211
"time"
1312

13+
"github.com/YakDriver/regexache"
1414
"github.com/aws/aws-sdk-go-v2/aws"
1515
"github.com/aws/aws-sdk-go-v2/service/emrserverless"
1616
"github.com/aws/aws-sdk-go-v2/service/emrserverless/types"
@@ -248,7 +248,7 @@ func resourceApplication() *schema.Resource {
248248
Type: schema.TypeBool,
249249
Required: true,
250250
},
251-
"log_group_name": {
251+
names.AttrLogGroupName: {
252252
Type: schema.TypeString,
253253
Optional: true,
254254
ValidateFunc: validation.StringLenBetween(1, 512),
@@ -264,9 +264,21 @@ func resourceApplication() *schema.Resource {
264264
ValidateFunc: verify.ValidARN,
265265
},
266266
"log_types": {
267-
Type: schema.TypeMap,
267+
Type: schema.TypeSet,
268268
Optional: true,
269-
Elem: &schema.Schema{Type: schema.TypeString},
269+
Elem: &schema.Resource{
270+
Schema: map[string]*schema.Schema{
271+
names.AttrName: {
272+
Type: schema.TypeString,
273+
Required: true,
274+
},
275+
names.AttrValues: {
276+
Type: schema.TypeSet,
277+
Required: true,
278+
Elem: &schema.Schema{Type: schema.TypeString},
279+
},
280+
},
281+
},
270282
},
271283
},
272284
},
@@ -304,7 +316,7 @@ func resourceApplication() *schema.Resource {
304316
ValidateFunc: validation.All(
305317
validation.StringLenBetween(1, 10280),
306318
validation.StringMatch(
307-
regexp.MustCompile(`^https://aps-workspaces\.([a-z]{2}-[a-z-]{1,20}-[1-9])\.amazonaws(\.[0-9A-Za-z]{2,4})+/workspaces/[-_.0-9A-Za-z]{1,100}/api/v1/remote_write$`),
319+
regexache.MustCompile(`^https://aps-workspaces\.([a-z]{2}-[a-z-]{1,20}-[1-9])\.amazonaws(\.[0-9A-Za-z]{2,4})+/workspaces/[-_.0-9A-Za-z]{1,100}/api/v1/remote_write$`),
308320
"remote_write_url must be a valid Amazon Managed Service for Prometheus remote write URL",
309321
),
310322
),
@@ -428,14 +440,14 @@ func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, meta
428440
input.MaximumCapacity = expandMaximumCapacity(v.([]any)[0].(map[string]any))
429441
}
430442

431-
if v, ok := d.GetOk(names.AttrNetworkConfiguration); ok && len(v.([]any)) > 0 && v.([]any)[0] != nil {
432-
input.NetworkConfiguration = expandNetworkConfiguration(v.([]any)[0].(map[string]any))
433-
}
434-
435443
if v, ok := d.GetOk("monitoring_configuration"); ok && len(v.([]any)) > 0 && v.([]any)[0] != nil {
436444
input.MonitoringConfiguration = expandMonitoringConfiguration(v.([]any)[0].(map[string]any))
437445
}
438446

447+
if v, ok := d.GetOk(names.AttrNetworkConfiguration); ok && len(v.([]any)) > 0 && v.([]any)[0] != nil {
448+
input.NetworkConfiguration = expandNetworkConfiguration(v.([]any)[0].(map[string]any))
449+
}
450+
439451
if v, ok := d.GetOk("runtime_configuration"); ok && len(v.([]any)) > 0 {
440452
input.RuntimeConfiguration = expandRuntimeConfiguration(v.([]any))
441453
}
@@ -505,14 +517,14 @@ func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta a
505517
return sdkdiag.AppendErrorf(diags, "setting maximum_capacity: %s", err)
506518
}
507519

508-
if err := d.Set(names.AttrNetworkConfiguration, []any{flattenNetworkConfiguration(application.NetworkConfiguration)}); err != nil {
509-
return sdkdiag.AppendErrorf(diags, "setting network_configuration: %s", err)
510-
}
511-
512520
if err := d.Set("monitoring_configuration", flattenMonitoringConfiguration(application.MonitoringConfiguration)); err != nil {
513521
return sdkdiag.AppendErrorf(diags, "setting monitoring_configuration: %s", err)
514522
}
515523

524+
if err := d.Set(names.AttrNetworkConfiguration, []any{flattenNetworkConfiguration(application.NetworkConfiguration)}); err != nil {
525+
return sdkdiag.AppendErrorf(diags, "setting network_configuration: %s", err)
526+
}
527+
516528
if err := d.Set("runtime_configuration", flattenRuntimeConfiguration(application.RuntimeConfiguration)); err != nil {
517529
return sdkdiag.AppendErrorf(diags, "setting runtime_configuration: %s", err)
518530
}
@@ -1205,7 +1217,7 @@ func expandCloudWatchLoggingConfiguration(tfMap map[string]any) *types.CloudWatc
12051217
apiObject.Enabled = aws.Bool(v)
12061218
}
12071219

1208-
if v, ok := tfMap["log_group_name"].(string); ok && v != "" {
1220+
if v, ok := tfMap[names.AttrLogGroupName].(string); ok && v != "" {
12091221
apiObject.LogGroupName = aws.String(v)
12101222
}
12111223

@@ -1217,8 +1229,8 @@ func expandCloudWatchLoggingConfiguration(tfMap map[string]any) *types.CloudWatc
12171229
apiObject.EncryptionKeyArn = aws.String(v)
12181230
}
12191231

1220-
if v, ok := tfMap["log_types"].(map[string]any); ok && len(v) > 0 {
1221-
apiObject.LogTypes = expandLogTypes(v)
1232+
if v, ok := tfMap["log_types"].(*schema.Set); ok && v.Len() > 0 {
1233+
apiObject.LogTypes = expandLogTypes(v.List())
12221234
}
12231235

12241236
return apiObject
@@ -1236,7 +1248,7 @@ func flattenCloudWatchLoggingConfiguration(apiObject *types.CloudWatchLoggingCon
12361248
}
12371249

12381250
if v := apiObject.LogGroupName; v != nil {
1239-
tfMap["log_group_name"] = aws.ToString(v)
1251+
tfMap[names.AttrLogGroupName] = aws.ToString(v)
12401252
}
12411253

12421254
if v := apiObject.LogStreamNamePrefix; v != nil {
@@ -1254,42 +1266,36 @@ func flattenCloudWatchLoggingConfiguration(apiObject *types.CloudWatchLoggingCon
12541266
return tfMap
12551267
}
12561268

1257-
func expandLogTypes(tfMap map[string]any) map[string][]string {
1258-
if tfMap == nil {
1269+
func expandLogTypes(tfList []any) map[string][]string {
1270+
if tfList == nil {
12591271
return nil
12601272
}
12611273

12621274
configs := make(map[string][]string)
1263-
1264-
for k, v := range tfMap {
1265-
if str, ok := v.(string); ok {
1266-
// Split comma-separated values into a slice
1267-
values := strings.Split(str, ",")
1268-
for i, val := range values {
1269-
values[i] = strings.TrimSpace(val)
1270-
}
1271-
configs[k] = values
1272-
}
1275+
for _, v := range tfList {
1276+
configData := v.(map[string]any)
1277+
key := configData[names.AttrName].(string)
1278+
values := flex.ExpandStringValueSet(configData[names.AttrValues].(*schema.Set))
1279+
configs[key] = values
12731280
}
12741281

12751282
return configs
12761283
}
12771284

1278-
func flattenLogTypes(apiObject map[string][]string) map[string]any {
1285+
func flattenLogTypes(apiObject map[string][]string) []any {
12791286
if apiObject == nil {
12801287
return nil
12811288
}
12821289

1283-
tfMap := map[string]any{}
1284-
1290+
var out []any
12851291
for k, v := range apiObject {
1286-
if len(v) > 0 {
1287-
// Join the slice with commas for storage in Terraform state
1288-
tfMap[k] = strings.Join(v, ",")
1289-
}
1292+
data := make(map[string]any)
1293+
data[names.AttrName] = k
1294+
data[names.AttrValues] = flex.FlattenStringValueSet(v)
1295+
out = append(out, data)
12901296
}
12911297

1292-
return tfMap
1298+
return out
12931299
}
12941300

12951301
func expandS3MonitoringConfiguration(tfMap map[string]any) *types.S3MonitoringConfiguration {

internal/service/emrserverless/application_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,18 @@ resource "aws_emrserverless_application" "test" {
10291029
10301030
monitoring_configuration {
10311031
cloudwatch_logging_configuration {
1032-
enabled = true
1033-
log_group_name = aws_cloudwatch_log_group.test.name
1032+
enabled = true
1033+
log_group_name = aws_cloudwatch_log_group.test.name
10341034
log_stream_name_prefix = "spark-logs"
1035-
log_types = {
1036-
"SPARK_DRIVER" = "STDOUT,STDERR"
1037-
"SPARK_EXECUTOR" = "STDOUT"
1035+
1036+
log_types {
1037+
name = "SPARK_DRIVER"
1038+
values = ["STDOUT", "STDERR"]
1039+
}
1040+
1041+
log_types {
1042+
name = "SPARK_EXECUTOR"
1043+
values = ["STDOUT"]
10381044
}
10391045
}
10401046
@@ -1068,7 +1074,7 @@ resource "aws_emrserverless_application" "test" {
10681074
10691075
monitoring_configuration {
10701076
cloudwatch_logging_configuration {
1071-
enabled = false
1077+
enabled = false
10721078
log_group_name = aws_cloudwatch_log_group.test.name
10731079
}
10741080

0 commit comments

Comments
 (0)