-
Notifications
You must be signed in to change notification settings - Fork 18
/
nlb_service_delete.go
69 lines (52 loc) · 1.8 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
package cmd
import (
"errors"
"fmt"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type nlbServiceDeleteCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"delete"`
NetworkLoadBalancer string `cli-arg:"#" cli-usage:"LOAD-BALANCER-NAME|ID"`
Service string `cli-arg:"#" cli-usage:"SERVICE-NAME|ID"`
Force bool `cli-short:"f" cli-usage:"don't prompt for confirmation"`
Zone string `cli-short:"z" cli-usage:"Network Load Balancer zone"`
}
func (c *nlbServiceDeleteCmd) cmdAliases() []string { return gRemoveAlias }
func (c *nlbServiceDeleteCmd) cmdShort() string { return "Delete a Network Load Balancer service" }
func (c *nlbServiceDeleteCmd) cmdLong() string { return "" }
func (c *nlbServiceDeleteCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *nlbServiceDeleteCmd) cmdRun(_ *cobra.Command, _ []string) error {
if !c.Force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete service %q?", c.Service)) {
return nil
}
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
nlb, err := cs.FindNetworkLoadBalancer(ctx, c.Zone, c.NetworkLoadBalancer)
if err != nil {
return err
}
for _, service := range nlb.Services {
if *service.ID == c.Service || *service.Name == c.Service {
s := service
decorateAsyncOperation(fmt.Sprintf("Deleting service %q...", c.Service), func() {
err = cs.DeleteNetworkLoadBalancerService(ctx, c.Zone, nlb, s)
})
if err != nil {
return err
}
return nil
}
}
return errors.New("service not found")
}
func init() {
cobra.CheckErr(registerCLICommand(nlbServiceCmd, &nlbServiceDeleteCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}