-
Notifications
You must be signed in to change notification settings - Fork 1
/
inmemstore.go
88 lines (70 loc) · 2.23 KB
/
inmemstore.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
package registry
// inmemstore is a non-thread-safe in-memory database for the moment
import "time"
// InMemoryStore implements a non-thread-safe registry of binder templates
type InMemoryStore struct {
templateMap map[string]Template
}
// NewInMemoryStore create a new InMemoryStore
func NewInMemoryStore() InMemoryStore {
var store InMemoryStore
store.templateMap = make(map[string]Template)
return store
}
// GetTemplate retrieves the template with name, erroring otherwise
func (store InMemoryStore) GetTemplate(name string) (Template, error) {
tmpl, ok := store.templateMap[name]
if !ok {
return Template{}, UnavailableTemplateError
}
return tmpl, nil
}
// RegisterTemplate registers the template in the Store
func (store InMemoryStore) RegisterTemplate(tmpl Template) (Template, error) {
// Ensure tmpl.Name is available
_, exists := store.templateMap[tmpl.Name]
if exists {
// Disallow registration if it exists
return Template{}, ExistingTemplateError
}
// Apply creation times
tmpl.TimeModified = time.Now().UTC()
tmpl.TimeCreated = tmpl.TimeModified
store.templateMap[tmpl.Name] = tmpl
return tmpl, nil
}
// ListTemplates provides a listing of all registered templates
func (store InMemoryStore) ListTemplates() ([]Template, error) {
templates := make([]Template, len(store.templateMap))
i := 0
for _, tmpl := range store.templateMap {
templates[i] = tmpl
i++
}
return templates, nil
}
// UpdateTemplate will allow for updating ImageName and Command
func (store InMemoryStore) UpdateTemplate(tmpl Template) (Template, error) {
updatedTemplate, ok := store.templateMap[tmpl.Name]
if !ok {
return Template{}, UnavailableTemplateError
}
// For now we allow updates to image name and command
if tmpl.ImageName != "" {
updatedTemplate.ImageName = tmpl.ImageName
}
if tmpl.Command != "" {
updatedTemplate.Command = tmpl.Command
}
updatedTemplate.TimeModified = time.Now().UTC()
store.templateMap[tmpl.Name] = updatedTemplate
return updatedTemplate, nil
}
func (store InMemoryStore) DeleteTemplate(name string) (Template, error) {
template, ok := store.templateMap[name]
if !ok {
return Template{}, UnavailableTemplateError
}
delete(store.templateMap, name);
return template, nil
}