-
Notifications
You must be signed in to change notification settings - Fork 20
/
security_group_rule_delete.go
77 lines (59 loc) · 2.02 KB
/
security_group_rule_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
package cmd
import (
"fmt"
"strings"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type securityGroupDeleteRuleCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"delete"`
SecurityGroup string `cli-arg:"#" cli-usage:"SECURITY-GROUP-ID|NAME"`
Rule string `cli-arg:"#"`
Force bool `cli-short:"f" cli-usage:"don't prompt for confirmation"`
}
func (c *securityGroupDeleteRuleCmd) cmdAliases() []string { return gRemoveAlias }
func (c *securityGroupDeleteRuleCmd) cmdShort() string {
return "Delete a Security Group rule"
}
func (c *securityGroupDeleteRuleCmd) cmdLong() string {
return fmt.Sprintf(`This command deletes a rule from a Compute instance Security Group.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&securityGroupShowOutput{}), ", "))
}
func (c *securityGroupDeleteRuleCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *securityGroupDeleteRuleCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := gCurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
securityGroup, err := cs.FindSecurityGroup(ctx, zone, c.SecurityGroup)
if err != nil {
return err
}
if !c.Force {
if !askQuestion(fmt.Sprintf(
"Are you sure you want to delete rule %s from Security Group %q?",
c.Rule,
*securityGroup.Name,
)) {
return nil
}
}
decorateAsyncOperation(fmt.Sprintf("Deleting Security Group rule %s...", c.Rule), func() {
err = cs.DeleteSecurityGroupRule(ctx, zone, securityGroup, &egoscale.SecurityGroupRule{ID: &c.Rule})
})
if err != nil {
return err
}
return (&securityGroupShowCmd{
cliCommandSettings: c.cliCommandSettings,
SecurityGroup: *securityGroup.ID,
}).cmdRun(nil, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(securityGroupRuleCmd, &securityGroupDeleteRuleCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}