code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/delete_security_group_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/command/flag"
     6  )
     7  
     8  type DeleteSecurityGroupCommand struct {
     9  	BaseCommand
    10  
    11  	RequiredArgs    flag.SecurityGroup `positional-args:"yes"`
    12  	Force           bool               `long:"force" short:"f" description:"Force deletion without confirmation"`
    13  	usage           interface{}        `usage:"CF_NAME delete-security-group SECURITY_GROUP [-f]\n\nTIP: Changes require an app restart (for running) or restage (for staging) to apply to existing applications."`
    14  	relatedCommands interface{}        `related_commands:"security-groups"`
    15  }
    16  
    17  func (cmd *DeleteSecurityGroupCommand) Execute(args []string) error {
    18  	var err error
    19  
    20  	err = cmd.SharedActor.CheckTarget(false, false)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	user, err := cmd.Config.CurrentUser()
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	if !cmd.Force {
    31  		promptMessage := "Really delete the security group {{.securityGroup}}?"
    32  		deleteSecurityGroup, promptErr := cmd.UI.DisplayBoolPrompt(false, promptMessage, map[string]interface{}{"securityGroup": cmd.RequiredArgs.SecurityGroup})
    33  
    34  		if promptErr != nil {
    35  			return promptErr
    36  		}
    37  
    38  		if !deleteSecurityGroup {
    39  			cmd.UI.DisplayText("Security group '{{.securityGroup}}' has not been deleted.", map[string]interface{}{
    40  				"securityGroup": cmd.RequiredArgs.SecurityGroup,
    41  			})
    42  			return nil
    43  		}
    44  	}
    45  
    46  	cmd.UI.DisplayTextWithFlavor("Deleting security group {{.securityGroup}} as {{.username}}...", map[string]interface{}{
    47  		"securityGroup": cmd.RequiredArgs.SecurityGroup,
    48  		"username":      user.Name,
    49  	})
    50  
    51  	warnings, err := cmd.Actor.DeleteSecurityGroup(cmd.RequiredArgs.SecurityGroup)
    52  	cmd.UI.DisplayWarnings(warnings)
    53  	if err != nil {
    54  		switch err.(type) {
    55  		case actionerror.SecurityGroupNotFoundError:
    56  			cmd.UI.DisplayWarning("Security group '{{.securityGroup}}' does not exist.", map[string]interface{}{
    57  				"securityGroup": cmd.RequiredArgs.SecurityGroup,
    58  			})
    59  		default:
    60  			return err
    61  		}
    62  	}
    63  
    64  	cmd.UI.DisplayOK()
    65  	cmd.UI.DisplayNewline()
    66  
    67  	cmd.UI.DisplayText("TIP: Changes require an app restart (for running) or restage (for staging) to apply to existing applications.")
    68  
    69  	return nil
    70  }