Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_db_instance: Adds warning when setting character_set_name on replica- or restore-type resources #36518

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/36518.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_db_instance: Adds warning when setting `character_set_name` when `replicate_source_db`, `restore_to_point_in_time`, or `snapshot_identifier` is set
```
35 changes: 32 additions & 3 deletions internal/errs/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ func withPath(d diag.Diagnostic, path cty.Path) diag.Diagnostic {
return d
}

// newAttributeConflictsError is included for use with NewAttributeConflictsWillBeError.
// The typical behavior is covered using the schema ConflictsWith parameter.
func newAttributeConflictsError(path, otherPath cty.Path) diag.Diagnostic {
return NewAttributeErrorDiagnostic(
path,
"Invalid Attribute Combination",
fmt.Sprintf("Attribute %q cannot be specified when %q is specified.",
PathString(path),
PathString(otherPath),
),
)
}

// NewAttributeConflictsWhenError returns an error diagnostic indicating that the attribute at the given path cannot be
// specified when the attribute at otherPath has the given value.
func NewAttributeConflictsWhenError(path, otherPath cty.Path, otherValue string) diag.Diagnostic {
return NewAttributeErrorDiagnostic(
path,
Expand All @@ -86,18 +101,32 @@ func NewAttributeConflictsWhenError(path, otherPath cty.Path, otherValue string)
)
}

func NewAttributeRequiredWhenError(neededPath, path cty.Path, value string) diag.Diagnostic {
// NewAttributeRequiredWhenError returns an error diagnostic indicating that the attribute at neededPath is required when the
// attribute at otherPath has the given value.
func NewAttributeRequiredWhenError(neededPath, otherPath cty.Path, value string) diag.Diagnostic {
return NewAttributeErrorDiagnostic(
path,
otherPath,
"Invalid Attribute Combination",
fmt.Sprintf("Attribute %q must be specified when %q is %q.",
PathString(neededPath),
PathString(path),
PathString(otherPath),
value,
),
)
}

// NewAttributeConflictsWillBeError returns a warning diagnostic indicating that the attribute at the given path cannot be
// specified when the attribute at otherPath is set.
// This is intended to be used for situations where the conflict will become an error in a future release.
func NewAttributeConflictsWillBeError(path, otherPath cty.Path) diag.Diagnostic {
return willBeError(
newAttributeConflictsError(path, otherPath),
)
}

// NewAttributeConflictsWhenWillBeError returns a warning diagnostic indicating that the attribute at the given path cannot be
// specified when the attribute at otherPath has the given value.
// This is intended to be used for situations where the conflict will become an error in a future release.
func NewAttributeConflictsWhenWillBeError(path, otherPath cty.Path, otherValue string) diag.Diagnostic {
return willBeError(
NewAttributeConflictsWhenError(path, otherPath, otherValue),
Expand Down
49 changes: 40 additions & 9 deletions internal/service/rds/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv2 "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand Down Expand Up @@ -152,6 +153,9 @@ func ResourceInstance() *schema.Resource {
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(backupTarget_Values(), false),
ConflictsWith: []string{
"s3_import",
},
},
"backup_window": {
Type: schema.TypeString,
Expand Down Expand Up @@ -182,6 +186,22 @@ func ResourceInstance() *schema.Resource {
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{
"s3_import",
},
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
for _, conflictAttr := range []string{
"replicate_source_db",
// s3_import is handled by the schema ConflictsWith
"snapshot_identifier",
"restore_to_point_in_time",
} {
if _, ok := d.GetOk(conflictAttr); ok {
return true
}
}
return false
},
},
"copy_tags_to_snapshot": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -628,6 +648,9 @@ func ResourceInstance() *schema.Resource {
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{
"s3_import",
},
},
"username": {
Type: schema.TypeString,
Expand Down Expand Up @@ -697,6 +720,23 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, meta in

var resourceID string // will be assigned depending on how it is created

if _, ok := d.GetOk("character_set_name"); ok {
charSetPath := cty.GetAttrPath("character_set_name")
for _, conflictAttr := range []string{
"replicate_source_db",
// s3_import is handled by the schema ConflictsWith
"snapshot_identifier",
"restore_to_point_in_time",
} {
if _, ok := d.GetOk(conflictAttr); ok {
diags = append(diags, errs.NewAttributeConflictsWillBeError(
charSetPath,
cty.GetAttrPath(conflictAttr),
))
}
}
}

if v, ok := d.GetOk("replicate_source_db"); ok {
sourceDBInstanceID := v.(string)
input := &rds.CreateDBInstanceReadReplicaInput{
Expand Down Expand Up @@ -902,15 +942,6 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, meta in
if _, ok := d.GetOk("username"); !ok {
diags = sdkdiag.AppendErrorf(diags, `"username": required field is not set`)
}
if _, ok := d.GetOk("character_set_name"); ok {
diags = sdkdiag.AppendErrorf(diags, `"character_set_name" doesn't work with restores"`)
}
if _, ok := d.GetOk("timezone"); ok {
diags = sdkdiag.AppendErrorf(diags, `"timezone" doesn't work with restores"`)
}
if _, ok := d.GetOk("backup_target"); ok {
diags = sdkdiag.AppendErrorf(diags, `"backup_target" doesn't work with restores"`)
}
if diags.HasError() {
return diags
}
Expand Down
Loading
Loading