-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverser.go
169 lines (153 loc) · 3.77 KB
/
traverser.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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"
)
type dynamoTraverser struct {
network string
uuid string
currentState string
dynamoSession *dynamodb.DynamoDB
dynamoTable string
dynamoData map[string]interface{}
}
func (d *dynamoTraverser) UUID() string {
return d.uuid
}
func (d *dynamoTraverser) SetUUID(newUUID string) {
// Update UUID on Dynamo
d.dynamoSession.UpdateItem(
&dynamodb.UpdateItemInput{
ExpressionAttributeNames: map[string]*string{
"#K": aws.String("uuid"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":v": &dynamodb.AttributeValue{
S: aws.String(newUUID),
},
},
Key: map[string]*dynamodb.AttributeValue{
"network": {
S: aws.String(d.network),
},
"uuid": {
S: aws.String(d.uuid),
},
},
TableName: aws.String(d.dynamoTable),
UpdateExpression: aws.String("SET #K = :v"),
},
)
// Update local UUID
d.uuid = newUUID
}
func (d *dynamoTraverser) CurrentState() string {
return d.currentState
}
func (d *dynamoTraverser) SetCurrentState(state string) {
// Update state on Dynamo
d.dynamoSession.UpdateItem(
&dynamodb.UpdateItemInput{
ExpressionAttributeNames: map[string]*string{
"#K": aws.String("currentState"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":v": &dynamodb.AttributeValue{
S: aws.String(state),
},
},
Key: map[string]*dynamodb.AttributeValue{
"network": {
S: aws.String(d.network),
},
"uuid": {
S: aws.String(d.uuid),
},
},
TableName: aws.String(d.dynamoTable),
UpdateExpression: aws.String("SET #K = :v"),
},
)
// Update local state
d.currentState = state
}
func (d *dynamoTraverser) Upsert(key string, value interface{}) error {
// Convert value to a DynamoAttribute
item, err := dynamodbattribute.ConvertTo(value)
if err != nil {
return err
}
// Update attribute in Dynamo
res, err := d.dynamoSession.UpdateItem(
&dynamodb.UpdateItemInput{
ExpressionAttributeNames: map[string]*string{
"#D": aws.String("data"),
"#K": aws.String(key),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":v": item,
},
Key: map[string]*dynamodb.AttributeValue{
"network": {
S: aws.String(d.network),
},
"uuid": {
S: aws.String(d.uuid),
},
},
ReturnValues: aws.String("ALL_NEW"),
TableName: aws.String(d.dynamoTable),
UpdateExpression: aws.String("SET #D.#K = :v"),
},
)
// Update local
if err == nil {
data := make(map[string]interface{}, 0)
err = dynamodbattribute.ConvertFromMap(res.Attributes["data"].M, &data)
if err == nil {
d.dynamoData = data
}
}
return err
}
func (d *dynamoTraverser) Fetch(key string) (interface{}, error) {
if val, ok := d.dynamoData[key]; ok {
return val, nil
}
return nil, errors.New("Key not set")
}
func (d *dynamoTraverser) Delete(key string) error {
// Update attribute in Dynamo
res, err := d.dynamoSession.UpdateItem(
&dynamodb.UpdateItemInput{
ExpressionAttributeNames: map[string]*string{
"#D": aws.String("data"),
"#K": aws.String(key),
},
Key: map[string]*dynamodb.AttributeValue{
"network": {
S: aws.String(d.network),
},
"uuid": {
S: aws.String(d.uuid),
},
},
ReturnValues: aws.String("ALL_NEW"),
TableName: aws.String(d.dynamoTable),
UpdateExpression: aws.String("REMOVE #D.#K"),
},
)
if err != nil {
return err
}
// Update local
data := make(map[string]interface{}, 0)
err = dynamodbattribute.ConvertFromMap(res.Attributes["data"].M, &data)
if err == nil {
d.dynamoData = data
}
// OK
return nil
}