Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 22 additions & 1 deletion internal/commands/server/plan_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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{
Expand All @@ -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),
Expand Down
74 changes: 74 additions & 0 deletions internal/commands/server/plan_list_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}
}
})
}
}
13 changes: 12 additions & 1 deletion internal/commands/server/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down Expand Up @@ -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
}
Expand Down
55 changes: 55 additions & 0 deletions internal/commands/server/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"encoding/json"
"testing"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
Expand All @@ -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) {
Expand Down Expand Up @@ -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"])
}
8 changes: 6 additions & 2 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading