-
Notifications
You must be signed in to change notification settings - Fork 240
/
create.go
240 lines (189 loc) · 5.96 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
232
233
234
235
236
237
238
239
240
package redis
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
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"
"github.com/superfly/flyctl/internal/spinner"
)
const (
redisPlanFree = "x7M0gyB764ggwt6YZLRK"
redisPlanPayAsYouGo = "ekQ85Yjkw155ohQ5ALYq0M"
)
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.ReplicaRegions(),
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",
},
)
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.GetBool(ctx, "no-replicas"), enableEviction, nil)
return err
}
func Create(ctx context.Context, org *fly.Organization, name string, region *fly.Region, disallowReplicas bool, enableEviction bool, readRegions *[]fly.Region) (addOn *gql.AddOn, err error) {
var (
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
)
excludedRegions, err := GetExcludedRegions(ctx)
if err != nil {
return nil, err
}
excludedRegions = append(excludedRegions, region.Code)
if readRegions == nil {
readRegions = &[]fly.Region{}
if !disallowReplicas {
readRegions, err = prompt.MultiRegion(ctx, "Optionally, choose one or more replica regions (can be changed later):", !org.PaidPlan, []string{}, excludedRegions, "replica-regions")
if err != nil {
return
}
}
} else {
// Validate that the read regions are not in the excluded regions
var invalidRegions []string
for _, readRegion := range *readRegions {
if slices.Contains(excludedRegions, readRegion.Code) {
invalidRegions = append(invalidRegions, readRegion.Code)
}
}
if len(invalidRegions) > 0 {
return nil, fmt.Errorf("invalid replica regions: %v", invalidRegions)
}
}
plan, err := DeterminePlan(ctx, org)
if err != nil {
return nil, err
}
s := spinner.Run(io, "Launching...")
params := RedisConfiguration{
Name: name,
PlanId: plan.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. Check the pricing details at https://upstash.com/pricing.\n", colorize.Green(addOn.Name))
fmt.Fprintf(io.Out, "Apps in the %s org can connect to Redis at %s\n", colorize.Green(org.Slug), colorize.Green(addOn.PublicUrl))
fmt.Fprintf(io.Out, "If you have redis-cli installed, use %s to get a Redis console.\n", colorize.Green("fly redis connect"))
return addOn, err
}
type RedisConfiguration struct {
Name string
PlanId string
PrimaryRegion *fly.Region
ReadRegions []fly.Region
Eviction bool
}
func ProvisionDatabase(ctx context.Context, org *fly.Organization, config RedisConfiguration) (addOn *gql.AddOn, err error) {
client := fly.ClientFromContext(ctx).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
}
func DeterminePlan(ctx context.Context, org *fly.Organization) (*gql.ListAddOnPlansAddOnPlansAddOnPlanConnectionNodesAddOnPlan, error) {
client := fly.ClientFromContext(ctx)
// All new databases are pay-as-you-go
planId := redisPlanPayAsYouGo
// Now that we have the Plan ID, look up the actual plan
allAddons, err := gql.ListAddOnPlans(ctx, client.GenqClient)
if err != nil {
return nil, err
}
for _, addon := range allAddons.AddOnPlans.Nodes {
if addon.Id == planId {
return &addon, nil
}
}
return nil, errors.New("plan not found")
}