Add storage API abstraction
This commit is contained in:
committed by
Marcos Lilljedahl
commit
08b64dadde
@@ -31,6 +31,10 @@ func (store *storage) SessionPut(s *types.Session) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
// Initialize instances map if nil
|
||||
if s.Instances == nil {
|
||||
s.Instances = map[string]*types.Instance{}
|
||||
}
|
||||
store.db[s.Id] = s
|
||||
|
||||
return store.save()
|
||||
@@ -85,6 +89,24 @@ func (store *storage) InstanceFindByAlias(sessionPrefix, alias string) (*types.I
|
||||
return nil, fmt.Errorf("%s", notFound)
|
||||
}
|
||||
|
||||
func (store *storage) InstanceCreate(sessionId string, instance *types.Instance) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
s, found := store.db[sessionId]
|
||||
if !found {
|
||||
return fmt.Errorf("Session %s", notFound)
|
||||
}
|
||||
|
||||
s.Instances[instance.Name] = instance
|
||||
|
||||
return store.save()
|
||||
}
|
||||
|
||||
func (store *storage) InstanceDelete(sessionId, name string) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (store *storage) SessionCount() (int, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
@@ -191,6 +191,33 @@ func TestInstanceFindByAlias(t *testing.T) {
|
||||
assert.Nil(t, foundInstance)
|
||||
}
|
||||
|
||||
func TestInstanceCreate(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tmpfile.Close()
|
||||
os.Remove(tmpfile.Name())
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
storage, err := NewFileStorage(tmpfile.Name())
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
i1 := &types.Instance{Name: "i1", Alias: "foo", IP: "10.0.0.1"}
|
||||
s1 := &types.Session{Id: "session1"}
|
||||
err = storage.SessionPut(s1)
|
||||
assert.Nil(t, err)
|
||||
err = storage.InstanceCreate(s1.Id, i1)
|
||||
assert.Nil(t, err)
|
||||
|
||||
loadedSession, err := storage.SessionGet("session1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, i1, loadedSession.Instances["i1"])
|
||||
|
||||
}
|
||||
|
||||
func TestCounts(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
@@ -226,9 +253,6 @@ func TestCounts(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 2, num)
|
||||
|
||||
num, err = storage.ClientCount()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, num)
|
||||
}
|
||||
|
||||
func TestSessionDelete(t *testing.T) {
|
||||
|
||||
@@ -9,16 +9,17 @@ func NotFound(e error) bool {
|
||||
}
|
||||
|
||||
type StorageApi interface {
|
||||
SessionGet(sessionId string) (*types.Session, error)
|
||||
SessionGet(string) (*types.Session, error)
|
||||
SessionPut(*types.Session) error
|
||||
SessionCount() (int, error)
|
||||
SessionDelete(sessionId string) error
|
||||
SessionDelete(string) error
|
||||
|
||||
InstanceFindByAlias(sessionPrefix, alias string) (*types.Instance, error)
|
||||
// Should have the session id too, soon
|
||||
InstanceFindByIP(ip string) (*types.Instance, error)
|
||||
InstanceFindByIPAndSession(sessionPrefix, ip string) (*types.Instance, error)
|
||||
InstanceCount() (int, error)
|
||||
InstanceCreate(sessionId string, instance *types.Instance) error
|
||||
InstanceDelete(sessionId, instanceName string) error
|
||||
|
||||
ClientCount() (int, error)
|
||||
InstanceCount() (int, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user