From 62fd92543a0669a9a1c00526359193d303b24d10 Mon Sep 17 00:00:00 2001 From: aardbol <14614620+aardbol@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:28:17 +0100 Subject: [PATCH 1/3] fix(server): include prices in server plans machine output --- internal/commands/server/plan_list.go | 23 ++++++- internal/commands/server/plan_list_test.go | 74 ++++++++++++++++++++++ internal/mock/mock.go | 8 ++- 3 files changed, 102 insertions(+), 3 deletions(-) diff --git a/internal/commands/server/plan_list.go b/internal/commands/server/plan_list.go index e415501e3..9d012166c 100644 --- a/internal/commands/server/plan_list.go +++ b/internal/commands/server/plan_list.go @@ -33,6 +33,14 @@ type planListCommand struct { pricesDuration string } +// planWithPrice embeds a plan with its price for the requested zone and duration, +// so machine-readable output includes the same pricing as the human output. +type planWithPrice struct { + upcloud.Plan + + Price *float64 `json:"price,omitempty"` +} + // InitCommand initializes the command flags func (s *planListCommand) InitCommand() { flagSet := &pflag.FlagSet{} @@ -104,6 +112,7 @@ func (s *planListCommand) ExecuteWithoutArguments(exec commands.Executor) (outpu } rows := make(map[string][]output.TableRow) + plansWithPrices := make([]planWithPrice, 0, len(plans)) for _, p := range plans { key := planType(p) row := output.TableRow{ @@ -124,13 +133,25 @@ func (s *planListCommand) ExecuteWithoutArguments(exec commands.Executor) (outpu if showPrices && prices != nil { cost := getPlanCost(p, prices, duration) row = append(row, cost) + + plan := planWithPrice{Plan: p} + if !math.IsNaN(cost) { + plan.Price = &cost + } + plansWithPrices = append(plansWithPrices, plan) } rows[key] = append(rows[key], row) } + // Embed prices in the machine-readable output, not just the human output + var value any = plans + if showPrices && prices != nil { + value = plansWithPrices + } + return output.MarshaledWithHumanOutput{ - Value: plans, + Value: value, Output: output.Combined{ planSection("premium", "Premium", rows["premium"], showPrices, s.pricesDuration), planSection("starter", "Starter", rows["starter"], showPrices, s.pricesDuration), diff --git a/internal/commands/server/plan_list_test.go b/internal/commands/server/plan_list_test.go index bf9bc7a77..96e9de89c 100644 --- a/internal/commands/server/plan_list_test.go +++ b/internal/commands/server/plan_list_test.go @@ -1,10 +1,16 @@ package server import ( + "encoding/json" "testing" + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" + "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" + smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock" + "github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute" "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestDurationHeader(t *testing.T) { @@ -88,3 +94,71 @@ func TestGetPlanCost(t *testing.T) { }) } } + +func TestPlanListMachineOutput(t *testing.T) { + plans := &upcloud.Plans{ + Plans: []upcloud.Plan{ + {Name: "1xCPU-1GB", CoreNumber: 1, MemoryAmount: 1024, StorageSize: 25, StorageTier: "maxiops", PublicTrafficOut: 1024}, + {Name: "PREMIUM-2xCPU-2GB", CoreNumber: 2, MemoryAmount: 2048, StorageSize: 50, StorageTier: "maxiops", PublicTrafficOut: 2048}, + {Name: "DEV-1xCPU-1GB", CoreNumber: 1, MemoryAmount: 1024, StorageSize: 25, StorageTier: "maxiops", PublicTrafficOut: 1024}, + }, + } + prices := upcloud.PricesByZone{ + "de-fra1": map[string]upcloud.Price{ + "server_plan_1xCPU-1GB": {Amount: 1, Price: 100}, + "server_plan_PREMIUM-2xCPU-2GB": {Amount: 1, Price: 200}, + }, + } + + for _, test := range []struct { + name string + args []string + expectedPrices map[string]float64 + }{ + { + name: "no prices requested", + args: []string{}, + expectedPrices: map[string]float64{}, + }, + { + name: "hourly prices", + args: []string{"--prices", "de-fra1", "--prices-duration", "hour"}, + expectedPrices: map[string]float64{"1xCPU-1GB": 1, "PREMIUM-2xCPU-2GB": 2}, + }, + { + name: "monthly prices", + args: []string{"--prices", "de-fra1"}, + expectedPrices: map[string]float64{"1xCPU-1GB": 672, "PREMIUM-2xCPU-2GB": 1344}, + }, + } { + t.Run(test.name, func(t *testing.T) { + mService := new(smock.Service) + mService.On("GetPlans").Return(plans, nil) + mService.On("GetPricesByZone").Return(&prices, nil) + + conf := config.New() + conf.Viper().Set(config.KeyOutput, config.ValueOutputJSON) + + command := commands.BuildCommand(PlanListCommand(), nil, conf) + command.Cobra().SetArgs(test.args) + out, err := mockexecute.MockExecute(command, mService, conf) + assert.NoError(t, err) + + var parsed []map[string]any + require.NoError(t, json.Unmarshal([]byte(out), &parsed)) + require.Len(t, parsed, len(plans.Plans)) + for _, p := range parsed { + name, _ := p["name"].(string) + assert.Contains(t, p, "core_number", "plan %s should keep API fields", name) + expected, wantPrice := test.expectedPrices[name] + price, hasPrice := p["price"] + if wantPrice { + assert.True(t, hasPrice, "plan %s should include price", name) + assert.Equal(t, expected, price) + } else { + assert.False(t, hasPrice, "plan %s should not include price", name) + } + } + }) + } +} diff --git a/internal/mock/mock.go b/internal/mock/mock.go index e220f4234..05b7ef3fe 100644 --- a/internal/mock/mock.go +++ b/internal/mock/mock.go @@ -92,9 +92,13 @@ func (m *Service) GetPriceZones(context.Context) (*upcloud.PriceZones, error) { return nil, nil } -// GetPricingByZone implements service.Zones.GetPricingByZone +// GetPricesByZone implements service.Zones.GetPricesByZone func (m *Service) GetPricesByZone(context.Context) (*upcloud.PricesByZone, error) { - return nil, nil + args := m.Called() + if args[0] == nil { + return nil, args.Error(1) + } + return args[0].(*upcloud.PricesByZone), args.Error(1) } // GetTimeZones implements service.Zones.GetPriceZones From 3401e909217d138ae138ce33b7e2fcc30233e967 Mon Sep 17 00:00:00 2001 From: aardbol <14614620+aardbol@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:28:32 +0100 Subject: [PATCH 2/3] fix(server): include firewall rules in server show machine output --- internal/commands/server/show.go | 13 ++++++- internal/commands/server/show_test.go | 55 +++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/internal/commands/server/show.go b/internal/commands/server/show.go index f5a5a832c..a0d9de0d0 100644 --- a/internal/commands/server/show.go +++ b/internal/commands/server/show.go @@ -38,6 +38,14 @@ type showCommand struct { completion.Server } +// serverWithFirewallRules embeds server details with the separately fetched firewall +// rules, so machine-readable output includes the same data as the human output. +type serverWithFirewallRules struct { + *upcloud.ServerDetails + + FirewallRules []upcloud.FirewallRule `json:"firewall_rules"` +} + func (s *showCommand) InitCommand() { } @@ -269,7 +277,10 @@ func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Outpu } return output.MarshaledWithHumanOutput{ - Value: server, + Value: serverWithFirewallRules{ + ServerDetails: server, + FirewallRules: firewallRules.FirewallRules, + }, Output: combined, }, nil } diff --git a/internal/commands/server/show_test.go b/internal/commands/server/show_test.go index cd2a1a0d7..e86de46aa 100644 --- a/internal/commands/server/show_test.go +++ b/internal/commands/server/show_test.go @@ -2,6 +2,7 @@ package server import ( "context" + "encoding/json" "testing" "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" @@ -13,6 +14,7 @@ import ( "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/require" ) func TestServerHumanOutput(t *testing.T) { @@ -222,3 +224,56 @@ func TestServerHumanOutput(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expected, output) } + +func TestServerJSONOutput(t *testing.T) { + uuid := "0077fa3d-32db-4b09-9f5f-30d9e9afb565" + srv := &upcloud.ServerDetails{ + Server: upcloud.Server{ + Hostname: "server1.example.com", + Plan: "1xCPU-2GB", + State: "started", + UUID: uuid, + Zone: "fi-hel1", + }, + } + firewallRules := &upcloud.FirewallRules{ + FirewallRules: []upcloud.FirewallRule{ + { + Direction: upcloud.FirewallRuleDirectionIn, + Action: upcloud.FirewallRuleActionAccept, + Family: upcloud.IPAddressFamilyIPv4, + Protocol: upcloud.FirewallRuleProtocolTCP, + Position: 1, + }, + }, + } + + mService := new(smock.Service) + mService.On("GetServers").Return(&upcloud.Servers{Servers: []upcloud.Server{srv.Server}}, nil) + mService.On("GetServerDetails", &request.GetServerDetailsRequest{UUID: uuid}).Return(srv, nil) + mService.On("GetFirewallRules", &request.GetFirewallRulesRequest{ServerUUID: uuid}).Return(firewallRules, nil) + + conf := config.New() + conf.Viper().Set(config.KeyOutput, config.ValueOutputJSON) + + command := commands.BuildCommand(ShowCommand(), nil, conf) + + // get resolver to initialize command cache + _, err := command.(*showCommand).Get(context.TODO(), mService) + require.NoError(t, err) + + command.Cobra().SetArgs([]string{uuid}) + out, err := mockexecute.MockExecute(command, mService, conf) + assert.NoError(t, err) + + var parsed map[string]any + require.NoError(t, json.Unmarshal([]byte(out), &parsed)) + assert.Equal(t, uuid, parsed["uuid"]) + rules, ok := parsed["firewall_rules"].([]any) + require.True(t, ok, "JSON output should include firewall_rules") + require.Len(t, rules, 1) + rule, ok := rules[0].(map[string]any) + require.True(t, ok) + assert.Equal(t, "accept", rule["action"]) + assert.Equal(t, "in", rule["direction"]) +} From e09811b55ee031661b9d732bd1665c3247a5cc8e Mon Sep 17 00:00:00 2001 From: aardbol <14614620+aardbol@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:28:40 +0100 Subject: [PATCH 3/3] docs: record server JSON output fixes --- CHANGELOG.md | 9 ++++++++- FORK.md | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecf0c4147..d6aaed6e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.37.2-community] + +### 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. + ## [3.37.1-community] - 2026-11-15 ### Changed @@ -765,7 +771,8 @@ Initial public beta release :tada: ### Added - Current feature set added! First internal release -[Unreleased]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.1-community...HEAD +[Unreleased]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.2-community...HEAD +[3.37.2-community]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.1-community...v3.37.2-community [3.37.1-community]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.0...v3.37.1-community [3.37.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.36.0...v3.37.0 [3.36.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.35.0...v3.36.0 diff --git a/FORK.md b/FORK.md index 2cd84abe1..8b93a4943 100644 --- a/FORK.md +++ b/FORK.md @@ -21,11 +21,14 @@ Changes are kept small and self-contained so that upstream can merge them at any | ------ | ------------- | ------------- | --------------- | | `kubernetes node-group scale --count 0` sends `{"count": 0}` instead of silently doing nothing. The command now uses the v9 SDK; JSON/YAML output shape is preserved. | 3.37.1-community | [#1](https://github.com/upcloud-tools/upcloud-cli/issues/1) | Not fixed as of upstream v3.37.0 | | 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 | ### Notes for upstream merging - The scale fix works around a bug in the `upcloud-go-api` v8 SDK: `request.ModifyKubernetesNodeGroup.Count` is tagged `json:"count,omitempty"`, so a zero count is dropped from the PATCH body and the API no-ops while the CLI reports success. The v9 SDK (`KubernetesNodeGroupPatch`) models `count` without `omitempty` and is not affected. Fixing the v8 SDK would additionally benefit all other v8 consumers. - The help renderer change is in `internal/ui/usage.go` (`formatFlags`) and applies to every command with required flags. +- The JSON output fixes follow the existing `server list --show-ip-addresses` pattern (`serverWithIPAddress`): wrapper structs embed the SDK response, so existing JSON/YAML fields are preserved and the enriched fields are additive. An audit of all commands found these two were the only ones rendering data in human output that was missing from machine output. ## CI and releases