Skip to content

Commit

Permalink
Handles automatically turning off Logging.EnableLogContext on Updat…
Browse files Browse the repository at this point in the history
…e if `Logging.EnableLogging` is `false`
  • Loading branch information
gdavison committed Apr 16, 2024
1 parent 62b81cb commit d4d0d19
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
27 changes: 23 additions & 4 deletions internal/service/dms/replication_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ func TestAccDMSReplicationConfig_settings_EnableLogging(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"start_replication"},
},
{
Config: testAccReplicationConfigConfig_settings_EnableLogContext(rName, true, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckReplicationConfigExists(ctx, resourceName, &v),
acctest.CheckResourceAttrJMES(resourceName, "replication_settings", "Logging.EnableLogging", "true"),
acctest.CheckResourceAttrJMES(resourceName, "replication_settings", "Logging.EnableLogContext", "true"),
acctest.CheckResourceAttrJMES(resourceName, "replication_settings", "Logging.LogComponents[?Id=='DATA_STRUCTURE'].Severity | [0]", "LOGGER_SEVERITY_DEFAULT"),
acctest.CheckResourceAttrJMES(resourceName, "replication_settings", "type(Logging.CloudWatchLogGroup)", "null"),
acctest.CheckResourceAttrJMES(resourceName, "replication_settings", "type(Logging.CloudWatchLogStream)", "null"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"start_replication"},
},
{
Config: testAccReplicationConfigConfig_settings_EnableLogging(rName, false),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -163,7 +180,7 @@ func TestAccDMSReplicationConfig_settings_LoggingValidation(t *testing.T) {
CheckDestroy: testAccCheckReplicationTaskDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccReplicationConfigConfig_settings_EnableLogContext(rName, true),
Config: testAccReplicationConfigConfig_settings_EnableLogContext(rName, false, true),
ExpectError: regexache.MustCompile(`The parameter Logging.EnableLogContext is not allowed when\s+Logging.EnableLogging is not set to true.`),
},
{
Expand Down Expand Up @@ -497,7 +514,7 @@ resource "aws_dms_replication_config" "test" {
`, rName, enabled))
}

func testAccReplicationConfigConfig_settings_EnableLogContext(rName string, enabled bool) string {
func testAccReplicationConfigConfig_settings_EnableLogContext(rName string, enableLogging, enableLogContext bool) string {
return acctest.ConfigCompose(
testAccReplicationConfigConfig_base_DummyDatabase(rName),
fmt.Sprintf(`
Expand All @@ -515,9 +532,9 @@ resource "aws_dms_replication_config" "test" {
}
# terrafmt can't handle this using jsonencode or a heredoc
replication_settings = "{\"Logging\":{\"EnableLogContext\":%[2]t}}"
replication_settings = "{\"Logging\":{\"EnableLogging\":%[2]t,\"EnableLogContext\":%[3]t}}"
}
`, rName, enabled))
`, rName, enableLogging, enableLogContext))
}

func testAccReplicationConfigConfig_settings_LoggingReadOnly(rName, field string) string {
Expand Down Expand Up @@ -640,6 +657,8 @@ resource "aws_dms_replication_config" "test" {
min_capacity_units = "2"
preferred_maintenance_window = "sun:23:45-mon:00:30"
}
depends_on = [aws_rds_cluster_instance.source, aws_rds_cluster_instance.target]
}
`, rName, start))
}
Expand Down
6 changes: 5 additions & 1 deletion internal/service/dms/replication_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ func resourceReplicationTaskUpdate(ctx context.Context, d *schema.ResourceData,

if d.HasChange("replication_task_settings") {
if v, ok := d.GetOk("replication_task_settings"); ok {
input.ReplicationTaskSettings = aws.String(v.(string))
s, err := normalizeReplicationSettings(v.(string))
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating DMS Replication Task (%s): %s", d.Id(), err)
}
input.ReplicationTaskSettings = aws.String(s)
}
}

Expand Down
51 changes: 47 additions & 4 deletions internal/service/dms/replication_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
dms "github.com/aws/aws-sdk-go/service/databasemigrationservice"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -198,6 +199,7 @@ func TestAccDMSReplicationTask_settings_EnableLogging(t *testing.T) {
testAccCheckReplicationTaskExists(ctx, resourceName, &v),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.EnableLogging", "true"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.EnableLogContext", "false"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.LogComponents[?Id=='DATA_STRUCTURE'].Severity | [0]", "LOGGER_SEVERITY_DEFAULT"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.CloudWatchLogGroup", fmt.Sprintf("dms-tasks-%s", rName)),
func(s *terraform.State) error {
arn, err := arn.Parse(aws.StringValue(v.ReplicationTaskArn))
Expand All @@ -219,12 +221,46 @@ func TestAccDMSReplicationTask_settings_EnableLogging(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"start_replication_task"},
},
{
Config: testAccReplicationTaskConfig_settings_EnableLogContext(rName, true, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckReplicationTaskExists(ctx, resourceName, &v),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.EnableLogging", "true"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.EnableLogContext", "true"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.LogComponents[?Id=='DATA_STRUCTURE'].Severity | [0]", "LOGGER_SEVERITY_DEFAULT"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.CloudWatchLogGroup", fmt.Sprintf("dms-tasks-%s", rName)),
func(s *terraform.State) error {
arn, err := arn.Parse(aws.StringValue(v.ReplicationTaskArn))
if err != nil {
return err
}
l := strings.Split(arn.Resource, ":")
if len(l) != 2 {
return fmt.Errorf("expected 2 parts in %s", arn.Resource)
}
id := l[1]
return acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.CloudWatchLogStream", fmt.Sprintf("dms-task-%s", id))(s)
},
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
},
},
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"start_replication_task"},
},
{
Config: testAccReplicationTaskConfig_settings_EnableLogging(rName, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckReplicationTaskExists(ctx, resourceName, &v),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.EnableLogging", "false"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.EnableLogContext", "false"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.LogComponents[?Id=='DATA_STRUCTURE'].Severity | [0]", "LOGGER_SEVERITY_DEFAULT"),
acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.CloudWatchLogGroup", fmt.Sprintf("dms-tasks-%s", rName)),
func(s *terraform.State) error {
arn, err := arn.Parse(aws.StringValue(v.ReplicationTaskArn))
Expand All @@ -239,6 +275,11 @@ func TestAccDMSReplicationTask_settings_EnableLogging(t *testing.T) {
return acctest.CheckResourceAttrJMES(resourceName, "replication_task_settings", "Logging.CloudWatchLogStream", fmt.Sprintf("dms-task-%s", id))(s)
},
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
},
},
},
{
ResourceName: resourceName,
Expand All @@ -261,7 +302,7 @@ func TestAccDMSReplicationTask_settings_LoggingValidation(t *testing.T) {
CheckDestroy: testAccCheckReplicationTaskDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccReplicationTaskConfig_settings_EnableLogContext(rName, true),
Config: testAccReplicationTaskConfig_settings_EnableLogContext(rName, false, true),
ExpectError: regexache.MustCompile(`The parameter Logging.EnableLogContext is not allowed when\s+Logging.EnableLogging is not set to true.`),
},
{
Expand Down Expand Up @@ -771,7 +812,7 @@ resource "aws_dms_replication_task" "test" {
`, rName, enabled))
}

func testAccReplicationTaskConfig_settings_EnableLogContext(rName string, enabled bool) string {
func testAccReplicationTaskConfig_settings_EnableLogContext(rName string, enableLogging, enableLogContext bool) string {
return acctest.ConfigCompose(testAccReplicationTaskConfig_base(rName), fmt.Sprintf(`
resource "aws_dms_replication_task" "test" {
replication_task_id = %[1]q
Expand All @@ -796,9 +837,9 @@ resource "aws_dms_replication_task" "test" {
}
)
# terrafmt can't handle this using jsonencode or a heredoc
replication_task_settings = "{\"Logging\":{\"EnableLogContext\":%[2]t}}"
replication_task_settings = "{\"Logging\":{\"EnableLogging\":%[2]t,\"EnableLogContext\":%[3]t}}"
}
`, rName, enabled))
`, rName, enableLogging, enableLogContext))
}

func testAccReplicationTaskConfig_settings_LoggingReadOnly(rName, field string) string {
Expand Down Expand Up @@ -958,6 +999,8 @@ resource "aws_dms_replication_task" "test" {
)
start_replication_task = %[2]t
depends_on = [aws_rds_cluster_instance.source, aws_rds_cluster_instance.target]
}
resource "aws_dms_replication_instance" "test" {
Expand Down
30 changes: 30 additions & 0 deletions internal/service/dms/task_settings_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,33 @@ func validateReplicationSettings(i any, path cty.Path) diag.Diagnostics {

return diags
}

func normalizeReplicationSettings(s string) (string, error) {
if s == "" {
return "", nil
}

var m map[string]any

if err := json.Unmarshal([]byte(s), &m); err != nil {
return s, err
}

// If EnableLogging is false, set EnableLogContext to false unless it is explicitly set.
// Normally, if EnableLogContext is not set, it uses the existing value.
if l, ok := m["Logging"].(map[string]any); ok {
if enabled, ok := l["EnableLogging"]; ok && !enabled.(bool) {
delete(l, "EnableLogContext")
if _, ok := l["EnableLogContext"]; !ok {
l["EnableLogContext"] = false
b, err := json.Marshal(m)
if err != nil {
return s, err
}
s = string(b)
}
}
}

return s, nil
}

0 comments on commit d4d0d19

Please sign in to comment.