Skip to content
Open
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
29 changes: 29 additions & 0 deletions common/aws/dynamodb_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package aws

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)

type DynamoDBClient interface {
GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
}
45 changes: 45 additions & 0 deletions common/aws/mock/dynamodb_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package awsmock

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)

type DynamoDBClient struct {
GetItemFn func(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
PutItemFn func(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
DeleteItemFn func(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
TransactWriteItemsFn func(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
}

func (m DynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error) {
return m.GetItemFn(ctx, params, optFns...)
}

func (m DynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error) {
return m.PutItemFn(ctx, params, optFns...)
}

func (m DynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error) {
return m.DeleteItemFn(ctx, params, optFns...)
}

func (m DynamoDBClient) TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error) {
return m.TransactWriteItemsFn(ctx, params, optFns...)
}
22 changes: 14 additions & 8 deletions state/aws/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type StateStore struct {
ttlAttributeName string
partitionKey string

dynamodbClient *dynamodb.Client
dynamodbClient awsCommon.DynamoDBClient
}

type dynamoDBMetadata struct {
Expand Down Expand Up @@ -88,8 +88,13 @@ func NewDynamoDBStateStore(logger logger.Logger) state.Store {
return s
}

// Init does metadata and connection parsing.
// Init does metadata and connection parsing
func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
return d.InitWithOptions(ctx, metadata)
}

// InitWithOptions does metadata and connection parsing and extra aws options
func (d *StateStore) InitWithOptions(ctx context.Context, metadata state.Metadata, opts ...awsCommon.ConfigOption) error {
meta, err := d.getDynamoDBMetadata(metadata)
if err != nil {
return err
Expand All @@ -107,13 +112,14 @@ func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
SessionToken: meta.SessionToken,
}

awsConfig, err := awsCommon.NewConfig(ctx, configOpts)
if err != nil {
return err
}

d.dynamodbClient = dynamodb.NewFromConfig(awsConfig)
if d.dynamodbClient == nil {
awsConfig, err := awsCommon.NewConfig(ctx, configOpts, opts...)
if err != nil {
return err
}

d.dynamodbClient = dynamodb.NewFromConfig(awsConfig)
}
d.table = meta.Table
d.ttlAttributeName = meta.TTLAttributeName
d.partitionKey = meta.PartitionKey
Expand Down
Loading