-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1147be
commit 1a65ad3
Showing
6 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,9 @@ jobs: | |
- name: Install Linux packages | ||
run: sudo apt install -y --no-install-recommends pandoc | ||
|
||
- name: Install and start DynamoDB | ||
uses: rrainn/[email protected] | ||
|
||
- name: Install Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dynamo | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
) | ||
|
||
// Service is simple abstraction layer to work with a DynamoDB-compatible database | ||
type Service struct { | ||
Client *dynamodb.Client | ||
tablePrefix string | ||
} | ||
|
||
// NewService creates a new S3 service with the given credentials and configuration | ||
func NewService(accessKey, secretKey, region, endpoint, tablePrefix string) (*Service, error) { | ||
opts := []func(*config.LoadOptions) error{config.WithRegion(region)} | ||
|
||
if accessKey != "" && secretKey != "" { | ||
opts = append(opts, config.WithCredentialsProvider(credentials.StaticCredentialsProvider{Value: aws.Credentials{ | ||
AccessKeyID: accessKey, SecretAccessKey: secretKey, | ||
}})) | ||
} | ||
|
||
cfg, err := config.LoadDefaultConfig(context.TODO(), opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := dynamodb.NewFromConfig(cfg, func(o *dynamodb.Options) { | ||
if endpoint != "" { | ||
o.BaseEndpoint = aws.String(endpoint) | ||
} | ||
}) | ||
|
||
return &Service{Client: client, tablePrefix: tablePrefix}, nil | ||
} | ||
|
||
func (s *Service) Test(ctx context.Context, table string) error { | ||
_, err := s.Client.DescribeTable(ctx, &dynamodb.DescribeTableInput{TableName: aws.String(s.TableName(table))}) | ||
return err | ||
} | ||
|
||
func (s *Service) TableName(base string) string { | ||
return s.tablePrefix + base | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package dynamo_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/nyaruka/gocommon/aws/dynamo" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestService(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
svc, err := dynamo.NewService("root", "tembatemba", "us-east-1", "http://localhost:6000", "Test") | ||
assert.NoError(t, err) | ||
|
||
err = svc.Test(ctx, "Things") | ||
assert.ErrorContains(t, err, "ResourceNotFoundException") | ||
|
||
_, err = svc.Client.CreateTable(ctx, &dynamodb.CreateTableInput{ | ||
TableName: aws.String("TestThings"), | ||
KeySchema: []types.KeySchemaElement{ | ||
{AttributeName: aws.String("UUID"), KeyType: types.KeyTypeHash}, | ||
}, | ||
AttributeDefinitions: []types.AttributeDefinition{ | ||
{AttributeName: aws.String("UUID"), AttributeType: types.ScalarAttributeTypeS}, | ||
}, | ||
BillingMode: types.BillingModePayPerRequest, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
err = svc.Test(ctx, "Things") | ||
assert.NoError(t, err) | ||
|
||
_, err = svc.Client.DeleteTable(ctx, &dynamodb.DeleteTableInput{TableName: aws.String("TestThings")}) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters