-
Notifications
You must be signed in to change notification settings - Fork 224
/
clusters_create.go
86 lines (69 loc) · 1.87 KB
/
clusters_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
package lfsc
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newClustersCreate() *cobra.Command {
const (
long = `Creates a new LiteFS Cloud cluster.`
short = "Creates a LiteFS Cloud cluster"
usage = "create CLUSTERNAME"
)
cmd := command.New(usage, short, long, runClustersCreate,
command.RequireSession,
command.LoadAppNameIfPresentNoFlag,
)
cmd.Args = cobra.RangeArgs(0, 1)
flag.Add(cmd,
urlFlag(),
regionFlag(),
flag.Org(),
flag.JSONOutput(),
)
return cmd
}
func runClustersCreate(ctx context.Context) error {
apiClient := fly.ClientFromContext(ctx)
orgID, err := getOrgID(ctx)
if err != nil {
return err
}
clusterName := flag.FirstArg(ctx)
if clusterName == "" {
return errors.New("cluster name required as first argument")
}
region := flag.GetString(ctx, "region")
if region == "" {
return errors.New("required: --region CODE")
}
lfscClient, err := newLFSCClient(ctx, "")
if err != nil {
return err
}
cluster, err := lfscClient.CreateCluster(ctx, clusterName, region)
if err != nil {
return err
}
resp, err := gql.CreateLimitedAccessToken(ctx, apiClient.GenqClient, clusterName, orgID, "litefs_cloud",
&gql.LimitedAccessTokenOptions{
"cluster": clusterName,
},
"",
)
if err != nil {
return fmt.Errorf("failed creating cluster token: %w", err)
}
out := iostreams.FromContext(ctx).Out
fmt.Fprintf(out, "Cluster %q successfully created in %s.\n\n", cluster.Name, cluster.Region)
fmt.Fprintf(out, "Run the following to set the auth token on your application:\n\n")
fmt.Fprintf(out, "fly secrets set LFSC_AUTH_TOKEN=%q\n\n",
resp.CreateLimitedAccessToken.LimitedAccessToken.TokenHeader)
return nil
}