logical/framework: Supporting list of path map

This commit is contained in:
Armon Dadgar 2015-04-23 18:04:26 -07:00
parent 634a69a2fe
commit c4a92a276d
2 changed files with 25 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package framework
import (
"fmt"
"strings"
"sync"
"github.com/hashicorp/vault/logical"
@ -56,6 +57,21 @@ func (p *PathMap) Put(s logical.Storage, k string, v map[string]interface{}) err
return p.pathStruct(k).Put(s, v)
}
// List reads the keys under a given path
func (p *PathMap) List(s logical.Storage, prefix string) ([]string, error) {
stripPrefix := fmt.Sprintf("struct/map/%s/", p.Name)
fullPrefix := fmt.Sprintf("%s%s", stripPrefix, prefix)
out, err := s.List(fullPrefix)
if err != nil {
return nil, err
}
stripped := make([]string, len(out))
for idx, k := range out {
stripped[idx] = strings.TrimPrefix(k, stripPrefix)
}
return stripped, nil
}
// Paths are the paths to append to the Backend paths.
func (p *PathMap) Paths() []*Path {
p.once.Do(p.init)

View File

@ -45,6 +45,15 @@ func TestPathMap(t *testing.T) {
if v["value"] != "bar" {
t.Fatalf("bad: %#v", v)
}
// Verify List
keys, err := p.List(storage, "")
if err != nil {
t.Fatalf("bad: %#v", err)
}
if len(keys) != 1 || keys[0] != "a" {
t.Fatalf("bad: %#v", keys)
}
}
func TestPathMap_getInvalid(t *testing.T) {