Skip to content

Commit

Permalink
feat: allow RFC3339 date for cdc_start_time
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychauvet committed Jun 13, 2023
1 parent d26698b commit 6ab260e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
17 changes: 14 additions & 3 deletions internal/service/dms/replication_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -134,9 +135,19 @@ func resourceReplicationTaskCreate(ctx context.Context, d *schema.ResourceData,
}

if v, ok := d.GetOk("cdc_start_time"); ok {
// Parse the RFC3339 date string into a time.Time object
dateTime, _ := time.Parse(time.RFC3339, v.(string))
request.CdcStartTime = aws.Time(dateTime)
// Check if input is RFC3339 date string or UNIX timestamp.
dateTime, err := time.Parse(time.RFC3339, v.(string))

if err != nil {
// Not a valid RF3339 date, checking if this is a UNIX timestamp.
seconds, err := strconv.ParseInt(v.(string), 10, 64)
if err != nil {
return sdkdiag.AppendErrorf(diags, "DMS create replication task. Invalid Unix timestamp given for cdc_start_time parameter: %s", err)
}
request.CdcStartTime = aws.Time(time.Unix(seconds, 0))
} else {
request.CdcStartTime = aws.Time(dateTime)
}
}

if v, ok := d.GetOk("replication_task_settings"); ok {
Expand Down
36 changes: 35 additions & 1 deletion internal/service/dms/replication_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
tfdms "github.com/hashicorp/terraform-provider-aws/internal/service/dms"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -180,7 +181,7 @@ func TestAccDMSReplicationTask_cdcStartPosition(t *testing.T) {
})
}

func TestAccDMSReplicationTask_cdcStartTime(t *testing.T) {
func TestAccDMSReplicationTask_cdcStartTime_rfc3339_date(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_dms_replication_task.test"
Expand Down Expand Up @@ -211,6 +212,39 @@ func TestAccDMSReplicationTask_cdcStartTime(t *testing.T) {
})
}

func TestAccDMSReplicationTask_cdcStartTime_unix_timestamp(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_dms_replication_task.test"

currentTime := time.Now().UTC()
rfc3339Time := currentTime.Format(time.RFC3339)
awsDmsExpectedOutput := strings.TrimRight(rfc3339Time, "Z") // AWS API drop "Z" part.
dateTime, _ := time.Parse(time.RFC3339, rfc3339Time)
unixDateTime := strconv.Itoa(int(dateTime.Unix()))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, dms.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckReplicationTaskDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccReplicationTaskConfig_cdcStartTime(rName, unixDateTime),
Check: resource.ComposeTestCheckFunc(
testAccCheckReplicationTaskExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "cdc_start_position", awsDmsExpectedOutput),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerifyIgnore: []string{"start_replication_task"},
},
},
})
}

func TestAccDMSReplicationTask_startReplicationTask(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
Expand Down

0 comments on commit 6ab260e

Please sign in to comment.