func (cmd CreateQuota) Run(context *cli.Context) { name := context.Args()[0] cmd.ui.Say(cmd.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(cmd.T("Invalid memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": memoryLimit, "Err": err})) } quota.MemoryLimit = 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(cmd.T("Quota Definition {{.QuotaName}} already exists", map[string]interface{}{"QuotaName": quota.Name})) return } if err != nil { cmd.ui.Failed(err.Error()) } cmd.ui.Ok() }
}) }) Context("when the user is logged in", func() { BeforeEach(func() { requirementsFactory.LoginSuccess = true }) It("fails requirements when called without a quota name", func() { runCommand() Expect(ui.FailedWithUsage).To(BeTrue()) }) Context("When the quota provided exists", func() { BeforeEach(func() { quota := models.QuotaFields{} quota.Name = "my-quota" quota.Guid = "my-quota-guid" quotaRepo.FindByNameReturns.Quota = quota }) It("deletes a quota with a given name when the user confirms", func() { ui.Inputs = []string{"y"} runCommand("my-quota") Expect(quotaRepo.DeleteCalledWith.Guid).To(Equal("my-quota-guid")) Expect(ui.Prompts).To(ContainSubstrings( []string{"Really delete the quota", "my-quota"}, ))
testcmd "github.com/cloudfoundry/cli/testhelpers/commands" testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" . "github.com/cloudfoundry/cli/testhelpers/matchers" testreq "github.com/cloudfoundry/cli/testhelpers/requirements" testterm "github.com/cloudfoundry/cli/testhelpers/terminal" "github.com/cloudfoundry/cli/cf/models" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("app Command", func() { var ( ui *testterm.FakeUI requirementsFactory *testreq.FakeReqFactory quotaRepo *fakes.FakeQuotaRepository quota models.QuotaFields configRepo core_config.Repository deps command_registry.Dependency ) updateCommandDependency := func(pluginCall bool) { deps.Ui = ui deps.Config = configRepo deps.RepoLocator = deps.RepoLocator.SetQuotaRepository(quotaRepo) command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("update-quota").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{} configRepo = testconfig.NewRepositoryWithDefaults() requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
Context("when the user is logged in", func() { BeforeEach(func() { requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) }) It("fails requirements when called without a quota name", func() { runCommand() Expect(ui.Outputs()).To(ContainSubstrings( []string{"Incorrect Usage", "Requires an argument"}, )) }) Context("When the quota provided exists", func() { BeforeEach(func() { quota := models.QuotaFields{} quota.Name = "my-quota" quota.GUID = "my-quota-guid" quotaRepo.FindByNameReturns(quota, nil) }) It("deletes a quota with a given name when the user confirms", func() { ui.Inputs = []string{"y"} runCommand("my-quota") Expect(quotaRepo.DeleteArgsForCall(0)).To(Equal("my-quota-guid")) Expect(ui.Prompts).To(ContainSubstrings( []string{"Really delete the quota", "my-quota"}, ))
func (cmd *CreateQuota) Execute(context flags.FlagContext) error { 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 { return errors.New(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 { return errors.New(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("a") { quota.AppInstanceLimit = context.Int("a") } else { quota.AppInstanceLimit = resources.UnlimitedAppInstances } if context.IsSet("allow-paid-service-plans") { quota.NonBasicServicesAllowed = true } if context.IsSet("reserved-route-ports") { quota.ReservedRoutePorts = json.Number(context.String("reserved-route-ports")) } err := cmd.quotaRepo.Create(quota) httpErr, ok := err.(errors.HTTPError) if ok && httpErr.ErrorCode() == errors.QuotaDefinitionNameTaken { cmd.ui.Ok() cmd.ui.Warn(T("Quota Definition {{.QuotaName}} already exists", map[string]interface{}{"QuotaName": quota.Name})) return nil } if err != nil { return err } cmd.ui.Ok() return nil }