-
Notifications
You must be signed in to change notification settings - Fork 240
/
ips.go
152 lines (111 loc) · 3.95 KB
/
ips.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
149
150
151
152
package cmd
import (
"fmt"
"net"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/docstrings"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmd/presenters"
)
func newIPAddressesCommand(client *client.Client) *Command {
ipsStrings := docstrings.Get("ips")
cmd := BuildCommandKS(nil, nil, ipsStrings, client, requireSession, requireAppName)
ipsListStrings := docstrings.Get("ips.list")
BuildCommandKS(cmd, runIPAddressesList, ipsListStrings, client, requireSession, requireAppName)
ipsPrivateListStrings := docstrings.Get("ips.private")
BuildCommandKS(cmd, runPrivateIPAddressesList, ipsPrivateListStrings, client, requireSession, requireAppName)
ipsAllocateV4Strings := docstrings.Get("ips.allocate-v4")
allocateV4Command := BuildCommandKS(cmd, runAllocateIPAddressV4, ipsAllocateV4Strings, client, requireSession, requireAppName)
allocateV4Command.AddStringFlag(StringFlagOpts{
Name: "region",
Description: "The region where the address should be allocated",
})
ipsAllocateV6Strings := docstrings.Get("ips.allocate-v6")
allocateV6Command := BuildCommandKS(cmd, runAllocateIPAddressV6, ipsAllocateV6Strings, client, requireSession, requireAppName)
allocateV6Command.AddStringFlag(StringFlagOpts{
Name: "region",
Description: "The region where the address should be allocated.",
})
ipsReleaseStrings := docstrings.Get("ips.release")
release := BuildCommandKS(cmd, runReleaseIPAddress, ipsReleaseStrings, client, requireSession, requireAppName)
release.Args = cobra.ExactArgs(1)
return cmd
}
func runIPAddressesList(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
ipAddresses, err := cmdCtx.Client.API().GetIPAddresses(ctx, cmdCtx.AppName)
if err != nil {
return err
}
return cmdCtx.Frender(cmdctx.PresenterOption{
Presentable: &presenters.IPAddresses{IPAddresses: ipAddresses},
})
}
func runAllocateIPAddressV4(ctx *cmdctx.CmdContext) error {
return runAllocateIPAddress(ctx, "v4")
}
func runAllocateIPAddressV6(ctx *cmdctx.CmdContext) error {
return runAllocateIPAddress(ctx, "v6")
}
func runAllocateIPAddress(cmdCtx *cmdctx.CmdContext, addrType string) error {
ctx := cmdCtx.Command.Context()
appName := cmdCtx.AppName
regionCode := cmdCtx.Config.GetString("region")
ipAddress, err := cmdCtx.Client.API().AllocateIPAddress(ctx, appName, addrType, regionCode)
if err != nil {
return err
}
return cmdCtx.Frender(cmdctx.PresenterOption{
Presentable: &presenters.IPAddresses{IPAddresses: []api.IPAddress{*ipAddress}},
})
}
func runReleaseIPAddress(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
appName := cmdCtx.AppName
address := cmdCtx.Args[0]
if ip := net.ParseIP(address); ip == nil {
return fmt.Errorf("Invalid IP address: '%s'", address)
}
ipAddress, err := cmdCtx.Client.API().FindIPAddress(ctx, appName, address)
if err != nil {
return err
}
if err := cmdCtx.Client.API().ReleaseIPAddress(ctx, ipAddress.ID); err != nil {
return err
}
fmt.Printf("Released %s from %s\n", ipAddress.Address, appName)
return nil
}
func runPrivateIPAddressesList(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
appstatus, err := cmdCtx.Client.API().GetAppStatus(ctx, cmdCtx.AppName, false)
if err != nil {
return err
}
_, backupRegions, err := cmdCtx.Client.API().ListAppRegions(ctx, cmdCtx.AppName)
if err != nil {
return err
}
if cmdCtx.OutputJSON() {
cmdCtx.WriteJSON(appstatus.Allocations)
return nil
}
table := helpers.MakeSimpleTable(cmdCtx.Out, []string{"ID", "Region", "IP"})
for _, alloc := range appstatus.Allocations {
region := alloc.Region
if len(backupRegions) > 0 {
for _, r := range backupRegions {
if alloc.Region == r.Code {
region = alloc.Region + "(B)"
break
}
}
}
table.Append([]string{alloc.IDShort, region, alloc.PrivateIP})
}
table.Render()
return nil
}