Skip to content

Commit

Permalink
DynamoDB Deprecation Fixes (#4534)
Browse files Browse the repository at this point in the history
* Use the AWS SDK's UnmarshalMap method for dynamodb backend, not the deprecated ConvertFromMap method

* Use the AWS SDK's MarshalMap method for dynamodb backend, not the deprecated ConvertToMap method

* Use the AWS SDK's session.NewSession method for dynamodb backend, not the deprecated session.New method

* Fix variable name awserr that colides with imported package in dynamodb backend
  • Loading branch information
MattSurabian authored and jefferai committed May 10, 2018
1 parent 2f6fb5b commit 39f2d66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
20 changes: 13 additions & 7 deletions physical/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,13 @@ func NewDynamoDBBackend(conf map[string]string, logger log.Logger) (physical.Bac
Transport: pooledTransport,
}).
WithMaxRetries(dynamodbMaxRetry)
client := dynamodb.New(session.New(awsConf))

awsSession, err := session.NewSession(awsConf)
if err != nil {
return nil, errwrap.Wrapf("Could not establish AWS session: {{err}}", err)
}

client := dynamodb.New(awsSession)

if err := ensureTableExists(client, table, readCapacity, writeCapacity); err != nil {
return nil, err
Expand Down Expand Up @@ -259,7 +265,7 @@ func (d *DynamoDBBackend) Put(ctx context.Context, entry *physical.Entry) error
Key: recordKeyForVaultKey(entry.Key),
Value: entry.Value,
}
item, err := dynamodbattribute.ConvertToMap(record)
item, err := dynamodbattribute.MarshalMap(record)
if err != nil {
return errwrap.Wrapf("could not convert prefix record to DynamoDB item: {{err}}", err)
}
Expand All @@ -274,7 +280,7 @@ func (d *DynamoDBBackend) Put(ctx context.Context, entry *physical.Entry) error
Path: recordPathForVaultKey(prefix),
Key: fmt.Sprintf("%s/", recordKeyForVaultKey(prefix)),
}
item, err := dynamodbattribute.ConvertToMap(record)
item, err := dynamodbattribute.MarshalMap(record)
if err != nil {
return errwrap.Wrapf("could not convert prefix record to DynamoDB item: {{err}}", err)
}
Expand Down Expand Up @@ -311,7 +317,7 @@ func (d *DynamoDBBackend) Get(ctx context.Context, key string) (*physical.Entry,
}

record := &DynamoDBRecord{}
if err := dynamodbattribute.ConvertFromMap(resp.Item, record); err != nil {
if err := dynamodbattribute.UnmarshalMap(resp.Item, record); err != nil {
return nil, err
}

Expand Down Expand Up @@ -385,7 +391,7 @@ func (d *DynamoDBBackend) List(ctx context.Context, prefix string) ([]string, er
err := d.client.QueryPages(queryInput, func(out *dynamodb.QueryOutput, lastPage bool) bool {
var record DynamoDBRecord
for _, item := range out.Items {
dynamodbattribute.ConvertFromMap(item, &record)
dynamodbattribute.UnmarshalMap(item, &record)
if !strings.HasPrefix(record.Key, DynamoDBLockPrefix) {
keys = append(keys, record.Key)
}
Expand Down Expand Up @@ -697,8 +703,8 @@ func ensureTableExists(client *dynamodb.DynamoDB, table string, readCapacity, wr
_, err := client.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(table),
})
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "ResourceNotFoundException" {
if awsError, ok := err.(awserr.Error); ok {
if awsError.Code() == "ResourceNotFoundException" {
_, err = client.CreateTable(&dynamodb.CreateTableInput{
TableName: aws.String(table),
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
Expand Down
18 changes: 14 additions & 4 deletions physical/dynamodb/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ func TestDynamoDBBackend(t *testing.T) {
region = "us-east-1"
}

conn := dynamodb.New(session.New(&aws.Config{
awsSession, err := session.NewSession(&aws.Config{
Credentials: credsProvider,
Endpoint: aws.String(endpoint),
Region: aws.String(region),
}))
})
if err != nil {
t.Fatalf("err: %v", err)
}

conn := dynamodb.New(awsSession)

var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int()
table := fmt.Sprintf("vault-dynamodb-testacc-%d", randInt)
Expand Down Expand Up @@ -80,11 +85,16 @@ func TestDynamoDBHABackend(t *testing.T) {
region = "us-east-1"
}

conn := dynamodb.New(session.New(&aws.Config{
awsSession, err := session.NewSession(&aws.Config{
Credentials: credsProvider,
Endpoint: aws.String(endpoint),
Region: aws.String(region),
}))
})
if err != nil {
t.Fatalf("err: %v", err)
}

conn := dynamodb.New(awsSession)

var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int()
table := fmt.Sprintf("vault-dynamodb-testacc-%d", randInt)
Expand Down

0 comments on commit 39f2d66

Please sign in to comment.