-
Notifications
You must be signed in to change notification settings - Fork 233
/
ips.go
133 lines (98 loc) · 3.56 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
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")
BuildCommandKS(cmd, runAllocateIPAddressV4, ipsAllocateV4Strings, client, requireSession, requireAppName)
ipsAllocateV6Strings := docstrings.Get("ips.allocate-v6")
BuildCommandKS(cmd, runAllocateIPAddressV6, ipsAllocateV6Strings, client, requireSession, requireAppName)
ipsReleaseStrings := docstrings.Get("ips.release")
release := BuildCommandKS(cmd, runReleaseIPAddress, ipsReleaseStrings, client, requireSession, requireAppName)
release.Args = cobra.ExactArgs(1)
return cmd
}
func runIPAddressesList(commandContext *cmdctx.CmdContext) error {
ipAddresses, err := commandContext.Client.API().GetIPAddresses(commandContext.AppName)
if err != nil {
return err
}
return commandContext.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(commandContext *cmdctx.CmdContext, addrType string) error {
appName := commandContext.AppName
ipAddress, err := commandContext.Client.API().AllocateIPAddress(appName, addrType)
if err != nil {
return err
}
return commandContext.Frender(cmdctx.PresenterOption{
Presentable: &presenters.IPAddresses{IPAddresses: []api.IPAddress{*ipAddress}},
})
}
func runReleaseIPAddress(commandContext *cmdctx.CmdContext) error {
appName := commandContext.AppName
address := commandContext.Args[0]
if ip := net.ParseIP(address); ip == nil {
return fmt.Errorf("Invalid IP address: '%s'", address)
}
ipAddress, err := commandContext.Client.API().FindIPAddress(appName, address)
if err != nil {
return err
}
if err := commandContext.Client.API().ReleaseIPAddress(ipAddress.ID); err != nil {
return err
}
fmt.Printf("Released %s from %s\n", ipAddress.Address, appName)
return nil
}
func runPrivateIPAddressesList(commandContext *cmdctx.CmdContext) error {
appstatus, err := commandContext.Client.API().GetAppStatus(commandContext.AppName, false)
if err != nil {
return err
}
_, backupRegions, err := commandContext.Client.API().ListAppRegions(commandContext.AppName)
if err != nil {
return err
}
if commandContext.OutputJSON() {
commandContext.WriteJSON(appstatus.Allocations)
return nil
}
table := helpers.MakeSimpleTable(commandContext.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
}