-
Notifications
You must be signed in to change notification settings - Fork 18
/
vm_update_ip.go
111 lines (92 loc) · 2.7 KB
/
vm_update_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
package cmd
import (
"fmt"
"os"
"text/tabwriter"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmUpdateIPCmd = &cobra.Command{
Use: "updateip <vm name|id> <network name|id> [flags]",
Short: "Update the static DHCP lease of an instance",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return cmd.Usage()
}
vmName := args[0]
netName := args[1]
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return err
}
network, err := getNetwork(netName, nil)
if err != nil {
return err
}
nic := vm.NicByNetworkID(*network.ID)
if nic == nil {
return fmt.Errorf("the virtual machine %s is not associated to the network %s", vm.DisplayName, network.Name)
}
ipAddress, err := getIPValue(cmd, "ip")
if err != nil {
return err
}
var msg string
if ipAddress.IP != nil {
msg = fmt.Sprintf("setting the static lease to %q, network %q: %s", vmName, netName, ipAddress.IP.String())
} else {
msg = fmt.Sprintf("removing the static lease from %q, network %q", vmName, netName)
}
req := &egoscale.UpdateVMNicIP{
IPAddress: ipAddress.Value(),
NicID: nic.ID,
}
resp, err := asyncRequest(req, msg)
if err != nil {
return err
}
return showVMWithNics(resp.(*egoscale.VirtualMachine))
},
}
func showVMWithNics(vm *egoscale.VirtualMachine) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
fmt.Fprintf(w, "\nInstance ID:\t%s\n", vm.ID) // nolint: errcheck
fmt.Fprintf(w, "Name:\t%s\n", vm.DisplayName) // nolint: errcheck
if vm.DisplayName != vm.Name {
fmt.Fprintf(w, "Hostname:\t%s\n", vm.Name) // nolint: errcheck
}
fmt.Fprintf(w, "Network Interfaces:\n") // nolint: errcheck
defaultNic := vm.DefaultNic()
if defaultNic != nil {
fmt.Fprintf(w, "-\tNetwork:\tPublic\n") // nolint: errcheck
fmt.Fprintf(w, " \tIP Address:\t%s\n", defaultNic.IPAddress.String()) // nolint: errcheck
}
for _, nic := range vm.Nic {
if nic.IsDefault {
} else {
resp, err := cs.GetWithContext(gContext, &egoscale.Network{
ID: nic.NetworkID,
})
if err != nil {
return err
}
network := resp.(*egoscale.Network)
networkName := network.Name
if networkName == "" {
networkName = network.ID.String()
}
fmt.Fprintf(w, "-\tNetwork:\t%s\n", networkName)
if network.Name == "" {
fmt.Fprintf(w, " \tID:\t%s\n", network.ID.String())
}
fmt.Fprintf(w, " \tIP Address:\t%s\n", nicIP(nic))
}
}
fmt.Fprintln(w) // nolint: errcheck
return w.Flush()
}
func init() {
ipAddress := new(ipValue)
vmUpdateIPCmd.Flags().VarP(ipAddress, "ip", "i", "the static IP address lease, no values unsets it.")
vmCmd.AddCommand(vmUpdateIPCmd)
}