forked from kelseyhightower/confd
/
client.go
111 lines (97 loc) · 2.86 KB
/
client.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package dynamodb
import (
"os"
"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/kelseyhightower/confd/log"
)
// Client is a wrapper around the DynamoDB client
// and also holds the table to lookup key value pairs from
type Client struct {
client *dynamodb.DynamoDB
table string
}
// NewDynamoDBClient returns an *dynamodb.Client with a connection to the region
// configured via the AWS_REGION environment variable.
// It returns an error if the connection cannot be made or the table does not exist.
func NewDynamoDBClient(table string) (*Client, error) {
var c *aws.Config
if os.Getenv("DYNAMODB_LOCAL") != "" {
log.Debug("DYNAMODB_LOCAL is set")
endpoint := "http://localhost:8000"
c = &aws.Config{
Endpoint: &endpoint,
}
} else {
c = nil
}
session := session.New(c)
// Fail early, if no credentials can be found
_, err := session.Config.Credentials.Get()
if err != nil {
return nil, err
}
d := dynamodb.New(session)
// Check if the table exists
_, err = d.DescribeTable(&dynamodb.DescribeTableInput{TableName: &table})
if err != nil {
return nil, err
}
return &Client{d, table}, nil
}
// GetValues retrieves the values for the given keys from DynamoDB
func (c *Client) GetValues(keys []string) (map[string]string, error) {
vars := make(map[string]string)
for _, key := range keys {
// Check if we can find the single item
m := make(map[string]*dynamodb.AttributeValue)
m["key"] = &dynamodb.AttributeValue{S: aws.String(key)}
g, err := c.client.GetItem(&dynamodb.GetItemInput{Key: m, TableName: &c.table})
if err != nil {
return vars, err
}
if g.Item != nil {
if val, ok := g.Item["value"]; ok {
if val.S != nil {
vars[key] = *val.S
} else {
log.Warning("Skipping key '%s'. 'value' is not of type 'string'.", key)
}
continue
}
}
// Check for nested keys
q, err := c.client.Scan(
&dynamodb.ScanInput{
ScanFilter: map[string]*dynamodb.Condition{
"key": &dynamodb.Condition{
AttributeValueList: []*dynamodb.AttributeValue{
&dynamodb.AttributeValue{S: aws.String(key)}},
ComparisonOperator: aws.String("BEGINS_WITH")}},
AttributesToGet: []*string{aws.String("key"), aws.String("value")},
TableName: aws.String(c.table),
Select: aws.String("SPECIFIC_ATTRIBUTES"),
})
if err != nil {
return vars, err
}
for _, i := range q.Items {
item := i
if val, ok := item["value"]; ok {
if val.S != nil {
vars[*item["key"].S] = *val.S
} else {
log.Warning("Skipping key '%s'. 'value' is not of type 'string'.", *item["key"].S)
}
continue
}
}
}
return vars, nil
}
// WatchPrefix is not implemented
func (c *Client) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
<-stopChan
return 0, nil
}