vault: Adding ClearView method

This commit is contained in:
Armon Dadgar 2015-04-02 10:45:00 -07:00
parent 8433b3bfa6
commit d5e5499ddd
2 changed files with 53 additions and 0 deletions

View File

@ -143,3 +143,20 @@ func CollectKeys(view *BarrierView) ([]string, error) {
}
return existing, nil
}
// ClearView is used to delete all the keys in a view
func ClearView(view *BarrierView) error {
// Collect all the keys
keys, err := CollectKeys(view)
if err != nil {
return err
}
// Delete all the keys
for _, key := range keys {
if err := view.Delete(key); err != nil {
return err
}
}
return nil
}

View File

@ -246,3 +246,39 @@ func TestBarrierView_CollectKeys(t *testing.T) {
t.Fatalf("out: %v expect: %v", out, expect)
}
}
func TestBarrierView_ClearView(t *testing.T) {
_, barrier, _ := mockBarrier(t)
view := NewBarrierView(barrier, "view/")
expect := []string{}
ent := []*logical.StorageEntry{
&logical.StorageEntry{Key: "foo", Value: []byte("test")},
&logical.StorageEntry{Key: "zip", Value: []byte("test")},
&logical.StorageEntry{Key: "foo/bar", Value: []byte("test")},
&logical.StorageEntry{Key: "foo/zap", Value: []byte("test")},
&logical.StorageEntry{Key: "foo/bar/baz", Value: []byte("test")},
&logical.StorageEntry{Key: "foo/bar/zoo", Value: []byte("test")},
}
for _, e := range ent {
expect = append(expect, e.Key)
if err := view.Put(e); err != nil {
t.Fatalf("err: %v", err)
}
}
// Clear the keys
if err := ClearView(view); err != nil {
t.Fatalf("err: %v", err)
}
// Collect the keys
out, err := CollectKeys(view)
if err != nil {
t.Fatalf("err: %v", err)
}
if len(out) != 0 {
t.Fatalf("have keys: %#v", out)
}
}