-
Notifications
You must be signed in to change notification settings - Fork 221
/
allocate.go
83 lines (68 loc) · 1.73 KB
/
allocate.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
package ips
import (
"context"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
)
func newAllocatev4() *cobra.Command {
const (
long = `Allocates an IPv4 address to the application`
short = `Allocate an IPv4 address`
)
cmd := command.New("allocate-v4", short, long, runAllocateIPAddressV4,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Region(),
)
return cmd
}
func newAllocatev6() *cobra.Command {
const (
long = `Allocates an IPv6 address to the application`
short = `Allocate an IPv6 address`
)
cmd := command.New("allocate-v6", short, long, runAllocateIPAddressV6,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Region(),
flag.Bool{
Name: "private",
Description: "Allocate a private IPv6 address",
},
)
return cmd
}
func runAllocateIPAddressV4(ctx context.Context) error {
return runAllocateIPAddress(ctx, "v4")
}
func runAllocateIPAddressV6(ctx context.Context) error {
private := flag.GetBool(ctx, "private")
if private {
return runAllocateIPAddress(ctx, "private_v6")
}
return runAllocateIPAddress(ctx, "v6")
}
func runAllocateIPAddress(ctx context.Context, addrType string) error {
client := client.FromContext(ctx).API()
appName := app.NameFromContext(ctx)
region := flag.GetRegion(ctx)
ipAddress, err := client.AllocateIPAddress(ctx, appName, addrType, region)
if err != nil {
return err
}
ipAddresses := []api.IPAddress{*ipAddress}
renderListTable(ctx, ipAddresses)
return nil
}