diff --git a/.changelog/23039.txt b/.changelog/23039.txt new file mode 100644 index 00000000000..59e85173f32 --- /dev/null +++ b/.changelog/23039.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_rds_cluster: Fix crash when configured `engine_version` string is shorter than the `EngineVersion` string returned from the AWS API +``` \ No newline at end of file diff --git a/internal/service/rds/cluster.go b/internal/service/rds/cluster.go index 1bc99d5ade6..7a359200f62 100644 --- a/internal/service/rds/cluster.go +++ b/internal/service/rds/cluster.go @@ -1541,8 +1541,15 @@ func rdsClusterSetResourceDataEngineVersionFromCluster(d *schema.ResourceData, c } func compareActualEngineVersion(d *schema.ResourceData, oldVersion string, newVersion string) { - if oldVersion != newVersion && string(append([]byte(oldVersion), []byte(".")...)) != string([]byte(newVersion)[0:len(oldVersion)+1]) { + newVersionSubstr := newVersion + + if len(newVersion) > len(oldVersion) { + newVersionSubstr = string([]byte(newVersion)[0 : len(oldVersion)+1]) + } + + if oldVersion != newVersion && string(append([]byte(oldVersion), []byte(".")...)) != newVersionSubstr { d.Set("engine_version", newVersion) } + d.Set("engine_version_actual", newVersion) } diff --git a/internal/service/rds/engine_version_test.go b/internal/service/rds/engine_version_test.go new file mode 100644 index 00000000000..f9613539224 --- /dev/null +++ b/internal/service/rds/engine_version_test.go @@ -0,0 +1,76 @@ +package rds + +import ( + "testing" +) + +func TestCompareActualEngineVersion(t *testing.T) { + t.Parallel() + + type testCase struct { + configuredVersion string + actualVersion string + expectedEngineVersion string + expectedEngineVersionActual string + } + tests := map[string]testCase{ + "point version upgrade": { + configuredVersion: "8.0", + actualVersion: "8.0.27", + expectedEngineVersion: "", + expectedEngineVersionActual: "8.0.27", + }, + "minor version upgrade": { + configuredVersion: "8.0", + actualVersion: "8.1.1", + expectedEngineVersion: "8.1.1", + expectedEngineVersionActual: "8.1.1", + }, + "major version upgrade": { + configuredVersion: "8.1", + actualVersion: "9.0.0", + expectedEngineVersion: "9.0.0", + expectedEngineVersionActual: "9.0.0", + }, + "aurora upgrade": { + configuredVersion: "5.7.mysql_aurora.2.07", + actualVersion: "5.7.serverless_mysql_aurora.2.08.3", + expectedEngineVersion: "5.7.serverless_mysql_aurora.2.08.3", + expectedEngineVersionActual: "5.7.serverless_mysql_aurora.2.08.3", + }, + "aurora upgrade - used to crash": { + configuredVersion: "5.7.serverless_mysql_aurora.2.07", + actualVersion: "5.7.mysql_aurora.2.08.3", + expectedEngineVersion: "5.7.mysql_aurora.2.08.3", + expectedEngineVersionActual: "5.7.mysql_aurora.2.08.3", + }, + "oracle": { + configuredVersion: "12.1.0.2.v20", + actualVersion: "19.0.0.0.ru-2021-04.rur-2021-04.r1", + expectedEngineVersion: "19.0.0.0.ru-2021-04.rur-2021-04.r1", + expectedEngineVersionActual: "19.0.0.0.ru-2021-04.rur-2021-04.r1", + }, + "oracle - used to crash": { + configuredVersion: "19.0.0.0.ru-2021-04.rur-2021-04.r1", + actualVersion: "12.1.0.2.v20", + expectedEngineVersion: "12.1.0.2.v20", + expectedEngineVersionActual: "12.1.0.2.v20", + }, + } + + for name, test := range tests { + name, test := name, test + t.Run(name, func(t *testing.T) { + r := ResourceCluster() + d := r.Data(nil) + compareActualEngineVersion(d, test.configuredVersion, test.actualVersion) + + if want, got := test.expectedEngineVersion, d.Get("engine_version"); got != want { + t.Errorf("unexpected engine_version; want: %q, got: %q", want, got) + } + if want, got := test.expectedEngineVersionActual, d.Get("engine_version_actual"); got != want { + t.Errorf("unexpected engine_version_actual; want: %q, got: %q", want, got) + } + }) + } +}