Skip to content

Commit 8c81aca

Browse files
committed
feat(db): add database start and database stop commands
1 parent 0047863 commit 8c81aca

File tree

7 files changed

+212
-3
lines changed

7 files changed

+212
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99
### Added
1010
- Add `--show-ip-addresses` flag to `server list` command to optionally include IP addresses in command output.
11-
- Add `database connection list`, and `database connection cancel` commands.
11+
- Add `database connection list`, `database connection cancel`, `database start`, and `database stop` commands.
1212

1313
### Changed
1414
- Make `--family` parameter of `server firewall create` command optional to allow editing the default rules.

internal/commands/all/all.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
107107
commands.BuildCommand(database.ShowCommand(), databaseCommand.Cobra(), conf)
108108
commands.BuildCommand(database.TypesCommand(), databaseCommand.Cobra(), conf)
109109
commands.BuildCommand(database.PlansCommand(), databaseCommand.Cobra(), conf)
110+
commands.BuildCommand(database.StartCommand(), databaseCommand.Cobra(), conf)
111+
commands.BuildCommand(database.StopCommand(), databaseCommand.Cobra(), conf)
110112

111113
// Database connections
112114
connectionsCommand := commands.BuildCommand(databaseconnection.BaseConnectionCommand(), databaseCommand.Cobra(), conf)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package database
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/internal/completion"
8+
"github.com/UpCloudLtd/upcloud-cli/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/resolver"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
12+
)
13+
14+
// StartCommand creates the "database start" command
15+
func StartCommand() commands.Command {
16+
return &startCommand{
17+
BaseCommand: commands.New(
18+
"start",
19+
"Start on a managed database",
20+
"upctl database start b0952286-1193-4a81-a1af-62efc014ae4b",
21+
"upctl database start b0952286-1193-4a81-a1af-62efc014ae4b 666bcd3c-5c63-428d-a4fd-07c27469a5a6",
22+
"upctl database start pg-1x1xcpu-2gb-25gb-pl-waw1",
23+
),
24+
}
25+
}
26+
27+
type startCommand struct {
28+
*commands.BaseCommand
29+
completion.Database
30+
resolver.CachingDatabase
31+
}
32+
33+
// Execute implements commands.MultipleArgumentCommand
34+
func (s *startCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
35+
svc := exec.All()
36+
msg := fmt.Sprintf("Starting database %v", uuid)
37+
logline := exec.NewLogEntry(msg)
38+
39+
logline.StartedNow()
40+
logline.SetMessage(fmt.Sprintf("%s: sending request", msg))
41+
42+
res, err := svc.StartManagedDatabase(&request.StartManagedDatabaseRequest{
43+
UUID: uuid,
44+
})
45+
if err != nil {
46+
return commands.HandleError(logline, fmt.Sprintf("%s: failed", msg), err)
47+
}
48+
49+
logline.SetMessage(fmt.Sprintf("%s: done", msg))
50+
logline.MarkDone()
51+
52+
return output.OnlyMarshaled{Value: res}, nil
53+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package database
2+
3+
import (
4+
"testing"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/internal/config"
8+
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
10+
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
11+
12+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
13+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestStartCommand(t *testing.T) {
18+
targetMethod := "StartManagedDatabase"
19+
20+
var db = upcloud.ManagedDatabase{
21+
State: upcloud.ManagedDatabaseStateRunning,
22+
Title: "database-title",
23+
UUID: "1fdfda29-ead1-4855-b71f-1e33eb2ca9de",
24+
}
25+
26+
req := request.StartManagedDatabaseRequest{
27+
UUID: db.UUID,
28+
}
29+
30+
conf := config.New()
31+
testCmd := StartCommand()
32+
mService := new(smock.Service)
33+
34+
conf.Service = internal.Wrapper{Service: mService}
35+
mService.On(targetMethod, &req).Return(&db, nil)
36+
37+
command := commands.BuildCommand(testCmd, nil, conf)
38+
39+
command.Cobra().SetArgs([]string{db.UUID})
40+
_, err := mockexecute.MockExecute(command, mService, conf)
41+
42+
assert.NoError(t, err)
43+
mService.AssertNumberOfCalls(t, targetMethod, 1)
44+
}

internal/commands/database/stop.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package database
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/internal/completion"
8+
"github.com/UpCloudLtd/upcloud-cli/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/resolver"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
12+
)
13+
14+
// StopCommand creates the "database stop" command
15+
func StopCommand() commands.Command {
16+
return &stopCommand{
17+
BaseCommand: commands.New(
18+
"stop",
19+
"Stop a managed database",
20+
"upctl database stop b0952286-1193-4a81-a1af-62efc014ae4b",
21+
"upctl database stop b0952286-1193-4a81-a1af-62efc014ae4b 666bcd3c-5c63-428d-a4fd-07c27469a5a6",
22+
"upctl database stop pg-1x1xcpu-2gb-25gb-pl-waw1",
23+
),
24+
}
25+
}
26+
27+
type stopCommand struct {
28+
*commands.BaseCommand
29+
completion.Database
30+
resolver.CachingDatabase
31+
}
32+
33+
// InitCommand implements Command.InitCommand
34+
func (s *stopCommand) InitCommand() {
35+
s.Cobra().Aliases = []string{"shutdown"}
36+
}
37+
38+
// Execute implements commands.MultipleArgumentCommand
39+
func (s *stopCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
40+
svc := exec.All()
41+
msg := fmt.Sprintf("Stopping database %v", uuid)
42+
logline := exec.NewLogEntry(msg)
43+
44+
logline.StartedNow()
45+
logline.SetMessage(fmt.Sprintf("%s: sending request", msg))
46+
47+
res, err := svc.ShutdownManagedDatabase(&request.ShutdownManagedDatabaseRequest{
48+
UUID: uuid,
49+
})
50+
if err != nil {
51+
return commands.HandleError(logline, fmt.Sprintf("%s: failed", msg), err)
52+
}
53+
54+
logline.SetMessage(fmt.Sprintf("%s: done", msg))
55+
logline.MarkDone()
56+
57+
return output.OnlyMarshaled{Value: res}, nil
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package database
2+
3+
import (
4+
"testing"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/internal/config"
8+
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
10+
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
11+
12+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
13+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestStopCommand(t *testing.T) {
18+
targetMethod := "ShutdownManagedDatabase"
19+
20+
var db = upcloud.ManagedDatabase{
21+
State: upcloud.ManagedDatabaseStateRunning,
22+
Title: "database-title",
23+
UUID: "1fdfda29-ead1-4855-b71f-1e33eb2ca9de",
24+
}
25+
26+
req := request.ShutdownManagedDatabaseRequest{
27+
UUID: db.UUID,
28+
}
29+
30+
conf := config.New()
31+
testCmd := StopCommand()
32+
mService := new(smock.Service)
33+
34+
conf.Service = internal.Wrapper{Service: mService}
35+
mService.On(targetMethod, &req).Return(&db, nil)
36+
37+
command := commands.BuildCommand(testCmd, nil, conf)
38+
39+
command.Cobra().SetArgs([]string{db.UUID})
40+
_, err := mockexecute.MockExecute(command, mService, conf)
41+
42+
assert.NoError(t, err)
43+
mService.AssertNumberOfCalls(t, targetMethod, 1)
44+
}

internal/mock/mock.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,19 @@ func (m *Service) GetManagedDatabaseVersions(r *request.GetManagedDatabaseVersio
639639
}
640640

641641
func (m *Service) StartManagedDatabase(r *request.StartManagedDatabaseRequest) (*upcloud.ManagedDatabase, error) {
642-
return nil, nil
642+
args := m.Called(r)
643+
if args[0] == nil {
644+
return nil, args.Error(1)
645+
}
646+
return args[0].(*upcloud.ManagedDatabase), args.Error(1)
643647
}
644648

645649
func (m *Service) ShutdownManagedDatabase(r *request.ShutdownManagedDatabaseRequest) (*upcloud.ManagedDatabase, error) {
646-
return nil, nil
650+
args := m.Called(r)
651+
if args[0] == nil {
652+
return nil, args.Error(1)
653+
}
654+
return args[0].(*upcloud.ManagedDatabase), args.Error(1)
647655
}
648656

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

0 commit comments

Comments
 (0)