From a4c20164f3e12548f449cb670a4bd95a33667fd8 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 31 Aug 2020 10:06:21 -0400 Subject: [PATCH] Add add/remove/set hosts functions to host-sets command (#316) --- internal/cmd/commands.go | 18 +++++ internal/cmd/commands/hostsets/hostset.go | 98 ++++++++++++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index 8401bd8d8..653fee009 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -310,6 +310,24 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Func: "update", }, nil }, + "host-sets add-hosts": func() (cli.Command, error) { + return &hostsets.Command{ + Command: base.NewCommand(ui), + Func: "add-hosts", + }, nil + }, + "host-sets remove-hosts": func() (cli.Command, error) { + return &hostsets.Command{ + Command: base.NewCommand(ui), + Func: "remove-hosts", + }, nil + }, + "host-sets set-hosts": func() (cli.Command, error) { + return &hostsets.Command{ + Command: base.NewCommand(ui), + Func: "set-hosts", + }, nil + }, "hosts create": func() (cli.Command, error) { return &hosts.CreateCommand{ diff --git a/internal/cmd/commands/hostsets/hostset.go b/internal/cmd/commands/hostsets/hostset.go index 17c5dc9eb..d2f37839e 100644 --- a/internal/cmd/commands/hostsets/hostset.go +++ b/internal/cmd/commands/hostsets/hostset.go @@ -23,6 +23,7 @@ type Command struct { Func string flagHostCatalogId string + flagHosts []string } func (c *Command) Synopsis() string { @@ -31,14 +32,23 @@ func (c *Command) Synopsis() string { return "Create host-set resources within Boundary" case "update": return "Update host-set resources within Boundary" + case "add-hosts": + return "Add hosts to the specified host-set" + case "remove-hosts": + return "Remove hosts from the specified host-set" + case "set-hosts": + return "Set the full contents of the hosts on the specified host-set" default: return common.SynopsisFunc(c.Func, "host-set") } } var flagsMap = map[string][]string{ - "read": {"id"}, - "delete": {"id"}, + "read": {"id"}, + "delete": {"id"}, + "add-hosts": {"id", "host", "version"}, + "set-hosts": {"id", "host", "version"}, + "remove-hosts": {"id", "host", "version"}, } func (c *Command) Help() string { @@ -80,6 +90,36 @@ func (c *Command) Help() string { "", " Please see the typed subcommand help for detailed usage information.", }) + case "add-hosts": + return base.WrapForHelpText([]string{ + "Usage: boundary host-sets add-hosts [sub command] [options] [args]", + "", + " This command allows adding hosts to host-set resources, if the types match and the operation is allowed by the given host-set type. Example:", + "", + " Add static-type hosts to a static-type host-set:", + "", + ` $ boundary host-sets add-hosts -id hsst_1234567890 -host hst_1234567890 -host hst_0987654321`, + }) + case "remove-hosts": + return base.WrapForHelpText([]string{ + "Usage: boundary host-sets remove-hosts [sub command] [options] [args]", + "", + " This command allows removing hosts from host-set resources, if the types match and the operation is allowed by the given host-set type. Example:", + "", + " Remove static-type hosts from a static-type host-set:", + "", + ` $ boundary host-sets remove-hosts -id hsst_1234567890 -host hst_0987654321`, + }) + case "set-hosts": + return base.WrapForHelpText([]string{ + "Usage: boundary host-sets set-hosts [sub command] [options] [args]", + "", + " This command allows setting the complete set of hosts on host-set resources, if the types match and the operation is allowed by the given host-set type. Example:", + "", + " Set the complete set of static-type hosts on a static-type host-set:", + "", + ` $ boundary host-sets remove-hosts -id hsst_1234567890 -host hst_1234567890`, + }) default: return helpMap[c.Func]() + c.Flags().Help() } @@ -100,6 +140,17 @@ func (c *Command) Flags() *base.FlagSets { Usage: "The host-catalog resource in which to create or update the host-set resource", }) + for _, name := range flagsMap[c.Func] { + switch name { + case "host": + f.StringSliceVar(&base.StringSliceVar{ + Name: "host", + Target: &c.flagHosts, + Usage: "The hosts to add, remove, or set. May be specified multiple times.", + }) + } + } + return set } @@ -153,6 +204,43 @@ func (c *Command) Run(args []string) int { opts = append(opts, hostsets.WithDescription(c.FlagDescription)) } + hosts := c.flagHosts + switch c.Func { + case "add-hosts", "remove-hosts": + if len(c.flagHosts) == 0 { + c.UI.Error("No hosts supplied via -host") + return 1 + } + + case "set-hosts": + switch len(c.flagHosts) { + case 0: + case 1: + if c.flagHosts[0] == "null" { + hosts = []string{} + } + } + if hosts == nil { + c.UI.Error("No hosts supplied via -host") + return 1 + } + } + + // Perform check-and-set when needed + var version uint32 + switch c.Func { + case "add-hosts", "remove-hosts", "set-hosts": + switch c.FlagVersion { + case 0: + opts = append(opts, hostsets.WithAutomaticVersioning()) + default: + version = uint32(c.FlagVersion) + } + default: + // The only other one that needs it is update, handled by the static + // file + } + hostsetClient := hostsets.NewClient(client) var existed bool @@ -167,6 +255,12 @@ func (c *Command) Run(args []string) int { existed, apiErr, err = hostsetClient.Delete(c.Context, c.flagHostCatalogId, c.FlagId, opts...) case "list": listedSets, apiErr, err = hostsetClient.List(c.Context, c.flagHostCatalogId, opts...) + case "add-hosts": + set, apiErr, err = hostsetClient.AddHosts(c.Context, c.flagHostCatalogId, c.FlagId, version, c.flagHosts, opts...) + case "remove-hosts": + set, apiErr, err = hostsetClient.RemoveHosts(c.Context, c.flagHostCatalogId, c.FlagId, version, c.flagHosts, opts...) + case "set-hosts": + set, apiErr, err = hostsetClient.SetHosts(c.Context, c.flagHostCatalogId, c.FlagId, version, c.flagHosts, opts...) } plural := "host set"