github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/quota/create_quota.go (about) 1 package quota 2 3 import ( 4 "fmt" 5 6 "encoding/json" 7 8 "code.cloudfoundry.org/cli/cf/api/quotas" 9 "code.cloudfoundry.org/cli/cf/api/resources" 10 "code.cloudfoundry.org/cli/cf/commandregistry" 11 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 12 "code.cloudfoundry.org/cli/cf/errors" 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/models" 17 "code.cloudfoundry.org/cli/cf/requirements" 18 "code.cloudfoundry.org/cli/cf/terminal" 19 ) 20 21 type CreateQuota struct { 22 ui terminal.UI 23 config coreconfig.Reader 24 quotaRepo quotas.QuotaRepository 25 } 26 27 func init() { 28 commandregistry.Register(&CreateQuota{}) 29 } 30 31 func (cmd *CreateQuota) MetaData() commandregistry.CommandMetadata { 32 fs := make(map[string]flags.FlagSet) 33 fs["allow-paid-service-plans"] = &flags.BoolFlag{Name: "allow-paid-service-plans", Usage: T("Can provision instances of paid service plans")} 34 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.")} 35 fs["m"] = &flags.StringFlag{ShortName: "m", Usage: T("Total amount of memory (e.g. 1024M, 1G, 10G)")} 36 fs["r"] = &flags.IntFlag{ShortName: "r", Usage: T("Total number of routes")} 37 fs["s"] = &flags.IntFlag{ShortName: "s", Usage: T("Total number of service instances")} 38 fs["a"] = &flags.IntFlag{ShortName: "a", Usage: T("Total number of application instances. -1 represents an unlimited amount. (Default: unlimited)")} 39 fs["reserved-route-ports"] = &flags.StringFlag{Name: "reserved-route-ports", Usage: T("Maximum number of routes that may be created with reserved ports (Default: 0)")} 40 41 return commandregistry.CommandMetadata{ 42 Name: "create-quota", 43 Description: T("Define a new resource quota"), 44 Usage: []string{ 45 "CF_NAME create-quota ", 46 T("QUOTA"), 47 " ", 48 fmt.Sprintf("[-m %s] ", T("TOTAL_MEMORY")), 49 fmt.Sprintf("[-i %s] ", T("INSTANCE_MEMORY")), 50 fmt.Sprintf("[-r %s] ", T("ROUTES")), 51 fmt.Sprintf("[-s %s] ", T("SERVICE_INSTANCES")), 52 fmt.Sprintf("[-a %s] ", T("APP_INSTANCES")), 53 "[--allow-paid-service-plans] ", 54 fmt.Sprintf("[--reserved-route-ports %s] ", T("RESERVED_ROUTE_PORTS")), 55 }, 56 Flags: fs, 57 } 58 } 59 60 func (cmd *CreateQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 61 if len(fc.Args()) != 1 { 62 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("create-quota")) 63 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 64 } 65 66 reqs := []requirements.Requirement{ 67 requirementsFactory.NewLoginRequirement(), 68 } 69 70 return reqs, nil 71 } 72 73 func (cmd *CreateQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 74 cmd.ui = deps.UI 75 cmd.config = deps.Config 76 cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository() 77 return cmd 78 } 79 80 func (cmd *CreateQuota) Execute(context flags.FlagContext) error { 81 name := context.Args()[0] 82 83 cmd.ui.Say(T("Creating quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{ 84 "QuotaName": terminal.EntityNameColor(name), 85 "Username": terminal.EntityNameColor(cmd.config.Username()), 86 })) 87 88 quota := models.QuotaFields{ 89 Name: name, 90 } 91 92 memoryLimit := context.String("m") 93 if memoryLimit != "" { 94 parsedMemory, err := formatters.ToMegabytes(memoryLimit) 95 if err != nil { 96 return errors.New(T("Invalid memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": memoryLimit, "Err": err})) 97 } 98 99 quota.MemoryLimit = parsedMemory 100 } 101 102 instanceMemoryLimit := context.String("i") 103 if instanceMemoryLimit == "-1" || instanceMemoryLimit == "" { 104 quota.InstanceMemoryLimit = -1 105 } else { 106 parsedMemory, errr := formatters.ToMegabytes(instanceMemoryLimit) 107 if errr != nil { 108 return errors.New(T("Invalid instance memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": instanceMemoryLimit, "Err": errr})) 109 } 110 quota.InstanceMemoryLimit = parsedMemory 111 } 112 113 if context.IsSet("r") { 114 quota.RoutesLimit = context.Int("r") 115 } 116 117 if context.IsSet("s") { 118 quota.ServicesLimit = context.Int("s") 119 } 120 121 if context.IsSet("a") { 122 quota.AppInstanceLimit = context.Int("a") 123 } else { 124 quota.AppInstanceLimit = resources.UnlimitedAppInstances 125 } 126 127 if context.IsSet("allow-paid-service-plans") { 128 quota.NonBasicServicesAllowed = true 129 } 130 131 if context.IsSet("reserved-route-ports") { 132 quota.ReservedRoutePorts = json.Number(context.String("reserved-route-ports")) 133 } 134 135 err := cmd.quotaRepo.Create(quota) 136 137 httpErr, ok := err.(errors.HTTPError) 138 if ok && httpErr.ErrorCode() == errors.QuotaDefinitionNameTaken { 139 cmd.ui.Ok() 140 cmd.ui.Warn(T("Quota Definition {{.QuotaName}} already exists", map[string]interface{}{"QuotaName": quota.Name})) 141 return nil 142 } 143 144 if err != nil { 145 return err 146 } 147 148 cmd.ui.Ok() 149 return nil 150 }