forked from cloudfoundry/cli
/
delete_security_group_command.go
70 lines (56 loc) · 2.35 KB
/
delete_security_group_command.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
package v7
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/command/flag"
)
type DeleteSecurityGroupCommand struct {
BaseCommand
RequiredArgs flag.SecurityGroup `positional-args:"yes"`
Force bool `long:"force" short:"f" description:"Force deletion without confirmation"`
usage interface{} `usage:"CF_NAME delete-security-group SECURITY_GROUP [-f]\n\nTIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart (for running) or restage (for staging) to apply to existing applications."`
relatedCommands interface{} `related_commands:"security-groups"`
}
func (cmd *DeleteSecurityGroupCommand) Execute(args []string) error {
var err error
err = cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
user, err := cmd.Actor.GetCurrentUser()
if err != nil {
return err
}
if !cmd.Force {
promptMessage := "Really delete the security group {{.securityGroup}}?"
deleteSecurityGroup, promptErr := cmd.UI.DisplayBoolPrompt(false, promptMessage, map[string]interface{}{"securityGroup": cmd.RequiredArgs.SecurityGroup})
if promptErr != nil {
return promptErr
}
if !deleteSecurityGroup {
cmd.UI.DisplayText("Security group '{{.securityGroup}}' has not been deleted.", map[string]interface{}{
"securityGroup": cmd.RequiredArgs.SecurityGroup,
})
return nil
}
}
cmd.UI.DisplayTextWithFlavor("Deleting security group {{.securityGroup}} as {{.username}}...", map[string]interface{}{
"securityGroup": cmd.RequiredArgs.SecurityGroup,
"username": user.Name,
})
warnings, err := cmd.Actor.DeleteSecurityGroup(cmd.RequiredArgs.SecurityGroup)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
switch err.(type) {
case actionerror.SecurityGroupNotFoundError:
cmd.UI.DisplayWarning("Security group '{{.securityGroup}}' does not exist.", map[string]interface{}{
"securityGroup": cmd.RequiredArgs.SecurityGroup,
})
default:
return err
}
}
cmd.UI.DisplayOK()
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart (for running) or restage (for staging) to apply to existing applications.")
return nil
}