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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Add `--show-ip-addresses` flag to `server list` command to optionally include IP addresses in command output.
- Add `database connection list`, and `database connection cancel` commands.
- Add `database connection list`, `database connection cancel`, `database start`, and `database stop` commands.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I would keep each command as a separate item on the list, for me that easier to quickly parse with eyes. Not a blocker or anything ofc.


### Changed
- Make `--family` parameter of `server firewall create` command optional to allow editing the default rules.
Expand Down
2 changes: 2 additions & 0 deletions internal/commands/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
commands.BuildCommand(database.ShowCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(database.TypesCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(database.PlansCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(database.StartCommand(), databaseCommand.Cobra(), conf)
commands.BuildCommand(database.StopCommand(), databaseCommand.Cobra(), conf)

// Database connections
connectionsCommand := commands.BuildCommand(databaseconnection.BaseConnectionCommand(), databaseCommand.Cobra(), conf)
Expand Down
53 changes: 53 additions & 0 deletions internal/commands/database/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package database

import (
"fmt"

"github.com/UpCloudLtd/upcloud-cli/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/internal/output"
"github.com/UpCloudLtd/upcloud-cli/internal/resolver"

"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
)

// StartCommand creates the "database start" command
func StartCommand() commands.Command {
return &startCommand{
BaseCommand: commands.New(
"start",
"Start on a managed database",
"upctl database start b0952286-1193-4a81-a1af-62efc014ae4b",
"upctl database start b0952286-1193-4a81-a1af-62efc014ae4b 666bcd3c-5c63-428d-a4fd-07c27469a5a6",
"upctl database start pg-1x1xcpu-2gb-25gb-pl-waw1",
),
}
}

type startCommand struct {
*commands.BaseCommand
completion.Database
resolver.CachingDatabase
}

// Execute implements commands.MultipleArgumentCommand
func (s *startCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
msg := fmt.Sprintf("Starting database %v", uuid)
logline := exec.NewLogEntry(msg)

logline.StartedNow()
logline.SetMessage(fmt.Sprintf("%s: sending request", msg))

res, err := svc.StartManagedDatabase(&request.StartManagedDatabaseRequest{
UUID: uuid,
})
if err != nil {
return commands.HandleError(logline, fmt.Sprintf("%s: failed", msg), err)
}

logline.SetMessage(fmt.Sprintf("%s: done", msg))
logline.MarkDone()

return output.OnlyMarshaled{Value: res}, nil
}
44 changes: 44 additions & 0 deletions internal/commands/database/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package database

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"

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

func TestStartCommand(t *testing.T) {
targetMethod := "StartManagedDatabase"

var db = upcloud.ManagedDatabase{
State: upcloud.ManagedDatabaseStateRunning,
Title: "database-title",
UUID: "1fdfda29-ead1-4855-b71f-1e33eb2ca9de",
}

req := request.StartManagedDatabaseRequest{
UUID: db.UUID,
}

conf := config.New()
testCmd := StartCommand()
mService := new(smock.Service)

conf.Service = internal.Wrapper{Service: mService}
mService.On(targetMethod, &req).Return(&db, nil)

command := commands.BuildCommand(testCmd, nil, conf)

command.Cobra().SetArgs([]string{db.UUID})
_, err := mockexecute.MockExecute(command, mService, conf)

assert.NoError(t, err)
mService.AssertNumberOfCalls(t, targetMethod, 1)
}
58 changes: 58 additions & 0 deletions internal/commands/database/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package database

import (
"fmt"

"github.com/UpCloudLtd/upcloud-cli/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/internal/output"
"github.com/UpCloudLtd/upcloud-cli/internal/resolver"

"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
)

// StopCommand creates the "database stop" command
func StopCommand() commands.Command {
return &stopCommand{
BaseCommand: commands.New(
"stop",
"Stop a managed database",
"upctl database stop b0952286-1193-4a81-a1af-62efc014ae4b",
"upctl database stop b0952286-1193-4a81-a1af-62efc014ae4b 666bcd3c-5c63-428d-a4fd-07c27469a5a6",
"upctl database stop pg-1x1xcpu-2gb-25gb-pl-waw1",
),
}
}

type stopCommand struct {
*commands.BaseCommand
completion.Database
resolver.CachingDatabase
}

// InitCommand implements Command.InitCommand
func (s *stopCommand) InitCommand() {
s.Cobra().Aliases = []string{"shutdown"}
}

// Execute implements commands.MultipleArgumentCommand
func (s *stopCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
msg := fmt.Sprintf("Stopping database %v", uuid)
logline := exec.NewLogEntry(msg)

logline.StartedNow()
logline.SetMessage(fmt.Sprintf("%s: sending request", msg))

res, err := svc.ShutdownManagedDatabase(&request.ShutdownManagedDatabaseRequest{
UUID: uuid,
})
if err != nil {
return commands.HandleError(logline, fmt.Sprintf("%s: failed", msg), err)
}

logline.SetMessage(fmt.Sprintf("%s: done", msg))
logline.MarkDone()

return output.OnlyMarshaled{Value: res}, nil
}
44 changes: 44 additions & 0 deletions internal/commands/database/stop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package database

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"

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

func TestStopCommand(t *testing.T) {
targetMethod := "ShutdownManagedDatabase"

var db = upcloud.ManagedDatabase{
State: upcloud.ManagedDatabaseStateRunning,
Title: "database-title",
UUID: "1fdfda29-ead1-4855-b71f-1e33eb2ca9de",
}

req := request.ShutdownManagedDatabaseRequest{
UUID: db.UUID,
}

conf := config.New()
testCmd := StopCommand()
mService := new(smock.Service)

conf.Service = internal.Wrapper{Service: mService}
mService.On(targetMethod, &req).Return(&db, nil)

command := commands.BuildCommand(testCmd, nil, conf)

command.Cobra().SetArgs([]string{db.UUID})
_, err := mockexecute.MockExecute(command, mService, conf)

assert.NoError(t, err)
mService.AssertNumberOfCalls(t, targetMethod, 1)
}
12 changes: 10 additions & 2 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,19 @@ func (m *Service) GetManagedDatabaseVersions(r *request.GetManagedDatabaseVersio
}

func (m *Service) StartManagedDatabase(r *request.StartManagedDatabaseRequest) (*upcloud.ManagedDatabase, error) {
return nil, nil
args := m.Called(r)
if args[0] == nil {
return nil, args.Error(1)
}
return args[0].(*upcloud.ManagedDatabase), args.Error(1)
}

func (m *Service) ShutdownManagedDatabase(r *request.ShutdownManagedDatabaseRequest) (*upcloud.ManagedDatabase, error) {
return nil, nil
args := m.Called(r)
if args[0] == nil {
return nil, args.Error(1)
}
return args[0].(*upcloud.ManagedDatabase), args.Error(1)
}

func (m *Service) WaitForManagedDatabaseState(r *request.WaitForManagedDatabaseStateRequest) (*upcloud.ManagedDatabase, error) {
Expand Down