-
Notifications
You must be signed in to change notification settings - Fork 0
/
inmemory.go
148 lines (133 loc) · 3.88 KB
/
inmemory.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
package persistence
import (
"fmt"
"sort"
"sync"
"github.com/cheld/miniprow/pkg/boskos/common"
"github.com/cheld/miniprow/pkg/common/core"
)
type inMemoryStore struct {
resources map[string]map[string]common.Resource
drlc map[string]map[string]common.DynamicResourceLifeCycle
lock sync.RWMutex
}
func NewClientCache() ClientCache {
return NewPersistence()
}
// NewMemoryStorage creates an in memory persistence layer
func NewPersistence() Persistence {
res := map[string]map[string]common.Resource{}
drlc := map[string]map[string]common.DynamicResourceLifeCycle{}
return &inMemoryStore{
resources: res,
drlc: drlc,
}
}
func (im *inMemoryStore) Add(r common.Resource, tenant core.Tenant) error {
im.lock.Lock()
defer im.lock.Unlock()
if im.resources[tenant.ID()] == nil {
im.resources[tenant.ID()] = map[string]common.Resource{}
}
_, ok := im.resources[tenant.ID()][r.Name]
if ok {
return fmt.Errorf("resource %s already exists", r.Name)
}
im.resources[tenant.ID()][r.Name] = r
return nil
}
func (im *inMemoryStore) Delete(name string, tenant core.Tenant) error {
im.lock.Lock()
defer im.lock.Unlock()
if im.resources[tenant.ID()] == nil {
return fmt.Errorf("cannot find item %s", name)
}
_, ok := im.resources[tenant.ID()][name]
if !ok {
return fmt.Errorf("cannot find item %s", name)
}
delete(im.resources[tenant.ID()], name)
return nil
}
func (im *inMemoryStore) Update(r common.Resource, tenant core.Tenant) (common.Resource, error) {
im.lock.Lock()
defer im.lock.Unlock()
if im.resources[tenant.ID()] == nil {
return common.Resource{}, fmt.Errorf("cannot find item %s", r.Name)
}
_, ok := im.resources[tenant.ID()][r.Name]
if !ok {
return common.Resource{}, fmt.Errorf("cannot find item %s", r.Name)
}
im.resources[tenant.ID()][r.Name] = r
return r, nil
}
func (im *inMemoryStore) Get(name string, tenant core.Tenant) (common.Resource, error) {
im.lock.RLock()
defer im.lock.RUnlock()
if im.resources[tenant.ID()] == nil {
return common.Resource{}, fmt.Errorf("cannot find item %s", name)
}
r, ok := im.resources[tenant.ID()][name]
if !ok {
return common.Resource{}, fmt.Errorf("cannot find item %s", name)
}
return r, nil
}
func (im *inMemoryStore) List(tenant core.Tenant) ([]common.Resource, error) {
im.lock.RLock()
defer im.lock.RUnlock()
if im.resources[tenant.ID()] == nil {
return []common.Resource{}, nil
}
var resources []common.Resource
for _, r := range im.resources[tenant.ID()] {
resources = append(resources, r)
}
sort.SliceStable(resources, func(i, j int) bool {
return resources[i].Name < resources[j].Name
})
return resources, nil
}
func (im *inMemoryStore) AddDynamicResourceLifeCycle(r common.DynamicResourceLifeCycle, tenant core.Tenant) error {
im.lock.Lock()
defer im.lock.Unlock()
if im.drlc[tenant.ID()] == nil {
im.drlc[tenant.ID()] = map[string]common.DynamicResourceLifeCycle{}
}
_, ok := im.drlc[tenant.ID()][r.Type]
if ok {
return fmt.Errorf("drlc %s already exists", r.Type)
}
im.drlc[tenant.ID()][r.Type] = r
return nil
}
func (im *inMemoryStore) GetDynamicResourceLifeCycle(rtype string, tenant core.Tenant) (common.DynamicResourceLifeCycle, error) {
im.lock.Lock()
defer im.lock.Unlock()
if im.drlc[tenant.ID()] == nil {
return common.DynamicResourceLifeCycle{}, fmt.Errorf("cannot find drlc %s", rtype)
}
r, ok := im.drlc[tenant.ID()][rtype]
if !ok {
return common.DynamicResourceLifeCycle{}, fmt.Errorf("cannot find drlc %s", rtype)
}
return r, nil
}
func (im *inMemoryStore) AddToken(token string, tenant core.Tenant) error {
im.lock.Lock()
defer im.lock.Unlock()
return nil
}
func (im *inMemoryStore) DeleteToken(tenant core.Tenant) error {
im.lock.Lock()
defer im.lock.Unlock()
return nil
}
func (im *inMemoryStore) GetTenantFromToken(token, project string) (core.Tenant, error) {
im.lock.Lock()
defer im.lock.Unlock()
return core.NewTenant(), nil
}
func (im *inMemoryStore) Close() {
}