-
Notifications
You must be signed in to change notification settings - Fork 20
/
privnet_associate.go
98 lines (85 loc) · 2.42 KB
/
privnet_associate.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
package cmd
import (
"fmt"
"net"
"os"
"text/tabwriter"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var privnetAssociateCmd = &cobra.Command{
Use: "associate NETWORK-NAME|ID INSTANCE-NAME|ID [IP-ADDRESS]",
Short: "Associate a Private Network to a Compute instance",
Aliases: gAssociateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
network, err := getNetwork(args[0], nil)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
dhcp := dhcpRange(*network)
fmt.Fprintf(w, "Network:\t%s\n", network.Name) // nolint: errcheck
fmt.Fprintf(w, "Description:\t%s\n", network.DisplayText) // nolint: errcheck
fmt.Fprintf(w, "Zone:\t%s\n", network.ZoneName) // nolint: errcheck
fmt.Fprintf(w, "IP Range:\t%s\n", dhcp) // nolint: errcheck
// FIXME: this implementation mixes side effects with user reporting,
// this is not great and should be split.
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Compute instance", "IP Address"})
for i := 1; i < len(args); i++ {
name := args[i]
if i != len(args)-1 {
ip := net.ParseIP(args[i+1])
if ip != nil {
// the next param is an ip
nic, vm, err := associatePrivNet(network, name, ip)
if err != nil {
return err
}
table.Append([]string{
vm.DisplayName,
nicIP(*nic),
})
i = i + 1
continue
}
}
nic, vm, err := associatePrivNet(network, name, nil)
if err != nil {
return err
}
table.Append([]string{
vm.DisplayName,
nicIP(*nic),
})
}
w.Flush() // nolint: errcheck
if !gQuiet {
table.Render()
}
return nil
},
}
func associatePrivNet(privnet *egoscale.Network, vmName string, ip net.IP) (*egoscale.Nic, *egoscale.VirtualMachine, error) {
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return nil, nil, err
}
req := &egoscale.AddNicToVirtualMachine{NetworkID: privnet.ID, VirtualMachineID: vm.ID, IPAddress: ip}
resp, err := cs.RequestWithContext(gContext, req)
if err != nil {
return nil, nil, err
}
nic := resp.(*egoscale.VirtualMachine).NicByNetworkID(*privnet.ID)
if nic == nil {
return nil, nil, fmt.Errorf("no NIC found for network %q", privnet.ID)
}
return nic, vm, nil
}
func init() {
privnetCmd.AddCommand(privnetAssociateCmd)
}