github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/organization/set_quota.go (about)

     1  package organization
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/quotas"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  type SetQuota struct {
    16  	ui        terminal.UI
    17  	config    coreconfig.Reader
    18  	quotaRepo quotas.QuotaRepository
    19  	orgReq    requirements.OrganizationRequirement
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&SetQuota{})
    24  }
    25  
    26  func (cmd *SetQuota) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "set-quota",
    29  		Description: T("Assign a quota to an org"),
    30  		Usage: []string{
    31  			T("CF_NAME set-quota ORG QUOTA\n\n"),
    32  			T("TIP:\n"),
    33  			T("   View allowable quotas with 'CF_NAME quotas'"),
    34  		},
    35  	}
    36  }
    37  
    38  func (cmd *SetQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    39  	if len(fc.Args()) != 2 {
    40  		cmd.ui.Failed(T("Incorrect Usage. Requires ORG_NAME, QUOTA as arguments\n\n") + commandregistry.Commands.CommandUsage("set-quota"))
    41  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    42  	}
    43  
    44  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[0])
    45  
    46  	reqs := []requirements.Requirement{
    47  		requirementsFactory.NewLoginRequirement(),
    48  		cmd.orgReq,
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *SetQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *SetQuota) Execute(c flags.FlagContext) error {
    62  	org := cmd.orgReq.GetOrganization()
    63  	quotaName := c.Args()[1]
    64  	quota, err := cmd.quotaRepo.FindByName(quotaName)
    65  
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	cmd.ui.Say(T("Setting quota {{.QuotaName}} to org {{.OrgName}} as {{.Username}}...",
    71  		map[string]interface{}{
    72  			"QuotaName": terminal.EntityNameColor(quota.Name),
    73  			"OrgName":   terminal.EntityNameColor(org.Name),
    74  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    75  
    76  	err = cmd.quotaRepo.AssignQuotaToOrg(org.GUID, quota.GUID)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	cmd.ui.Ok()
    82  	return nil
    83  }