Skip to content

Commit db53edc

Browse files
authored
fix(storage-box): implement missing subaccount label commands (#1553)
Fixes #1548
1 parent 434b489 commit db53edc

8 files changed

Lines changed: 263 additions & 0 deletions

File tree

‎docs/reference/manual/hcloud_storage-box_subaccount.md‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/reference/manual/hcloud_storage-box_subaccount_add-label.md‎

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/reference/manual/hcloud_storage-box_subaccount_remove-label.md‎

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package subaccount
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/hetznercloud/cli/internal/cmd/base"
9+
"github.com/hetznercloud/cli/internal/cmd/cmpl"
10+
"github.com/hetznercloud/cli/internal/hcapi2"
11+
"github.com/hetznercloud/cli/internal/state"
12+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
13+
)
14+
15+
var LabelCmds = base.LabelCmds[*hcloud.StorageBoxSubaccount]{
16+
ResourceNameSingular: "Storage Box Subaccount",
17+
ShortDescriptionAdd: "Add a label to a Storage Box Subaccount",
18+
ShortDescriptionRemove: "Remove a label from a Storage Box Subaccount",
19+
20+
PositionalArgumentOverride: []string{"storage-box", "subaccount"},
21+
ValidArgsFunction: func(client hcapi2.Client) []cobra.CompletionFunc {
22+
return []cobra.CompletionFunc{
23+
cmpl.SuggestCandidatesF(client.StorageBox().Names),
24+
SuggestSubaccounts(client),
25+
cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string {
26+
if len(args) < 2 {
27+
return nil
28+
}
29+
return client.StorageBox().SubaccountLabelKeys(args[0], args[1])
30+
}),
31+
}
32+
},
33+
34+
FetchWithArgs: func(s state.State, args []string) (*hcloud.StorageBoxSubaccount, error) {
35+
storageBoxIDOrName, subaccountIDOrName := args[0], args[1]
36+
37+
storageBox, _, err := s.Client().StorageBox().Get(s, storageBoxIDOrName)
38+
if err != nil {
39+
return nil, err
40+
}
41+
if storageBox == nil {
42+
return nil, fmt.Errorf("Storage Box not found: %s", storageBoxIDOrName)
43+
}
44+
45+
subaccount, _, err := s.Client().StorageBox().GetSubaccount(s, storageBox, subaccountIDOrName)
46+
if err != nil {
47+
return nil, err
48+
}
49+
if subaccount == nil {
50+
return nil, fmt.Errorf("Storage Box Subaccount not found: %s", subaccountIDOrName)
51+
}
52+
return subaccount, nil
53+
},
54+
55+
SetLabels: func(s state.State, subaccount *hcloud.StorageBoxSubaccount, labels map[string]string) error {
56+
opts := hcloud.StorageBoxSubaccountUpdateOpts{
57+
Labels: labels,
58+
}
59+
_, _, err := s.Client().StorageBox().UpdateSubaccount(s, subaccount, opts)
60+
return err
61+
},
62+
63+
GetLabels: func(subaccount *hcloud.StorageBoxSubaccount) map[string]string {
64+
return subaccount.Labels
65+
},
66+
67+
GetIDOrName: func(subaccount *hcloud.StorageBoxSubaccount) string {
68+
return subaccount.Name
69+
},
70+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package subaccount_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
"go.uber.org/mock/gomock"
9+
10+
"github.com/hetznercloud/cli/internal/cmd/storagebox/subaccount"
11+
"github.com/hetznercloud/cli/internal/testutil"
12+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
13+
)
14+
15+
func TestLabelAdd(t *testing.T) {
16+
fx := testutil.NewFixture(t)
17+
defer fx.Finish()
18+
19+
cmd := subaccount.LabelCmds.AddCobraCommand(fx.State())
20+
fx.ExpectEnsureToken()
21+
22+
sb := &hcloud.StorageBox{
23+
ID: 123,
24+
Name: "test",
25+
}
26+
sbs := &hcloud.StorageBoxSubaccount{
27+
ID: 456,
28+
Name: "my-subaccount",
29+
StorageBox: sb,
30+
}
31+
32+
fx.Client.StorageBoxClient.EXPECT().
33+
Get(gomock.Any(), "123").
34+
Return(sb, nil, nil)
35+
fx.Client.StorageBoxClient.EXPECT().
36+
GetSubaccount(gomock.Any(), sb, "my-subaccount").
37+
Return(sbs, nil, nil)
38+
fx.Client.StorageBoxClient.EXPECT().
39+
UpdateSubaccount(gomock.Any(), sbs, hcloud.StorageBoxSubaccountUpdateOpts{
40+
Labels: map[string]string{
41+
"key": "value",
42+
},
43+
})
44+
45+
out, errOut, err := fx.Run(cmd, []string{"123", "my-subaccount", "key=value"})
46+
47+
expOut := "Label(s) key added to Storage Box Subaccount my-subaccount\n"
48+
49+
require.NoError(t, err)
50+
assert.Empty(t, errOut)
51+
assert.Equal(t, expOut, out)
52+
}
53+
54+
func TestLabelRemove(t *testing.T) {
55+
fx := testutil.NewFixture(t)
56+
defer fx.Finish()
57+
58+
cmd := subaccount.LabelCmds.RemoveCobraCommand(fx.State())
59+
fx.ExpectEnsureToken()
60+
61+
sb := &hcloud.StorageBox{
62+
ID: 123,
63+
Name: "test",
64+
}
65+
sbs := &hcloud.StorageBoxSubaccount{
66+
ID: 456,
67+
Name: "my-subaccount",
68+
Labels: map[string]string{
69+
"key": "value",
70+
},
71+
StorageBox: sb,
72+
}
73+
74+
fx.Client.StorageBoxClient.EXPECT().
75+
Get(gomock.Any(), "123").
76+
Return(sb, nil, nil)
77+
fx.Client.StorageBoxClient.EXPECT().
78+
GetSubaccount(gomock.Any(), sb, "my-subaccount").
79+
Return(sbs, nil, nil)
80+
fx.Client.StorageBoxClient.EXPECT().
81+
UpdateSubaccount(gomock.Any(), sbs, hcloud.StorageBoxSubaccountUpdateOpts{
82+
Labels: make(map[string]string),
83+
})
84+
85+
out, errOut, err := fx.Run(cmd, []string{"123", "my-subaccount", "key"})
86+
87+
expOut := "Label(s) key removed from Storage Box Subaccount my-subaccount\n"
88+
89+
require.NoError(t, err)
90+
assert.Empty(t, errOut)
91+
assert.Equal(t, expOut, out)
92+
}

‎internal/cmd/storagebox/subaccount/subaccount.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func NewCommand(s state.State) *cobra.Command {
2222
DeleteCmd.CobraCommand(s),
2323
UpdateCmd.CobraCommand(s),
2424
DescribeCmd.CobraCommand(s),
25+
LabelCmds.AddCobraCommand(s),
26+
LabelCmds.RemoveCobraCommand(s),
2527
ResetPasswordCmd.CobraCommand(s),
2628
UpdateAccessSettingsCmd.CobraCommand(s),
2729
ChangeHomeDirectoryCmd.CobraCommand(s),

‎internal/hcapi2/mock/zz_hcapi_mock.go‎

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/hcapi2/storage_box.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type StorageBoxClient interface {
1212
Names() []string
1313
LabelKeys(string) []string
1414
SnapshotLabelKeys(string, string) []string
15+
SubaccountLabelKeys(string, string) []string
1516
}
1617

1718
func NewStorageBoxClient(client hcloud.IStorageBoxClient) StorageBoxClient {
@@ -65,3 +66,17 @@ func (c *storageBoxClient) SnapshotLabelKeys(storageBoxNameOrID, snapshotNameOrI
6566
}
6667
return labelKeys(storageBoxSnapshot.Labels)
6768
}
69+
70+
// SubaccountLabelKeys returns a slice containing the keys of all labels assigned to
71+
// the Storage Box Subaccount with the passed name or id.
72+
func (c *storageBoxClient) SubaccountLabelKeys(storageBoxNameOrID, subaccountNameOrID string) []string {
73+
storageBox, _, err := c.Get(context.Background(), storageBoxNameOrID)
74+
if err != nil || storageBox == nil {
75+
return nil
76+
}
77+
storageBoxSubaccount, _, err := c.GetSubaccount(context.Background(), storageBox, subaccountNameOrID)
78+
if err != nil || storageBoxSubaccount == nil || len(storageBoxSubaccount.Labels) == 0 {
79+
return nil
80+
}
81+
return labelKeys(storageBoxSubaccount.Labels)
82+
}

0 commit comments

Comments
 (0)