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_dynamodb_table: Allow disabling stream w/ empty view type #3197

Merged
merged 1 commit into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion aws/resource_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func resourceAwsDynamoDbTable() *schema.Resource {
Update: schema.DefaultTimeout(10 * time.Minute),
},

CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error {
return validateDynamoDbStreamSpec(diff)
},

SchemaVersion: 1,
MigrateState: resourceAwsDynamoDbTableMigrateState,

Expand Down Expand Up @@ -173,7 +177,6 @@ func resourceAwsDynamoDbTable() *schema.Resource {
"stream_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"stream_view_type": {
Type: schema.TypeString,
Expand Down
72 changes: 32 additions & 40 deletions aws/resource_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -316,18 +317,39 @@ func TestAccAWSDynamoDbTable_streamSpecification(t *testing.T) {
CheckDestroy: testAccCheckAWSDynamoDbTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDynamoDbConfigStreamSpecification(),
Config: testAccAWSDynamoDbConfigStreamSpecification(true, "KEYS_ONLY"),
Check: resource.ComposeTestCheckFunc(
testAccCheckInitialAWSDynamoDbTableExists("aws_dynamodb_table.basic-dynamodb-table", &conf),
testAccCheckInitialAWSDynamoDbTableConf("aws_dynamodb_table.basic-dynamodb-table"),
resource.TestCheckResourceAttr(
"aws_dynamodb_table.basic-dynamodb-table", "stream_enabled", "true"),
resource.TestCheckResourceAttr(
"aws_dynamodb_table.basic-dynamodb-table", "stream_view_type", "KEYS_ONLY"),
resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_enabled", "true"),
resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "stream_view_type", "KEYS_ONLY"),
resource.TestCheckResourceAttrSet("aws_dynamodb_table.basic-dynamodb-table", "stream_arn"),
resource.TestCheckResourceAttrSet("aws_dynamodb_table.basic-dynamodb-table", "stream_label"),
),
},
{
Config: testAccAWSDynamoDbConfigStreamSpecification(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"),
),
},
},
})
}

func TestAccAWSDynamoDbTable_streamSpecificationValidation(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDynamoDbTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDynamoDbConfigStreamSpecification(true, ""),
ExpectError: regexp.MustCompile(`stream_view_type is required when stream_enabled = true$`),
},
},
})
}
Expand Down Expand Up @@ -950,53 +972,23 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" {
}`, rName)
}

func testAccAWSDynamoDbConfigStreamSpecification() string {
func testAccAWSDynamoDbConfigStreamSpecification(enabled bool, viewType string) string {
return fmt.Sprintf(`
resource "aws_dynamodb_table" "basic-dynamodb-table" {
name = "TerraformTestStreamTable-%d"
read_capacity = 10
write_capacity = 20
hash_key = "TestTableHashKey"
range_key = "TestTableRangeKey"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making the configuration more relevant to what we're trying to test here 👍


attribute {
name = "TestTableHashKey"
type = "S"
}

attribute {
name = "TestTableRangeKey"
type = "S"
}

attribute {
name = "TestLSIRangeKey"
type = "N"
}

attribute {
name = "TestGSIRangeKey"
type = "S"
}

local_secondary_index {
name = "TestTableLSI"
range_key = "TestLSIRangeKey"
projection_type = "ALL"
}

global_secondary_index {
name = "InitialTestTableGSI"
hash_key = "TestTableHashKey"
range_key = "TestGSIRangeKey"
write_capacity = 10
read_capacity = 10
projection_type = "KEYS_ONLY"
}
stream_enabled = true
stream_view_type = "KEYS_ONLY"
stream_enabled = %t
stream_view_type = "%s"
}
`, acctest.RandInt())
`, acctest.RandInt(), enabled, viewType)
}

func testAccAWSDynamoDbConfigTags() string {
Expand Down
21 changes: 21 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"errors"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -182,6 +183,11 @@ func validateDbParamGroupNamePrefix(v interface{}, k string) (ws []string, error

func validateStreamViewType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if value == "" {
return
}

viewTypes := map[string]bool{
"KEYS_ONLY": true,
"NEW_IMAGE": true,
Expand Down Expand Up @@ -2192,3 +2198,18 @@ func validateGuardDutyThreatIntelSetFormat(v interface{}, k string) (ws []string
errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, validType, value))
return
}

func validateDynamoDbStreamSpec(d *schema.ResourceDiff) error {
enabled := d.Get("stream_enabled").(bool)
if enabled {
if v, ok := d.GetOk("stream_view_type"); ok {
value := v.(string)
if len(value) == 0 {
return errors.New("stream_view_type must be non-empty when stream_enabled = true")
}
return nil
}
return errors.New("stream_view_type is required when stream_enabled = true")
}
return nil
}