-
Notifications
You must be signed in to change notification settings - Fork 19
/
privnet_dissociate.go
57 lines (47 loc) · 1.21 KB
/
privnet_dissociate.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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// dissociateCmd represents the dissociate command
var dissociateCmd = &cobra.Command{
Use: "dissociate <privnet name | id> <vm name | vm id> [vm name | vm id] [...]",
Short: "Dissociate a private network from instance(s)",
Aliases: gDissociateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
network, err := getNetworkByName(args[0])
if err != nil {
return err
}
for _, vm := range args[1:] {
resp, err := dissociatePrivNet(network, vm)
if err != nil {
return err
}
fmt.Println(resp)
}
return nil
},
}
func dissociatePrivNet(privnet *egoscale.Network, vmName string) (*egoscale.UUID, error) {
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return nil, err
}
nic := vm.NicByNetworkID(*privnet.ID)
if nic == nil {
return nil, fmt.Errorf("no nics found for network %q", privnet.ID)
}
_, err = cs.RequestWithContext(gContext, &egoscale.RemoveNicFromVirtualMachine{NicID: nic.ID, VirtualMachineID: vm.ID})
if err != nil {
return nil, err
}
return nic.ID, nil
}
func init() {
privnetCmd.AddCommand(dissociateCmd)
}