-
Notifications
You must be signed in to change notification settings - Fork 19
/
instance_pool_delete.go
90 lines (75 loc) · 2 KB
/
instance_pool_delete.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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var instancePoolDeleteCmd = &cobra.Command{
Use: "delete NAME|ID",
Short: "Delete an Instance Pool",
Aliases: gDeleteAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
zoneName, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone, err := getZoneByNameOrID(zoneName)
if err != nil {
return err
}
// Ensure the Instance Pool is not attached to a NLB service.
nlbs, err := cs.ListNetworkLoadBalancers(gContext, zone.Name)
if err != nil {
return fmt.Errorf("unable to list Network Load Balancers: %v", err)
}
tasks := make([]task, 0, len(args))
for _, arg := range args {
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete Instance Pool %q?", arg)) {
continue
}
}
i, err := getInstancePoolByNameOrID(arg, zone.ID)
if err != nil {
return err
}
for _, nlb := range nlbs {
for _, svc := range nlb.Services {
if svc.InstancePoolID == i.ID.String() {
return fmt.Errorf("Instance Pool %q is still referenced by NLB service %s/%s", // nolint
i.Name, nlb.Name, svc.Name)
}
}
}
tasks = append(tasks, task{
egoscale.DestroyInstancePool{
ID: i.ID,
ZoneID: zone.ID,
},
fmt.Sprintf("Deleting Instance Pool %q", args[0]),
})
}
r := asyncTasks(tasks)
errs := filterErrors(r)
if len(errs) > 0 {
return errs[0]
}
return nil
},
}
func init() {
instancePoolDeleteCmd.Flags().StringP("zone", "z", "", "Instance pool zone")
instancePoolDeleteCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
instancePoolCmd.AddCommand(instancePoolDeleteCmd)
}