-
Notifications
You must be signed in to change notification settings - Fork 6
/
ip.go
210 lines (185 loc) · 6.58 KB
/
ip.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cmd
import (
"fmt"
"github.com/fi-ts/cloud-go/api/client/ip"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/fi-ts/cloud-go/api/models"
"github.com/fi-ts/cloudctl/cmd/helper"
"github.com/fi-ts/cloudctl/cmd/output"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newIPCmd(c *config) *cobra.Command {
ipCmd := &cobra.Command{
Use: "ip",
Short: "manage ips",
Long: "TODO",
}
ipListCmd := &cobra.Command{
Use: "list",
Short: "list ips",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
return c.ipList()
},
PreRun: bindPFlags,
}
ipStaticCmd := &cobra.Command{
Use: "static <ip>",
Short: "make an ephemeral ip static such that it won't be deleted if not used anymore",
RunE: func(cmd *cobra.Command, args []string) error {
return c.ipStatic(args)
},
PreRun: bindPFlags,
}
ipAllocateCmd := &cobra.Command{
Use: "allocate <ip>",
Short: "allocate a static IP address for your project that can be used for your cluster's service type load balancer",
RunE: func(cmd *cobra.Command, args []string) error {
return c.ipAllocate()
},
PreRun: bindPFlags,
}
ipFreeCmd := &cobra.Command{
Use: "delete <ip>",
Aliases: []string{"destroy", "rm", "remove", "free"},
Short: "delete an ip",
RunE: func(cmd *cobra.Command, args []string) error {
return c.ipFree(args)
},
PreRun: bindPFlags,
}
ipCmd.AddCommand(ipListCmd)
ipCmd.AddCommand(ipStaticCmd)
ipCmd.AddCommand(ipFreeCmd)
ipCmd.AddCommand(ipAllocateCmd)
ipListCmd.Flags().StringP("ipaddress", "", "", "ipaddress to filter [optional]")
ipListCmd.Flags().StringP("project", "", "", "project to filter [optional]")
ipListCmd.Flags().StringP("prefix", "", "", "prefix to filter [optional]")
ipListCmd.Flags().StringP("machineid", "", "", "machineid to filter [optional]")
ipListCmd.Flags().StringP("network", "", "", "network to filter [optional]")
must(ipListCmd.RegisterFlagCompletionFunc("project", c.comp.ProjectListCompletion))
must(ipListCmd.RegisterFlagCompletionFunc("network", c.comp.NetworkListCompletion))
ipStaticCmd.Flags().StringP("name", "", "", "set name of the ip address [required]")
ipStaticCmd.Flags().StringP("description", "", "", "set description of the ip address [required]")
must(ipStaticCmd.MarkFlagRequired("name"))
must(ipStaticCmd.MarkFlagRequired("description"))
ipAllocateCmd.Flags().StringP("name", "", "", "set name of the ip address [required]")
ipAllocateCmd.Flags().StringP("description", "", "", "set description of the ip address [required]")
ipAllocateCmd.Flags().StringP("specific-ip", "", "", "try allocating a specific ip address from a network [optional]")
ipAllocateCmd.Flags().StringP("network", "", "", "the network of the ip address [required]")
ipAllocateCmd.Flags().StringP("project", "", "", "the project of the ip address [required]")
ipAllocateCmd.Flags().StringSliceP("tags", "", []string{}, "set tags of the ip address [optional]")
must(ipAllocateCmd.MarkFlagRequired("name"))
must(ipAllocateCmd.MarkFlagRequired("description"))
must(ipAllocateCmd.MarkFlagRequired("network"))
must(ipAllocateCmd.MarkFlagRequired("project"))
must(ipAllocateCmd.RegisterFlagCompletionFunc("project", c.comp.ProjectListCompletion))
return ipCmd
}
func (c *config) ipList() error {
if helper.AtLeastOneViperStringFlagGiven("ipaddress", "project", "prefix", "machineid", "network") {
params := ip.NewFindIPsParams()
ifr := &models.V1IPFindRequest{
Ipaddress: pointer.SafeDeref(helper.ViperString("ipaddress")),
Projectid: pointer.SafeDeref(helper.ViperString("project")),
Networkprefix: pointer.SafeDeref(helper.ViperString("prefix")),
Networkid: pointer.SafeDeref(helper.ViperString("network")),
Machineid: pointer.SafeDeref(helper.ViperString("machineid")),
}
params.SetBody(ifr)
resp, err := c.cloud.IP.FindIPs(params, nil)
if err != nil {
return err
}
return output.New().Print(resp.Payload)
}
resp, err := c.cloud.IP.ListIPs(nil, nil)
if err != nil {
return err
}
return output.New().Print(resp.Payload)
}
func (c *config) ipStatic(args []string) error {
ipAddress, err := c.getIPFromArgs(args)
if err != nil {
return err
}
params := ip.NewUpdateIPParams()
iur := &models.V1IPUpdateRequest{
Ipaddress: &ipAddress,
Type: pointer.Pointer("static"),
}
if helper.ViperString("name") != nil {
iur.Name = *helper.ViperString("name")
}
if helper.ViperString("description") != nil {
iur.Description = *helper.ViperString("description")
}
if !viper.GetBool("yes-i-really-mean-it") {
fmt.Println("Turning an IP from ephemeral to static is irreversible. The IP address is not cleaned up automatically on cluster deletion. The address will be accounted until the IP address gets freed manually from your side.")
err = helper.Prompt("Are you sure? (y/n)", "y")
if err != nil {
return err
}
}
params.SetBody(iur)
resp, err := c.cloud.IP.UpdateIP(params, nil)
if err != nil {
return err
}
return output.New().Print(resp.Payload)
}
func (c *config) ipAllocate() error {
params := ip.NewAllocateIPParams()
iar := &models.V1IPAllocateRequest{
Name: *helper.ViperString("name"),
Description: *helper.ViperString("description"),
Type: pointer.Pointer("static"),
Networkid: helper.ViperString("network"),
Projectid: helper.ViperString("project"),
Tags: helper.ViperStringSlice("tags"),
}
if helper.ViperString("specific-ip") != nil {
iar.SpecificIP = helper.ViperString("specific-ip")
}
if !viper.GetBool("yes-i-really-mean-it") {
fmt.Println("Allocating a static IP address costs additional money because addresses are limited. The IP address is not cleaned up automatically on cluster deletion. The address will be accounted until the IP address gets freed manually from your side.")
err := helper.Prompt("Are you sure? (y/n)", "y")
if err != nil {
return err
}
}
params.SetBody(iar)
resp, err := c.cloud.IP.AllocateIP(params, nil)
if err != nil {
return err
}
return output.New().Print(resp.Payload)
}
func (c *config) ipFree(args []string) error {
ipAddress, err := c.getIPFromArgs(args)
if err != nil {
return err
}
params := ip.NewFreeIPParams()
params.SetIP(ipAddress)
resp, err := c.cloud.IP.FreeIP(params, nil)
if err != nil {
return err
}
return output.New().Print(resp.Payload)
}
func (c *config) getIPFromArgs(args []string) (string, error) {
if len(args) < 1 {
return "", fmt.Errorf("no ip given")
}
ipAddress := args[0]
params := ip.NewGetIPParams()
params.SetIP(ipAddress)
_, err := c.cloud.IP.GetIP(params, nil)
if err != nil {
return "", err
}
return ipAddress, nil
}