-
Notifications
You must be signed in to change notification settings - Fork 221
/
update.go
154 lines (120 loc) · 3.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package redis
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"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`
short = long
usage = "update <name>"
)
cmd = command.New(usage, short, long, runUpdate, command.RequireSession)
flag.Add(cmd,
flag.Org(),
flag.Region(),
)
cmd.Args = cobra.ExactArgs(1)
return cmd
}
func runUpdate(ctx context.Context) (err error) {
var (
out = iostreams.FromContext(ctx).Out
client = client.FromContext(ctx).API().GenqClient
)
id := flag.FirstArg(ctx)
_ = `# @genqlient
query GetAddOn($name: String) {
addOn(name: $name) {
id
name
publicUrl
privateIp
password
primaryRegion
readRegions
options
organization {
slug
paidPlan
}
addOnPlan {
id
name
displayName
}
}
}
`
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)
if err != nil {
return
}
var index int
var promptOptions []string
result, err := gql.ListAddOnPlans(ctx, client)
if err != nil {
return
}
for _, plan := range result.AddOnPlans.Nodes {
promptOptions = append(promptOptions, fmt.Sprintf("%s: %s Max Data Size, $%d/month/region", plan.DisplayName, plan.MaxDataSize, plan.PricePerMonth))
}
err = prompt.Select(ctx, &index, "Select an Upstash Redis plan", "", promptOptions...)
if err != nil {
return fmt.Errorf("failed to select a plan: %w", err)
}
// type Options struct {
// Eviction bool
// }
// options := &Options{}
options, _ := addOn.Options.(map[string]interface{})
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
}
_ = `# @genqlient
mutation UpdateAddOn($addOnId: ID!, $planId: ID!, $readRegions: [String!]!, $options: JSON!) {
updateAddOn(input: {addOnId: $addOnId, planId: $planId, readRegions: $readRegions, options: $options}) {
addOn {
id
}
}
}
`
readRegionCodes := []string{}
for _, region := range *readRegions {
readRegionCodes = append(readRegionCodes, region.Code)
}
_, err = gql.UpdateAddOn(ctx, client, addOn.Id, result.AddOnPlans.Nodes[index].Id, readRegionCodes, options)
if err != nil {
return
}
fmt.Fprintf(out, "Your Upstash Redis database %s was updated.\n", addOn.Name)
return
}