From edb26df626b30166f69d5f43dd26d7dce8c7b242 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 12 Dec 2017 04:58:33 -0500 Subject: [PATCH] r/aws_dynamodb_table: Ensure ttl is properly read (#2452) * r/aws_dynamodb_table: Ensure ttl is properly read * r/aws_dynamodb_table: #2452 review updates * Add timeToLiveOutput.TimeToLiveDescription nil check * Simplify logic to d.Set ttl --- aws/import_aws_dynamodb_table_test.go | 22 ++++++++++++++++++++++ aws/resource_aws_dynamodb_table.go | 22 ++++++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/aws/import_aws_dynamodb_table_test.go b/aws/import_aws_dynamodb_table_test.go index 00fa2169dff..666573c71b2 100644 --- a/aws/import_aws_dynamodb_table_test.go +++ b/aws/import_aws_dynamodb_table_test.go @@ -49,3 +49,25 @@ func TestAccAWSDynamoDbTable_importTags(t *testing.T) { }, }) } + +func TestAccAWSDynamoDbTable_importTimeToLive(t *testing.T) { + resourceName := "aws_dynamodb_table.basic-dynamodb-table" + rName := acctest.RandomWithPrefix("TerraformTestTable-") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDynamoDbTableDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDynamoDbConfigAddTimeToLive(rName), + }, + + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/aws/resource_aws_dynamodb_table.go b/aws/resource_aws_dynamodb_table.go index 7cea1895aa1..a3d71fd8b5c 100644 --- a/aws/resource_aws_dynamodb_table.go +++ b/aws/resource_aws_dynamodb_table.go @@ -816,15 +816,21 @@ func flattenAwsDynamoDbTableResource(d *schema.ResourceData, meta interface{}, t if err != nil { return err } - timeToLive := []interface{}{} - attribute := map[string]*string{ - "name": timeToLiveOutput.TimeToLiveDescription.AttributeName, - "type": timeToLiveOutput.TimeToLiveDescription.TimeToLiveStatus, - } - timeToLive = append(timeToLive, attribute) - d.Set("timeToLive", timeToLive) - log.Printf("[DEBUG] Loaded TimeToLive data for DynamoDB table '%s'", d.Id()) + if timeToLiveOutput.TimeToLiveDescription != nil && timeToLiveOutput.TimeToLiveDescription.AttributeName != nil { + timeToLiveList := []interface{}{ + map[string]interface{}{ + "attribute_name": *timeToLiveOutput.TimeToLiveDescription.AttributeName, + "enabled": (*timeToLiveOutput.TimeToLiveDescription.TimeToLiveStatus == dynamodb.TimeToLiveStatusEnabled), + }, + } + err := d.Set("ttl", timeToLiveList) + if err != nil { + return err + } + + log.Printf("[DEBUG] Loaded TimeToLive data for DynamoDB table '%s'", d.Id()) + } tags, err := readTableTags(d, meta) if err != nil {