func (cmd *CreateQuota) Execute(context flags.FlagContext) { name := context.Args()[0] cmd.ui.Say(T("Creating quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{ "QuotaName": terminal.EntityNameColor(name), "Username": terminal.EntityNameColor(cmd.config.Username()), })) quota := models.QuotaFields{ Name: name, } memoryLimit := context.String("m") if memoryLimit != "" { parsedMemory, err := formatters.ToMegabytes(memoryLimit) if err != nil { cmd.ui.Failed(T("Invalid memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": memoryLimit, "Err": err})) } quota.MemoryLimit = parsedMemory } instanceMemoryLimit := context.String("i") if instanceMemoryLimit == "-1" || instanceMemoryLimit == "" { quota.InstanceMemoryLimit = -1 } else { parsedMemory, errr := formatters.ToMegabytes(instanceMemoryLimit) if errr != nil { cmd.ui.Failed(T("Invalid instance memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": instanceMemoryLimit, "Err": errr})) } quota.InstanceMemoryLimit = parsedMemory } if context.IsSet("r") { quota.RoutesLimit = context.Int("r") } if context.IsSet("s") { quota.ServicesLimit = context.Int("s") } if context.IsSet("allow-paid-service-plans") { quota.NonBasicServicesAllowed = true } err := cmd.quotaRepo.Create(quota) httpErr, ok := err.(errors.HttpError) if ok && httpErr.ErrorCode() == errors.QUOTA_EXISTS { cmd.ui.Ok() cmd.ui.Warn(T("Quota Definition {{.QuotaName}} already exists", map[string]interface{}{"QuotaName": quota.Name})) return } if err != nil { cmd.ui.Failed(err.Error()) } cmd.ui.Ok() }
func (cmd *updateQuota) Run(c *cli.Context) { oldQuotaName := c.Args()[0] quota, err := cmd.quotaRepo.FindByName(oldQuotaName) if err != nil { cmd.ui.Failed(err.Error()) } allowPaidServices := c.Bool("allow-paid-service-plans") disallowPaidServices := c.Bool("disallow-paid-service-plans") if allowPaidServices && disallowPaidServices { cmd.ui.Failed(cmd.T("Please choose either allow or disallow. Both flags are not permitted to be passed in the same command.")) } if allowPaidServices { quota.NonBasicServicesAllowed = true } if disallowPaidServices { quota.NonBasicServicesAllowed = false } if c.String("m") != "" { memory, formatError := formatters.ToMegabytes(c.String("m")) if formatError != nil { cmd.ui.FailWithUsage(c) } quota.MemoryLimit = memory } if c.String("n") != "" { quota.Name = c.String("n") } if c.IsSet("s") { quota.ServicesLimit = c.Int("s") } if c.IsSet("r") { quota.RoutesLimit = c.Int("r") } cmd.ui.Say(cmd.T("Updating quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{ "QuotaName": terminal.EntityNameColor(oldQuotaName), "Username": terminal.EntityNameColor(cmd.config.Username()), })) err = cmd.quotaRepo.Update(quota) if err != nil { cmd.ui.Failed(err.Error()) } cmd.ui.Ok() }
func bytesVal(yamlMap generic.Map, key string, errs *[]error) *uint64 { yamlVal := yamlMap.Get(key) if yamlVal == nil { return nil } value, err := formatters.ToMegabytes(yamlVal.(string)) if err != nil { *errs = append(*errs, errors.NewWithFmt("Unexpected value for %s :\n%s", key, err.Error())) return nil } return &value }
func bytesVal(yamlMap generic.Map, key string, errs *[]error) *uint64 { yamlVal := yamlMap.Get(key) if yamlVal == nil { return nil } value, err := formatters.ToMegabytes(yamlVal.(string)) if err != nil { *errs = append(*errs, errors.NewWithFmt(T("Unexpected value for {{.PropertyName}} :\n{{.Error}}", map[string]interface{}{"PropertyName": key, "Error": err.Error()}))) return nil } return &value }
func bytesVal(yamlMap generic.Map, key string, errs *[]error) *int64 { yamlVal := yamlMap.Get(key) if yamlVal == nil { return nil } stringVal := coerceToString(yamlVal) value, err := formatters.ToMegabytes(stringVal) if err != nil { *errs = append(*errs, fmt.Errorf(T("Invalid value for '{{.PropertyName}}': {{.StringVal}}\n{{.Error}}", map[string]interface{}{ "PropertyName": key, "Error": err.Error(), "StringVal": stringVal, }))) return nil } return &value }
func (cmd *Push) getAppParamsFromContext(c *cli.Context) (appParams models.AppParams) { if len(c.Args()) > 0 { appParams.Name = &c.Args()[0] } appParams.NoRoute = c.Bool("no-route") appParams.UseRandomHostname = c.Bool("random-route") if c.String("n") != "" { hostname := c.String("n") appParams.Host = &hostname } if c.String("b") != "" { buildpack := c.String("b") if buildpack == "null" || buildpack == "default" { buildpack = "" } appParams.BuildpackUrl = &buildpack } if c.String("c") != "" { command := c.String("c") if command == "null" || command == "default" { command = "" } appParams.Command = &command } if c.String("d") != "" { domain := c.String("d") appParams.Domain = &domain } if c.IsSet("i") { instances := c.Int("i") if instances < 1 { cmd.ui.Failed(T("Invalid instance count: {{.InstancesCount}}\nInstance count must be a positive integer", map[string]interface{}{"InstancesCount": instances})) } appParams.InstanceCount = &instances } if c.String("k") != "" { diskQuota, err := formatters.ToMegabytes(c.String("k")) if err != nil { cmd.ui.Failed(T("Invalid disk quota: {{.DiskQuota}}\n{{.Err}}", map[string]interface{}{"DiskQuota": c.String("k"), "Err": err.Error()})) } appParams.DiskQuota = &diskQuota } if c.String("m") != "" { memory, err := formatters.ToMegabytes(c.String("m")) if err != nil { cmd.ui.Failed(T("Invalid memory limit: {{.MemLimit}}\n{{.Err}}", map[string]interface{}{"MemLimit": c.String("m"), "Err": err.Error()})) } appParams.Memory = &memory } if c.String("p") != "" { path := c.String("p") appParams.Path = &path } if c.String("s") != "" { stackName := c.String("s") appParams.StackName = &stackName } if c.String("t") != "" { timeout, err := strconv.Atoi(c.String("t")) if err != nil { cmd.ui.Failed("Error: %s", errors.NewWithFmt(T("Invalid timeout param: {{.Timeout}}\n{{.Err}}", map[string]interface{}{"Timeout": c.String("t"), "Err": err.Error()}))) } appParams.HealthCheckTimeout = &timeout } return }
func (cmd *CreateSpaceQuota) Execute(context flags.FlagContext) { name := context.Args()[0] org := cmd.config.OrganizationFields() cmd.ui.Say(T("Creating space quota {{.QuotaName}} for org {{.OrgName}} as {{.Username}}...", map[string]interface{}{ "QuotaName": terminal.EntityNameColor(name), "OrgName": terminal.EntityNameColor(org.Name), "Username": terminal.EntityNameColor(cmd.config.Username()), })) quota := models.SpaceQuota{ Name: name, OrgGuid: org.Guid, } memoryLimit := context.String("m") if memoryLimit != "" { parsedMemory, errr := formatters.ToMegabytes(memoryLimit) if errr != nil { cmd.ui.Failed(T("Invalid memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": memoryLimit, "Err": errr})) } quota.MemoryLimit = parsedMemory } instanceMemoryLimit := context.String("i") var parsedMemory int64 var err error if instanceMemoryLimit == "-1" || instanceMemoryLimit == "" { parsedMemory = -1 } else { parsedMemory, err = formatters.ToMegabytes(instanceMemoryLimit) if err != nil { cmd.ui.Failed(T("Invalid instance memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": instanceMemoryLimit, "Err": err})) } } quota.InstanceMemoryLimit = parsedMemory if context.IsSet("r") { quota.RoutesLimit = context.Int("r") } if context.IsSet("s") { quota.ServicesLimit = context.Int("s") } if context.IsSet("allow-paid-service-plans") { quota.NonBasicServicesAllowed = true } if context.IsSet("a") { quota.AppInstanceLimit = context.Int("a") } else { quota.AppInstanceLimit = resources.UnlimitedAppInstances } err = cmd.quotaRepo.Create(quota) httpErr, ok := err.(errors.HttpError) if ok && httpErr.ErrorCode() == errors.QuotaDefinitionNameTaken { cmd.ui.Ok() cmd.ui.Warn(T("Space Quota Definition {{.QuotaName}} already exists", map[string]interface{}{"QuotaName": quota.Name})) return } if err != nil { cmd.ui.Failed(err.Error()) } cmd.ui.Ok() }
func (cmd *UpdateSpaceQuota) Execute(c flags.FlagContext) { name := c.Args()[0] spaceQuota, apiErr := cmd.spaceQuotaRepo.FindByName(name) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } allowPaidServices := c.Bool("allow-paid-service-plans") disallowPaidServices := c.Bool("disallow-paid-service-plans") if allowPaidServices && disallowPaidServices { cmd.ui.Failed(T("Please choose either allow or disallow. Both flags are not permitted to be passed in the same command.")) } if allowPaidServices { spaceQuota.NonBasicServicesAllowed = true } if disallowPaidServices { spaceQuota.NonBasicServicesAllowed = false } if c.String("i") != "" { var memory int64 var formatError error memFlag := c.String("i") if memFlag == "-1" { memory = -1 } else { memory, formatError = formatters.ToMegabytes(memFlag) if formatError != nil { cmd.ui.Failed(T("Incorrect Usage.\n\n") + command_registry.Commands.CommandUsage("update-space-quota")) } } spaceQuota.InstanceMemoryLimit = memory } if c.String("m") != "" { memory, formatError := formatters.ToMegabytes(c.String("m")) if formatError != nil { cmd.ui.Failed(T("Incorrect Usage.\n\n") + command_registry.Commands.CommandUsage("update-space-quota")) } spaceQuota.MemoryLimit = memory } if c.String("n") != "" { spaceQuota.Name = c.String("n") } if c.IsSet("s") { spaceQuota.ServicesLimit = c.Int("s") } if c.IsSet("r") { spaceQuota.RoutesLimit = c.Int("r") } cmd.ui.Say(T("Updating space quota {{.Quota}} as {{.Username}}...", map[string]interface{}{ "Quota": terminal.EntityNameColor(name), "Username": terminal.EntityNameColor(cmd.config.Username()), })) apiErr = cmd.spaceQuotaRepo.Update(spaceQuota) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() }
func (cmd *Push) getAppParamsFromContext(c flags.FlagContext) (appParams models.AppParams) { if len(c.Args()) > 0 { appParams.Name = &c.Args()[0] } appParams.NoRoute = c.Bool("no-route") appParams.UseRandomHostname = c.Bool("random-route") appParams.NoHostname = c.Bool("no-hostname") if c.String("n") != "" { appParams.Hosts = &[]string{c.String("n")} } if c.String("route-path") != "" { routePath := c.String("route-path") appParams.RoutePath = &routePath } if c.String("b") != "" { buildpack := c.String("b") if buildpack == "null" || buildpack == "default" { buildpack = "" } appParams.BuildpackUrl = &buildpack } if c.String("c") != "" { command := c.String("c") if command == "null" || command == "default" { command = "" } appParams.Command = &command } if c.String("d") != "" { appParams.Domains = &[]string{c.String("d")} } if c.IsSet("i") { instances := c.Int("i") if instances < 1 { cmd.ui.Failed(T("Invalid instance count: {{.InstancesCount}}\nInstance count must be a positive integer", map[string]interface{}{"InstancesCount": instances})) } appParams.InstanceCount = &instances } if c.String("k") != "" { diskQuota, err := formatters.ToMegabytes(c.String("k")) if err != nil { cmd.ui.Failed(T("Invalid disk quota: {{.DiskQuota}}\n{{.Err}}", map[string]interface{}{"DiskQuota": c.String("k"), "Err": err.Error()})) } appParams.DiskQuota = &diskQuota } if c.String("m") != "" { memory, err := formatters.ToMegabytes(c.String("m")) if err != nil { cmd.ui.Failed(T("Invalid memory limit: {{.MemLimit}}\n{{.Err}}", map[string]interface{}{"MemLimit": c.String("m"), "Err": err.Error()})) } appParams.Memory = &memory } if c.String("docker-image") != "" { dockerImage := c.String("docker-image") appParams.DockerImage = &dockerImage } if c.String("p") != "" { path := c.String("p") appParams.Path = &path } if c.String("s") != "" { stackName := c.String("s") appParams.StackName = &stackName } if c.String("t") != "" { timeout, err := strconv.Atoi(c.String("t")) if err != nil { cmd.ui.Failed("Error: %s", fmt.Errorf(T("Invalid timeout param: {{.Timeout}}\n{{.Err}}", map[string]interface{}{"Timeout": c.String("t"), "Err": err.Error()}))) } appParams.HealthCheckTimeout = &timeout } if healthCheckType := c.String("u"); healthCheckType != "" { if healthCheckType != "port" && healthCheckType != "none" { cmd.ui.Failed("Error: %s", fmt.Errorf(T("Invalid health-check-type param: {{.healthCheckType}}", map[string]interface{}{"healthCheckType": healthCheckType}))) } appParams.HealthCheckType = &healthCheckType } return }
func (cmd *Scale) Execute(c flags.FlagContext) { currentApp := cmd.appReq.GetApplication() if !anyFlagsSet(c) { cmd.ui.Say(T("Showing current scale of app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "AppName": terminal.EntityNameColor(currentApp.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) cmd.ui.Ok() cmd.ui.Say("") cmd.ui.Say("%s %s", terminal.HeaderColor(T("memory:")), formatters.ByteSize(currentApp.Memory*bytesInAMegabyte)) cmd.ui.Say("%s %s", terminal.HeaderColor(T("disk:")), formatters.ByteSize(currentApp.DiskQuota*bytesInAMegabyte)) cmd.ui.Say("%s %d", terminal.HeaderColor(T("instances:")), currentApp.InstanceCount) return } params := models.AppParams{} shouldRestart := false if c.String("m") != "" { memory, err := formatters.ToMegabytes(c.String("m")) if err != nil { cmd.ui.Failed(T("Invalid memory limit: {{.Memory}}\n{{.ErrorDescription}}", map[string]interface{}{ "Memory": c.String("m"), "ErrorDescription": err, })) } params.Memory = &memory shouldRestart = true } if c.String("k") != "" { diskQuota, err := formatters.ToMegabytes(c.String("k")) if err != nil { cmd.ui.Failed(T("Invalid disk quota: {{.DiskQuota}}\n{{.ErrorDescription}}", map[string]interface{}{ "DiskQuota": c.String("k"), "ErrorDescription": err, })) } params.DiskQuota = &diskQuota shouldRestart = true } if c.IsSet("i") { instances := c.Int("i") if instances > 0 { params.InstanceCount = &instances } else { cmd.ui.Failed(T("Invalid instance count: {{.InstanceCount}}\nInstance count must be a positive integer", map[string]interface{}{ "InstanceCount": instances, })) } } if shouldRestart && !cmd.confirmRestart(c, currentApp.Name) { return } cmd.ui.Say(T("Scaling app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "AppName": terminal.EntityNameColor(currentApp.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) updatedApp, apiErr := cmd.appRepo.Update(currentApp.Guid, params) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() if shouldRestart { cmd.restarter.ApplicationRestart(updatedApp, cmd.config.OrganizationFields().Name, cmd.config.SpaceFields().Name) } }
func (cmd *Push) getAppParamsFromContext(c flags.FlagContext) (models.AppParams, error) { noHostBool := c.Bool("no-hostname") appParams := models.AppParams{ NoRoute: c.Bool("no-route"), UseRandomRoute: c.Bool("random-route"), NoHostname: &noHostBool, } if len(c.Args()) > 0 { appParams.Name = &c.Args()[0] } if c.String("n") != "" { appParams.Hosts = []string{c.String("n")} } if c.String("route-path") != "" { routePath := c.String("route-path") appParams.RoutePath = &routePath } if c.String("app-ports") != "" { appPortStrings := strings.Split(c.String("app-ports"), ",") appPorts := make([]int, len(appPortStrings)) for i, s := range appPortStrings { p, err := strconv.Atoi(s) if err != nil { return models.AppParams{}, errors.New(T("Invalid app port: {{.AppPort}}\nApp port must be a number", map[string]interface{}{ "AppPort": s, })) } appPorts[i] = p } appParams.AppPorts = &appPorts } if c.String("b") != "" { buildpack := c.String("b") if buildpack == "null" || buildpack == "default" { buildpack = "" } appParams.BuildpackURL = &buildpack } if c.String("c") != "" { command := c.String("c") if command == "null" || command == "default" { command = "" } appParams.Command = &command } if c.String("d") != "" { appParams.Domains = []string{c.String("d")} } if c.IsSet("i") { instances := c.Int("i") if instances < 1 { return models.AppParams{}, errors.New(T("Invalid instance count: {{.InstancesCount}}\nInstance count must be a positive integer", map[string]interface{}{"InstancesCount": instances})) } appParams.InstanceCount = &instances } if c.String("k") != "" { diskQuota, err := formatters.ToMegabytes(c.String("k")) if err != nil { return models.AppParams{}, errors.New(T("Invalid disk quota: {{.DiskQuota}}\n{{.Err}}", map[string]interface{}{"DiskQuota": c.String("k"), "Err": err.Error()})) } appParams.DiskQuota = &diskQuota } if c.String("m") != "" { memory, err := formatters.ToMegabytes(c.String("m")) if err != nil { return models.AppParams{}, errors.New(T("Invalid memory limit: {{.MemLimit}}\n{{.Err}}", map[string]interface{}{"MemLimit": c.String("m"), "Err": err.Error()})) } appParams.Memory = &memory } if c.String("docker-image") != "" { dockerImage := c.String("docker-image") appParams.DockerImage = &dockerImage } if c.String("p") != "" { path := c.String("p") appParams.Path = &path } if c.String("s") != "" { stackName := c.String("s") appParams.StackName = &stackName } if c.String("t") != "" { timeout, err := strconv.Atoi(c.String("t")) if err != nil { return models.AppParams{}, fmt.Errorf("Error: %s", fmt.Errorf(T("Invalid timeout param: {{.Timeout}}\n{{.Err}}", map[string]interface{}{"Timeout": c.String("t"), "Err": err.Error()}))) } appParams.HealthCheckTimeout = &timeout } if healthCheckType := c.String("u"); healthCheckType != "" { if healthCheckType != "port" && healthCheckType != "none" { return models.AppParams{}, fmt.Errorf("Error: %s", fmt.Errorf(T("Invalid health-check-type param: {{.healthCheckType}}", map[string]interface{}{"healthCheckType": healthCheckType}))) } appParams.HealthCheckType = &healthCheckType } return appParams, nil }
func (cmd *UpdateQuota) Execute(c flags.FlagContext) error { oldQuotaName := c.Args()[0] quota, err := cmd.quotaRepo.FindByName(oldQuotaName) if err != nil { return err } allowPaidServices := c.Bool("allow-paid-service-plans") disallowPaidServices := c.Bool("disallow-paid-service-plans") if allowPaidServices && disallowPaidServices { return errors.New(T("Please choose either allow or disallow. Both flags are not permitted to be passed in the same command.")) } if allowPaidServices { quota.NonBasicServicesAllowed = true } if disallowPaidServices { quota.NonBasicServicesAllowed = false } if c.IsSet("i") { var memory int64 if c.String("i") == "-1" { memory = -1 } else { var formatError error memory, formatError = formatters.ToMegabytes(c.String("i")) if formatError != nil { return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("update-quota")) } } quota.InstanceMemoryLimit = memory } if c.IsSet("a") { quota.AppInstanceLimit = c.Int("a") } if c.IsSet("m") { memory, formatError := formatters.ToMegabytes(c.String("m")) if formatError != nil { return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("update-quota")) } quota.MemoryLimit = memory } if c.IsSet("n") { quota.Name = c.String("n") } if c.IsSet("s") { quota.ServicesLimit = c.Int("s") } if c.IsSet("r") { quota.RoutesLimit = c.Int("r") } if c.IsSet("reserved-route-ports") { quota.ReservedRoutePorts = json.Number(c.String("reserved-route-ports")) } cmd.ui.Say(T("Updating quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{ "QuotaName": terminal.EntityNameColor(oldQuotaName), "Username": terminal.EntityNameColor(cmd.config.Username()), })) err = cmd.quotaRepo.Update(quota) if err != nil { return err } cmd.ui.Ok() return nil }
func (cmd *Scale) Run(c *cli.Context) { currentApp := cmd.appReq.GetApplication() if !anyFlagsSet(c) { cmd.ui.Say("Showing current scale of app %s in org %s / space %s as %s...", terminal.EntityNameColor(currentApp.Name), terminal.EntityNameColor(cmd.config.OrganizationFields().Name), terminal.EntityNameColor(cmd.config.SpaceFields().Name), terminal.EntityNameColor(cmd.config.Username()), ) cmd.ui.Ok() cmd.ui.Say("") cmd.ui.Say("%s %s", terminal.HeaderColor("memory:"), formatters.ByteSize(currentApp.Memory*bytesInAMegabyte)) cmd.ui.Say("%s %s", terminal.HeaderColor("disk:"), formatters.ByteSize(currentApp.DiskQuota*bytesInAMegabyte)) cmd.ui.Say("%s %d", terminal.HeaderColor("instances:"), currentApp.InstanceCount) return } params := models.AppParams{} shouldRestart := false if c.String("m") != "" { memory, err := formatters.ToMegabytes(c.String("m")) if err != nil { cmd.ui.Failed("Invalid memory limit: %s\n%s", c.String("m"), err) } params.Memory = &memory shouldRestart = true } if c.String("k") != "" { diskQuota, err := formatters.ToMegabytes(c.String("k")) if err != nil { cmd.ui.Failed("Invalid disk quota: %s\n%s", c.String("k"), err) } params.DiskQuota = &diskQuota shouldRestart = true } if c.IsSet("i") { instances := c.Int("i") if instances > 0 { params.InstanceCount = &instances } else { cmd.ui.Failed("Invalid instance count: %d\nInstance count must be a positive integer", instances) } } if shouldRestart && !cmd.confirmRestart(c, currentApp.Name) { return } cmd.ui.Say("Scaling app %s in org %s / space %s as %s...", terminal.EntityNameColor(currentApp.Name), terminal.EntityNameColor(cmd.config.OrganizationFields().Name), terminal.EntityNameColor(cmd.config.SpaceFields().Name), terminal.EntityNameColor(cmd.config.Username()), ) updatedApp, apiErr := cmd.appRepo.Update(currentApp.Guid, params) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() if shouldRestart { cmd.restarter.ApplicationRestart(updatedApp) } }