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.
310 lines
6.5 KiB
Go
310 lines
6.5 KiB
Go
// Code generated by "make api"; DO NOT EDIT.
|
|
package hostsetscmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/boundary/api"
|
|
"github.com/hashicorp/boundary/api/hostsets"
|
|
"github.com/hashicorp/boundary/internal/cmd/base"
|
|
"github.com/hashicorp/boundary/internal/cmd/common"
|
|
"github.com/hashicorp/boundary/sdk/strutil"
|
|
"github.com/mitchellh/cli"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
func initFlags() {
|
|
flagsOnce.Do(func() {
|
|
extraFlags := extraActionsFlagsMapFunc()
|
|
for k, v := range extraFlags {
|
|
flagsMap[k] = append(flagsMap[k], v...)
|
|
}
|
|
})
|
|
}
|
|
|
|
var (
|
|
_ cli.Command = (*Command)(nil)
|
|
_ cli.CommandAutocomplete = (*Command)(nil)
|
|
)
|
|
|
|
type Command struct {
|
|
*base.Command
|
|
|
|
Func string
|
|
|
|
plural string
|
|
|
|
extraCmdVars
|
|
}
|
|
|
|
func (c *Command) AutocompleteArgs() complete.Predictor {
|
|
initFlags()
|
|
return complete.PredictAnything
|
|
}
|
|
|
|
func (c *Command) AutocompleteFlags() complete.Flags {
|
|
initFlags()
|
|
return c.Flags().Completions()
|
|
}
|
|
|
|
func (c *Command) Synopsis() string {
|
|
if extra := extraSynopsisFunc(c); extra != "" {
|
|
return extra
|
|
}
|
|
|
|
synopsisStr := "host set"
|
|
|
|
return common.SynopsisFunc(c.Func, synopsisStr)
|
|
}
|
|
|
|
func (c *Command) Help() string {
|
|
initFlags()
|
|
|
|
var helpStr string
|
|
helpMap := common.HelpMap("host set")
|
|
|
|
switch c.Func {
|
|
|
|
case "read":
|
|
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
|
|
|
case "delete":
|
|
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
|
|
|
case "list":
|
|
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
|
|
|
default:
|
|
|
|
helpStr = c.extraHelpFunc(helpMap)
|
|
|
|
}
|
|
|
|
// Keep linter from complaining if we don't actually generate code using it
|
|
_ = helpMap
|
|
return helpStr
|
|
}
|
|
|
|
var flagsMap = map[string][]string{
|
|
|
|
"read": {"id"},
|
|
|
|
"delete": {"id"},
|
|
|
|
"list": {"host-catalog-id", "filter"},
|
|
}
|
|
|
|
func (c *Command) Flags() *base.FlagSets {
|
|
if len(flagsMap[c.Func]) == 0 {
|
|
return c.FlagSet(base.FlagSetNone)
|
|
}
|
|
|
|
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
|
|
f := set.NewFlagSet("Command Options")
|
|
common.PopulateCommonFlags(c.Command, f, "host set", flagsMap[c.Func])
|
|
|
|
extraFlagsFunc(c, set, f)
|
|
|
|
return set
|
|
}
|
|
|
|
func (c *Command) Run(args []string) int {
|
|
initFlags()
|
|
|
|
switch c.Func {
|
|
case "":
|
|
return cli.RunResultHelp
|
|
|
|
case "create", "update":
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
c.plural = "host set"
|
|
switch c.Func {
|
|
case "list":
|
|
c.plural = "host sets"
|
|
}
|
|
|
|
f := c.Flags()
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandUserError
|
|
}
|
|
|
|
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
|
|
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
|
|
return base.CommandUserError
|
|
}
|
|
|
|
var opts []hostsets.Option
|
|
|
|
if strutil.StrListContains(flagsMap[c.Func], "host-catalog-id") {
|
|
switch c.Func {
|
|
case "list":
|
|
if c.FlagHostCatalogId == "" {
|
|
c.PrintCliError(errors.New("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID"))
|
|
return base.CommandUserError
|
|
}
|
|
}
|
|
}
|
|
|
|
client, err := c.Client()
|
|
if err != nil {
|
|
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
|
|
return base.CommandCliError
|
|
}
|
|
hostsetsClient := hostsets.NewClient(client)
|
|
|
|
switch c.FlagName {
|
|
case "":
|
|
case "null":
|
|
opts = append(opts, hostsets.DefaultName())
|
|
default:
|
|
opts = append(opts, hostsets.WithName(c.FlagName))
|
|
}
|
|
|
|
switch c.FlagDescription {
|
|
case "":
|
|
case "null":
|
|
opts = append(opts, hostsets.DefaultDescription())
|
|
default:
|
|
opts = append(opts, hostsets.WithDescription(c.FlagDescription))
|
|
}
|
|
|
|
if c.FlagFilter != "" {
|
|
opts = append(opts, hostsets.WithFilter(c.FlagFilter))
|
|
}
|
|
|
|
var version uint32
|
|
|
|
switch c.Func {
|
|
|
|
case "add-hosts":
|
|
switch c.FlagVersion {
|
|
case 0:
|
|
opts = append(opts, hostsets.WithAutomaticVersioning(true))
|
|
default:
|
|
version = uint32(c.FlagVersion)
|
|
}
|
|
|
|
case "set-hosts":
|
|
switch c.FlagVersion {
|
|
case 0:
|
|
opts = append(opts, hostsets.WithAutomaticVersioning(true))
|
|
default:
|
|
version = uint32(c.FlagVersion)
|
|
}
|
|
|
|
case "remove-hosts":
|
|
switch c.FlagVersion {
|
|
case 0:
|
|
opts = append(opts, hostsets.WithAutomaticVersioning(true))
|
|
default:
|
|
version = uint32(c.FlagVersion)
|
|
}
|
|
|
|
}
|
|
|
|
if ok := extraFlagsHandlingFunc(c, f, &opts); !ok {
|
|
return base.CommandUserError
|
|
}
|
|
|
|
var result api.GenericResult
|
|
|
|
var listResult api.GenericListResult
|
|
|
|
switch c.Func {
|
|
|
|
case "read":
|
|
result, err = hostsetsClient.Read(c.Context, c.FlagId, opts...)
|
|
|
|
case "delete":
|
|
result, err = hostsetsClient.Delete(c.Context, c.FlagId, opts...)
|
|
|
|
case "list":
|
|
listResult, err = hostsetsClient.List(c.Context, c.FlagHostCatalogId, opts...)
|
|
|
|
}
|
|
|
|
result, err = executeExtraActions(c, result, err, hostsetsClient, version, opts)
|
|
|
|
if err != nil {
|
|
if apiErr := api.AsServerError(err); apiErr != nil {
|
|
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
|
|
return base.CommandApiError
|
|
}
|
|
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
|
|
return base.CommandCliError
|
|
}
|
|
|
|
output, err := printCustomActionOutput(c)
|
|
if err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandUserError
|
|
}
|
|
if output {
|
|
return base.CommandSuccess
|
|
}
|
|
|
|
switch c.Func {
|
|
|
|
case "delete":
|
|
switch base.Format(c.UI) {
|
|
case "json":
|
|
if ok := c.PrintJsonItem(result); !ok {
|
|
return base.CommandCliError
|
|
}
|
|
|
|
case "table":
|
|
c.UI.Output("The delete operation completed successfully.")
|
|
}
|
|
|
|
return base.CommandSuccess
|
|
|
|
case "list":
|
|
switch base.Format(c.UI) {
|
|
case "json":
|
|
if ok := c.PrintJsonItems(listResult); !ok {
|
|
return base.CommandCliError
|
|
}
|
|
|
|
case "table":
|
|
listedItems := listResult.GetItems().([]*hostsets.HostSet)
|
|
c.UI.Output(c.printListTable(listedItems))
|
|
}
|
|
|
|
return base.CommandSuccess
|
|
|
|
}
|
|
|
|
switch base.Format(c.UI) {
|
|
case "table":
|
|
item := result.GetItem().(*hostsets.HostSet)
|
|
c.UI.Output(printItemTable(item))
|
|
|
|
case "json":
|
|
if ok := c.PrintJsonItem(result); !ok {
|
|
return base.CommandCliError
|
|
}
|
|
}
|
|
|
|
return base.CommandSuccess
|
|
}
|
|
|
|
var (
|
|
flagsOnce = new(sync.Once)
|
|
|
|
extraActionsFlagsMapFunc = func() map[string][]string { return nil }
|
|
extraSynopsisFunc = func(*Command) string { return "" }
|
|
extraFlagsFunc = func(*Command, *base.FlagSets, *base.FlagSet) {}
|
|
extraFlagsHandlingFunc = func(*Command, *base.FlagSets, *[]hostsets.Option) bool { return true }
|
|
executeExtraActions = func(_ *Command, inResult api.GenericResult, inErr error, _ *hostsets.Client, _ uint32, _ []hostsets.Option) (api.GenericResult, error) {
|
|
return inResult, inErr
|
|
}
|
|
printCustomActionOutput = func(*Command) (bool, error) { return false, nil }
|
|
)
|