open-vault/logical/storage.go

40 lines
939 B
Go
Raw Normal View History

package logical
2015-03-20 16:59:48 +00:00
import (
"fmt"
"github.com/hashicorp/vault/helper/jsonutil"
2015-03-20 16:59:48 +00:00
)
// Storage is the way that logical backends are able read/write data.
type Storage interface {
List(prefix string) ([]string, error)
Get(string) (*StorageEntry, error)
Put(*StorageEntry) error
Delete(string) error
}
// StorageEntry is the entry for an item in a Storage implementation.
type StorageEntry struct {
Key string
Value []byte
}
2015-03-20 16:59:48 +00:00
// DecodeJSON decodes the 'Value' present in StorageEntry.
2015-03-20 16:59:48 +00:00
func (e *StorageEntry) DecodeJSON(out interface{}) error {
return jsonutil.DecodeJSON(e.Value, out)
2015-03-20 16:59:48 +00:00
}
// StorageEntryJSON creates a StorageEntry with a JSON-encoded value.
func StorageEntryJSON(k string, v interface{}) (*StorageEntry, error) {
encodedBytes, err := jsonutil.EncodeJSON(v)
if err != nil {
return nil, fmt.Errorf("failed to encode storage entry: %v", err)
2015-03-20 16:59:48 +00:00
}
return &StorageEntry{
Key: k,
Value: encodedBytes,
2015-03-20 16:59:48 +00:00
}, nil
}