Skip to content

Commit

Permalink
Merge pull request #3224 from terraform-providers/b-dynamodb-table-fi…
Browse files Browse the repository at this point in the history
…x-stream-disabling

resource/aws_dynamodb_table: (actually) fix disabling stream
  • Loading branch information
radeksimko authored Feb 1, 2018
2 parents a0f8229 + 04fa88e commit 390263b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
14 changes: 9 additions & 5 deletions aws/resource_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,16 @@ func resourceAwsDynamoDbTableUpdate(d *schema.ResourceData, meta interface{}) er
}

if (d.HasChange("stream_enabled") || d.HasChange("stream_view_type")) && !d.IsNewResource() {
toEnable := d.Get("stream_enabled").(bool)
streamSpec := dynamodb.StreamSpecification{
StreamEnabled: aws.Bool(toEnable),
}
if toEnable {
streamSpec.StreamViewType = aws.String(d.Get("stream_view_type").(string))
}
input := &dynamodb.UpdateTableInput{
TableName: aws.String(d.Id()),
StreamSpecification: &dynamodb.StreamSpecification{
StreamEnabled: aws.Bool(d.Get("stream_enabled").(bool)),
StreamViewType: aws.String(d.Get("stream_view_type").(string)),
},
TableName: aws.String(d.Id()),
StreamSpecification: &streamSpec,
}
_, err := conn.UpdateTable(input)
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions aws/resource_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,15 @@ func TestAccAWSDynamoDbTable_basic(t *testing.T) {
func TestAccAWSDynamoDbTable_streamSpecification(t *testing.T) {
var conf dynamodb.DescribeTableOutput

tableName := fmt.Sprintf("TerraformTestStreamTable-%s", acctest.RandString(8))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDynamoDbTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDynamoDbConfigStreamSpecification(true, "KEYS_ONLY"),
Config: testAccAWSDynamoDbConfigStreamSpecification(tableName, true, "KEYS_ONLY"),
Check: resource.ComposeTestCheckFunc(
testAccCheckInitialAWSDynamoDbTableExists("aws_dynamodb_table.basic-dynamodb-table", &conf),
resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_enabled", "true"),
Expand All @@ -327,13 +329,13 @@ func TestAccAWSDynamoDbTable_streamSpecification(t *testing.T) {
),
},
{
Config: testAccAWSDynamoDbConfigStreamSpecification(false, ""),
Config: testAccAWSDynamoDbConfigStreamSpecification(tableName, false, ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckInitialAWSDynamoDbTableExists("aws_dynamodb_table.basic-dynamodb-table", &conf),
resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_enabled", "false"),
resource.TestCheckNoResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_view_type"),
resource.TestCheckNoResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_arn"),
resource.TestCheckNoResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_label"),
resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_view_type", ""),
resource.TestCheckResourceAttrSet("aws_dynamodb_table.basic-dynamodb-table", "stream_arn"),
resource.TestCheckResourceAttrSet("aws_dynamodb_table.basic-dynamodb-table", "stream_label"),
),
},
},
Expand All @@ -347,7 +349,7 @@ func TestAccAWSDynamoDbTable_streamSpecificationValidation(t *testing.T) {
CheckDestroy: testAccCheckAWSDynamoDbTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDynamoDbConfigStreamSpecification(true, ""),
Config: testAccAWSDynamoDbConfigStreamSpecification("anything", true, ""),
ExpectError: regexp.MustCompile(`stream_view_type is required when stream_enabled = true$`),
},
},
Expand Down Expand Up @@ -972,10 +974,10 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" {
}`, rName)
}

func testAccAWSDynamoDbConfigStreamSpecification(enabled bool, viewType string) string {
func testAccAWSDynamoDbConfigStreamSpecification(tableName string, enabled bool, viewType string) string {
return fmt.Sprintf(`
resource "aws_dynamodb_table" "basic-dynamodb-table" {
name = "TerraformTestStreamTable-%d"
name = "%s"
read_capacity = 10
write_capacity = 20
hash_key = "TestTableHashKey"
Expand All @@ -988,7 +990,7 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" {
stream_enabled = %t
stream_view_type = "%s"
}
`, acctest.RandInt(), enabled, viewType)
`, tableName, enabled, viewType)
}

func testAccAWSDynamoDbConfigTags() string {
Expand Down
8 changes: 6 additions & 2 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3287,10 +3287,14 @@ func flattenAwsDynamoDbTableResource(d *schema.ResourceData, table *dynamodb.Tab
if table.StreamSpecification != nil {
d.Set("stream_view_type", table.StreamSpecification.StreamViewType)
d.Set("stream_enabled", table.StreamSpecification.StreamEnabled)
d.Set("stream_arn", table.LatestStreamArn)
d.Set("stream_label", table.LatestStreamLabel)
} else {
d.Set("stream_view_type", "")
d.Set("stream_enabled", false)
}

d.Set("stream_arn", table.LatestStreamArn)
d.Set("stream_label", table.LatestStreamLabel)

err = d.Set("global_secondary_index", gsiList)
if err != nil {
return err
Expand Down

0 comments on commit 390263b

Please sign in to comment.