forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_inmem.go
67 lines (53 loc) · 1.48 KB
/
storage_inmem.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
package logical
import (
"context"
"sync"
"github.com/hashicorp/vault/physical"
"github.com/hashicorp/vault/physical/inmem"
)
// InmemStorage implements Storage and stores all data in memory. It is
// basically a straight copy of physical.Inmem, but it prevents backends from
// having to load all of physical's dependencies (which are legion) just to
// have some testing storage.
type InmemStorage struct {
underlying physical.Backend
once sync.Once
}
func (s *InmemStorage) Get(ctx context.Context, key string) (*StorageEntry, error) {
s.once.Do(s.init)
entry, err := s.underlying.Get(ctx, key)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
return &StorageEntry{
Key: entry.Key,
Value: entry.Value,
SealWrap: entry.SealWrap,
}, nil
}
func (s *InmemStorage) Put(ctx context.Context, entry *StorageEntry) error {
s.once.Do(s.init)
return s.underlying.Put(ctx, &physical.Entry{
Key: entry.Key,
Value: entry.Value,
SealWrap: entry.SealWrap,
})
}
func (s *InmemStorage) Delete(ctx context.Context, key string) error {
s.once.Do(s.init)
return s.underlying.Delete(ctx, key)
}
func (s *InmemStorage) List(ctx context.Context, prefix string) ([]string, error) {
s.once.Do(s.init)
return s.underlying.List(ctx, prefix)
}
func (s *InmemStorage) Underlying() *inmem.InmemBackend {
s.once.Do(s.init)
return s.underlying.(*inmem.InmemBackend)
}
func (s *InmemStorage) init() {
s.underlying, _ = inmem.NewInmem(nil, nil)
}