forked from vmware-archive/dispatch
/
store.go
345 lines (288 loc) · 9.46 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package entitystore
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"time"
"github.com/docker/libkv/store"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
)
const (
// StatusINITIALIZED objet is INITIALIZED
// this status is used by image manager
StatusINITIALIZED Status = "INITIALIZED"
// StatusCREATING object is CREATING
// it is not a guarantee that the object is also created and ready in the underlying driver
// should wait until the READY state to use the object
StatusCREATING Status = "CREATING"
// StatusREADY object is READY to be used
StatusREADY Status = "READY"
// StatusUPDATING object is UPDATING
// it is not a guarantee that changes will be reflected by the underlying driver
// when updated, will transfer to READY state
StatusUPDATING Status = "UPDATING"
// StatusDELETING object is DELETING
// it is not a guarantee that it has been deleted from the underlying driver
// user should not reuse the object name until it is transfered to DELETED state
StatusDELETING Status = "DELETING"
// StatusDELETED object is DELETED
// Note for serverless team:
// leave this here, reserved for when we use UUID instead of entity name
StatusDELETED Status = "DELETED"
// StatusERROR unexpected error state
// you should not use the object until the state is tranfered to READY
// or the object is deleted
StatusERROR Status = "ERROR"
)
// Status represents the current state
type Status string
// Spec represents the desired state/status
type Spec map[string]string
// dataType represents the stored struct type
type dataType string
// Tags are filterable metadata as key pairs
type Tags map[string]string
// Entity is the base interface for all stored objects
type Entity interface {
setID(string)
setCreatedTime(time.Time)
setModifiedTime(time.Time)
setStatus(Status)
setReason([]string)
setRevision(uint64)
GetName() string
GetRevision() uint64
GetTags() Tags
GetStatus() Status
GetModifiedTime() time.Time
getKey(dataType) string
}
// BaseEntity is the base struct for all stored objects
type BaseEntity struct {
ID string `json:"id"`
Name string `json:"name"`
OrganizationID string `json:"organizationId"`
CreatedTime time.Time `json:"createdTime,omitempty"`
ModifiedTime time.Time `json:"modifiedTime,omitempty"`
Revision uint64 `json:"revision"`
Version uint64 `json:"version"`
Spec Spec `json:"state"`
Status Status `json:"status"`
Reason []string `json:"reason"`
Tags Tags `json:"tags"`
Delete bool `json:"delete"`
}
// buildKey is a utility for building the object key (also works for directories)
func buildKey(organizationID string, dt dataType, id ...string) string {
sub := strings.Join(id, "/")
return fmt.Sprintf("%s/%s/%s", organizationID, dt, sub)
}
func getKey(entity Entity) string {
return entity.getKey(getDataType(entity))
}
func getDataType(entity Entity) dataType {
return dataType(reflect.ValueOf(entity).Type().Elem().Name())
}
func GetDataType(entity Entity) string {
return string(getDataType(entity))
}
func (e *BaseEntity) setID(id string) {
e.ID = id
}
func (e *BaseEntity) setRevision(revision uint64) {
e.Revision = revision
}
func (e *BaseEntity) GetRevision() uint64 {
return e.Revision
}
func (e *BaseEntity) setCreatedTime(createdTime time.Time) {
e.CreatedTime = createdTime
}
func (e *BaseEntity) setModifiedTime(modifiedTime time.Time) {
e.ModifiedTime = modifiedTime
}
func (e *BaseEntity) setStatus(status Status) {
e.Status = status
}
func (e *BaseEntity) setReason(reason []string) {
e.Reason = reason
}
func (e *BaseEntity) GetModifiedTime() time.Time {
return e.ModifiedTime
}
// GetName retreives the entity name
func (e *BaseEntity) GetName() string {
return e.Name
}
// GetTags retreives the entity tags
func (e *BaseEntity) GetTags() Tags {
return e.Tags
}
// getKey builds the key for a give entity
func (e *BaseEntity) getKey(dt dataType) string {
return buildKey(e.OrganizationID, dt, e.Name)
}
func (e *BaseEntity) GetStatus() Status {
return e.Status
}
type entityStore struct {
kv store.Store
}
// Filter is a function type that operates on returned list results
type Filter func(Entity) bool
// EntityStore is a wrapper around libkv and provides convenience methods to
// serializing and deserializing objects
type EntityStore interface {
// Add adds new entities to the store
Add(entity Entity) (id string, err error)
// Update updates existing entities to the store
Update(lastRevision uint64, entity Entity) (revision int64, err error)
// GetById gets a single entity by key from the store
Get(organizationID string, key string, entity Entity) error
// List fetches a list of entities of a single data type satisfying the filter.
// entities is a placeholder for results and must be a pointer to an empty slice of the desired entity type.
List(organizationID string, filter Filter, entities interface{}) error
// Delete delets a single entity from the store.
Delete(organizationID string, id string, entity Entity) error
// UpdateWithError is used by entity handlers to save changes and/or error status
// e.g. `defer func() { h.store.UpdateWithError(e, err) }()`
UpdateWithError(e Entity, err error)
}
// New is the EntityStore constructor
func New(kv store.Store) EntityStore {
return &entityStore{
kv: kv,
}
}
func (es *entityStore) precondition(entity Entity) error {
var validName = regexp.MustCompile(`^[\w\d\-]+$`)
if validName.MatchString(entity.GetName()) {
return nil
}
return errors.Errorf("Invalid name %s, names may only contain letters, numbers, underscores and dashes", entity.GetName())
}
func (es *entityStore) UpdateWithError(e Entity, err error) {
if err != nil {
e.setStatus(StatusERROR)
e.setReason([]string{err.Error()})
}
if _, err2 := es.Update(e.GetRevision(), e); err2 != nil {
log.Error(err2)
}
}
// Add adds new entities to the store
func (es *entityStore) Add(entity Entity) (id string, err error) {
err = es.precondition(entity)
if err != nil {
return "", errors.Wrap(err, "Precondition failed")
}
id = uuid.NewV4().String()
entity.setID(id)
key := getKey(entity)
now := time.Now()
entity.setCreatedTime(now)
entity.setModifiedTime(now)
data, err := json.Marshal(entity)
if err != nil {
return "", errors.Wrap(err, "serialization error, before adding")
}
_, resp, err := es.kv.AtomicPut(key, data, nil, &store.WriteOptions{IsDir: false})
if err != nil {
return "", err
}
entity.setRevision(resp.LastIndex)
return id, nil
}
// Update updates existing entities to the store
func (es *entityStore) Update(lastRevision uint64, entity Entity) (revision int64, err error) {
key := getKey(entity)
exists, err := es.kv.Exists(key)
if !exists {
return 0, errors.Errorf("Entity not found, cannot update")
}
if err != nil {
return 0, err
}
entity.setModifiedTime(time.Now())
data, err := json.Marshal(entity)
if err != nil {
return 0, errors.Wrap(err, "serialization error, before updating")
}
previous := &store.KVPair{
Key: key,
LastIndex: lastRevision,
}
_, kv, err := es.kv.AtomicPut(key, data, previous, &store.WriteOptions{IsDir: false})
if err != nil {
return 0, err
}
entity.setRevision(kv.LastIndex)
return int64(kv.LastIndex), nil
}
// Delete delets a single entity from the store
// entity should be a zero-value of entity to be deleted.
func (es *entityStore) Delete(organizationID string, name string, entity Entity) error {
key := buildKey(organizationID, getDataType(entity), name)
return es.kv.Delete(key)
}
// Get gets a single entity by name from the store
func (es *entityStore) Get(organizationID string, name string, entity Entity) error {
key := buildKey(organizationID, getDataType(entity), name)
kv, err := es.kv.Get(key)
if err != nil {
return err
}
err = json.Unmarshal(kv.Value, entity)
if err != nil {
return errors.Wrap(err, "deserialization error, while getting")
}
entity.setRevision(kv.LastIndex)
return nil
}
// List fetches a list of entities of a single data type satisfying the filter.
// entities is a placeholder for results and must be a pointer to an empty slice of the desired entity type.
func (es *entityStore) List(organizationID string, filter Filter, entities interface{}) error {
rv := reflect.ValueOf(entities)
if entities == nil || rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Slice {
return errors.New("need a non-nil entity slice pointer")
}
slice := reflect.MakeSlice(rv.Elem().Type(), 0, 0)
elemType := rv.Elem().Type().Elem()
if !elemType.Implements(reflect.TypeOf((*Entity)(nil)).Elem()) {
return errors.New("non-entity element type: maybe use pointers")
}
key := buildKey(organizationID, dataType(elemType.Elem().Name()))
kvs, err := es.kv.List(key)
if err != nil {
if err == store.ErrKeyNotFound {
rv.Elem().Set(slice)
return nil
}
return err
}
for _, kv := range kvs {
obj := reflect.New(elemType.Elem())
entity := obj.Interface().(Entity)
err = json.Unmarshal(kv.Value, entity)
if err != nil {
return errors.Wrap(err, "deserialization error, while listing")
}
if filter != nil {
if !filter(entity) {
continue
}
}
entity.setRevision(kv.LastIndex)
slice = reflect.Append(slice, obj)
}
rv.Elem().Set(slice)
return nil
}