-
Notifications
You must be signed in to change notification settings - Fork 19
/
eip_associate.go
62 lines (49 loc) · 1.24 KB
/
eip_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
package cmd
import (
"fmt"
"net"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// associateCmd represents the associate command
var eipAssociateCmd = &cobra.Command{
Use: "associate <IP address> <instance name | instance id> [instance name | instance id] [...]",
Short: "Associate an IP to instance(s)",
Aliases: gAssociateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
for _, arg := range args[1:] {
res, err := associateIP(args[0], arg)
if err != nil {
return err
}
println(res)
}
return nil
},
}
func associateIP(ipAddr, instance string) (*egoscale.UUID, error) {
ip := net.ParseIP(ipAddr)
if ip == nil {
return nil, fmt.Errorf("invalid IP address %q", ipAddr)
}
vm, err := getVMWithNameOrID(instance)
if err != nil {
return nil, err
}
defaultNic := vm.DefaultNic()
if defaultNic == nil {
return nil, fmt.Errorf("the instance %q has not default NIC", vm.ID)
}
resp, err := cs.RequestWithContext(gContext, &egoscale.AddIPToNic{NicID: defaultNic.ID, IPAddress: ip})
if err != nil {
return nil, err
}
result := resp.(*egoscale.NicSecondaryIP)
return result.NicID, nil
}
func init() {
eipCmd.AddCommand(eipAssociateCmd)
}