github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/set_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 SetSpaceQuota struct { 15 ui terminal.UI 16 config core_config.Reader 17 spaceRepo spaces.SpaceRepository 18 quotaRepo space_quotas.SpaceQuotaRepository 19 } 20 21 func init() { 22 command_registry.Register(&SetSpaceQuota{}) 23 } 24 25 func (cmd *SetSpaceQuota) MetaData() command_registry.CommandMetadata { 26 return command_registry.CommandMetadata{ 27 Name: "set-space-quota", 28 Description: T("Assign a space quota definition to a space"), 29 Usage: T("CF_NAME set-space-quota SPACE-NAME SPACE-QUOTA-NAME"), 30 } 31 } 32 33 func (cmd *SetSpaceQuota) 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-NAME and SPACE-QUOTA-NAME as arguments\n\n") + command_registry.Commands.CommandUsage("set-space-quota")) 36 } 37 38 return []requirements.Requirement{ 39 requirementsFactory.NewLoginRequirement(), 40 requirementsFactory.NewTargetedOrgRequirement(), 41 }, nil 42 } 43 44 func (cmd *SetSpaceQuota) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command { 45 cmd.ui = deps.Ui 46 cmd.config = deps.Config 47 cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository() 48 cmd.quotaRepo = deps.RepoLocator.GetSpaceQuotaRepository() 49 return cmd 50 } 51 52 func (cmd *SetSpaceQuota) Execute(c flags.FlagContext) { 53 54 spaceName := c.Args()[0] 55 quotaName := c.Args()[1] 56 57 cmd.ui.Say(T("Assigning space quota {{.QuotaName}} to space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ 58 "QuotaName": terminal.EntityNameColor(quotaName), 59 "SpaceName": terminal.EntityNameColor(spaceName), 60 "Username": terminal.EntityNameColor(cmd.config.Username()), 61 })) 62 63 space, err := cmd.spaceRepo.FindByName(spaceName) 64 if err != nil { 65 cmd.ui.Failed(err.Error()) 66 } 67 68 if space.SpaceQuotaGuid != "" { 69 cmd.ui.Failed(T("This space already has an assigned space quota.")) 70 } 71 72 quota, err := cmd.quotaRepo.FindByName(quotaName) 73 if err != nil { 74 cmd.ui.Failed(err.Error()) 75 } 76 77 err = cmd.quotaRepo.AssociateSpaceWithQuota(space.Guid, quota.Guid) 78 if err != nil { 79 cmd.ui.Failed(err.Error()) 80 } 81 82 cmd.ui.Ok() 83 }