provider/aws: Fix Diff Suppress function for aws_db_instance

Introduced in #11369, this fixes an issue with the diff suppress function when creating a new `aws_db_instance` resource, while using the default `engine_version`.

```
$ make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSDBInstance_diffSuppressInitialState'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/02/13 11:52:12 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSDBInstance_diffSuppressInitialState -timeout 120m
=== RUN   TestAccAWSDBInstance_diffSuppressInitialState
--- PASS: TestAccAWSDBInstance_diffSuppressInitialState (480.78s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/aws    480.793s
```
This commit is contained in:
Jake Champlin 2017-02-13 12:26:06 -05:00
parent c6b21d853a
commit 64f75ff0f9
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
3 changed files with 64 additions and 7 deletions

View File

@ -19,7 +19,17 @@ func suppressEquivalentAwsPolicyDiffs(k, old, new string, d *schema.ResourceData
// Suppresses minor version changes to the db_instance engine_version attribute // Suppresses minor version changes to the db_instance engine_version attribute
func suppressAwsDbEngineVersionDiffs(k, old, new string, d *schema.ResourceData) bool { func suppressAwsDbEngineVersionDiffs(k, old, new string, d *schema.ResourceData) bool {
if d.Get("auto_minor_version_upgrade").(bool) { // First check if the old/new values are nil.
// If both are nil, we have no state to compare the values with, so register a diff.
// This populates the attribute field during a plan/apply with fresh state, allowing
// the attribute to still be used in future resources.
// See https://github.com/hashicorp/terraform/issues/11881
if old == "" && new == "" {
return false
}
if v, ok := d.GetOk("auto_minor_version_upgrade"); ok {
if v.(bool) {
// If we're set to auto upgrade minor versions // If we're set to auto upgrade minor versions
// ignore a minor version diff between versions // ignore a minor version diff between versions
if strings.HasPrefix(old, new) { if strings.HasPrefix(old, new) {
@ -27,6 +37,7 @@ func suppressAwsDbEngineVersionDiffs(k, old, new string, d *schema.ResourceData)
return true return true
} }
} }
}
// Throw a diff by default // Throw a diff by default
return false return false

View File

@ -5,17 +5,21 @@ import (
"os" "os"
"testing" "testing"
"github.com/hashicorp/terraform/builtin/providers/template"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
var testAccProviders map[string]terraform.ResourceProvider var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider var testAccProvider *schema.Provider
var testAccTemplateProvider *schema.Provider
func init() { func init() {
testAccProvider = Provider().(*schema.Provider) testAccProvider = Provider().(*schema.Provider)
testAccTemplateProvider = template.Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{ testAccProviders = map[string]terraform.ResourceProvider{
"aws": testAccProvider, "aws": testAccProvider,
"template": testAccTemplateProvider,
} }
} }

View File

@ -325,6 +325,26 @@ func TestAccAWSDBInstance_MinorVersion(t *testing.T) {
}) })
} }
// See https://github.com/hashicorp/terraform/issues/11881
func TestAccAWSDBInstance_diffSuppressInitialState(t *testing.T) {
var v rds.DBInstance
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfigSuppressInitialState(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
),
},
},
})
}
func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error { func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).rdsconn conn := testAccProvider.Meta().(*AWSClient).rdsconn
@ -1163,3 +1183,25 @@ resource "aws_db_instance" "bar" {
skip_final_snapshot = true skip_final_snapshot = true
} }
`, acctest.RandInt()) `, acctest.RandInt())
func testAccAWSDBInstanceConfigSuppressInitialState(rInt int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
identifier = "foobarbaz-test-terraform-%d"
allocated_storage = 10
engine = "MySQL"
instance_class = "db.t1.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
skip_final_snapshot = true
}
data "template_file" "test" {
template = ""
vars = {
test_var = "${aws_db_instance.bar.engine_version}"
}
}
`, rInt)
}