Skip to content

Commit cd25530

Browse files
committed
Added snippet tags to DynamoDB DescribeTable code example in Go v2
1 parent 065a618 commit cd25530

File tree

6 files changed

+216
-84
lines changed

6 files changed

+216
-84
lines changed
-8.35 MB
Binary file not shown.
+60-45
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,75 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
// snippet-start:[dynamodb.gov2.DescribeTable]
14
package main
25

6+
// snippet-start:[dynamodb.gov2.DescribeTable.imports]
37
import (
4-
"context"
5-
"flag"
6-
"fmt"
8+
"context"
9+
"flag"
10+
"fmt"
711

8-
"github.com/aws/aws-sdk-go-v2/config"
9-
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
12+
"github.com/aws/aws-sdk-go-v2/config"
13+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
1014
)
15+
// snippet-end:[dynamodb.gov2.DescribeTable.imports]
1116

12-
// DynamoDBDescribeTableAPI defines the interface for DescribeTable function
17+
// DynamoDBDescribeTableAPI defines the interface for DescribeTable function.
18+
// snippet-start:[dynamodb.gov2.DescribeTable.interface]
1319
type DynamoDBDescribeTableAPI interface {
14-
DescribeTable(ctx context.Context,
15-
params *dynamodb.DescribeTableInput,
16-
optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
20+
DescribeTable(ctx context.Context,
21+
params *dynamodb.DescribeTableInput,
22+
optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
1723
}
24+
// snippet-end:[dynamodb.gov2.DescribeTable.interface]
1825

19-
// GetTableInfo retrieves information about the tabl
26+
// GetTableInfo retrieves information about the table.
27+
// snippet-start:[dynamodb.gov2.DescribeTable.GetTableInfo]
2028
func GetTableInfo(c context.Context, api DynamoDBDescribeTableAPI, input *dynamodb.DescribeTableInput) (*dynamodb.DescribeTableOutput, error) {
21-
resp, err := api.DescribeTable(c, input)
29+
resp, err := api.DescribeTable(c, input)
2230

23-
return resp, err
31+
return resp, err
2432
}
33+
// snippet-end:[dynamodb.gov2.DescribeTable.GetTableInfo]
2534

2635
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))
36+
// snippet-start:[dynamodb.gov2.DescribeTable.args]
37+
table := flag.String("t", "", "The name of the table")
38+
flag.Parse()
39+
40+
if *table == "" {
41+
fmt.Println("You must specify a table name (-t TABLE)")
42+
return
43+
}
44+
// snippet-end:[dynamodb.gov2.DescribeTable.args]
45+
46+
// snippet-start:[dynamodb.gov2.DescribeTable.config_and_client]
47+
// Use the SDK's default configuration.
48+
cfg, err := config.LoadDefaultConfig()
49+
if err != nil {
50+
panic("unable to load SDK config, " + err.Error())
51+
}
52+
53+
// Using the Config value, create the DynamoDB client
54+
// Create a new DynamoDB Service Client
55+
client := dynamodb.NewFromConfig(cfg)
56+
// snippet-end:[dynamodb.gov2.DescribeTable.config_and_client]
57+
58+
// snippet-start:[dynamodb.gov2.DescribeTable.call]
59+
// Build the request with its input parameters
60+
resp, err := client.DescribeTable(context.Background(), &dynamodb.DescribeTableInput{
61+
TableName: table,
62+
})
63+
if err != nil {
64+
panic("failed to describe table, " + err.Error())
65+
}
66+
// snippet-end:[dynamodb.gov2.DescribeTable.call]
67+
68+
// snippet-start:[dynamodb.gov2.DescribeTable.print]
69+
fmt.Println("Info about " + *table + ":")
70+
fmt.Println(" #items: ", *resp.Table.ItemCount)
71+
fmt.Println(" Size (bytes)", *resp.Table.TableSizeBytes)
72+
fmt.Println(" Status: ", string(resp.Table.TableStatus))
73+
// snippet-end:[dynamodb.gov2.DescribeTable.print]
6074
}
75+
// snippet-end:[dynamodb.gov2.DescribeTable]
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,91 @@
11
package main
22

33
import (
4-
"context"
5-
"testing"
4+
"context"
5+
"encoding/json"
6+
"io/ioutil"
7+
"testing"
8+
"time"
69

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"
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
12+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
1113
)
1214

1315
type DynamoDBDescribeTableImpl struct{}
1416

1517
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
18+
params *dynamodb.DescribeTableInput,
19+
optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error) {
20+
desc := &types.TableDescription{
21+
ItemCount: aws.Int64(1),
22+
TableSizeBytes: aws.Int64(64),
23+
TableStatus: "Active",
24+
}
25+
26+
output := &dynamodb.DescribeTableOutput{
27+
Table: desc,
28+
}
29+
30+
return output, nil
31+
}
32+
33+
type Config struct {
34+
Table string `json:"Table"`
35+
}
36+
37+
var configFileName = "config.json"
38+
39+
var globalConfig Config
40+
41+
func populateConfiguration(t *testing.T) error {
42+
content, err := ioutil.ReadFile(configFileName)
43+
if err != nil {
44+
return err
45+
}
46+
47+
text := string(content)
48+
49+
err = json.Unmarshal([]byte(text), &globalConfig)
50+
if err != nil {
51+
return err
52+
}
53+
54+
t.Log("Table: " + globalConfig.Table)
55+
56+
return nil
2957
}
3058

3159
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))
60+
thisTime := time.Now()
61+
nowString := thisTime.Format("2006-01-02 15:04:05 Monday")
62+
t.Log("Starting unit test at " + nowString)
63+
64+
err := populateConfiguration(t)
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
69+
if globalConfig.Table == "" {
70+
t.Fatal("You must set a value for Table in " + configFileName)
71+
}
72+
73+
// Build the request with its input parameters
74+
input := dynamodb.DescribeTableInput{
75+
TableName: &globalConfig.Table,
76+
}
77+
78+
api := &DynamoDBDescribeTableImpl{}
79+
80+
resp, err := GetTableInfo(context.Background(), *api, &input)
81+
if err != nil {
82+
t.Log("Got an error retrieving the table status:")
83+
t.Log(err)
84+
return
85+
}
86+
87+
t.Log("Info about " + globalConfig.Table + ":")
88+
t.Log(" #items: ", *resp.Table.ItemCount)
89+
t.Log(" Size (bytes)", *resp.Table.TableSizeBytes)
90+
t.Log(" Status: ", string(resp.Table.TableStatus))
5291
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Table": "MyGroovyTable"
3+
}

gov2/dynamodb/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# AWS SDK for Go V2 code examples for Amazon DynamoDB
2+
3+
## Purpose
4+
5+
These examples demonstrates how to perform several DynamoDB operations
6+
using version 2 of the AWS SDK for Go.
7+
8+
## Prerequisites
9+
10+
You must have an AWS account, and have your default credentials and AWS Region
11+
configured as described in
12+
[Configuring the AWS SDK for Go](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html)
13+
in the AWS SDK for Go Developer Guide.
14+
15+
## Running the code
16+
17+
### DescribeTable/DescribeTable.go
18+
19+
This example lists the following properties of a DynamoDB table.
20+
21+
- Number of items
22+
- Size, in bytes
23+
- Status, such as Active
24+
25+
`go run DescribeTable.go -t TABLE`
26+
27+
- _TABLE_ is the name of the table.
28+
29+
The unit test accepts a similar value in _config.json_.
30+
31+
### Notes
32+
33+
- We recommend that you grant this code least privilege,
34+
or at most the minimum permissions required to perform the task.
35+
For more information, see
36+
[Grant Least Privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
37+
in the AWS Identity and Access Management User Guide.
38+
- This code has not been tested in all AWS Regions.
39+
Some AWS services are available only in specific
40+
[Regions](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
41+
- Running this code might result in charges to your AWS account.
42+
43+
## Running the unit tests
44+
45+
Unit tests should delete any resources they create.
46+
However, they might result in charges to your
47+
AWS account.
48+
49+
To run a unit test, enter:
50+
51+
`go test`
52+
53+
You should see something like the following,
54+
where PATH is the path to the folder containing the Go files:
55+
56+
```sh
57+
PASS
58+
ok PATH 6.593s
59+
```
60+
61+
If you want to see any log messages, enter:
62+
63+
`go test -test.v`
64+
65+
You should see some additional log messages.
66+
The last two lines should be similar to the previous output shown.
67+
68+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0

gov2/dynamodb/metadata.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
files:
2+
- path: DescribeTable/DescribeTable.go
3+
services:
4+
- dynamodb
5+
- path: DescribeTable/DescribeTable_test.go
6+
services:
7+
- dynamodb

0 commit comments

Comments
 (0)