github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/delete_quota.go (about)

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