Skip to content

Commit

Permalink
feat(dbaas): session support (#248)
Browse files Browse the repository at this point in the history
Co-authored-by: Toni Kangas <[email protected]>
  • Loading branch information
villevsv-upcloud and kangasta authored Oct 16, 2023
1 parent 6d1d250 commit 34d798e
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 97 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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.
- Add `kubernetes modify` command for modifying IP addresses that are allowed to access cluster's Kubernetes API.
- Add `--kubernetes-api-allow-ip` argument to `kubernetes create` command.
- Add `Kubernetes API allowed IPs` field to `kubernetes show` output.
- Add `database session list` for listing active database sessions
- Add `database session cancel` for cancelling an active database session

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

## Removed
- **Breaking**: Remove `database connection list` and `database connection cancel` commands in favor of `database session` counterparts

## [2.10.0] - 2023-07-17
### Added
- Add `--disable-utility-network-access` for `kubernetes nodegroup create` command
Expand Down
12 changes: 6 additions & 6 deletions internal/commands/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/account"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database"
databaseconnection "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/connection"
databaseindex "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/index"
databaseproperties "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/properties"
databasesession "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/database/session"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/ipaddress"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/kubernetes"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands/kubernetes/nodegroup"
Expand Down Expand Up @@ -117,12 +117,12 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
commands.BuildCommand(database.StopCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(database.DeleteCommand(), databaseCommand.Cobra(), conf)

// Database connections
connectionsCommand := commands.BuildCommand(databaseconnection.BaseConnectionCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(databaseconnection.CancelCommand(), connectionsCommand.Cobra(), conf)
commands.BuildCommand(databaseconnection.ListCommand(), connectionsCommand.Cobra(), conf)
// Database sessions
sessionsCommand := commands.BuildCommand(databasesession.BaseSessionCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(databasesession.CancelCommand(), sessionsCommand.Cobra(), conf)
commands.BuildCommand(databasesession.ListCommand(), sessionsCommand.Cobra(), conf)

// Database connections
// Database sessions
propertiesCommand := commands.BuildCommand(databaseproperties.PropertiesCommand(), databaseCommand.Cobra(), conf)
for _, i := range []struct {
serviceName string
Expand Down
16 changes: 0 additions & 16 deletions internal/commands/database/connection/connection.go

This file was deleted.

59 changes: 0 additions & 59 deletions internal/commands/database/connection/list.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package databaseconnection
package databasesession

import (
"fmt"
Expand All @@ -9,6 +9,7 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v2/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/resolver"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
"github.com/spf13/pflag"
)
Expand All @@ -21,22 +22,22 @@ type cancelCommand struct {
terminate config.OptionalBoolean
}

// CancelCommand creates the "connection cancel" command
// CancelCommand creates the "session cancel" command
func CancelCommand() commands.Command {
return &cancelCommand{
BaseCommand: commands.New(
"cancel",
"Terminate client connection or cancel running query for a database",
`upctl database connection cancel 0fa980c4-0e4f-460b-9869-11b7bd62b833 --pid 2345421`,
`upctl database connection cancel 0fa980c4-0e4f-460b-9869-11b7bd62b833 --pid 2345421 --terminate`,
"Terminate client session or cancel running query for a database",
`upctl database session cancel 0fa980c4-0e4f-460b-9869-11b7bd62b832 --pid 2345422`,
`upctl database session cancel mysql-1 --pid 2345422 --terminate`,
),
}
}

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

s.AddFlags(flagSet)
Expand All @@ -46,11 +47,28 @@ func (s *cancelCommand) InitCommand() {
// Execute implements commands.MultipleArgumentCommand
func (s *cancelCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
db, err := svc.GetManagedDatabase(exec.Context(), &request.GetManagedDatabaseRequest{UUID: uuid})
if err != nil {
return nil, err
}

switch db.Type {
case upcloud.ManagedDatabaseServiceTypeMySQL:
break
case upcloud.ManagedDatabaseServiceTypePostgreSQL:
break
default:
return nil, fmt.Errorf("session cancel not supported for database type %s", db.Type)
}

if db.State != upcloud.ManagedDatabaseStateRunning {
return nil, fmt.Errorf("database is not in running state")
}

msg := fmt.Sprintf("Cancelling connection %v to database %v", s.pid, uuid)
msg := fmt.Sprintf("Cancelling session %v to database %v", s.pid, uuid)
exec.PushProgressStarted(msg)

if err := svc.CancelManagedDatabaseConnection(exec.Context(), &request.CancelManagedDatabaseConnection{ //nolint:staticcheck // Deprecated, replace in a feature PR
if err := svc.CancelManagedDatabaseSession(exec.Context(), &request.CancelManagedDatabaseSession{
UUID: uuid,
Pid: s.pid,
Terminate: s.terminate.Value(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package databaseconnection
package databasesession

import (
"testing"
Expand All @@ -8,18 +8,19 @@ import (
smock "github.com/UpCloudLtd/upcloud-cli/v2/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/mockexecute"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
"github.com/stretchr/testify/assert"
)

func TestCreateCommand(t *testing.T) {
targetMethod := "CancelManagedDatabaseConnection"
func TestCancelCommand(t *testing.T) {
targetMethod := "CancelManagedDatabaseSession"
uuid := "0fa980c4-0e4f-460b-9869-11b7bd62b833"
for _, test := range []struct {
name string
args []string
error string
expected request.CancelManagedDatabaseConnection
expected request.CancelManagedDatabaseSession
}{
{
name: "no process id",
Expand All @@ -29,7 +30,7 @@ func TestCreateCommand(t *testing.T) {
{
name: "soft cancel",
args: []string{"--pid", "123456"},
expected: request.CancelManagedDatabaseConnection{
expected: request.CancelManagedDatabaseSession{
UUID: uuid,
Pid: 123456,
Terminate: false,
Expand All @@ -38,7 +39,7 @@ func TestCreateCommand(t *testing.T) {
{
name: "terminate",
args: []string{"--pid", "987654", "--terminate"},
expected: request.CancelManagedDatabaseConnection{
expected: request.CancelManagedDatabaseSession{
UUID: uuid,
Pid: 987654,
Terminate: true,
Expand All @@ -50,6 +51,13 @@ func TestCreateCommand(t *testing.T) {
testCmd := CancelCommand()
mService := new(smock.Service)

mService.On("GetManagedDatabase", &request.GetManagedDatabaseRequest{UUID: uuid}).
Return(&upcloud.ManagedDatabase{
State: upcloud.ManagedDatabaseStateRunning,
Type: upcloud.ManagedDatabaseServiceTypeMySQL,
UUID: uuid,
}, nil)

mService.On(targetMethod, &test.expected).Return(nil)

c := commands.BuildCommand(testCmd, nil, config.New())
Expand Down
Loading

0 comments on commit 34d798e

Please sign in to comment.