github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/spacequota/set_space_quota.go (about)

     1  package spacequota
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/spacequotas"
     8  	"code.cloudfoundry.org/cli/cf/api/spaces"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type SetSpaceQuota struct {
    18  	ui        terminal.UI
    19  	config    coreconfig.Reader
    20  	spaceRepo spaces.SpaceRepository
    21  	quotaRepo spacequotas.SpaceQuotaRepository
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&SetSpaceQuota{})
    26  }
    27  
    28  func (cmd *SetSpaceQuota) MetaData() commandregistry.CommandMetadata {
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "set-space-quota",
    31  		Description: T("Assign a space quota definition to a space"),
    32  		Usage: []string{
    33  			T("CF_NAME set-space-quota SPACE-NAME SPACE-QUOTA-NAME"),
    34  		},
    35  	}
    36  }
    37  
    38  func (cmd *SetSpaceQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    39  	if len(fc.Args()) != 2 {
    40  		cmd.ui.Failed(T("Incorrect Usage. Requires SPACE-NAME and SPACE-QUOTA-NAME as arguments\n\n") + commandregistry.Commands.CommandUsage("set-space-quota"))
    41  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    42  	}
    43  
    44  	reqs := []requirements.Requirement{
    45  		requirementsFactory.NewLoginRequirement(),
    46  		requirementsFactory.NewTargetedOrgRequirement(),
    47  	}
    48  
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *SetSpaceQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.config = deps.Config
    55  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    56  	cmd.quotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
    57  	return cmd
    58  }
    59  
    60  func (cmd *SetSpaceQuota) Execute(c flags.FlagContext) error {
    61  
    62  	spaceName := c.Args()[0]
    63  	quotaName := c.Args()[1]
    64  
    65  	cmd.ui.Say(T("Assigning space quota {{.QuotaName}} to space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    66  		"QuotaName": terminal.EntityNameColor(quotaName),
    67  		"SpaceName": terminal.EntityNameColor(spaceName),
    68  		"Username":  terminal.EntityNameColor(cmd.config.Username()),
    69  	}))
    70  
    71  	space, err := cmd.spaceRepo.FindByName(spaceName)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	if space.SpaceQuotaGUID != "" {
    77  		return errors.New(T("This space already has an assigned space quota."))
    78  	}
    79  
    80  	quota, err := cmd.quotaRepo.FindByName(quotaName)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	err = cmd.quotaRepo.AssociateSpaceWithQuota(space.GUID, quota.GUID)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	cmd.ui.Ok()
    91  	return nil
    92  }