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

dynamodb_table_item create: use ConditionExpression #27517

Merged
merged 6 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions .changelog/27517.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_dynamodb_table_item: Allow the creation of items with the same hash key but different rage keys
```
9 changes: 3 additions & 6 deletions internal/service/dynamodb/table_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,9 @@ func resourceTableItemCreate(d *schema.ResourceData, meta interface{}) error {
_, err = conn.PutItem(&dynamodb.PutItemInput{
Item: attributes,
// Explode if item exists. We didn't create it.
Expected: map[string]*dynamodb.ExpectedAttributeValue{
hashKey: {
Exists: aws.Bool(false),
},
},
TableName: aws.String(tableName),
ConditionExpression: aws.String("attribute_not_exists(#hk)"),
ExpressionAttributeNames: aws.StringMap(map[string]string{"#hk": hashKey}),
TableName: aws.String(tableName),
})
if err != nil {
return err
Expand Down
77 changes: 77 additions & 0 deletions internal/service/dynamodb/table_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dynamodb_test

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -135,6 +136,82 @@ func TestAccDynamoDBTableItem_withMultipleItems(t *testing.T) {
})
}

func TestAccDynamoDBTableItem_withDuplicateItemsSameRangeKey(t *testing.T) {
tableName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
hashKey := "hashKey"
rangeKey := "rangeKey"
firstItem := `{
"hashKey": {"S": "something"},
"rangeKey": {"S": "first"},
"one": {"N": "11111"},
"two": {"N": "22222"},
"three": {"N": "33333"}
}`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, dynamodb.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckTableItemDestroy,
Steps: []resource.TestStep{
{
Config: testAccTableItemConfig_multiple(tableName, hashKey, rangeKey, firstItem, firstItem),
ExpectError: regexp.MustCompile(`ConditionalCheckFailedException: The conditional request failed`),
},
},
})
}

func TestAccDynamoDBTableItem_withDuplicateItemsDifferentRangeKey(t *testing.T) {
var conf1 dynamodb.GetItemOutput
var conf2 dynamodb.GetItemOutput

tableName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
hashKey := "hashKey"
rangeKey := "rangeKey"
firstItem := `{
"hashKey": {"S": "something"},
"rangeKey": {"S": "first"},
"one": {"N": "11111"},
"two": {"N": "22222"},
"three": {"N": "33333"}
}`
secondItem := `{
"hashKey": {"S": "something"},
"rangeKey": {"S": "second"},
"one": {"N": "11111"},
"two": {"N": "22222"},
"three": {"N": "33333"}
}`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, dynamodb.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckTableItemDestroy,
Steps: []resource.TestStep{
{
Config: testAccTableItemConfig_multiple(tableName, hashKey, rangeKey, firstItem, secondItem),
Check: resource.ComposeTestCheckFunc(
testAccCheckTableItemExists("aws_dynamodb_table_item.test1", &conf1),
testAccCheckTableItemExists("aws_dynamodb_table_item.test2", &conf2),
testAccCheckTableItemCount(tableName, 2),

resource.TestCheckResourceAttr("aws_dynamodb_table_item.test1", "hash_key", hashKey),
resource.TestCheckResourceAttr("aws_dynamodb_table_item.test1", "range_key", rangeKey),
resource.TestCheckResourceAttr("aws_dynamodb_table_item.test1", "table_name", tableName),
resource.TestCheckResourceAttr("aws_dynamodb_table_item.test1", "item", firstItem+"\n"),

resource.TestCheckResourceAttr("aws_dynamodb_table_item.test2", "hash_key", hashKey),
resource.TestCheckResourceAttr("aws_dynamodb_table_item.test2", "range_key", rangeKey),
resource.TestCheckResourceAttr("aws_dynamodb_table_item.test2", "table_name", tableName),
resource.TestCheckResourceAttr("aws_dynamodb_table_item.test2", "item", secondItem+"\n"),
),
},
},
})
}

func TestAccDynamoDBTableItem_wonkyItems(t *testing.T) {
var conf1 dynamodb.GetItemOutput

Expand Down