Skip to content

Commit 2dee3df

Browse files
committed
chore: Use aws client v2 in dynamodb tests
Signed-off-by: Javier Aliaga <[email protected]>
1 parent c30af7d commit 2dee3df

File tree

4 files changed

+423
-541
lines changed

4 files changed

+423
-541
lines changed

common/aws/dynamodb_client.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package aws
17+
18+
import (
19+
"context"
20+
21+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
22+
)
23+
24+
type DynamoDBClient interface {
25+
GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
26+
PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
27+
DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
28+
TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
29+
}

common/aws/mock/dynamodb_client.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2025 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package awsmock
17+
18+
import (
19+
"context"
20+
21+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
22+
)
23+
24+
type DynamoDBClient struct {
25+
GetItemFn func(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
26+
PutItemFn func(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
27+
DeleteItemFn func(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
28+
TransactWriteItemsFn func(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
29+
}
30+
31+
func (m DynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error) {
32+
return m.GetItemFn(ctx, params, optFns...)
33+
}
34+
35+
func (m DynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error) {
36+
return m.PutItemFn(ctx, params, optFns...)
37+
}
38+
39+
func (m DynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error) {
40+
return m.DeleteItemFn(ctx, params, optFns...)
41+
}
42+
43+
func (m DynamoDBClient) TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error) {
44+
return m.TransactWriteItemsFn(ctx, params, optFns...)
45+
}

state/aws/dynamodb/dynamodb.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type StateStore struct {
4949
ttlAttributeName string
5050
partitionKey string
5151

52-
dynamodbClient *dynamodb.Client
52+
dynamodbClient awsCommon.DynamoDBClient
5353
}
5454

5555
type dynamoDBMetadata struct {
@@ -88,8 +88,13 @@ func NewDynamoDBStateStore(logger logger.Logger) state.Store {
8888
return s
8989
}
9090

91-
// Init does metadata and connection parsing.
91+
// Init does metadata and connection parsing
9292
func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
93+
return d.InitWithOptions(ctx, metadata)
94+
}
95+
96+
// InitWithOptions does metadata and connection parsing and extra aws options
97+
func (d *StateStore) InitWithOptions(ctx context.Context, metadata state.Metadata, opts ...awsCommon.ConfigOption) error {
9398
meta, err := d.getDynamoDBMetadata(metadata)
9499
if err != nil {
95100
return err
@@ -107,13 +112,14 @@ func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
107112
SessionToken: meta.SessionToken,
108113
}
109114

110-
awsConfig, err := awsCommon.NewConfig(ctx, configOpts)
111-
if err != nil {
112-
return err
113-
}
114-
115-
d.dynamodbClient = dynamodb.NewFromConfig(awsConfig)
115+
if d.dynamodbClient == nil {
116+
awsConfig, err := awsCommon.NewConfig(ctx, configOpts, opts...)
117+
if err != nil {
118+
return err
119+
}
116120

121+
d.dynamodbClient = dynamodb.NewFromConfig(awsConfig)
122+
}
117123
d.table = meta.Table
118124
d.ttlAttributeName = meta.TTLAttributeName
119125
d.partitionKey = meta.PartitionKey

0 commit comments

Comments
 (0)