Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/manual/hcloud_storage-box_subaccount.md

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.

67 changes: 67 additions & 0 deletions internal/cmd/storagebox/subaccount/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 := args[0]

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, args[1])
if err != nil {
return nil, err
}
return subaccount, nil
Comment thread
jooola marked this conversation as resolved.
Resolved
},

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
},
}
92 changes: 92 additions & 0 deletions internal/cmd/storagebox/subaccount/labels_test.go
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)
}
2 changes: 2 additions & 0 deletions internal/cmd/storagebox/subaccount/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewCommand(s state.State) *cobra.Command {
DeleteCmd.CobraCommand(s),
UpdateCmd.CobraCommand(s),
DescribeCmd.CobraCommand(s),
LabelCmds.AddCobraCommand(s),
LabelCmds.RemoveCobraCommand(s),
ResetPasswordCmd.CobraCommand(s),
UpdateAccessSettingsCmd.CobraCommand(s),
ChangeHomeDirectoryCmd.CobraCommand(s),
Expand Down
14 changes: 14 additions & 0 deletions internal/hcapi2/mock/zz_hcapi_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/hcapi2/storage_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type StorageBoxClient interface {
Names() []string
LabelKeys(string) []string
SnapshotLabelKeys(string, string) []string
SubaccountLabelKeys(string, string) []string
}

func NewStorageBoxClient(client hcloud.IStorageBoxClient) StorageBoxClient {
Expand Down Expand Up @@ -65,3 +66,17 @@ func (c *storageBoxClient) SnapshotLabelKeys(storageBoxNameOrID, snapshotNameOrI
}
return labelKeys(storageBoxSnapshot.Labels)
}

// SubaccountLabelKeys returns a slice containing the keys of all labels assigned to
// the Storage Box Subaccount with the passed name or id.
func (c *storageBoxClient) SubaccountLabelKeys(storageBoxNameOrID, subaccountNameOrID string) []string {
storageBox, _, err := c.Get(context.Background(), storageBoxNameOrID)
if err != nil || storageBox == nil {
return nil
}
storageBoxSubaccount, _, err := c.GetSubaccount(context.Background(), storageBox, subaccountNameOrID)
if err != nil || storageBoxSubaccount == nil || len(storageBoxSubaccount.Labels) == 0 {
return nil
}
return labelKeys(storageBoxSubaccount.Labels)
}
Loading