github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/spacequota/delete_space_quota.go (about)

     1  package spacequota
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/spacequotas"
     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/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/i18n"
    15  )
    16  
    17  type DeleteSpaceQuota struct {
    18  	ui             terminal.UI
    19  	config         coreconfig.Reader
    20  	spaceQuotaRepo spacequotas.SpaceQuotaRepository
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&DeleteSpaceQuota{})
    25  }
    26  
    27  func (cmd *DeleteSpaceQuota) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force delete (do not prompt for confirmation)")}
    30  
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "delete-space-quota",
    33  		Description: T("Delete a space quota definition and unassign the space quota from all spaces"),
    34  		Usage: []string{
    35  			T("CF_NAME delete-space-quota SPACE-QUOTA-NAME [-f]"),
    36  		},
    37  		Flags: fs,
    38  	}
    39  }
    40  
    41  func (cmd *DeleteSpaceQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 1 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-space-quota"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    45  	}
    46  
    47  	reqs := []requirements.Requirement{
    48  		requirementsFactory.NewLoginRequirement(),
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *DeleteSpaceQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.spaceQuotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *DeleteSpaceQuota) Execute(c flags.FlagContext) error {
    62  	quotaName := c.Args()[0]
    63  
    64  	if !c.Bool("f") {
    65  		response := cmd.ui.ConfirmDelete("quota", quotaName)
    66  		if !response {
    67  			return nil
    68  		}
    69  	}
    70  
    71  	cmd.ui.Say(T("Deleting space quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{
    72  		"QuotaName": terminal.EntityNameColor(quotaName),
    73  		"Username":  terminal.EntityNameColor(cmd.config.Username()),
    74  	}))
    75  
    76  	quota, err := cmd.spaceQuotaRepo.FindByName(quotaName)
    77  	switch (err).(type) {
    78  	case nil: // no error
    79  	case *errors.ModelNotFoundError:
    80  		cmd.ui.Ok()
    81  		cmd.ui.Warn(T("Quota {{.QuotaName}} does not exist", map[string]interface{}{"QuotaName": quotaName}))
    82  		return nil
    83  	default:
    84  		return err
    85  	}
    86  
    87  	err = cmd.spaceQuotaRepo.Delete(quota.GUID)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	cmd.ui.Ok()
    93  	return nil
    94  }