-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas_update_pg.go
128 lines (109 loc) · 3.44 KB
/
dbaas_update_pg.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
120
121
122
123
124
125
126
127
128
package cmd
import (
"fmt"
"net/http"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
"github.com/spf13/cobra"
)
func (c *dbaasServiceUpdateCmd) updatePG(cmd *cobra.Command, _ []string) error {
var updated bool
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
databaseService := oapi.UpdateDbaasServicePgJSONRequestBody{}
settingsSchema, err := cs.GetDbaasSettingsPgWithResponse(ctx)
if err != nil {
return fmt.Errorf("unable to retrieve database Service settings: %w", err)
}
if settingsSchema.StatusCode() != http.StatusOK {
return fmt.Errorf("API request error: unexpected status %s", settingsSchema.Status())
}
if c.PGBackupSchedule != "" {
bh, bm, err := parseDatabaseBackupSchedule(c.PGBackupSchedule)
if err != nil {
return err
}
databaseService.BackupSchedule = &struct {
BackupHour *int64 `json:"backup-hour,omitempty"`
BackupMinute *int64 `json:"backup-minute,omitempty"`
}{
BackupHour: &bh,
BackupMinute: &bm,
}
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.PGIPFilter)) {
databaseService.IpFilter = &c.PGIPFilter
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Plan)) {
databaseService.Plan = &c.Plan
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.TerminationProtection)) {
databaseService.TerminationProtection = &c.TerminationProtection
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.MaintenanceDOW)) &&
cmd.Flags().Changed(mustCLICommandFlagName(c, &c.MaintenanceTime)) {
databaseService.Maintenance = &struct {
Dow oapi.UpdateDbaasServicePgJSONBodyMaintenanceDow `json:"dow"`
Time string `json:"time"`
}{
Dow: oapi.UpdateDbaasServicePgJSONBodyMaintenanceDow(c.MaintenanceDOW),
Time: c.MaintenanceTime,
}
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.PGBouncerSettings)) {
settings, err := validateDatabaseServiceSettings(
c.PGBouncerSettings,
settingsSchema.JSON200.Settings.Pgbouncer,
)
if err != nil {
return fmt.Errorf("invalid settings: %w", err)
}
databaseService.PgbouncerSettings = &settings
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.PGLookoutSettings)) {
settings, err := validateDatabaseServiceSettings(
c.PGLookoutSettings,
settingsSchema.JSON200.Settings.Pglookout,
)
if err != nil {
return fmt.Errorf("invalid settings: %w", err)
}
databaseService.PglookoutSettings = &settings
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.PGSettings)) {
settings, err := validateDatabaseServiceSettings(
c.PGSettings,
settingsSchema.JSON200.Settings.Pg,
)
if err != nil {
return fmt.Errorf("invalid settings: %w", err)
}
databaseService.PgSettings = &settings
updated = true
}
if updated {
var res *oapi.UpdateDbaasServicePgResponse
decorateAsyncOperation(fmt.Sprintf("Updating Database Service %q...", c.Name), func() {
res, err = cs.UpdateDbaasServicePgWithResponse(ctx, oapi.DbaasServiceName(c.Name), databaseService)
})
if err != nil {
return err
}
if res.StatusCode() != http.StatusOK {
return fmt.Errorf("API request error: unexpected status %s", res.Status())
}
}
if !gQuiet {
return c.outputFunc((&dbaasServiceShowCmd{
Name: c.Name,
Zone: c.Zone,
}).showDatabaseServicePG(ctx))
}
return nil
}