From fe2fa0030f5117849ee5330b6252e524f2ba6028 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 4 Apr 2018 04:41:46 -0400 Subject: [PATCH] Rejig 404 handling again. (#4264) Done this way, existing tests pass, and it makes logical sense, so we're likely to have the least impact like this. --- api/logical.go | 22 ++++++++++------------ command/delete.go | 6 +++++- command/write.go | 3 +++ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/api/logical.go b/api/logical.go index 4e5d7ce5a..84437391b 100644 --- a/api/logical.go +++ b/api/logical.go @@ -51,8 +51,8 @@ func (c *Logical) Read(path string) (*Secret, error) { defer resp.Body.Close() } if resp != nil && resp.StatusCode == 404 { - secret, err := ParseSecret(resp.Body) - switch err { + secret, parseErr := ParseSecret(resp.Body) + switch parseErr { case nil: case io.EOF: return nil, nil @@ -82,8 +82,8 @@ func (c *Logical) List(path string) (*Secret, error) { defer resp.Body.Close() } if resp != nil && resp.StatusCode == 404 { - secret, err := ParseSecret(resp.Body) - switch err { + secret, parseErr := ParseSecret(resp.Body) + switch parseErr { case nil: case io.EOF: return nil, nil @@ -113,8 +113,8 @@ func (c *Logical) Write(path string, data map[string]interface{}) (*Secret, erro defer resp.Body.Close() } if resp != nil && resp.StatusCode == 404 { - secret, err := ParseSecret(resp.Body) - switch err { + secret, parseErr := ParseSecret(resp.Body) + switch parseErr { case nil: case io.EOF: return nil, nil @@ -122,9 +122,8 @@ func (c *Logical) Write(path string, data map[string]interface{}) (*Secret, erro return nil, err } if secret != nil && (len(secret.Warnings) > 0 || len(secret.Data) > 0) { - return secret, nil + return secret, err } - return nil, nil } if err != nil { return nil, err @@ -144,8 +143,8 @@ func (c *Logical) Delete(path string) (*Secret, error) { defer resp.Body.Close() } if resp != nil && resp.StatusCode == 404 { - secret, err := ParseSecret(resp.Body) - switch err { + secret, parseErr := ParseSecret(resp.Body) + switch parseErr { case nil: case io.EOF: return nil, nil @@ -153,9 +152,8 @@ func (c *Logical) Delete(path string) (*Secret, error) { return nil, err } if secret != nil && (len(secret.Warnings) > 0 || len(secret.Data) > 0) { - return secret, nil + return secret, err } - return nil, nil } if err != nil { return nil, err diff --git a/command/delete.go b/command/delete.go index 12c7f9611..221c5b72e 100644 --- a/command/delete.go +++ b/command/delete.go @@ -84,8 +84,12 @@ func (c *DeleteCommand) Run(args []string) int { path := sanitizePath(args[0]) - if _, err := client.Logical().Delete(path); err != nil { + secret, err := client.Logical().Delete(path) + if err != nil { c.UI.Error(fmt.Sprintf("Error deleting %s: %s", path, err)) + if secret != nil { + OutputSecret(c.UI, secret) + } return 2 } diff --git a/command/write.go b/command/write.go index 05766f836..793c9ba53 100644 --- a/command/write.go +++ b/command/write.go @@ -131,6 +131,9 @@ func (c *WriteCommand) Run(args []string) int { secret, err := client.Logical().Write(path, data) if err != nil { c.UI.Error(fmt.Sprintf("Error writing data to %s: %s", path, err)) + if secret != nil { + OutputSecret(c.UI, secret) + } return 2 } if secret == nil {