-
Notifications
You must be signed in to change notification settings - Fork 233
/
update.go
104 lines (78 loc) · 2.32 KB
/
update.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
package redis
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
)
func newUpdate() (cmd *cobra.Command) {
const (
long = `Update an Upstash Redis database settings, payment plan or replica regions`
short = long
usage = "update <name>"
)
cmd = command.New(usage, short, long, runUpdate, command.RequireSession)
flag.Add(cmd,
flag.Org(),
flag.Region(),
flag.ReplicaRegions(),
)
cmd.Args = cobra.ExactArgs(1)
return cmd
}
func runUpdate(ctx context.Context) (err error) {
var (
out = iostreams.FromContext(ctx).Out
client = fly.ClientFromContext(ctx).GenqClient
)
id := flag.FirstArg(ctx)
response, err := gql.GetAddOn(ctx, client, id)
if err != nil {
return
}
addOn := response.AddOn
excludedRegions, err := GetExcludedRegions(ctx)
if err != nil {
return err
}
excludedRegions = append(excludedRegions, addOn.PrimaryRegion)
readRegions, err := prompt.MultiRegion(ctx, "Choose replica regions, or unselect to remove replica regions:", !addOn.Organization.PaidPlan, addOn.ReadRegions, excludedRegions, "replica-regions")
if err != nil {
return
}
options, _ := addOn.Options.(map[string]interface{})
plan := addOn.AddOnPlan.Id
if addOn.AddOnPlan.Id == redisPlanFree {
if upgradePlan, err := prompt.Confirm(ctx, "Would you like to upgrade to the unrestricted Pay-as-you-go plan?"); upgradePlan || err != nil {
plan = redisPlanPayAsYouGo
}
}
if err != nil {
return
}
if options["eviction"] != nil && options["eviction"].(bool) {
if disableEviction, err := prompt.Confirm(ctx, "Would you like to disable eviction?"); disableEviction || err != nil {
options["eviction"] = false
}
} else {
options["eviction"], err = prompt.Confirm(ctx, "Would you like to enable eviction?")
}
if err != nil {
return
}
readRegionCodes := []string{}
for _, region := range *readRegions {
readRegionCodes = append(readRegionCodes, region.Code)
}
_, err = gql.UpdateAddOn(ctx, client, addOn.Id, plan, readRegionCodes, options)
if err != nil {
return
}
fmt.Fprintf(out, "Your Upstash Redis database %s was updated.\n", addOn.Name)
return
}