-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamodb.go
130 lines (109 loc) · 2.97 KB
/
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package awscertmagic
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/mholt/certmagic"
"time"
)
type DynamoDb struct {
api dynamodbiface.DynamoDBAPI
table string
}
type item struct {
PKey string `dynamodbav:"PKey"`
Key string `dynamodbav:"SKey"`
Value []byte
Modified time.Time
}
func NewDynamoDb(api dynamodbiface.DynamoDBAPI, table string) *DynamoDb {
return &DynamoDb{
api: api,
table: table,
}
}
func (d *DynamoDb) Lock(key string) error {
return nil // TODO
}
func (d *DynamoDb) Unlock(key string) error {
return nil // TODO
}
func (d *DynamoDb) Store(key string, value []byte) error {
item := item{PKey: "pk", Key: key, Value: value, Modified: time.Now()}
ddbItem, err := dynamodbattribute.MarshalMap(item)
if err != nil {
return err
}
input := &dynamodb.PutItemInput{TableName: &d.table, Item: ddbItem}
_, err = d.api.PutItem(input)
return err
}
func (d *DynamoDb) Load(key string) ([]byte, error) {
item, err := d.get(key)
if err != nil {
return nil, err
}
return item.Value, nil
}
func (d *DynamoDb) Delete(key string) error {
input := &dynamodb.DeleteItemInput{TableName: &d.table, Key: ddbKey(key)}
_, err := d.api.DeleteItem(input)
return err
}
func (d *DynamoDb) Exists(key string) bool {
_, err := d.get(key)
return err == nil
}
func (d *DynamoDb) List(prefix string, recursive bool) ([]string, error) {
output := []string{}
err := d.api.QueryPages(&dynamodb.QueryInput{
TableName: &d.table,
ConsistentRead: aws.Bool(true),
KeyConditionExpression: aws.String("PKey = :PKey AND begins_with(SKey, :SKeyPrefix)"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":PKey": {S: aws.String("pk")},
":SKeyPrefix": {S: &prefix},
},
}, func(page *dynamodb.QueryOutput, lastPage bool) bool {
items := []item{}
_ = dynamodbattribute.UnmarshalListOfMaps(page.Items, &items)
for _, item := range items {
output = append(output, item.Key)
}
return !lastPage
})
return output, err
}
func (d *DynamoDb) Stat(key string) (certmagic.KeyInfo, error) {
item, err := d.get(key)
if err != nil {
return certmagic.KeyInfo{}, err
}
return certmagic.KeyInfo{Key: key, Modified: item.Modified}, nil
}
func (d *DynamoDb) get(key string) (*item, error) {
output, err := d.api.GetItem(&dynamodb.GetItemInput{
TableName: &d.table,
ConsistentRead: aws.Bool(true),
Key: ddbKey(key),
})
if err != nil {
return nil, err
} else if output.Item == nil {
return nil, errors.New("not found")
}
item := item{}
err = dynamodbattribute.UnmarshalMap(output.Item, &item)
if err != nil {
return nil, err
}
return &item, nil
}
func ddbKey(key string) map[string]*dynamodb.AttributeValue {
return map[string]*dynamodb.AttributeValue{
"PKey": {S: aws.String("pk")},
"SKey": {S: &key},
}
}