physical: Factory constructor style for backends

This commit is contained in:
Armon Dadgar
2015-03-05 13:47:10 -08:00
parent aa1145b53b
commit ce7d02f3b0
6 changed files with 49 additions and 12 deletions

View File

@@ -1,17 +1,14 @@
package physical package physical
import "github.com/hashicorp/consul/api"
// ConsulBackend is a physical backend that stores data at specific // ConsulBackend is a physical backend that stores data at specific
// prefix within Consul. It is used for most production situations as // prefix within Consul. It is used for most production situations as
// it allows Vault to run on multiple machines in a highly-available manner. // it allows Vault to run on multiple machines in a highly-available manner.
type ConsulBackend struct { type ConsulBackend struct {
} }
// NewConsulBackend constructs a Consul backend using the given API client // newConsulBackend constructs a Consul backend using the given API client
// and the prefix in the KV store. // and the prefix in the KV store.
func NewConsulBackend(client *api.Client, prefix string) (*ConsulBackend, error) { func newConsulBackend(conf map[string]string) (Backend, error) {
// TODO // TODO
c := &ConsulBackend{} return nil, nil
return c, nil
} }

View File

@@ -6,9 +6,8 @@ package physical
type FileBackend struct { type FileBackend struct {
} }
// NewFileBackend constructs a Filebackend using the given directory // newFileBackend constructs a Filebackend using the given directory
func NewFileBackend(dir string) (*FileBackend, error) { func newFileBackend(conf map[string]string) (Backend, error) {
// TODO: // TODO:
f := &FileBackend{} return nil, nil
return f, nil
} }

View File

@@ -16,7 +16,7 @@ type InmemBackend struct {
} }
// NewInmem constructs a new in-memory backend // NewInmem constructs a new in-memory backend
func NewInmem() *InmemBackend { func newInmem() *InmemBackend {
in := &InmemBackend{ in := &InmemBackend{
root: radix.New(), root: radix.New(),
} }

View File

@@ -3,7 +3,7 @@ package physical
import "testing" import "testing"
func TestInmem(t *testing.T) { func TestInmem(t *testing.T) {
inm := NewInmem() inm := newInmem()
testBackend(t, inm) testBackend(t, inm)
testBackend_ListPrefix(t, inm) testBackend_ListPrefix(t, inm)
} }

View File

@@ -1,5 +1,7 @@
package physical package physical
import "fmt"
// Backend is the interface required for a physical // Backend is the interface required for a physical
// backend. A physical backend is used to durably store // backend. A physical backend is used to durably store
// datd outside of Vault. As such, it is completely untrusted, // datd outside of Vault. As such, it is completely untrusted,
@@ -26,3 +28,26 @@ type Entry struct {
Key string Key string
Value []byte Value []byte
} }
// Factory is the factory function to create a physical backend.
type Factory func(map[string]string) (Backend, error)
// NewBackend returns a new Bckend with the given type and configuration.
// The backend is looked up in the BuiltinBackends variable.
func NewBackend(t string, conf map[string]string) (Backend, error) {
f, ok := BuiltinBackends[t]
if !ok {
return nil, fmt.Errorf("unknown physical backend type: %s", t)
}
return f(conf)
}
// BuiltinBackends is the list of built-in physical backends that can
// be used with NewBackend.
var BuiltinBackends = map[string]Factory{
"inmem": func(map[string]string) (Backend, error) {
return newInmem(), nil
},
"consul": newConsulBackend,
"file": newFileBackend,
}

View File

@@ -5,6 +5,22 @@ import (
"testing" "testing"
) )
func testNewBackend(t *testing.T) {
_, err := NewBackend("foobar", nil)
if err == nil {
t.Fatalf("expected error")
}
b, err := NewBackend("inmem", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
if b == nil {
t.Fatalf("expected backend")
}
}
func testBackend(t *testing.T, b Backend) { func testBackend(t *testing.T, b Backend) {
// Should be empty // Should be empty
keys, err := b.List("") keys, err := b.List("")