command/delete

This commit is contained in:
Mitchell Hashimoto 2015-04-07 11:15:20 -07:00
parent 3001c245e5
commit 7442bc1ef6
4 changed files with 148 additions and 4 deletions

View File

@ -133,14 +133,17 @@ func (c *Client) RawRequest(r *Request) (*Response, error) {
return nil, err
}
var result *Response
resp, err := c.config.HttpClient.Do(req)
if resp != nil {
result = &Response{Response: resp}
}
if err != nil {
return nil, err
return result, err
}
result := &Response{Response: resp}
if err := result.Error(); err != nil {
return nil, err
return result, err
}
return result, nil

View File

@ -13,6 +13,9 @@ func (c *Client) Logical() *Logical {
func (c *Logical) Read(path string) (*Secret, error) {
r := c.c.NewRequest("GET", "/v1/"+path)
resp, err := c.c.RawRequest(r)
if resp != nil && resp.StatusCode == 404 {
return nil, nil
}
if err != nil {
return nil, err
}
@ -48,5 +51,9 @@ func (c *Logical) Delete(path string) (*Secret, error) {
}
defer resp.Body.Close()
return ParseSecret(resp.Body)
if resp.StatusCode == 200 {
return ParseSecret(resp.Body)
}
return nil, nil
}

79
command/delete.go Normal file
View File

@ -0,0 +1,79 @@
package command
import (
"fmt"
"strings"
)
// DeleteCommand is a Command that puts data into the Vault.
type DeleteCommand struct {
Meta
}
func (c *DeleteCommand) Run(args []string) int {
flags := c.Meta.FlagSet("delete", FlagSetDefault)
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) != 1 {
c.Ui.Error("delete expects one argument")
flags.Usage()
return 1
}
path := args[0]
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
if _, err := client.Logical().Delete(path); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error deleting '%s': %s", path, err))
return 1
}
c.Ui.Output(fmt.Sprintf("Success! Deleted '%s'", path))
return 0
}
func (c *DeleteCommand) Synopsis() string {
return "Delete operation on secrets in Vault"
}
func (c *DeleteCommand) Help() string {
helpText := `
Usage: vault delete [options] path
Delete data (secrets or configuration) from Vault.
Delete sends a delete operation request to the given path. The
behavior of the delete is determined by the backend at the given
path. For example, deleting "aws/policy/ops" will delete the "ops"
policy for the AWS backend. Use "vault help" for more details on
whether delete is supported for a path and what the behavior is.
General Options:
-address=TODO The address of the Vault server.
-ca-cert=path Path to a PEM encoded CA cert file to use to
verify the Vault server SSL certificate.
-ca-path=path Path to a directory of PEM encoded CA cert files
to verify the Vault server SSL certificate. If both
-ca-cert and -ca-path are specified, -ca-path is used.
-insecure Do not verify TLS certificate. This is highly
not recommended. This is especially not recommended
for unsealing a vault.
`
return strings.TrimSpace(helpText)
}

55
command/delete_test.go Normal file
View File

@ -0,0 +1,55 @@
package command
import (
"testing"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
"github.com/mitchellh/cli"
)
func TestDelete(t *testing.T) {
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := http.TestServer(t, core)
defer ln.Close()
ui := new(cli.MockUi)
c := &DeleteCommand{
Meta: Meta{
ClientToken: token,
Ui: ui,
},
}
args := []string{
"-address", addr,
"secret/foo",
}
// Run once so the client is setup, ignore errors
c.Run(args)
// Get the client so we can write data
client, err := c.Client()
if err != nil {
t.Fatalf("err: %s", err)
}
data := map[string]interface{}{"value": "bar"}
if _, err := client.Logical().Write("secret/foo", data); err != nil {
t.Fatalf("err: %s", err)
}
// Run the delete
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
resp, err := client.Logical().Read("secret/foo")
if err != nil {
t.Fatalf("err: %s", err)
}
if resp != nil {
t.Fatalf("bad: %#v", resp)
}
}