-
Notifications
You must be signed in to change notification settings - Fork 221
/
create.go
119 lines (99 loc) · 3.01 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
package tigris
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
extensions_core "github.com/superfly/flyctl/internal/command/extensions/core"
"github.com/superfly/flyctl/internal/command/orgs"
"github.com/superfly/flyctl/internal/command/secrets"
"github.com/superfly/flyctl/internal/flag"
)
func create() (cmd *cobra.Command) {
const (
short = "Provision a Tigris object storage bucket"
long = short + "\n"
)
cmd = command.New("create", short, long, runCreate, command.RequireSession, command.LoadAppNameIfPresent)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Org(),
extensions_core.SharedFlags,
SharedFlags,
flag.String{
Name: "name",
Shorthand: "n",
Description: "The name of your bucket",
},
)
return cmd
}
func runCreate(ctx context.Context) (err error) {
appName := appconfig.NameFromContext(ctx)
params := extensions_core.ExtensionParams{}
if appName != "" {
params.AppName = appName
} else {
org, err := orgs.OrgFromFlagOrSelect(ctx)
if err != nil {
return err
}
params.Organization = org
}
options := gql.AddOnOptions{}
options["public"] = flag.GetBool(ctx, "public")
options["accelerate"] = flag.GetBool(ctx, "accelerate")
accessKey := flag.GetString(ctx, "shadow-access-key")
secretKey := flag.GetString(ctx, "shadow-secret-key")
region := flag.GetString(ctx, "shadow-region")
name := flag.GetString(ctx, "shadow-name")
endpoint := flag.GetString(ctx, "shadow-endpoint")
writeThrough := flag.GetBool(ctx, "shadow-write-through")
// Check for shadow bucket values
shadowBucketSpecified, err := isShadowBucketSpecified(accessKey, secretKey, region, name, endpoint)
if err != nil {
return err
}
// Include 'shadow_bucket' if all values are provided
if shadowBucketSpecified {
options["shadow_bucket"] = map[string]interface{}{
"access_key": accessKey,
"secret_key": secretKey,
"region": region,
"name": name,
"endpoint": endpoint,
"write_through": writeThrough,
}
}
// Always include 'website' because the Tigris API expects it
options["website"] = map[string]interface{}{
"domain_name": "",
}
// Assign the options to params
params.Options = options
params.Provider = "tigris"
extension, err := extensions_core.ProvisionExtension(ctx, params)
if err != nil {
return err
}
if extension.SetsSecrets {
err = secrets.DeploySecrets(ctx, gql.ToAppCompact(*extension.App), false, false)
}
return err
}
func isShadowBucketSpecified(accessKey, secretKey, region, name, endpoint string) (bool, error) {
values := []string{accessKey, secretKey, region, name, endpoint}
var n int
for _, value := range values {
if value != "" {
n++
}
}
if n > 0 && n < len(values) {
return false, fmt.Errorf("You must set all required shadow bucket values: shadow-access-key, shadow-secret-key, shadow-region, shadow-name, shadow-endpoint")
}
return n == len(values), nil
}