logical/framework: Add delete to PathStruct

This commit is contained in:
Jonathan Sokolowski 2015-05-14 22:16:55 +10:00
parent 45d8c923f0
commit 896f9cd4d3
2 changed files with 49 additions and 1 deletions

View file

@ -51,6 +51,11 @@ func (p *PathStruct) Put(s logical.Storage, v map[string]interface{}) error {
}) })
} }
// Delete removes the structure.
func (p *PathStruct) Delete(s logical.Storage) error {
return s.Delete(fmt.Sprintf("struct/%s", p.Name))
}
// Paths are the paths to append to the Backend paths. // Paths are the paths to append to the Backend paths.
func (p *PathStruct) Paths() []*Path { func (p *PathStruct) Paths() []*Path {
// The single path we support to read/write this config // The single path we support to read/write this config
@ -59,7 +64,8 @@ func (p *PathStruct) Paths() []*Path {
Fields: p.Schema, Fields: p.Schema,
Callbacks: map[logical.Operation]OperationFunc{ Callbacks: map[logical.Operation]OperationFunc{
logical.WriteOperation: p.pathWrite, logical.WriteOperation: p.pathWrite,
logical.DeleteOperation: p.pathDelete,
}, },
HelpSynopsis: p.HelpSynopsis, HelpSynopsis: p.HelpSynopsis,
@ -91,3 +97,9 @@ func (p *PathStruct) pathWrite(
err := p.Put(req.Storage, d.Raw) err := p.Put(req.Storage, d.Raw)
return nil, err return nil, err
} }
func (p *PathStruct) pathDelete(
req *logical.Request, d *FieldData) (*logical.Response, error) {
err := p.Delete(req.Storage)
return nil, err
}

View file

@ -53,4 +53,40 @@ func TestPathStruct(t *testing.T) {
if v["value"] != "baz" { if v["value"] != "baz" {
t.Fatalf("bad: %#v", v) t.Fatalf("bad: %#v", v)
} }
// Delete via HTTP
resp, err = b.HandleRequest(&logical.Request{
Operation: logical.DeleteOperation,
Path: "bar",
Data: nil,
Storage: storage,
})
if err != nil {
t.Fatalf("bad: %#v", err)
}
if resp != nil {
t.Fatalf("bad: %#v", resp)
}
// Re-read via HTTP
resp, err = b.HandleRequest(&logical.Request{
Operation: logical.ReadOperation,
Path: "bar",
Storage: storage,
})
if err != nil {
t.Fatalf("bad: %#v", err)
}
if _, ok := resp.Data["value"]; ok {
t.Fatalf("bad: %#v", resp)
}
// Re-read via API
v, err = p.Get(storage)
if err != nil {
t.Fatalf("bad: %#v", err)
}
if v != nil {
t.Fatalf("bad: %#v", v)
}
} }