Skip to content

Commit 4dedb56

Browse files
committed
Merge pull request #17156 from brent-au/GH-17037
2 parents c7f9a88 + 609f858 commit 4dedb56

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

.changelog/17156.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_db_instance: Adds plan-time validation for `username` and `name` when `snapshot_identifier` is set
3+
```

aws/resource_aws_db_instance.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aws
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"regexp"
@@ -11,6 +12,7 @@ import (
1112
"github.com/aws/aws-sdk-go/aws"
1213
"github.com/aws/aws-sdk-go/service/rds"
1314
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1517
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1618
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -413,9 +415,10 @@ func resourceAwsDbInstance() *schema.Resource {
413415
},
414416

415417
"snapshot_identifier": {
416-
Type: schema.TypeString,
417-
Optional: true,
418-
ForceNew: true,
418+
Type: schema.TypeString,
419+
Optional: true,
420+
ForceNew: true,
421+
ConflictsWith: []string{"username"},
419422
},
420423

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

530533
"tags": tagsSchema(),
531534
},
535+
536+
CustomizeDiff: customdiff.All(
537+
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
538+
if _, ok := diff.GetOk("snapshot_identifier"); ok {
539+
switch strings.ToLower(diff.Get("engine").(string)) {
540+
case "mysql", "postgres", "mariadb":
541+
if _, ok := diff.GetOk("name"); ok {
542+
return fmt.Errorf("name attribute is not supported with snapshot_identifier when engine is %s", diff.Get("engine").(string))
543+
}
544+
}
545+
}
546+
return nil
547+
},
548+
),
532549
}
533550
}
534551

@@ -547,7 +564,7 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
547564
}
548565

549566
// Some ModifyDBInstance parameters (e.g. DBParameterGroupName) require
550-
// a database instance reboot to take affect. During resource creation,
567+
// a database instance reboot to take effect. During resource creation,
551568
// we expect everything to be in sync before returning completion.
552569
var requiresRebootDbInstance bool
553570

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

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

907917
if attr, ok := d.GetOk("allocated_storage"); ok {

aws/resource_aws_db_instance_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,10 +2201,8 @@ func TestAccAWSDBInstance_MySQL_SnapshotRestoreWithEngineVersion(t *testing.T) {
22012201
Check: resource.ComposeTestCheckFunc(
22022202
testAccCheckAWSDBInstanceExists("aws_db_instance.mysql_restore", &vRestoredInstance),
22032203
testAccCheckAWSDBInstanceExists("aws_db_instance.mysql", &v),
2204-
resource.TestCheckResourceAttr(
2205-
"aws_db_instance.mysql", "engine_version", "5.6.35"),
2206-
resource.TestCheckResourceAttr(
2207-
"aws_db_instance.mysql_restore", "engine_version", "5.6.41"),
2204+
resource.TestCheckResourceAttr("aws_db_instance.mysql", "engine_version", "5.6.35"),
2205+
resource.TestCheckResourceAttr("aws_db_instance.mysql_restore", "engine_version", "5.6.41"),
22082206
),
22092207
},
22102208
},
@@ -4577,8 +4575,6 @@ resource "aws_db_instance" "mysql_restore" {
45774575
45784576
instance_class = data.aws_rds_orderable_db_instance.test.instance_class
45794577
allocated_storage = 20
4580-
username = "root"
4581-
password = "password"
45824578
engine = data.aws_rds_orderable_db_instance.test.engine
45834579
engine_version = "5.6.41"
45844580
backup_retention_period = 0

0 commit comments

Comments
 (0)