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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `--plan-compute`, `--plan-node-count`, `--plan-storage-gib`, and `--plan-backups` flags to `database create` for selecting PostgreSQL and MySQL plans by their components. Run `upctl database plans <type>` to list valid values.

### Changed

- `database create`, `database list`, `database show`, and `database delete` use the v9 SDK. Their existing JSON and YAML output shape is preserved.
- `database show` displays the plan components of componentised plans.
- `database create` without a plan flag now creates PostgreSQL and MySQL databases with the `--plan-*` defaults instead of the `2x2xCPU-4GB-100GB` plan: `rdb.standard.2CPU-8GB` compute with 8 GB memory per node instead of 4 GB, 2 nodes, 100 GiB storage per node, and the `regular` backup tier instead of the legacy backup retention of 8 days for MySQL and 15 days for PostgreSQL.
- `database create`: `--plan` is deprecated for PostgreSQL and MySQL, and it cannot be combined with the `--plan-*` flags. `--plan` no longer has a default, and it is required for database types other than PostgreSQL and MySQL.
- `database create`: `--maintenance-dow` and `--maintenance-time` must be used together.

### Fixed

- Fix `database plans` returning no plans for OpenSearch and Valkey unless `--show-legacy` was set. Engines without componentised plans now show their normal plan catalog by default.
- Fix the termination protection flag in the `database create` example.
- Fix `database plans --show-legacy` showing legacy plan memory and storage in MB instead of GB.
- Fix `database create` sending string properties, such as `version`, as numbers when the value looks like a number.

## [3.36.0] - 2026-07-28

### Added
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require (
github.com/UpCloudLtd/progress v1.2.1
github.com/UpCloudLtd/upcloud-go-api/credentials v0.1.1
github.com/UpCloudLtd/upcloud-go-api/v8 v8.40.0
github.com/UpCloudLtd/upcloud-go-api/v9 v9.0.0-20260917090650-3e1a69f90c36
github.com/UpCloudLtd/upcloud-go-api/v9 v9.0.0-20260930115011-40a5b398d8a0
github.com/adrg/xdg v0.5.3
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/google/uuid v1.6.0
github.com/jedib0t/go-pretty/v6 v6.7.9
github.com/joho/godotenv v1.5.1
github.com/m7shapan/cidr v0.0.0-20200427124835-7eba0889a5d2
Expand Down Expand Up @@ -74,7 +75,6 @@ require (
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/UpCloudLtd/upcloud-go-api/credentials v0.1.1 h1:eTfQsv58ufALOk9BZ7WbS
github.com/UpCloudLtd/upcloud-go-api/credentials v0.1.1/go.mod h1:7OtVs2UqtfvjkC1HfE+Oud0MnbMv7qUWnbEgxnTAqts=
github.com/UpCloudLtd/upcloud-go-api/v8 v8.40.0 h1:MvG4zuzKb0WXruxDebF1OgFXZ+zxPH2MBa0jHDQ9dBo=
github.com/UpCloudLtd/upcloud-go-api/v8 v8.40.0/go.mod h1:ex/UPAIOJxtzwe0DqX2gwCC2/oce7pBqg78pB+YnH9k=
github.com/UpCloudLtd/upcloud-go-api/v9 v9.0.0-20260917090650-3e1a69f90c36 h1:VCwOEnq3lUKWeAg9BFejef48BlFUwcjO6UmOINgkxtg=
github.com/UpCloudLtd/upcloud-go-api/v9 v9.0.0-20260917090650-3e1a69f90c36/go.mod h1:mHX52YNu+cWHzK7fNoT0g5TOzbVxgnfWZtJdWuhKHgs=
github.com/UpCloudLtd/upcloud-go-api/v9 v9.0.0-20260930115011-40a5b398d8a0 h1:Mos1nlJfDnBs0+pJnrP/X6Pkjs+Gh9oY1scK88maqmw=
github.com/UpCloudLtd/upcloud-go-api/v9 v9.0.0-20260930115011-40a5b398d8a0/go.mod h1:mHX52YNu+cWHzK7fNoT0g5TOzbVxgnfWZtJdWuhKHgs=
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
Expand Down
83 changes: 83 additions & 0 deletions internal/apierror/apierror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Package apierror provides the CLI's own error type for UpCloud API errors, independent of the SDK version.
package apierror

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)

// Problem is an UpCloud API error. Its Error and ErrorCode output match the v8 SDK's upcloud.Problem.
type Problem struct {
Type string `json:"type"`
Title string `json:"title"`
InvalidParams []InvalidParam `json:"invalid_params,omitempty"`
CorrelationID string `json:"correlation_id,omitempty"`
Status int `json:"status"`
}

type InvalidParam struct {
Name string `json:"name"`
Reason string `json:"reason"`
}

func (p *Problem) Error() string {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, "%s (type=%s, status=%d", p.Title, p.Type, p.Status)
if p.CorrelationID != "" {
_, _ = fmt.Fprintf(&sb, ", correlation_id=%s", p.CorrelationID)
}
for _, ip := range p.InvalidParams {
_, _ = fmt.Fprintf(&sb, ", invalid_params_%s='%s'", ip.Name, ip.Reason)
}
sb.WriteString(")")
return sb.String()
}

// ErrorCode returns the error code, e.g. SERVICE_NOT_FOUND, also when Type is a documentation URL.
func (p *Problem) ErrorCode() string {
parsedURL, err := url.Parse(p.Type)
if err != nil || parsedURL.Scheme == "" || parsedURL.Host == "" {
return p.Type
}
return strings.Replace(parsedURL.Fragment, "ERROR_", "", 1)
}

// FromResponse converts an unsuccessful API response into a *Problem. It accepts problem+json and legacy error bodies.
func FromResponse(status int, body []byte) error {
if status >= 200 && status < 300 {
return fmt.Errorf("unexpected API response (status %d)", status)
}

prob := &Problem{}
if json.Unmarshal(body, prob) == nil && prob.Title != "" {
if prob.Status == 0 {
prob.Status = status
}
return prob
}

legacy := struct {
Error struct {
ErrorCode string `json:"error_code"`
ErrorMessage string `json:"error_message"`
} `json:"error"`
}{}
if json.Unmarshal(body, &legacy) == nil && legacy.Error.ErrorCode != "" {
return &Problem{Type: legacy.Error.ErrorCode, Title: legacy.Error.ErrorMessage, Status: status}
}

title := strings.TrimSpace(string(body))
if title == "" {
title = http.StatusText(status)
}
return &Problem{Title: title, Status: status}
}

func IsNotFound(err error) bool {
var prob *Problem
return errors.As(err, &prob) && prob.Status == http.StatusNotFound
}
75 changes: 75 additions & 0 deletions internal/apierror/apierror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package apierror

import (
"net/http"
"testing"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFromResponse(t *testing.T) {
for _, test := range []struct {
name string
status int
body string
expected Problem
}{
{
name: "problem details",
status: http.StatusBadRequest,
body: `{"type": "https://developers.upcloud.com/1.3/errors#ERROR_INVALID_REQUEST", "title": "Invalid request", "status": 400, "correlation_id": "abc", "invalid_params": [{"name": "plan", "reason": "unknown"}]}`,
expected: Problem{Type: "https://developers.upcloud.com/1.3/errors#ERROR_INVALID_REQUEST", Title: "Invalid request", Status: 400, CorrelationID: "abc", InvalidParams: []InvalidParam{{Name: "plan", Reason: "unknown"}}},
},
{
name: "problem details without status",
status: http.StatusConflict,
body: `{"type": "conflict", "title": "Conflict"}`,
expected: Problem{Type: "conflict", Title: "Conflict", Status: http.StatusConflict},
},
{
name: "legacy error",
status: http.StatusNotFound,
body: `{"error": {"error_code": "SERVICE_NOT_FOUND", "error_message": "Service not found"}}`,
expected: Problem{Type: "SERVICE_NOT_FOUND", Title: "Service not found", Status: http.StatusNotFound},
},
{
name: "plain text",
status: http.StatusBadGateway,
body: "bad gateway\n",
expected: Problem{Title: "bad gateway", Status: http.StatusBadGateway},
},
{
name: "empty body",
status: http.StatusServiceUnavailable,
expected: Problem{Title: "Service Unavailable", Status: http.StatusServiceUnavailable},
},
} {
t.Run(test.name, func(t *testing.T) {
err := FromResponse(test.status, []byte(test.body))
var problem *Problem
require.ErrorAs(t, err, &problem)
assert.Equal(t, test.expected, *problem)
assert.Equal(t, test.status == http.StatusNotFound, IsNotFound(err))
})
}

assert.EqualError(t, FromResponse(http.StatusOK, nil), "unexpected API response (status 200)")
}

// Users must see the same messages and error codes as with the v8 SDK error type.
func TestProblem_MatchesV8(t *testing.T) {
for _, p := range []Problem{
{Type: "https://developers.upcloud.com/1.3/errors#ERROR_AUTHENTICATION_FAILED", Title: "Authentication failed", Status: 401},
{Type: "SERVICE_NOT_FOUND", Title: "Service not found", Status: 404, CorrelationID: "abc"},
{Type: "invalid", Title: "Invalid", Status: 400, InvalidParams: []InvalidParam{{Name: "plan", Reason: "unknown"}, {Name: "zone", Reason: "missing"}}},
} {
v8 := upcloud.Problem{Type: p.Type, Title: p.Title, Status: p.Status, CorrelationID: p.CorrelationID}
for _, ip := range p.InvalidParams {
v8.InvalidParams = append(v8.InvalidParams, upcloud.ProblemInvalidParam{Name: ip.Name, Reason: ip.Reason})
}
assert.Equal(t, v8.Error(), p.Error())
assert.Equal(t, v8.ErrorCode(), p.ErrorCode())
}
}
15 changes: 7 additions & 8 deletions internal/clierrors/invalid_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clierrors
import (
"errors"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/apierror"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
)

Expand All @@ -19,14 +20,12 @@ func (err InvalidCredentialsError) Error() string {
}

func CheckAuthenticationFailed(err error) bool {
prob := &upcloud.Problem{}

if errors.As(err, &prob) {
errCode := prob.ErrorCode()
if errCode == upcloud.ErrCodeAuthenticationFailed || errCode == "INVALID_CREDENTIALS" {
return true
}
var errCode string
if prob := (*apierror.Problem)(nil); errors.As(err, &prob) {
errCode = prob.ErrorCode()
} else if prob := (*upcloud.Problem)(nil); errors.As(err, &prob) {
errCode = prob.ErrorCode()
}

return false
return errCode == upcloud.ErrCodeAuthenticationFailed || errCode == "INVALID_CREDENTIALS"
}
30 changes: 30 additions & 0 deletions internal/clierrors/invalid_credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package clierrors

import (
"fmt"
"testing"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/apierror"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/stretchr/testify/assert"
)

func TestCheckAuthenticationFailed(t *testing.T) {
for _, test := range []struct {
name string
err error
expected bool
}{
{name: "v8 authentication failed", err: &upcloud.Problem{Type: "https://developers.upcloud.com/1.3/errors#ERROR_AUTHENTICATION_FAILED"}, expected: true},
{name: "v8 invalid credentials", err: &upcloud.Problem{Type: "INVALID_CREDENTIALS"}, expected: true},
{name: "v9 authentication failed", err: &apierror.Problem{Type: "https://developers.upcloud.com/1.3/errors#ERROR_AUTHENTICATION_FAILED"}, expected: true},
{name: "wrapped v9 invalid credentials", err: fmt.Errorf("resolving: %w", &apierror.Problem{Type: "INVALID_CREDENTIALS"}), expected: true},
{name: "v8 other error", err: &upcloud.Problem{Type: "SERVICE_NOT_FOUND"}},
{name: "v9 other error", err: &apierror.Problem{Type: "SERVICE_NOT_FOUND"}},
{name: "plain error", err: fmt.Errorf("authentication failed")},
} {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, CheckAuthenticationFailed(test.err))
})
}
}
Loading
Loading