-
Notifications
You must be signed in to change notification settings - Fork 221
/
redis.go
73 lines (60 loc) · 1.74 KB
/
redis.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
package plan
import (
"context"
"fmt"
"github.com/samber/lo"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/terminal"
)
type RedisProvider interface {
Describe(ctx context.Context) (string, error)
}
type RedisPlan struct {
UpstashRedis *UpstashRedisPlan `json:"upstash_redis"`
}
func (p *RedisPlan) Provider() RedisProvider {
if p == nil {
return nil
}
if p.UpstashRedis != nil {
return p.UpstashRedis
}
return nil
}
func (p *RedisPlan) Describe(ctx context.Context) (string, error) {
if provider := p.Provider(); provider != nil {
return provider.Describe(ctx)
}
return descriptionNone, nil
}
func DefaultRedis(plan *LaunchPlan) RedisPlan {
return RedisPlan{
UpstashRedis: &UpstashRedisPlan{
AppName: fmt.Sprintf("%s-redis", plan.AppName),
PlanId: "upstash-redis-1",
Eviction: false,
},
}
}
type UpstashRedisPlan struct {
AppName string `json:"app_name"`
PlanId string `json:"plan_id"`
Eviction bool `json:"eviction"`
ReadReplicas []string `json:"read_replicas"`
}
func (p *UpstashRedisPlan) Describe(ctx context.Context) (string, error) {
apiClient := client.FromContext(ctx).API()
result, err := gql.ListAddOnPlans(ctx, apiClient.GenqClient)
if err != nil {
terminal.Debugf("Failed to list addon plans: %s\n", err)
return "<internal error>", err
}
for _, plan := range result.AddOnPlans.Nodes {
if plan.Id == p.PlanId {
evictionStatus := lo.Ternary(p.Eviction, "enabled", "disabled")
return fmt.Sprintf("%s: %s Max Data Size, ($%d / month), eviction %s", plan.DisplayName, plan.MaxDataSize, plan.PricePerMonth, evictionStatus), nil
}
}
return "<plan not found, this is an error>", fmt.Errorf("plan not found: %s", p.PlanId)
}