-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas_migration_stop.go
86 lines (67 loc) · 2.39 KB
/
dbaas_migration_stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cmd
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type dbaasMigrationStopCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"stop"`
Name string `cli-arg:"#"`
Zone string `cli-short:"z" cli-usage:"Database Service zone"`
}
func (c *dbaasMigrationStopCmd) cmdAliases() []string { return []string{} }
func (c *dbaasMigrationStopCmd) cmdShort() string {
return "Stop running migration of a Database"
}
func (c *dbaasMigrationStopCmd) cmdLong() string {
return "This command stops the currently running migration of a Database."
}
func (c *dbaasMigrationStopCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *dbaasMigrationStopCmd) cmdRun(cmd *cobra.Command, args []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, c.Zone))
dbType, err := dbaasGetType(ctx, c.Name, c.Zone)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("resource not found in zone %q", c.Zone)
}
return err
}
var stopMigrationFuncs = map[string]func(context.Context, string, string) error{
"mysql": globalstate.EgoscaleClient.StopMysqlDatabaseMigration,
"pg": globalstate.EgoscaleClient.StopPgDatabaseMigration,
"redis": globalstate.EgoscaleClient.StopRedisDatabaseMigration,
}
if _, ok := stopMigrationFuncs[dbType]; !ok {
return fmt.Errorf("migrations not supported for database type %q", dbType)
}
_, err = globalstate.EgoscaleClient.GetDatabaseMigrationStatus(ctx, c.Zone, c.Name)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("migration for database %q not running in zone %q", c.Name, c.Zone)
}
return fmt.Errorf("failed to retrieve migration status: %s", err)
}
decorateAsyncOperation("Stopping Database Migration...", func() {
err = stopMigrationFuncs[dbType](ctx, c.Zone, c.Name)
})
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("migration not running in zone %q", c.Zone)
}
return fmt.Errorf("failed to stop migration: %s", err)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(dbaasMigrationCmd, &dbaasMigrationStopCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}