|
| 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 | +} |
0 commit comments