-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_dynamodb.go
55 lines (44 loc) · 1.12 KB
/
config_dynamodb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
type DynamoDBConfigSource struct {
Table string
Key string
Endpoint string
Region string
}
func (src *DynamoDBConfigSource) List(ctx context.Context) ([]string, error) {
var err error
awsConfig := &aws.Config{
Endpoint: &src.Endpoint,
Region: &src.Region,
}
sess := session.Must(session.NewSession(awsConfig))
svc := dynamodb.New(sess)
resp, err := svc.GetItemWithContext(ctx, &dynamodb.GetItemInput{
ConsistentRead: aws.Bool(true),
Key: map[string]*dynamodb.AttributeValue{
"key": {S: aws.String(src.Key)},
},
TableName: aws.String(src.Table),
})
if err != nil {
return nil, err
}
itemsMap := make(map[string]string)
err = dynamodbattribute.UnmarshalMap(resp.Item, &itemsMap)
if err != nil {
return nil, err
}
delete(itemsMap, src.Key)
items := make([]string, 0, len(itemsMap))
for k, v := range itemsMap {
items = append(items, k+"="+v)
}
return items, nil
}