diff --git a/internal/cmd/commands/hostcatalogscmd/funcs.go b/internal/cmd/commands/hostcatalogscmd/funcs.go index 539090da4..7932ed8f7 100644 --- a/internal/cmd/commands/hostcatalogscmd/funcs.go +++ b/internal/cmd/commands/hostcatalogscmd/funcs.go @@ -111,6 +111,11 @@ func (c *Command) printListTable(items []*hostcatalogs.HostCatalog) string { fmt.Sprintf(" Description: %s", m.Description), ) } + if m.WorkerFilter != "" { + output = append(output, + fmt.Sprintf(" Worker Filter: %s", m.WorkerFilter), + ) + } if len(m.AuthorizedActions) > 0 { output = append(output, " Authorized Actions:", @@ -151,6 +156,9 @@ func printItemTable(item *hostcatalogs.HostCatalog, resp *api.Response) string { if item.SecretsHmac != "" { nonAttributeMap["Secrets HMAC"] = item.SecretsHmac } + if item.WorkerFilter != "" { + nonAttributeMap["Worker Filter"] = item.WorkerFilter + } maxLength := base.MaxAttributesLength(nonAttributeMap, item.Attributes, keySubstMap) diff --git a/internal/cmd/commands/hostcatalogscmd/plugin_funcs.go b/internal/cmd/commands/hostcatalogscmd/plugin_funcs.go index dac54c8a1..791e9aa0f 100644 --- a/internal/cmd/commands/hostcatalogscmd/plugin_funcs.go +++ b/internal/cmd/commands/hostcatalogscmd/plugin_funcs.go @@ -4,9 +4,68 @@ package hostcatalogscmd import ( + "fmt" + + "github.com/hashicorp/boundary/api/hostcatalogs" "github.com/hashicorp/boundary/internal/cmd/base" + "github.com/hashicorp/go-bexpr" ) +func init() { + extraPluginFlagsFunc = extraPluginFlagsFuncImpl + extraPluginActionsFlagsMapFunc = extraPluginActionsFlagsMapFuncImpl + extraPluginFlagsHandlingFunc = extraPluginFlagHandlingFuncImpl +} + +const ( + workerFilterFlagName = "worker-filter" +) + +type extraPluginCmdVars struct { + flagWorkerFilter string +} + +func extraPluginActionsFlagsMapFuncImpl() map[string][]string { + flags := map[string][]string{ + "create": { + workerFilterFlagName, + }, + } + flags["update"] = flags["create"] + return flags +} + +func extraPluginFlagsFuncImpl(c *PluginCommand, set *base.FlagSets, _ *base.FlagSet) { + f := set.NewFlagSet("Plugin Host Catalog Options") + + for _, name := range flagsPluginMap[c.Func] { + switch name { + case workerFilterFlagName: + f.StringVar(&base.StringVar{ + Name: workerFilterFlagName, + Target: &c.flagWorkerFilter, + Usage: `A boolean expression to filter which workers can handle dynamic host catalog commands for this host catalog.`, + }) + } + } +} + +func extraPluginFlagHandlingFuncImpl(c *PluginCommand, f *base.FlagSets, opts *[]hostcatalogs.Option) bool { + switch c.flagWorkerFilter { + case "": + case "null": + *opts = append(*opts, hostcatalogs.DefaultWorkerFilter()) + default: + if _, err := bexpr.CreateEvaluator(c.flagWorkerFilter); err != nil { + c.UI.Error(fmt.Sprintf("Unable to successfully parse filter expression: %s", err)) + return false + } + *opts = append(*opts, hostcatalogs.WithWorkerFilter(c.flagWorkerFilter)) + } + + return true +} + func (c *PluginCommand) extraPluginHelpFunc(helpMap map[string]func() string) string { var helpStr string switch c.Func { diff --git a/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go b/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go index b79678e67..f418294cf 100644 --- a/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go +++ b/internal/cmd/commands/hostcatalogscmd/plugin_hostcatalogs.gen.go @@ -37,6 +37,8 @@ type PluginCommand struct { Func string plural string + + extraPluginCmdVars } func (c *PluginCommand) AutocompleteArgs() complete.Predictor { diff --git a/internal/cmd/gencli/input.go b/internal/cmd/gencli/input.go index 977e62417..652146a96 100644 --- a/internal/cmd/gencli/input.go +++ b/internal/cmd/gencli/input.go @@ -460,6 +460,7 @@ var inputStructs = map[string][]*cmdInfo{ SubActionPrefix: "plugin", SkipNormalHelp: true, HasExtraHelpFunc: true, + HasExtraCommandVars: true, HasId: true, HasName: true, HasDescription: true,