github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/spacequota/update_space_quota.go (about) 1 package spacequota 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "strconv" 8 9 "code.cloudfoundry.org/cli/cf" 10 "code.cloudfoundry.org/cli/cf/api/spacequotas" 11 "code.cloudfoundry.org/cli/cf/commandregistry" 12 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 13 "code.cloudfoundry.org/cli/cf/flags" 14 "code.cloudfoundry.org/cli/cf/formatters" 15 . "code.cloudfoundry.org/cli/cf/i18n" 16 "code.cloudfoundry.org/cli/cf/requirements" 17 "code.cloudfoundry.org/cli/cf/terminal" 18 ) 19 20 type UpdateSpaceQuota struct { 21 ui terminal.UI 22 config coreconfig.Reader 23 spaceQuotaRepo spacequotas.SpaceQuotaRepository 24 } 25 26 func init() { 27 commandregistry.Register(&UpdateSpaceQuota{}) 28 } 29 30 func (cmd *UpdateSpaceQuota) MetaData() commandregistry.CommandMetadata { 31 fs := make(map[string]flags.FlagSet) 32 fs["i"] = &flags.StringFlag{ShortName: "i", Usage: T("Maximum amount of memory an application instance can have (e.g. 1024M, 1G, 10G). -1 represents an unlimited amount.")} 33 fs["m"] = &flags.StringFlag{ShortName: "m", Usage: T("Total amount of memory a space can have (e.g. 1024M, 1G, 10G)")} 34 fs["n"] = &flags.StringFlag{ShortName: "n", Usage: T("New name")} 35 fs["r"] = &flags.IntFlag{ShortName: "r", Usage: T("Total number of routes")} 36 fs["s"] = &flags.IntFlag{ShortName: "s", Usage: T("Total number of service instances")} 37 fs["allow-paid-service-plans"] = &flags.BoolFlag{Name: "allow-paid-service-plans", Usage: T("Can provision instances of paid service plans")} 38 fs["disallow-paid-service-plans"] = &flags.BoolFlag{Name: "disallow-paid-service-plans", Usage: T("Can not provision instances of paid service plans")} 39 fs["a"] = &flags.IntFlag{ShortName: "a", Usage: T("Total number of application instances. -1 represents an unlimited amount.")} 40 fs["reserved-route-ports"] = &flags.IntFlag{Name: "reserved-route-ports", Usage: T("Maximum number of routes that may be created with reserved ports")} 41 42 return commandregistry.CommandMetadata{ 43 Name: "update-space-quota", 44 Description: T("Update an existing space quota"), 45 Usage: []string{ 46 "CF_NAME update-space-quota ", 47 T("QUOTA"), 48 " ", 49 fmt.Sprintf("[-i %s] ", T("INSTANCE_MEMORY")), 50 fmt.Sprintf("[-m %s] ", T("MEMORY")), 51 fmt.Sprintf("[-n %s] ", T("NAME")), 52 fmt.Sprintf("[-r %s] ", T("ROUTES")), 53 fmt.Sprintf("[-s %s] ", T("SERVICE_INSTANCES")), 54 fmt.Sprintf("[-a %s] ", T("APP_INSTANCES")), 55 "[--allow-paid-service-plans | --disallow-paid-service-plans] ", 56 fmt.Sprintf("[--reserved-route-ports %s] ", T("RESERVED_ROUTE_PORTS")), 57 }, 58 Flags: fs, 59 } 60 } 61 62 func (cmd *UpdateSpaceQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 63 if len(fc.Args()) != 1 { 64 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("update-space-quota")) 65 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 66 } 67 68 reqs := []requirements.Requirement{ 69 requirementsFactory.NewLoginRequirement(), 70 requirementsFactory.NewTargetedOrgRequirement(), 71 } 72 73 if fc.IsSet("a") { 74 reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '-a'", cf.SpaceAppInstanceLimitMinimumAPIVersion)) 75 } 76 77 return reqs, nil 78 } 79 80 func (cmd *UpdateSpaceQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 81 cmd.ui = deps.UI 82 cmd.config = deps.Config 83 cmd.spaceQuotaRepo = deps.RepoLocator.GetSpaceQuotaRepository() 84 return cmd 85 } 86 87 func (cmd *UpdateSpaceQuota) Execute(c flags.FlagContext) error { 88 name := c.Args()[0] 89 90 spaceQuota, err := cmd.spaceQuotaRepo.FindByName(name) 91 if err != nil { 92 return err 93 } 94 95 allowPaidServices := c.Bool("allow-paid-service-plans") 96 disallowPaidServices := c.Bool("disallow-paid-service-plans") 97 if allowPaidServices && disallowPaidServices { 98 return errors.New(T("Please choose either allow or disallow. Both flags are not permitted to be passed in the same command.")) 99 } 100 101 if allowPaidServices { 102 spaceQuota.NonBasicServicesAllowed = true 103 } 104 105 if disallowPaidServices { 106 spaceQuota.NonBasicServicesAllowed = false 107 } 108 109 if c.String("i") != "" { 110 var memory int64 111 var formatError error 112 113 memFlag := c.String("i") 114 115 if memFlag == "-1" { 116 memory = -1 117 } else { 118 memory, formatError = formatters.ToMegabytes(memFlag) 119 if formatError != nil { 120 return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("update-space-quota")) 121 } 122 } 123 124 spaceQuota.InstanceMemoryLimit = memory 125 } 126 127 if c.String("m") != "" { 128 memory, formatError := formatters.ToMegabytes(c.String("m")) 129 130 if formatError != nil { 131 return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("update-space-quota")) 132 } 133 134 spaceQuota.MemoryLimit = memory 135 } 136 137 if c.String("n") != "" { 138 spaceQuota.Name = c.String("n") 139 } 140 141 if c.IsSet("s") { 142 spaceQuota.ServicesLimit = c.Int("s") 143 } 144 145 if c.IsSet("r") { 146 spaceQuota.RoutesLimit = c.Int("r") 147 } 148 149 if c.IsSet("a") { 150 spaceQuota.AppInstanceLimit = c.Int("a") 151 } 152 153 if c.IsSet("reserved-route-ports") { 154 spaceQuota.ReservedRoutePortsLimit = json.Number(strconv.Itoa(c.Int("reserved-route-ports"))) 155 } 156 157 cmd.ui.Say(T("Updating space quota {{.Quota}} as {{.Username}}...", 158 map[string]interface{}{ 159 "Quota": terminal.EntityNameColor(name), 160 "Username": terminal.EntityNameColor(cmd.config.Username()), 161 })) 162 163 err = cmd.spaceQuotaRepo.Update(spaceQuota) 164 if err != nil { 165 return err 166 } 167 168 cmd.ui.Ok() 169 return nil 170 }