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