-
Notifications
You must be signed in to change notification settings - Fork 240
/
create.go
115 lines (93 loc) · 2.61 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
package volumes
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
)
func newCreate() *cobra.Command {
const (
long = `Create new volume for app. --region flag must be included to specify
region the volume exists in. --size flag is optional, defaults to 10,
sets the size as the number of gigabytes the volume will consume.`
short = "Create new volume for app"
usage = "create <volumename>"
)
cmd := command.New(usage, short, long, runCreate,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Region(),
flag.Int{
Name: "size",
Shorthand: "s",
Default: 3,
Description: "Size of volume in gigabytes",
},
flag.Bool{
Name: "no-encryption",
Description: "Do not encrypt the volume contents",
Default: false,
},
flag.Bool{
Name: "require-unique-zone",
Description: "Require volume to be placed in separate hardware zone from existing volumes",
Default: true,
},
flag.String{
Name: "snapshot-id",
Description: "Create volume from a specified snapshot",
},
)
return cmd
}
func runCreate(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
client = client.FromContext(ctx).API()
volumeName = flag.FirstArg(ctx)
appName = app.NameFromContext(ctx)
)
appID, err := client.GetAppID(ctx, appName)
if err != nil {
return err
}
var region *api.Region
if region, err = prompt.Region(ctx, ""); err != nil {
return err
}
var snapshotID *string
if flag.GetString(ctx, "snapshot-id") != "" {
snapshotID = api.StringPointer(flag.GetString(ctx, "snapshot-id"))
}
input := api.CreateVolumeInput{
AppID: appID,
Name: volumeName,
Region: region.Code,
SizeGb: flag.GetInt(ctx, "size"),
Encrypted: !flag.GetBool(ctx, "no-encryption"),
RequireUniqueZone: flag.GetBool(ctx, "require-unique-zone"),
SnapshotID: snapshotID,
}
volume, err := client.CreateVolume(ctx, input)
if err != nil {
return fmt.Errorf("failed creating volume: %w", err)
}
out := iostreams.FromContext(ctx).Out
if cfg.JSONOutput {
return render.JSON(out, volume)
}
return printVolume(out, volume)
}