-
Notifications
You must be signed in to change notification settings - Fork 240
/
create.go
231 lines (181 loc) · 5.65 KB
/
create.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package redis
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"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"
"github.com/superfly/flyctl/internal/spinner"
)
func newCreate() (cmd *cobra.Command) {
const (
long = `Create an Upstash Redis database`
short = long
usage = "create"
)
cmd = command.New(usage, short, long, runCreate, command.RequireSession)
flag.Add(cmd,
flag.Org(),
flag.Region(),
flag.String{
Name: "name",
Shorthand: "n",
Description: "The name of your Redis database",
},
flag.Bool{
Name: "no-replicas",
Description: "Don't prompt for selecting replica regions",
},
flag.Bool{
Name: "enable-eviction",
Description: "Evict objects when memory is full",
},
flag.Bool{
Name: "disable-eviction",
Description: "Disallow writes when the max data size limit has been reached",
},
flag.String{
Name: "plan",
Description: "Upstash Redis plan",
},
)
return cmd
}
func runCreate(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
// pre-fetch platform regions for later use
prompt.PlatformRegions(ctx)
org, err := prompt.Org(ctx)
if err != nil {
return err
}
var name = flag.GetString(ctx, "name")
if name == "" {
err = prompt.String(ctx, &name, "Choose a Redis database name (leave blank to generate one):", "", false)
if err != nil {
return err
}
}
excludedRegions, err := GetExcludedRegions(ctx)
if err != nil {
return err
}
primaryRegion, err := prompt.Region(ctx, !org.PaidPlan, prompt.RegionParams{
Message: "Choose a primary region (can't be changed later)",
ExcludedRegionCodes: excludedRegions,
})
if err != nil {
return err
}
var enableEviction bool = false
if flag.GetBool(ctx, "enable-eviction") {
enableEviction = true
} else if !flag.GetBool(ctx, "disable-eviction") {
fmt.Fprintf(io.Out, "\nUpstash Redis can evict objects when memory is full. This is useful when caching in Redis. This setting can be changed later.\nLearn more at https://fly.io/docs/reference/redis/#memory-limits-and-object-eviction-policies\n")
enableEviction, err = prompt.Confirm(ctx, "Would you like to enable eviction?")
if err != nil {
return
}
}
_, err = Create(ctx, org, name, primaryRegion, flag.GetString(ctx, "plan"), flag.GetBool(ctx, "no-replicas"), enableEviction)
return err
}
func Create(ctx context.Context, org *api.Organization, name string, region *api.Region, planFlag string, disallowReplicas bool, enableEviction bool) (addOn *gql.AddOn, err error) {
var (
io = iostreams.FromContext(ctx)
client = client.FromContext(ctx).API().GenqClient
colorize = io.ColorScheme()
)
excludedRegions, err := GetExcludedRegions(ctx)
if err != nil {
return nil, err
}
readRegions := &[]api.Region{}
excludedRegions = append(excludedRegions, region.Code)
if !disallowReplicas {
readRegions, err = prompt.MultiRegion(ctx, "Optionally, choose one or more replica regions (can be changed later):", !org.PaidPlan, []string{}, excludedRegions)
if err != nil {
return
}
}
var planIndex int
result, err := gql.ListAddOnPlans(ctx, client)
if err != nil {
return
}
if planFlag != "" {
planIndex = -1
for index, plan := range result.AddOnPlans.Nodes {
if plan.DisplayName == planFlag {
planIndex = index
break
}
}
if planIndex == -1 {
return nil, fmt.Errorf("invalid plan name: %s", planFlag)
}
} else {
var planOptions []string
for _, plan := range result.AddOnPlans.Nodes {
planOptions = append(planOptions, fmt.Sprintf("%s: %s Max Data Size, ($%d / month)", plan.DisplayName, plan.MaxDataSize, plan.PricePerMonth))
}
err = prompt.Select(ctx, &planIndex, "Select an Upstash Redis plan", "", planOptions...)
if err != nil {
return nil, fmt.Errorf("failed to select a plan: %w", err)
}
}
s := spinner.Run(io, "Launching...")
params := RedisConfiguration{
Name: name,
PlanId: result.AddOnPlans.Nodes[planIndex].Id,
PrimaryRegion: region,
ReadRegions: *readRegions,
Eviction: enableEviction,
}
addOn, err = ProvisionDatabase(ctx, org, params)
s.Stop()
if err != nil {
return
}
fmt.Fprintf(io.Out, "\nYour Upstash Redis database %s is ready.\n", colorize.Green(addOn.Name))
fmt.Fprintf(io.Out, "Apps in the %s org can connect to at %s\n", colorize.Green(org.Slug), colorize.Green(addOn.PublicUrl))
fmt.Fprintf(io.Out, "If you have redis-cli installed, use %s to connect to your database.\n", colorize.Green("fly redis connect"))
return addOn, err
}
type RedisConfiguration struct {
Name string
PlanId string
PrimaryRegion *api.Region
ReadRegions []api.Region
Eviction bool
}
func ProvisionDatabase(ctx context.Context, org *api.Organization, config RedisConfiguration) (addOn *gql.AddOn, err error) {
client := client.FromContext(ctx).API().GenqClient
var readRegionCodes []string
for _, region := range config.ReadRegions {
readRegionCodes = append(readRegionCodes, region.Code)
}
options := gql.AddOnOptions{}
if config.Eviction {
options["eviction"] = true
}
input := gql.CreateAddOnInput{
OrganizationId: org.ID,
PrimaryRegion: config.PrimaryRegion.Code,
Name: config.Name,
PlanId: config.PlanId,
ReadRegions: readRegionCodes,
Type: "upstash_redis",
Options: options,
}
response, err := gql.CreateAddOn(ctx, client, input)
if err != nil {
return
}
return &response.CreateAddOn.AddOn, nil
}