-
Notifications
You must be signed in to change notification settings - Fork 220
/
update.go
148 lines (122 loc) · 3.85 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
package tigris
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/command"
extensions_core "github.com/superfly/flyctl/internal/command/extensions/core"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
func update() (cmd *cobra.Command) {
const (
short = "Update an existing Tigris object storage bucket"
long = short + "\n"
)
cmd = command.New("update <bucket_name>", short, long, runUpdate, command.RequireSession, command.LoadAppNameIfPresent)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Org(),
extensions_core.SharedFlags,
flag.String{
Name: "custom-domain",
Description: "A custom domain name pointing at your bucket",
Hidden: true,
},
flag.Bool{
Name: "clear-shadow",
Description: "Remove an existing shadow bucket",
},
flag.Bool{
Name: "clear-custom-domain",
Description: "Remove a custom domain from a bucket",
Hidden: true,
},
flag.Bool{
Name: "private",
Description: "Set a public bucket to be private",
},
SharedFlags,
)
return cmd
}
func runUpdate(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
client := flyutil.ClientFromContext(ctx).GenqClient()
id := flag.FirstArg(ctx)
response, err := gql.GetAddOn(ctx, client, id, string(gql.AddOnTypeTigris))
if err != nil {
return
}
addOn := response.AddOn
options, _ := addOn.Options.(map[string]interface{})
if options == nil {
options = make(map[string]interface{})
}
accessKey := flag.GetString(ctx, "shadow-access-key")
secretKey := flag.GetString(ctx, "shadow-secret-key")
region := flag.GetString(ctx, "shadow-region")
shadowName := flag.GetString(ctx, "shadow-name")
endpoint := flag.GetString(ctx, "shadow-endpoint")
writeThrough := flag.GetBool(ctx, "shadow-write-through")
clearShadow := flag.GetBool(ctx, "clear-shadow")
// Check for shadow bucket values
shadowBucketSpecified, err := isShadowBucketSpecified(accessKey, secretKey, region, shadowName, endpoint)
if err != nil {
return err
}
if clearShadow && shadowBucketSpecified {
return fmt.Errorf("You cannot specify both --clear-shadow-bucket and shadow bucket fields")
}
if clearShadow {
options["shadow_bucket"] = map[string]interface{}{}
} else if shadowBucketSpecified {
options["shadow_bucket"] = map[string]interface{}{
"access_key": accessKey,
"secret_key": secretKey,
"region": region,
"name": shadowName,
"endpoint": endpoint,
"write_through": writeThrough,
}
}
if flag.IsSpecified(ctx, "private") {
options["public"] = false
} else if flag.IsSpecified(ctx, "public") {
options["public"] = flag.GetBool(ctx, "public")
}
if flag.IsSpecified(ctx, "no-accelerate") {
options["accelerate"] = false
} else if flag.IsSpecified(ctx, "accelerate") {
options["accelerate"] = flag.GetBool(ctx, "accelerate")
}
if flag.IsSpecified(ctx, "custom-domain") {
domain := flag.GetString(ctx, "custom-domain")
if domain != addOn.Name {
return fmt.Errorf("The custom domain must match the bucket name: %s != %s", domain, addOn.Name)
}
fmt.Fprintf(io.Out, "Before continuing, set a DNS CNAME record to enable your custom domain: %s -> %s\n\n", domain, addOn.Name+".fly.storage.tigris.dev")
confirm, err := prompt.Confirm(ctx, "Continue with the update?")
if err != nil || !confirm {
return err
}
options["website"] = map[string]interface{}{
"domain_name": domain,
}
}
if flag.GetBool(ctx, "clear-custom-domain") {
options["website"] = map[string]interface{}{
"domain_name": "",
}
}
_, err = gql.UpdateAddOn(ctx, client, addOn.Id, addOn.AddOnPlan.Id, []string{}, options)
if err != nil {
return
}
err = runStatus(ctx)
return err
}