Skip to content

Commit 34d798e

Browse files
feat(dbaas): session support (#248)
Co-authored-by: Toni Kangas <kangasta@users.noreply.github.com>
1 parent 6d1d250 commit 34d798e

10 files changed

Lines changed: 294 additions & 97 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add `kubernetes nodegroup show` for displaying node-group details. This also adds _Nodes_ table and _Anti-affinity_ field that were not available in previous `kubernetes show` output.
1313
- Add `kubernetes modify` command for modifying IP addresses that are allowed to access cluster's Kubernetes API.
1414
- Add `--kubernetes-api-allow-ip` argument to `kubernetes create` command.
15+
- Add `Kubernetes API allowed IPs` field to `kubernetes show` output.
16+
- Add `database session list` for listing active database sessions
17+
- Add `database session cancel` for cancelling an active database session
1518

1619
### Changed
1720
- In human readable output of `kubernetes show` command, show node-groups as table. Node-group datails are available with `kubernetes nodegroup show` command.
1821

22+
## Removed
23+
- **Breaking**: Remove `database connection list` and `database connection cancel` commands in favor of `database session` counterparts
24+
1925
## [2.10.0] - 2023-07-17
2026
### Added
2127
- Add `--disable-utility-network-access` for `kubernetes nodegroup create` command

internal/commands/all/all.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
55
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/account"
66
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database"
7-
databaseconnection "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/connection"
87
databaseindex "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/index"
98
databaseproperties "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/properties"
9+
databasesession "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/session"
1010
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/ipaddress"
1111
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/kubernetes"
1212
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/kubernetes/nodegroup"
@@ -117,12 +117,12 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
117117
commands.BuildCommand(database.StopCommand(), databaseCommand.Cobra(), conf)
118118
commands.BuildCommand(database.DeleteCommand(), databaseCommand.Cobra(), conf)
119119

120-
// Database connections
121-
connectionsCommand := commands.BuildCommand(databaseconnection.BaseConnectionCommand(), databaseCommand.Cobra(), conf)
122-
commands.BuildCommand(databaseconnection.CancelCommand(), connectionsCommand.Cobra(), conf)
123-
commands.BuildCommand(databaseconnection.ListCommand(), connectionsCommand.Cobra(), conf)
120+
// Database sessions
121+
sessionsCommand := commands.BuildCommand(databasesession.BaseSessionCommand(), databaseCommand.Cobra(), conf)
122+
commands.BuildCommand(databasesession.CancelCommand(), sessionsCommand.Cobra(), conf)
123+
commands.BuildCommand(databasesession.ListCommand(), sessionsCommand.Cobra(), conf)
124124

125-
// Database connections
125+
// Database sessions
126126
propertiesCommand := commands.BuildCommand(databaseproperties.PropertiesCommand(), databaseCommand.Cobra(), conf)
127127
for _, i := range []struct {
128128
serviceName string

internal/commands/database/connection/connection.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

internal/commands/database/connection/list.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

internal/commands/database/connection/cancel.go renamed to internal/commands/database/session/cancel.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package databaseconnection
1+
package databasesession
22

33
import (
44
"fmt"
@@ -9,6 +9,7 @@ import (
99
"github.com/UpCloudLtd/upcloud-cli/v2/internal/output"
1010
"github.com/UpCloudLtd/upcloud-cli/v2/internal/resolver"
1111

12+
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
1213
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
1314
"github.com/spf13/pflag"
1415
)
@@ -21,22 +22,22 @@ type cancelCommand struct {
2122
terminate config.OptionalBoolean
2223
}
2324

24-
// CancelCommand creates the "connection cancel" command
25+
// CancelCommand creates the "session cancel" command
2526
func CancelCommand() commands.Command {
2627
return &cancelCommand{
2728
BaseCommand: commands.New(
2829
"cancel",
29-
"Terminate client connection or cancel running query for a database",
30-
`upctl database connection cancel 0fa980c4-0e4f-460b-9869-11b7bd62b833 --pid 2345421`,
31-
`upctl database connection cancel 0fa980c4-0e4f-460b-9869-11b7bd62b833 --pid 2345421 --terminate`,
30+
"Terminate client session or cancel running query for a database",
31+
`upctl database session cancel 0fa980c4-0e4f-460b-9869-11b7bd62b832 --pid 2345422`,
32+
`upctl database session cancel mysql-1 --pid 2345422 --terminate`,
3233
),
3334
}
3435
}
3536

3637
// InitCommand implements Command.InitCommand
3738
func (s *cancelCommand) InitCommand() {
3839
flagSet := &pflag.FlagSet{}
39-
flagSet.IntVar(&s.pid, "pid", 0, "Process ID of the connection to cancel.")
40+
flagSet.IntVar(&s.pid, "pid", 0, "Process ID of the session to cancel.")
4041
config.AddToggleFlag(flagSet, &s.terminate, "terminate", false, "Request immediate termination instead of soft cancel.")
4142

4243
s.AddFlags(flagSet)
@@ -46,11 +47,28 @@ func (s *cancelCommand) InitCommand() {
4647
// Execute implements commands.MultipleArgumentCommand
4748
func (s *cancelCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
4849
svc := exec.All()
50+
db, err := svc.GetManagedDatabase(exec.Context(), &request.GetManagedDatabaseRequest{UUID: uuid})
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
switch db.Type {
56+
case upcloud.ManagedDatabaseServiceTypeMySQL:
57+
break
58+
case upcloud.ManagedDatabaseServiceTypePostgreSQL:
59+
break
60+
default:
61+
return nil, fmt.Errorf("session cancel not supported for database type %s", db.Type)
62+
}
63+
64+
if db.State != upcloud.ManagedDatabaseStateRunning {
65+
return nil, fmt.Errorf("database is not in running state")
66+
}
4967

50-
msg := fmt.Sprintf("Cancelling connection %v to database %v", s.pid, uuid)
68+
msg := fmt.Sprintf("Cancelling session %v to database %v", s.pid, uuid)
5169
exec.PushProgressStarted(msg)
5270

53-
if err := svc.CancelManagedDatabaseConnection(exec.Context(), &request.CancelManagedDatabaseConnection{ //nolint:staticcheck // Deprecated, replace in a feature PR
71+
if err := svc.CancelManagedDatabaseSession(exec.Context(), &request.CancelManagedDatabaseSession{
5472
UUID: uuid,
5573
Pid: s.pid,
5674
Terminate: s.terminate.Value(),

internal/commands/database/connection/cancel_test.go renamed to internal/commands/database/session/cancel_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package databaseconnection
1+
package databasesession
22

33
import (
44
"testing"
@@ -8,18 +8,19 @@ import (
88
smock "github.com/UpCloudLtd/upcloud-cli/v2/internal/mock"
99
"github.com/UpCloudLtd/upcloud-cli/v2/internal/mockexecute"
1010

11+
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
1112
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
1213
"github.com/stretchr/testify/assert"
1314
)
1415

15-
func TestCreateCommand(t *testing.T) {
16-
targetMethod := "CancelManagedDatabaseConnection"
16+
func TestCancelCommand(t *testing.T) {
17+
targetMethod := "CancelManagedDatabaseSession"
1718
uuid := "0fa980c4-0e4f-460b-9869-11b7bd62b833"
1819
for _, test := range []struct {
1920
name string
2021
args []string
2122
error string
22-
expected request.CancelManagedDatabaseConnection
23+
expected request.CancelManagedDatabaseSession
2324
}{
2425
{
2526
name: "no process id",
@@ -29,7 +30,7 @@ func TestCreateCommand(t *testing.T) {
2930
{
3031
name: "soft cancel",
3132
args: []string{"--pid", "123456"},
32-
expected: request.CancelManagedDatabaseConnection{
33+
expected: request.CancelManagedDatabaseSession{
3334
UUID: uuid,
3435
Pid: 123456,
3536
Terminate: false,
@@ -38,7 +39,7 @@ func TestCreateCommand(t *testing.T) {
3839
{
3940
name: "terminate",
4041
args: []string{"--pid", "987654", "--terminate"},
41-
expected: request.CancelManagedDatabaseConnection{
42+
expected: request.CancelManagedDatabaseSession{
4243
UUID: uuid,
4344
Pid: 987654,
4445
Terminate: true,
@@ -50,6 +51,13 @@ func TestCreateCommand(t *testing.T) {
5051
testCmd := CancelCommand()
5152
mService := new(smock.Service)
5253

54+
mService.On("GetManagedDatabase", &request.GetManagedDatabaseRequest{UUID: uuid}).
55+
Return(&upcloud.ManagedDatabase{
56+
State: upcloud.ManagedDatabaseStateRunning,
57+
Type: upcloud.ManagedDatabaseServiceTypeMySQL,
58+
UUID: uuid,
59+
}, nil)
60+
5361
mService.On(targetMethod, &test.expected).Return(nil)
5462

5563
c := commands.BuildCommand(testCmd, nil, config.New())

0 commit comments

Comments
 (0)