-
Notifications
You must be signed in to change notification settings - Fork 8
/
region_migrations.go
119 lines (98 loc) · 4.25 KB
/
region_migrations.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package scalingo
import (
"context"
"time"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
const (
RegionMigrationStatusCreated RegionMigrationStatus = "created"
RegionMigrationStatusPreflightSuccess RegionMigrationStatus = "preflight-success"
RegionMigrationStatusPreflightError RegionMigrationStatus = "preflight-error"
RegionMigrationStatusRunning RegionMigrationStatus = "running"
RegionMigrationStatusPrepared RegionMigrationStatus = "prepared"
RegionMigrationStatusDataMigrated RegionMigrationStatus = "data-migrated"
RegionMigrationStatusAborting RegionMigrationStatus = "aborting"
RegionMigrationStatusAborted RegionMigrationStatus = "aborted"
RegionMigrationStatusError RegionMigrationStatus = "error"
RegionMigrationStatusDone RegionMigrationStatus = "done"
RegionMigrationStepAbort RegionMigrationStep = "abort"
RegionMigrationStepPreflight RegionMigrationStep = "preflight"
RegionMigrationStepPrepare RegionMigrationStep = "prepare"
RegionMigrationStepData RegionMigrationStep = "data"
RegionMigrationStepFinalize RegionMigrationStep = "finalize"
StepStatusRunning StepStatus = "running"
StepStatusDone StepStatus = "done"
StepStatusError StepStatus = "error"
)
type RegionMigrationsService interface {
CreateRegionMigration(ctx context.Context, appID string, params RegionMigrationParams) (RegionMigration, error)
RunRegionMigrationStep(ctx context.Context, appID, migrationID string, step RegionMigrationStep) error
ShowRegionMigration(ctx context.Context, appID, migrationID string) (RegionMigration, error)
ListRegionMigrations(ctx context.Context, appID string) ([]RegionMigration, error)
}
type RegionMigrationParams struct {
Destination string `json:"destination"`
DstAppName string `json:"dst_app_name"`
}
type RegionMigration struct {
ID string `json:"id"`
SrcAppName string `json:"src_app_name"`
DstAppName string `json:"dst_app_name"`
AppID string `json:"app_id"`
NewAppID string `json:"new_app_id"`
Source string `json:"source"`
Destination string `json:"destination"`
Status RegionMigrationStatus `json:"status"`
StartedAt time.Time `json:"started_at"`
FinishedAt time.Time `json:"finished_at"`
Steps Steps `json:"steps"`
}
type StepStatus string
type RegionMigrationStatus string
type RegionMigrationStep string
type Steps []Step
type Step struct {
ID string `json:"id"`
Name string `json:"name"`
Status StepStatus `json:"status"`
Logs string `json:"logs"`
}
func (c *Client) CreateRegionMigration(ctx context.Context, appID string, params RegionMigrationParams) (RegionMigration, error) {
var migration RegionMigration
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", appID, "region_migrations", map[string]RegionMigrationParams{
"migration": params,
}, &migration)
if err != nil {
return migration, errgo.Notef(err, "fail to create migration")
}
return migration, nil
}
func (c *Client) RunRegionMigrationStep(ctx context.Context, appID, migrationID string, step RegionMigrationStep) error {
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + appID + "/region_migrations/" + migrationID + "/run",
Params: map[string]RegionMigrationStep{"step": step},
Expected: http.Statuses{204},
}, nil)
if err != nil {
return errgo.Notef(err, "fail to run migration step")
}
return nil
}
func (c *Client) ShowRegionMigration(ctx context.Context, appID, migrationID string) (RegionMigration, error) {
var migration RegionMigration
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", appID, "region_migrations", migrationID, nil, &migration)
if err != nil {
return migration, errgo.Notef(err, "fail to get migration")
}
return migration, nil
}
func (c *Client) ListRegionMigrations(ctx context.Context, appID string) ([]RegionMigration, error) {
var migrations []RegionMigration
err := c.ScalingoAPI().SubresourceList(ctx, "apps", appID, "region_migrations", nil, &migrations)
if err != nil {
return migrations, errgo.Notef(err, "fail to list migrations")
}
return migrations, nil
}