Repository navigation
fix(storage-box): implement missing subaccount label commands #1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package subaccount | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/hetznercloud/cli/internal/cmd/base" | ||
| "github.com/hetznercloud/cli/internal/cmd/cmpl" | ||
| "github.com/hetznercloud/cli/internal/hcapi2" | ||
| "github.com/hetznercloud/cli/internal/state" | ||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
| ) | ||
|
|
||
| var LabelCmds = base.LabelCmds[*hcloud.StorageBoxSubaccount]{ | ||
| ResourceNameSingular: "Storage Box Subaccount", | ||
| ShortDescriptionAdd: "Add a label to a Storage Box Subaccount", | ||
| ShortDescriptionRemove: "Remove a label from a Storage Box Subaccount", | ||
|
|
||
| PositionalArgumentOverride: []string{"storage-box", "subaccount"}, | ||
| ValidArgsFunction: func(client hcapi2.Client) []cobra.CompletionFunc { | ||
| return []cobra.CompletionFunc{ | ||
| cmpl.SuggestCandidatesF(client.StorageBox().Names), | ||
| SuggestSubaccounts(client), | ||
| cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string { | ||
| if len(args) < 2 { | ||
| return nil | ||
| } | ||
| return client.StorageBox().SubaccountLabelKeys(args[0], args[1]) | ||
| }), | ||
| } | ||
| }, | ||
|
|
||
| FetchWithArgs: func(s state.State, args []string) (*hcloud.StorageBoxSubaccount, error) { | ||
| storageBoxIDOrName, subaccountIDOrName := args[0], args[1] | ||
|
|
||
| storageBox, _, err := s.Client().StorageBox().Get(s, storageBoxIDOrName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if storageBox == nil { | ||
| return nil, fmt.Errorf("Storage Box not found: %s", storageBoxIDOrName) | ||
| } | ||
|
|
||
| subaccount, _, err := s.Client().StorageBox().GetSubaccount(s, storageBox, subaccountIDOrName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if subaccount == nil { | ||
| return nil, fmt.Errorf("Storage Box Subaccount not found: %s", subaccountIDOrName) | ||
| } | ||
| return subaccount, nil | ||
| }, | ||
|
|
||
| SetLabels: func(s state.State, subaccount *hcloud.StorageBoxSubaccount, labels map[string]string) error { | ||
| opts := hcloud.StorageBoxSubaccountUpdateOpts{ | ||
| Labels: labels, | ||
| } | ||
| _, _, err := s.Client().StorageBox().UpdateSubaccount(s, subaccount, opts) | ||
| return err | ||
| }, | ||
|
|
||
| GetLabels: func(subaccount *hcloud.StorageBoxSubaccount) map[string]string { | ||
| return subaccount.Labels | ||
| }, | ||
|
|
||
| GetIDOrName: func(subaccount *hcloud.StorageBoxSubaccount) string { | ||
| return subaccount.Name | ||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package subaccount_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/mock/gomock" | ||
|
|
||
| "github.com/hetznercloud/cli/internal/cmd/storagebox/subaccount" | ||
| "github.com/hetznercloud/cli/internal/testutil" | ||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
| ) | ||
|
|
||
| func TestLabelAdd(t *testing.T) { | ||
| fx := testutil.NewFixture(t) | ||
| defer fx.Finish() | ||
|
|
||
| cmd := subaccount.LabelCmds.AddCobraCommand(fx.State()) | ||
| fx.ExpectEnsureToken() | ||
|
|
||
| sb := &hcloud.StorageBox{ | ||
| ID: 123, | ||
| Name: "test", | ||
| } | ||
| sbs := &hcloud.StorageBoxSubaccount{ | ||
| ID: 456, | ||
| Name: "my-subaccount", | ||
| StorageBox: sb, | ||
| } | ||
|
|
||
| fx.Client.StorageBoxClient.EXPECT(). | ||
| Get(gomock.Any(), "123"). | ||
| Return(sb, nil, nil) | ||
| fx.Client.StorageBoxClient.EXPECT(). | ||
| GetSubaccount(gomock.Any(), sb, "my-subaccount"). | ||
| Return(sbs, nil, nil) | ||
| fx.Client.StorageBoxClient.EXPECT(). | ||
| UpdateSubaccount(gomock.Any(), sbs, hcloud.StorageBoxSubaccountUpdateOpts{ | ||
| Labels: map[string]string{ | ||
| "key": "value", | ||
| }, | ||
| }) | ||
|
|
||
| out, errOut, err := fx.Run(cmd, []string{"123", "my-subaccount", "key=value"}) | ||
|
|
||
| expOut := "Label(s) key added to Storage Box Subaccount my-subaccount\n" | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Empty(t, errOut) | ||
| assert.Equal(t, expOut, out) | ||
| } | ||
|
|
||
| func TestLabelRemove(t *testing.T) { | ||
| fx := testutil.NewFixture(t) | ||
| defer fx.Finish() | ||
|
|
||
| cmd := subaccount.LabelCmds.RemoveCobraCommand(fx.State()) | ||
| fx.ExpectEnsureToken() | ||
|
|
||
| sb := &hcloud.StorageBox{ | ||
| ID: 123, | ||
| Name: "test", | ||
| } | ||
| sbs := &hcloud.StorageBoxSubaccount{ | ||
| ID: 456, | ||
| Name: "my-subaccount", | ||
| Labels: map[string]string{ | ||
| "key": "value", | ||
| }, | ||
| StorageBox: sb, | ||
| } | ||
|
|
||
| fx.Client.StorageBoxClient.EXPECT(). | ||
| Get(gomock.Any(), "123"). | ||
| Return(sb, nil, nil) | ||
| fx.Client.StorageBoxClient.EXPECT(). | ||
| GetSubaccount(gomock.Any(), sb, "my-subaccount"). | ||
| Return(sbs, nil, nil) | ||
| fx.Client.StorageBoxClient.EXPECT(). | ||
| UpdateSubaccount(gomock.Any(), sbs, hcloud.StorageBoxSubaccountUpdateOpts{ | ||
| Labels: make(map[string]string), | ||
| }) | ||
|
|
||
| out, errOut, err := fx.Run(cmd, []string{"123", "my-subaccount", "key"}) | ||
|
|
||
| expOut := "Label(s) key removed from Storage Box Subaccount my-subaccount\n" | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Empty(t, errOut) | ||
| assert.Equal(t, expOut, out) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.