From 20897640cd96033ee2882e7d44a0cdfca5b4baf2 Mon Sep 17 00:00:00 2001 From: Louis Ruch Date: Mon, 8 Jan 2024 23:04:23 -0800 Subject: [PATCH] feat(cli): Add support for Scope Storage Policy --- internal/cmd/commands.go | 10 +++++ internal/cmd/commands/scopescmd/funcs.go | 41 ++++++++++++++++++- internal/cmd/commands/scopescmd/scopes.gen.go | 16 ++++++++ internal/cmd/gencli/input.go | 2 +- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index 6681958ef3..202966a200 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -942,6 +942,16 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { &scopescmd.DestroyKeyVersionCommand{ Command: base.NewCommand(ui), }), + "scopes attach-storage-policy": clientCacheWrapper( + &scopescmd.Command{ + Command: base.NewCommand(ui), + Func: "attach-storage-policy", + }), + "scopes detach-storage-policy": clientCacheWrapper( + &scopescmd.Command{ + Command: base.NewCommand(ui), + Func: "detach-storage-policy", + }), "search": func() (cli.Command, error) { return &unsupported.UnsupportedCommand{ diff --git a/internal/cmd/commands/scopescmd/funcs.go b/internal/cmd/commands/scopescmd/funcs.go index fecc8f7a6b..66655a0dfa 100644 --- a/internal/cmd/commands/scopescmd/funcs.go +++ b/internal/cmd/commands/scopescmd/funcs.go @@ -17,18 +17,22 @@ const ( flagPrimaryAuthMethodIdName = "primary-auth-method-id" flagSkipAdminRoleCreationName = "skip-admin-role-creation" flagSkipDefaultRoleCreationName = "skip-default-role-creation" + flagStoragePolicyIdName = "storage-policy-id" ) func init() { extraActionsFlagsMapFunc = extraActionsFlagsMapFuncImpl extraFlagsFunc = extraFlagsFuncImpl extraFlagsHandlingFunc = extraFlagsHandlingFuncImpl + executeExtraActions = executeExtraActionsImpl } func extraActionsFlagsMapFuncImpl() map[string][]string { return map[string][]string{ - "create": {flagSkipAdminRoleCreationName, flagSkipDefaultRoleCreationName}, - "update": {flagPrimaryAuthMethodIdName}, + "create": {flagSkipAdminRoleCreationName, flagSkipDefaultRoleCreationName}, + "update": {flagPrimaryAuthMethodIdName}, + "attach-storage-policy": {"id", "version", flagStoragePolicyIdName}, + "detach-storage-policy": {"id", "version"}, } } @@ -36,6 +40,7 @@ type extraCmdVars struct { flagSkipAdminRoleCreation bool flagSkipDefaultRoleCreation bool flagPrimaryAuthMethodId string + flagStoragePolicyId string } func extraFlagsFuncImpl(c *Command, set *base.FlagSets, f *base.FlagSet) { @@ -59,6 +64,12 @@ func extraFlagsFuncImpl(c *Command, set *base.FlagSets, f *base.FlagSet) { Target: &c.flagPrimaryAuthMethodId, Usage: "If set, the primary auth method id for the scope. A primary auth method is allowed to create users on first login and is also used as a source for account full name and email for a scope's users", }) + case flagStoragePolicyIdName: + f.StringVar(&base.StringVar{ + Name: flagStoragePolicyIdName, + Target: &c.flagStoragePolicyId, + Usage: "The public ID of the Storage Policy to attach to this scope. Can only attach to the global scope and an Org scope.", + }) } } } @@ -77,6 +88,24 @@ func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, opts *[]scopes.Opt return true } +func executeExtraActionsImpl(c *Command, origResp *api.Response, origItem *scopes.Scope, origItems []*scopes.Scope, origError error, scopeClient *scopes.Client, version uint32, opts []scopes.Option) (*api.Response, *scopes.Scope, []*scopes.Scope, error) { + switch c.Func { + case "attach-storage-policy": + result, err := scopeClient.AttachStoragePolicy(c.Context, c.FlagId, version, c.flagStoragePolicyId, opts...) + if err != nil { + return nil, nil, nil, err + } + return result.GetResponse(), result.GetItem(), nil, err + case "detach-storage-policy": + result, err := scopeClient.DetachStoragePolicy(c.Context, c.FlagId, version, opts...) + if err != nil { + return nil, nil, nil, err + } + return result.GetResponse(), result.GetItem(), nil, err + } + return origResp, origItem, origItems, origError +} + func (c *Command) printListTable(items []*scopes.Scope) string { if len(items) == 0 { return "No child scopes found" @@ -120,6 +149,11 @@ func (c *Command) printListTable(items []*scopes.Scope) string { fmt.Sprintf(" PrimaryAuthMethodId: %s", item.PrimaryAuthMethodId), ) } + if item.StoragePolicyId != "" { + output = append(output, + fmt.Sprintf(" StoragePolicyId: %s", item.StoragePolicyId), + ) + } if len(item.AuthorizedActions) > 0 { output = append(output, " Authorized Actions:", @@ -154,6 +188,9 @@ func printItemTable(item *scopes.Scope, resp *api.Response) string { if item.PrimaryAuthMethodId != "" { nonAttributeMap["Primary Auth Method ID"] = item.PrimaryAuthMethodId } + if item.StoragePolicyId != "" { + nonAttributeMap["Storage Policy ID"] = item.StoragePolicyId + } maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) diff --git a/internal/cmd/commands/scopescmd/scopes.gen.go b/internal/cmd/commands/scopescmd/scopes.gen.go index 6f6e652c62..1e1089360b 100644 --- a/internal/cmd/commands/scopescmd/scopes.gen.go +++ b/internal/cmd/commands/scopescmd/scopes.gen.go @@ -221,6 +221,22 @@ func (c *Command) Run(args []string) int { version = uint32(c.FlagVersion) } + case "attach-storage-policy": + switch c.FlagVersion { + case 0: + opts = append(opts, scopes.WithAutomaticVersioning(true)) + default: + version = uint32(c.FlagVersion) + } + + case "detach-storage-policy": + switch c.FlagVersion { + case 0: + opts = append(opts, scopes.WithAutomaticVersioning(true)) + default: + version = uint32(c.FlagVersion) + } + } if ok := extraFlagsHandlingFunc(c, f, &opts); !ok { diff --git a/internal/cmd/gencli/input.go b/internal/cmd/gencli/input.go index a92337e3c9..504e132f15 100644 --- a/internal/cmd/gencli/input.go +++ b/internal/cmd/gencli/input.go @@ -580,7 +580,7 @@ var inputStructs = map[string][]*cmdInfo{ Container: "Scope", HasName: true, HasDescription: true, - VersionedActions: []string{"update"}, + VersionedActions: []string{"update", "attach-storage-policy", "detach-storage-policy"}, }, }, "sessions": {