Skip to content

Commit 62e961d

Browse files
committed
Added first Go V2 code example with unit test for DynamoDB
1 parent 1111d65 commit 62e961d

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
8.35 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
8+
"github.com/aws/aws-sdk-go-v2/config"
9+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
10+
)
11+
12+
// DynamoDBDescribeTableAPI defines the interface for DescribeTable function
13+
type DynamoDBDescribeTableAPI interface {
14+
DescribeTable(ctx context.Context,
15+
params *dynamodb.DescribeTableInput,
16+
optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
17+
}
18+
19+
// GetTableInfo retrieves information about the tabl
20+
func GetTableInfo(c context.Context, api DynamoDBDescribeTableAPI, input *dynamodb.DescribeTableInput) (*dynamodb.DescribeTableOutput, error) {
21+
resp, err := api.DescribeTable(c, input)
22+
23+
return resp, err
24+
}
25+
26+
func main() {
27+
table := flag.String("t", "", "The name of the table")
28+
region := flag.String("r", "us-west-2", "The region")
29+
flag.Parse()
30+
31+
if *table == "" {
32+
fmt.Println("You must specify a table name (-t TABLE)")
33+
return
34+
}
35+
36+
// Using the SDK's default configuration, loading additional config
37+
// and credentials values from the environment variables, shared
38+
// credentials, and shared configuration files
39+
cfg, err := config.LoadDefaultConfig(config.WithRegion(*region))
40+
if err != nil {
41+
panic("unable to load SDK config, " + err.Error())
42+
}
43+
44+
// Using the Config value, create the DynamoDB client
45+
// Create a new DynamoDB Service Client
46+
client := dynamodb.NewFromConfig(cfg)
47+
48+
// Build the request with its input parameters
49+
resp, err := client.DescribeTable(context.Background(), &dynamodb.DescribeTableInput{
50+
TableName: table,
51+
})
52+
if err != nil {
53+
panic("failed to describe table, " + err.Error())
54+
}
55+
56+
fmt.Println("Info about " + *table + ":")
57+
fmt.Println(" #items: ", *resp.Table.ItemCount)
58+
fmt.Println(" Size (bytes)", *resp.Table.TableSizeBytes)
59+
fmt.Println(" Status: ", string(resp.Table.TableStatus))
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
9+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
10+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
11+
)
12+
13+
type DynamoDBDescribeTableImpl struct{}
14+
15+
func (dt DynamoDBDescribeTableImpl) DescribeTable(ctx context.Context,
16+
params *dynamodb.DescribeTableInput,
17+
optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error) {
18+
desc := &types.TableDescription{
19+
ItemCount: aws.Int64(1),
20+
TableSizeBytes: aws.Int64(64),
21+
TableStatus: "Active",
22+
}
23+
24+
output := &dynamodb.DescribeTableOutput{
25+
Table: desc,
26+
}
27+
28+
return output, nil
29+
}
30+
31+
func TestDescribeTable(t *testing.T) {
32+
tableName := "MyGroovyTable"
33+
34+
// Build the request with its input parameters
35+
input := dynamodb.DescribeTableInput{
36+
TableName: &tableName,
37+
}
38+
39+
api := &DynamoDBDescribeTableImpl{}
40+
41+
resp, err := GetTableInfo(context.Background(), *api, &input)
42+
if err != nil {
43+
t.Log("Got an error retrieving the table status:")
44+
t.Log(err)
45+
return
46+
}
47+
48+
t.Log("Info about " + tableName + ":")
49+
t.Log(" #items: ", *resp.Table.ItemCount)
50+
t.Log(" Size (bytes)", *resp.Table.TableSizeBytes)
51+
t.Log(" Status: ", string(resp.Table.TableStatus))
52+
}

0 commit comments

Comments
 (0)