-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
93 lines (84 loc) · 2.1 KB
/
store.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
package dynamostore
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/go-carrot/fsm"
)
type DynamoStore struct {
DynamoSession *dynamodb.DynamoDB
DynamoTable string
Network string
}
func (d *DynamoStore) FetchTraverser(uuid string) (fsm.Traverser, error) {
// Fetch Item
result, err := d.DynamoSession.GetItem(
&dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"network": {
S: aws.String(d.Network),
},
"uuid": {
S: aws.String(uuid),
},
},
TableName: aws.String(d.DynamoTable),
},
)
// Checking for errors with the request
if err != nil {
return nil, err
}
// Dynamo actually doesn't return an error when the traverser doesn't exist
// It just returns an empty map. So we have to check this here to see
// if the traverser doesn't exist.
if len(result.Item) == 0 {
return nil, errors.New("Traverser does not exist")
}
// Get Data
data := make(map[string]interface{}, 0)
err = dynamodbattribute.ConvertFromMap(result.Item["data"].M, &data)
if err != nil {
return nil, err
}
// Create Traverser
return &dynamoTraverser{
network: d.Network,
uuid: uuid,
currentState: *result.Item["currentState"].S,
dynamoSession: d.DynamoSession,
dynamoTable: d.DynamoTable,
dynamoData: data,
}, nil
}
func (d *DynamoStore) CreateTraverser(uuid string) (fsm.Traverser, error) {
// Create element in Dynamo
_, err := d.DynamoSession.PutItem(
&dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
"network": {
S: aws.String(d.Network),
},
"uuid": {
S: aws.String(uuid),
},
"data": {
M: make(map[string]*dynamodb.AttributeValue, 0),
},
},
TableName: aws.String(d.DynamoTable),
},
)
if err != nil {
return nil, err
}
// Create Traverser
return &dynamoTraverser{
network: d.Network,
uuid: uuid,
dynamoSession: d.DynamoSession,
dynamoTable: d.DynamoTable,
dynamoData: make(map[string]interface{}, 0),
}, nil
}