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

     1  package spacequota
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/space_quotas"
     5  	"github.com/cloudfoundry/cli/cf/api/spaces"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  	"github.com/cloudfoundry/cli/cf/requirements"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  	"github.com/cloudfoundry/cli/flags"
    12  )
    13  
    14  type UnsetSpaceQuota struct {
    15  	ui        terminal.UI
    16  	config    core_config.Reader
    17  	quotaRepo space_quotas.SpaceQuotaRepository
    18  	spaceRepo spaces.SpaceRepository
    19  }
    20  
    21  func init() {
    22  	command_registry.Register(&UnsetSpaceQuota{})
    23  }
    24  
    25  func (cmd *UnsetSpaceQuota) MetaData() command_registry.CommandMetadata {
    26  	return command_registry.CommandMetadata{
    27  		Name:        "unset-space-quota",
    28  		Description: T("Unassign a quota from a space"),
    29  		Usage:       T("CF_NAME unset-space-quota SPACE QUOTA\n\n"),
    30  	}
    31  }
    32  
    33  func (cmd *UnsetSpaceQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    34  	if len(fc.Args()) != 2 {
    35  		cmd.ui.Failed(T("Incorrect Usage. Requires SPACE and QUOTA as arguments\n\n") + command_registry.Commands.CommandUsage("unset-space-quota"))
    36  	}
    37  
    38  	reqs = []requirements.Requirement{
    39  		requirementsFactory.NewLoginRequirement(),
    40  		requirementsFactory.NewTargetedOrgRequirement(),
    41  	}
    42  	return
    43  }
    44  
    45  func (cmd *UnsetSpaceQuota) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    46  	cmd.ui = deps.Ui
    47  	cmd.config = deps.Config
    48  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    49  	cmd.quotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
    50  	return cmd
    51  }
    52  
    53  func (cmd *UnsetSpaceQuota) Execute(c flags.FlagContext) {
    54  	spaceName := c.Args()[0]
    55  	quotaName := c.Args()[1]
    56  
    57  	space, apiErr := cmd.spaceRepo.FindByName(spaceName)
    58  	if apiErr != nil {
    59  		cmd.ui.Failed(apiErr.Error())
    60  		return
    61  	}
    62  
    63  	quota, apiErr := cmd.quotaRepo.FindByName(quotaName)
    64  	if apiErr != nil {
    65  		cmd.ui.Failed(apiErr.Error())
    66  		return
    67  	}
    68  
    69  	cmd.ui.Say(T("Unassigning space quota {{.QuotaName}} from space {{.SpaceName}} as {{.Username}}...",
    70  		map[string]interface{}{
    71  			"QuotaName": terminal.EntityNameColor(quota.Name),
    72  			"SpaceName": terminal.EntityNameColor(space.Name),
    73  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    74  
    75  	apiErr = cmd.quotaRepo.UnassignQuotaFromSpace(space.Guid, quota.Guid)
    76  	if apiErr != nil {
    77  		cmd.ui.Failed(apiErr.Error())
    78  		return
    79  	}
    80  
    81  	cmd.ui.Ok()
    82  }