Update delete action behavior and JSON format output (#1155)

This PR changes delete behavior:

* On success, a 204 is returned rather than a 200 with an empty JSON object
* The CLI now passes through the 404 error if encountered instead of swallowing it

It also updates formatting for JSON to use the response body, which means that the item (or items) in the JSON format are actually the format across the wire, instead of being reinterpreted by unmarshaling into the Go SDK and then remarshaling.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-04-26 20:38:55 -04:00
1 parent 93d8115775
commit 4690225c1e
60 files changed
+283 -474

No files matched your search

+10 -33
View File
@@ -4,7 +4,6 @@ package userscmd
import (
"errors"
"fmt"
"net/http"
"sync"
"github.com/hashicorp/boundary/api"
@@ -241,8 +240,6 @@ func (c *Command) Run(args []string) int {
return base.CommandUserError
}
existed := true
var result api.GenericResult
var listResult api.GenericListResult
@@ -259,11 +256,7 @@ func (c *Command) Run(args []string) int {
result, err = usersClient.Update(c.Context, c.FlagId, version, opts...)
case "delete":
_, err = usersClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
result, err = usersClient.Delete(c.Context, c.FlagId, opts...)
case "list":
listResult, err = usersClient.List(c.Context, c.FlagScopeId, opts...)
@@ -295,41 +288,25 @@ func (c *Command) Run(args []string) int {
case "delete":
switch base.Format(c.UI) {
case "json":
c.UI.Output(fmt.Sprintf("{ \"existed\": %t }", existed))
if ok := c.PrintJsonItem(result); !ok {
return base.CommandCliError
}
case "table":
output := "The delete operation completed successfully"
switch existed {
case true:
output += "."
default:
output += ", however the resource did not exist at the time."
}
c.UI.Output(output)
c.UI.Output("The delete operation completed successfully.")
}
return base.CommandSuccess
case "list":
listedItems := listResult.GetItems().([]*users.User)
switch base.Format(c.UI) {
case "json":
switch {
case len(listedItems) == 0:
c.UI.Output("null")
default:
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
if ok := c.PrintJsonItems(listResult, items); !ok {
return base.CommandCliError
}
if ok := c.PrintJsonItems(listResult); !ok {
return base.CommandCliError
}
case "table":
listedItems := listResult.GetItems().([]*users.User)
c.UI.Output(c.printListTable(listedItems))
}
@@ -337,13 +314,13 @@ func (c *Command) Run(args []string) int {
}
item := result.GetItem().(*users.User)
switch base.Format(c.UI) {
case "table":
item := result.GetItem().(*users.User)
c.UI.Output(printItemTable(item))
case "json":
if ok := c.PrintJsonItem(result, item); !ok {
if ok := c.PrintJsonItem(result); !ok {
return base.CommandCliError
}
}