-
Notifications
You must be signed in to change notification settings - Fork 18
/
nlb_service_delete.go
74 lines (60 loc) · 1.62 KB
/
nlb_service_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
package cmd
import (
"errors"
"fmt"
apiv2 "github.com/exoscale/egoscale/api/v2"
"github.com/spf13/cobra"
)
var nlbServiceDeleteCmd = &cobra.Command{
Use: "delete <NLB name | ID> <service name | ID>",
Short: "Delete a Network Load Balancer service",
Aliases: gRemoveAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
nlbRef = args[0]
svcRef = args[1]
)
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
if !askQuestion(fmt.Sprintf("Do you really want to delete service %q?", args[1])) {
return nil
}
}
ctx := apiv2.WithEndpoint(gContext, apiv2.NewReqEndpoint(gCurrentAccount.Environment, zone))
nlb, err := lookupNLB(ctx, zone, nlbRef)
if err != nil {
return err
}
for _, svc := range nlb.Services {
if svc.ID == svcRef || svc.Name == svcRef {
if err := nlb.DeleteService(ctx, svc); err != nil {
return err
}
if !gQuiet {
cmd.Println("Service deleted successfully")
}
return nil
}
}
return errors.New("service not found")
},
}
func init() {
nlbServiceDeleteCmd.Flags().BoolP("force", "f", false, "Attempt to delete without prompting for confirmation")
nlbServiceDeleteCmd.Flags().StringP("zone", "z", "", "Network Load Balancer zone")
nlbServiceCmd.AddCommand(nlbServiceDeleteCmd)
}