Skip to content

Commit

Permalink
Merge pull request #17755 from hashicorp/b-db-instance-forcenew-confi…
Browse files Browse the repository at this point in the history
…cts-snapshot-id

r/db_instance: update plan-time validation for snapshot_identifier/name/username
  • Loading branch information
anGie44 authored Feb 23, 2021
2 parents a39a16a + c9f78b5 commit 68f9c46
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/17755.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_db_instance: Fix conflicting argument validation error
```
32 changes: 11 additions & 21 deletions aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"context"
"fmt"
"log"
"regexp"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -415,10 +413,9 @@ func resourceAwsDbInstance() *schema.Resource {
},

"snapshot_identifier": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"username"},
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"auto_minor_version_upgrade": {
Expand Down Expand Up @@ -532,20 +529,6 @@ func resourceAwsDbInstance() *schema.Resource {

"tags": tagsSchema(),
},

CustomizeDiff: customdiff.All(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
if _, ok := diff.GetOk("snapshot_identifier"); ok {
switch strings.ToLower(diff.Get("engine").(string)) {
case "mysql", "postgres", "mariadb":
if _, ok := diff.GetOk("name"); ok {
return fmt.Errorf("name attribute is not supported with snapshot_identifier when engine is %s", diff.Get("engine").(string))
}
}
}
return nil
},
),
}
}

Expand Down Expand Up @@ -911,7 +894,14 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
}

if attr, ok := d.GetOk("name"); ok {
opts.DBName = aws.String(attr.(string))
// "Note: This parameter [DBName] doesn't apply to the MySQL, PostgreSQL, or MariaDB engines."
// https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html
switch strings.ToLower(d.Get("engine").(string)) {
case "mysql", "postgres", "mariadb":
// skip
default:
opts.DBName = aws.String(attr.(string))
}
}

if attr, ok := d.GetOk("allocated_storage"); ok {
Expand Down
2 changes: 2 additions & 0 deletions aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4575,6 +4575,8 @@ resource "aws_db_instance" "mysql_restore" {
instance_class = data.aws_rds_orderable_db_instance.test.instance_class
allocated_storage = 20
username = "root"
password = "password"
engine = data.aws_rds_orderable_db_instance.test.engine
engine_version = "5.6.41"
backup_retention_period = 0
Expand Down

0 comments on commit 68f9c46

Please sign in to comment.