forked from kelseyhightower/confd
/
client.go
102 lines (91 loc) · 2.62 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
package dynamodb
import (
"os"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/credentials"
"github.com/awslabs/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) {
creds := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.EnvProvider{},
&credentials.EC2RoleProvider{},
})
_, err := creds.Get()
if err != nil {
return nil, err
}
var c *aws.Config
if os.Getenv("DYNAMODB_LOCAL") != "" {
log.Debug("DYNAMODB_LOCAL is set")
c = &aws.Config{Endpoint: "http://localhost:8000"}
} else {
c = nil
}
d := dynamodb.New(c)
// 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
g, err := c.client.GetItem(&dynamodb.GetItemInput{
Key: &map[string]*dynamodb.AttributeValue{
"key": &dynamodb.AttributeValue{S: aws.String(key)},
},
TableName: &c.table})
if err != nil {
return vars, err
}
if g.Item != nil {
if val, ok := (*(g.Item))["value"]; ok {
vars[key] = *val.S
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 {
vars[*item["key"].S] = *val.S
continue
}
}
}
return vars, nil
}
// WatchPrefix is not implemented
func (c *Client) WatchPrefix(prefix string, waitIndex uint64, stopChan chan bool) (uint64, error) {
<-stopChan
return 0, nil
}