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

[WIP] r/aws_dynamodb_table: Support 11/15/2018 Server-Side Encryption enhancements #5666

Closed
wants to merge 1 commit into from
Closed
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
126 changes: 77 additions & 49 deletions aws/data_source_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,30 @@ func dataSourceAwsDynamoDbTable() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDynamoDbTableRead,
Schema: map[string]*schema.Schema{
"arn": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reordered the data_source_aws_dynamodb_table attributes to match resource_aws_dynamodb_table for easier checking of missing attributes.

Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"arn": {
"hash_key": {
Type: schema.TypeString,
Computed: true,
},
"range_key": {
Type: schema.TypeString,
Computed: true,
},
"read_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"write_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"attribute": {
Type: schema.TypeSet,
Computed: true,
Expand All @@ -45,24 +61,29 @@ func dataSourceAwsDynamoDbTable() *schema.Resource {
return hashcode.String(buf.String())
},
},
"global_secondary_index": {
"ttl": {
Type: schema.TypeSet,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
"attribute_name": {
Type: schema.TypeString,
Computed: true,
},
"write_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"read_capacity": {
Type: schema.TypeInt,
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"hash_key": {
},
},
},
"local_secondary_index": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -81,12 +102,14 @@ func dataSourceAwsDynamoDbTable() *schema.Resource {
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
return hashcode.String(buf.String())
},
},
"hash_key": {
Type: schema.TypeString,
Computed: true,
},
"local_secondary_index": {
"global_secondary_index": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Expand All @@ -95,6 +118,18 @@ func dataSourceAwsDynamoDbTable() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"write_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"read_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"hash_key": {
Type: schema.TypeString,
Computed: true,
},
"range_key": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -110,33 +145,11 @@ func dataSourceAwsDynamoDbTable() *schema.Resource {
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
return hashcode.String(buf.String())
},
},
"range_key": {
Type: schema.TypeString,
Computed: true,
},
"read_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"stream_arn": {
Type: schema.TypeString,
Computed: true,
},
"stream_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"stream_label": {
Type: schema.TypeString,
Computed: true,
},
"stream_view_type": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -145,31 +158,38 @@ func dataSourceAwsDynamoDbTable() *schema.Resource {
return strings.ToUpper(value)
},
},
"tags": tagsSchemaComputed(),
"ttl": {
Type: schema.TypeSet,
"stream_arn": {
Type: schema.TypeString,
Computed: true,
},
"stream_label": {
Type: schema.TypeString,
Computed: true,
},
"server_side_encryption": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"attribute_name": {
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"sse_type": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
"kms_master_key_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"write_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"server_side_encryption": {
"tags": tagsSchemaComputed(),
"point_in_time_recovery": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added point_in_time_recovery as the documentation says that the data source returns all the same attributes as the resource.

Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -222,5 +242,13 @@ func dataSourceAwsDynamoDbTableRead(d *schema.ResourceData, meta interface{}) er
}
d.Set("tags", tags)

pitrOut, err := conn.DescribeContinuousBackups(&dynamodb.DescribeContinuousBackupsInput{
TableName: aws.String(d.Id()),
})
if err != nil && !isAWSErr(err, "UnknownOperationException", "") {
return err
}
d.Set("point_in_time_recovery", flattenDynamoDbPitr(pitrOut))

return nil
}
3 changes: 2 additions & 1 deletion aws/data_source_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func TestAccDataSourceAwsDynamoDbTable_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "tags.%", "2"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "tags.Name", "dynamodb-table-1"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "tags.Environment", "test"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "server_side_encryption.#", "0"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "server_side_encryption.#", "1"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "server_side_encryption.0.enabled", "false"),
),
},
},
Expand Down
54 changes: 45 additions & 9 deletions aws/resource_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -46,6 +47,29 @@ func resourceAwsDynamoDbTable() *schema.Resource {
if isDynamoDbTableOptionDisabled(o) && isDynamoDbTableOptionDisabled(n) {
return diff.Clear("server_side_encryption")
}

// Ensure that no diff is shown if the persisted KMS key ID is the default DynamoDB KMS Master Key.
if !isDynamoDbTableOptionDisabled(o) && !isDynamoDbTableOptionDisabled(n) {
oldKeyType := o.([]interface{})[0].(map[string]interface{})["sse_type"].(string)
newKeyType := n.([]interface{})[0].(map[string]interface{})["sse_type"].(string)
if oldKeyType == newKeyType && oldKeyType == dynamodb.SSETypeKms {
const defaultKmsKeyAlias = "alias/aws/dynamodb"
oldKmsKeyId := o.([]interface{})[0].(map[string]interface{})["kms_master_key_id"].(string)
newKmsKeyId := n.([]interface{})[0].(map[string]interface{})["kms_master_key_id"].(string)
if newKmsKeyId == "" || newKmsKeyId == defaultKmsKeyAlias {
resp, err := v.(*AWSClient).kmsconn.DescribeKey(&kms.DescribeKeyInput{
KeyId: aws.String(defaultKmsKeyAlias),
})
if err != nil {
return err
}

if oldKmsKeyId == aws.StringValue(resp.KeyMetadata.Arn) {
return diff.Clear("server_side_encryption")
}
}
}
}
}
return nil
},
Expand Down Expand Up @@ -241,7 +265,24 @@ func resourceAwsDynamoDbTable() *schema.Resource {
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
Optional: true,
Default: false,
ForceNew: true,
},
"sse_type": {
Type: schema.TypeString,
Optional: true,
Default: dynamodb.SSETypeKms,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
// "One or more parameter values were invalid: SSEType AES256 is not supported"
// dynamodb.SSETypeAes256,
dynamodb.SSETypeKms,
}, false),
},
"kms_master_key_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
Expand Down Expand Up @@ -315,14 +356,9 @@ func resourceAwsDynamoDbTableCreate(d *schema.ResourceData, meta interface{}) er
}
}

if v, ok := d.GetOk("server_side_encryption"); ok {
options := v.([]interface{})
if options[0] == nil {
return fmt.Errorf("At least one field is expected inside server_side_encryption")
}

s := options[0].(map[string]interface{})
req.SSESpecification = expandDynamoDbEncryptAtRestOptions(s)
if v, ok := d.GetOk("server_side_encryption"); ok && len(v.([]interface{})) > 0 {
sseOptions := v.([]interface{})
req.SSESpecification = expandDynamoDbEncryptAtRestOptions(sseOptions[0].(map[string]interface{}))
}

var output *dynamodb.CreateTableOutput
Expand Down
Loading