github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/securitygroup/delete_security_group.go (about) 1 package securitygroup 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api/securitygroups" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/errors" 10 "code.cloudfoundry.org/cli/cf/flags" 11 . "code.cloudfoundry.org/cli/cf/i18n" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type DeleteSecurityGroup struct { 17 ui terminal.UI 18 securityGroupRepo securitygroups.SecurityGroupRepo 19 configRepo coreconfig.Reader 20 } 21 22 func init() { 23 commandregistry.Register(&DeleteSecurityGroup{}) 24 } 25 26 func (cmd *DeleteSecurityGroup) MetaData() commandregistry.CommandMetadata { 27 fs := make(map[string]flags.FlagSet) 28 fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")} 29 30 return commandregistry.CommandMetadata{ 31 Name: "delete-security-group", 32 Description: T("Deletes a security group"), 33 Usage: []string{ 34 T("CF_NAME delete-security-group SECURITY_GROUP [-f]"), 35 }, 36 Flags: fs, 37 } 38 } 39 40 func (cmd *DeleteSecurityGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 41 if len(fc.Args()) != 1 { 42 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-security-group")) 43 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 44 } 45 46 reqs := []requirements.Requirement{requirementsFactory.NewLoginRequirement()} 47 return reqs, nil 48 } 49 50 func (cmd *DeleteSecurityGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 51 cmd.ui = deps.UI 52 cmd.configRepo = deps.Config 53 cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository() 54 return cmd 55 } 56 57 func (cmd *DeleteSecurityGroup) Execute(context flags.FlagContext) error { 58 name := context.Args()[0] 59 cmd.ui.Say(T("Deleting security group {{.security_group}} as {{.username}}", 60 map[string]interface{}{ 61 "security_group": terminal.EntityNameColor(name), 62 "username": terminal.EntityNameColor(cmd.configRepo.Username()), 63 })) 64 65 if !context.Bool("f") { 66 response := cmd.ui.ConfirmDelete(T("security group"), name) 67 if !response { 68 return nil 69 } 70 } 71 72 group, err := cmd.securityGroupRepo.Read(name) 73 switch err.(type) { 74 case nil: 75 case *errors.ModelNotFoundError: 76 cmd.ui.Ok() 77 cmd.ui.Warn(T("Security group {{.security_group}} does not exist", map[string]interface{}{"security_group": name})) 78 return nil 79 default: 80 return err 81 } 82 83 err = cmd.securityGroupRepo.Delete(group.GUID) 84 if err != nil { 85 return err 86 } 87 88 cmd.ui.Ok() 89 return nil 90 }