From bb0f08f145e69b37137edcd55d6263d662f7d3b2 Mon Sep 17 00:00:00 2001 From: aardbol <14614620+aardbol@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:44:28 +0100 Subject: [PATCH 1/2] feat(storage): show attached servers in storage list --- internal/commands/storage/list.go | 135 ++++++++++++++++++++----- internal/commands/storage/list_test.go | 118 +++++++++++++++++++++ 2 files changed, 230 insertions(+), 23 deletions(-) diff --git a/internal/commands/storage/list.go b/internal/commands/storage/list.go index 26b2a086b..658006cbb 100644 --- a/internal/commands/storage/list.go +++ b/internal/commands/storage/list.go @@ -1,6 +1,8 @@ package storage import ( + "sync" + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" "github.com/UpCloudLtd/upcloud-cli/v3/internal/format" @@ -26,13 +28,14 @@ func ListCommand() commands.Command { type listCommand struct { *commands.BaseCommand - all config.OptionalBoolean - private config.OptionalBoolean - public config.OptionalBoolean - normal config.OptionalBoolean - backup config.OptionalBoolean - cdrom config.OptionalBoolean - template config.OptionalBoolean + all config.OptionalBoolean + private config.OptionalBoolean + public config.OptionalBoolean + normal config.OptionalBoolean + backup config.OptionalBoolean + cdrom config.OptionalBoolean + template config.OptionalBoolean + attachedServers config.OptionalBoolean } // InitCommand implements Command.InitCommand @@ -45,6 +48,7 @@ func (s *listCommand) InitCommand() { config.AddToggleFlag(flags, &s.backup, "backup", false, "Show only backup storages.") config.AddToggleFlag(flags, &s.cdrom, "cdrom", false, "Show only cdrom storages.") config.AddToggleFlag(flags, &s.template, "template", false, "Show only template storages.") + config.AddToggleFlag(flags, &s.attachedServers, "show-attached-servers", false, "Show the servers each storage is attached to.") s.AddFlags(flags) } @@ -94,9 +98,17 @@ func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou } } + var attachedServers map[string]upcloud.ServerUUIDSlice + if s.attachedServers.Value() { + attachedServers, err = getAttachedServersByStorageUUID(filtered, exec) + if err != nil { + return nil, err + } + } + rows := []output.TableRow{} for _, storage := range filtered { - rows = append(rows, output.TableRow{ + row := output.TableRow{ storage.UUID, storage.Title, storage.Encrypted, @@ -106,25 +118,102 @@ func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou storage.Tier, storage.Zone, storage.Access, - storage.Created, - }) + } + if s.attachedServers.Value() { + row = append(row, attachedServers[storage.UUID]) + } + row = append(row, storage.Created) + rows = append(rows, row) + } + + columns := []output.TableColumn{ + {Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours}, + {Key: "title", Header: "Title"}, + {Key: "encrypted", Header: "Encrypted", Format: format.Boolean}, + {Key: "type", Header: "Type"}, + {Key: "size", Header: "Size"}, + {Key: "state", Header: "State", Format: format.StorageState}, + {Key: "tier", Header: "Tier"}, + {Key: "zone", Header: "Zone"}, + {Key: "access", Header: "Access"}, + } + if s.attachedServers.Value() { + columns = append(columns, output.TableColumn{Key: "servers", Header: "Attached to", Format: formatShowServers}) + } + + var value any = upcloud.Storages{Storages: filtered} + if s.attachedServers.Value() { + enriched := storagesWithAttachedServers{Storages: make([]storageWithAttachedServers, 0, len(filtered))} + for _, storage := range filtered { + servers := attachedServers[storage.UUID] + if servers == nil { + servers = upcloud.ServerUUIDSlice{} + } + enriched.Storages = append(enriched.Storages, storageWithAttachedServers{ + Storage: storage, + ServerUUIDs: servers, + }) + } + value = enriched } return output.MarshaledWithHumanOutput{ - Value: upcloud.Storages{Storages: filtered}, + Value: value, Output: output.Table{ - Columns: []output.TableColumn{ - {Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours}, - {Key: "title", Header: "Title"}, - {Key: "encrypted", Header: "Encrypted", Format: format.Boolean}, - {Key: "type", Header: "Type"}, - {Key: "size", Header: "Size"}, - {Key: "state", Header: "State", Format: format.StorageState}, - {Key: "tier", Header: "Tier"}, - {Key: "zone", Header: "Zone"}, - {Key: "access", Header: "Access"}, - }, - Rows: rows, + Columns: columns, + Rows: rows, }, }, nil } + +// storageWithAttachedServers embeds a storage with the UUIDs of the servers it is +// attached to, so machine-readable output includes the same data as the human output. +type storageWithAttachedServers struct { + upcloud.Storage + + ServerUUIDs upcloud.ServerUUIDSlice `json:"servers"` +} + +type storagesWithAttachedServers struct { + Storages []storageWithAttachedServers `json:"storages"` +} + +// getAttachedServersByStorageUUID fetches the attached servers for each storage. +// The storage list endpoint does not include attachment information, so this +// requires a details request per storage. +func getAttachedServersByStorageUUID(storages []upcloud.Storage, exec commands.Executor) (map[string]upcloud.ServerUUIDSlice, error) { + type result struct { + storageUUID string + serverUUIDs upcloud.ServerUUIDSlice + err error + } + returnChan := make(chan result) + var wg sync.WaitGroup + + for _, storage := range storages { + wg.Add(1) + go func(storage upcloud.Storage) { + defer wg.Done() + details, err := exec.Storage().GetStorageDetails(exec.Context(), &request.GetStorageDetailsRequest{UUID: storage.UUID}) + var servers upcloud.ServerUUIDSlice + if details != nil { + servers = details.ServerUUIDs + } + returnChan <- result{storageUUID: storage.UUID, serverUUIDs: servers, err: err} + }(storage) + } + + go func() { + wg.Wait() + close(returnChan) + }() + + attached := make(map[string]upcloud.ServerUUIDSlice) + for res := range returnChan { + if res.err != nil { + return nil, res.err + } + attached[res.storageUUID] = res.serverUUIDs + } + return attached, nil +} diff --git a/internal/commands/storage/list_test.go b/internal/commands/storage/list_test.go index 14a270cb7..6ad4784a3 100644 --- a/internal/commands/storage/list_test.go +++ b/internal/commands/storage/list_test.go @@ -1,6 +1,7 @@ package storage import ( + "encoding/json" "testing" "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" @@ -9,8 +10,11 @@ import ( "github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute" "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" + "github.com/jedib0t/go-pretty/v6/text" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestListStorages(t *testing.T) { @@ -129,3 +133,117 @@ func TestListStorages(t *testing.T) { }) } } + +func TestListStoragesAttachedServers(t *testing.T) { + text.DisableColors() + attached := upcloud.Storage{ + UUID: UUID1, + Title: Title1, + Access: "private", + State: "online", + Type: "normal", + Zone: "fi-hel1", + Size: 40, + Tier: "maxiops", + } + detached := upcloud.Storage{ + UUID: UUID2, + Title: Title2, + Access: "private", + State: "online", + Type: "normal", + Zone: "fi-hel1", + Size: 10, + Tier: "maxiops", + } + serverUUID := "009d64ef-31d1-4684-a26b-c86c955cbf46" + storages := upcloud.Storages{Storages: []upcloud.Storage{attached, detached}} + + for _, test := range []struct { + name string + args []string + jsonOutput bool + detailsCalls int + contains []string + notContains []string + checkJSON bool + }{ + { + name: "human output with attached servers", + args: []string{"--show-attached-servers"}, + detailsCalls: 2, + contains: []string{"Attached to", serverUUID, "None"}, + }, + { + name: "human output without flag", + args: []string{}, + detailsCalls: 0, + notContains: []string{"Attached to", serverUUID}, + }, + { + name: "JSON output with attached servers", + args: []string{"--show-attached-servers"}, + jsonOutput: true, + detailsCalls: 2, + checkJSON: true, + }, + { + name: "JSON output without flag", + args: []string{}, + jsonOutput: true, + detailsCalls: 0, + notContains: []string{`"servers"`}, + }, + } { + t.Run(test.name, func(t *testing.T) { + CachedStorages = nil + mService := new(smock.Service) + mService.On("GetStorages", mock.Anything).Return(&storages, nil) + mService.On("GetStorageDetails", &request.GetStorageDetailsRequest{UUID: UUID1}).Return(&upcloud.StorageDetails{ + Storage: attached, + ServerUUIDs: upcloud.ServerUUIDSlice{serverUUID}, + }, nil) + mService.On("GetStorageDetails", &request.GetStorageDetailsRequest{UUID: UUID2}).Return(&upcloud.StorageDetails{ + Storage: detached, + ServerUUIDs: upcloud.ServerUUIDSlice{}, + }, nil) + + conf := config.New() + if test.jsonOutput { + conf.Viper().Set(config.KeyOutput, config.ValueOutputJSON) + } + + c := commands.BuildCommand(ListCommand(), nil, config.New()) + c.Cobra().SetArgs(test.args) + output, err := mockexecute.MockExecute(c, mService, conf) + assert.NoError(t, err) + mService.AssertNumberOfCalls(t, "GetStorageDetails", test.detailsCalls) + + for _, contains := range test.contains { + assert.Contains(t, output, contains) + } + for _, notContains := range test.notContains { + assert.NotContains(t, output, notContains) + } + + if test.checkJSON { + var parsed struct { + Storages []map[string]any `json:"storages"` + } + require.NoError(t, json.Unmarshal([]byte(output), &parsed)) + require.Len(t, parsed.Storages, 2) + for _, s := range parsed.Storages { + // servers uses the same embedded shape as the storage details API response + serversBlock, ok := s["servers"].(map[string]any) + require.True(t, ok, "storage %v should include servers", s["uuid"]) + servers, _ := serversBlock["server"].([]any) + if s["uuid"] == UUID1 { + assert.Equal(t, []any{serverUUID}, servers) + } else { + assert.Empty(t, servers) + } + } + } + }) + } +} From b009f246ad76f80003197f18267b725a9110b6fd Mon Sep 17 00:00:00 2001 From: aardbol <14614620+aardbol@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:44:36 +0100 Subject: [PATCH 2/2] docs: record storage attached servers feature --- CHANGELOG.md | 3 +++ FORK.md | 1 + 2 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6aaed6e3..611f5eca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [3.37.2-community] +### Added +- `storage list --show-attached-servers` shows the servers each storage is attached to, in the human-readable table and in `-o json`/`-o yaml` output (`servers` field, same shape as the storage details API response). Opt-in, as it requires a details request per storage ([upstream #803](https://github.com/UpCloudLtd/upcloud-cli/issues/803)). The SDK doesn't provide such functionality, so this has been implemented as a temporary workaround in the CLI. + ### Fixed - `server plans --prices`: the `-o json` and `-o yaml` output formats now include the computed price per plan, which previously appeared only in the default human-readable table ([upstream #805](https://github.com/UpCloudLtd/upcloud-cli/issues/805)). - `server show`: the `-o json` and `-o yaml` output formats now include firewall rules, which previously appeared only in the default human-readable view. diff --git a/FORK.md b/FORK.md index 8b93a4943..f55f55438 100644 --- a/FORK.md +++ b/FORK.md @@ -23,6 +23,7 @@ Changes are kept small and self-contained so that upstream can merge them at any | Command help no longer shows `Default:` for required flags (e.g. `scale --count` showed a misleading `Default: 0`). | 3.37.1-community | [#1](https://github.com/upcloud-tools/upcloud-cli/issues/1) | Not fixed as of upstream v3.37.0 | | `server plans --prices`: `-o json`/`-o yaml` now include the computed price (`price` field per plan, omitted when unknown). | 3.37.2-community | [#805](https://github.com/UpCloudLtd/upcloud-cli/issues/805) | Open upstream | | `server show`: `-o json`/`-o yaml` now include firewall rules (`firewall_rules` field); previously only the default human view showed them. | 3.37.2-community | [#805](https://github.com/UpCloudLtd/upcloud-cli/issues/805) | No upstream issue | +| `storage list --show-attached-servers` shows attached server UUIDs in the table and in `-o json`/`-o yaml` (`servers` field); opt-in, fetches storage details per disk. Implemented temporarily in the CLI due to missing in the SDK. | 3.37.2-community | [#803](https://github.com/UpCloudLtd/upcloud-cli/issues/803) | Open upstream | ### Notes for upstream merging