Skip to content

Commit

Permalink
Merge pull request #32956 from hashicorp/b-emr-cluster-bootstrap-acti…
Browse files Browse the repository at this point in the history
…on-empty-args

emr/cluster: Allow bootstrap action empty args
  • Loading branch information
YakDriver authored Aug 10, 2023
2 parents becf973 + 2dc1edb commit 40b85f8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/32956.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_emr_cluster: Fix to allow empty `args` for `bootstrap_action`
```
16 changes: 15 additions & 1 deletion internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ const (
func ExpandStringList(configured []interface{}) []*string {
vs := make([]*string, 0, len(configured))
for _, v := range configured {
if v, ok := v.(string); ok && v != "" {
if v, ok := v.(string); ok && v != "" { // v != "" may not do anything since in []interface{}, empty string will be nil so !ok
vs = append(vs, aws.String(v))
}
}
return vs
}

// ExpandStringListEmpty the result of flatmap. Expand for an array of strings
// and returns a []*string. Adds an empty element for every nil or uncastable.
func ExpandStringListEmpty(configured []interface{}) []*string {
vs := make([]*string, 0, len(configured))
for _, v := range configured {
if v, ok := v.(string); ok { // empty string in config turns into nil in []interface{} so !ok
vs = append(vs, aws.String(v))
} else {
vs = append(vs, aws.String(""))
}
}
return vs
}

// Takes the result of flatmap.Expand for an array of strings
// and returns a []*time.Time
func ExpandStringTimeList(configured []interface{}, format string) []*time.Time {
Expand Down
31 changes: 31 additions & 0 deletions internal/flex/flex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ func TestExpandStringList(t *testing.T) {
}
}

func TestExpandStringListEmpty(t *testing.T) {
t.Parallel()

testCases := []struct {
configured []interface{}
want []*string
}{
{
configured: []interface{}{"abc", "xyz123"},
want: []*string{aws.String("abc"), aws.String("xyz123")},
},
{
configured: []interface{}{"abc", 123, "xyz123"},
want: []*string{aws.String("abc"), aws.String(""), aws.String("xyz123")},
},
{
configured: []interface{}{"foo", "bar", "", "baz"},
want: []*string{aws.String("foo"), aws.String("bar"), aws.String(""), aws.String("baz")},
},
{
configured: []interface{}{"foo", "bar", nil, "baz"},
want: []*string{aws.String("foo"), aws.String("bar"), aws.String(""), aws.String("baz")},
},
}
for _, testCase := range testCases {
if got, want := ExpandStringListEmpty(testCase.configured), testCase.want; !cmp.Equal(got, want) {
t.Errorf("ExpandStringListEmpty(%v) = %v, want %v", testCase.configured, got, want)
}
}
}

func TestExpandStringTimeList(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion internal/service/emr/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ func expandBootstrapActions(bootstrapActions []interface{}) []*emr.BootstrapActi
Name: aws.String(actionName),
ScriptBootstrapAction: &emr.ScriptBootstrapActionConfig{
Path: aws.String(actionPath),
Args: flex.ExpandStringList(actionArgs),
Args: flex.ExpandStringListEmpty(actionArgs),
},
}
actionsOut = append(actionsOut, action)
Expand Down
12 changes: 6 additions & 6 deletions internal/service/emr/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "11"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.0", "0"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.1", "1"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.2", "\"\""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.2", ""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.3", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.4", "4"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.5", "5"),
Expand Down Expand Up @@ -1079,7 +1079,7 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "11"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.0", "0"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.1", "1"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.2", "\"\""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.2", ""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.3", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.4", "4"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.5", "5"),
Expand Down Expand Up @@ -1120,7 +1120,7 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.#", "11"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.0", "0"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.1", "1"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.2", "\"\""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.2", ""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.3", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.4", "4"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.5", "5"),
Expand Down Expand Up @@ -3115,7 +3115,7 @@ resource "aws_emr_cluster" "test" {
args = [
"0",
"1",
"\"\"",
"",
"3",
"4",
"5",
Expand Down Expand Up @@ -3181,7 +3181,7 @@ resource "aws_emr_cluster" "test" {
args = [
"0",
"1",
"\"\"",
"",
"3",
"4",
"5",
Expand Down Expand Up @@ -3259,7 +3259,7 @@ resource "aws_emr_cluster" "test" {
args = [
"0",
"1",
"\"\"",
"",
"3",
"4",
"5",
Expand Down

0 comments on commit 40b85f8

Please sign in to comment.