forked from Liam-Williams/i18n4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_quota.go
98 lines (79 loc) · 2.69 KB
/
update_quota.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package quota
import ()
type updateQuota struct {
ui terminal.UI
config configuration.Reader
quotaRepo api.QuotaRepository
}
func NewUpdateQuota(ui terminal.UI, config configuration.Reader, quotaRepo api.QuotaRepository) *updateQuota {
return &updateQuota{
ui: ui,
config: config,
quotaRepo: quotaRepo,
}
}
func (command *updateQuota) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "update-quota",
Description: "Update an existing resource quota",
Usage: "CF_NAME update-quota QUOTA [-m MEMORY] [-n NEW_NAME] [-r ROUTES] [-s SERVICE_INSTANCES] [--allow-paid-service-plans | --disallow-paid-service-plans]",
Flags: []cli.Flag{
flag_helpers.NewStringFlag("m", "Total amount of memory (e.g. 1024M, 1G, 10G)"),
flag_helpers.NewStringFlag("n", "New name"),
flag_helpers.NewIntFlag("r", "Total number of routes"),
flag_helpers.NewIntFlag("s", "Total number of service instances"),
cli.BoolFlag{Name: "allow-paid-service-plans", Usage: "Can provision instances of paid service plans"},
cli.BoolFlag{Name: "disallow-paid-service-plans", Usage: "Cannot provision instances of paid service plans"},
},
}
}
func (cmd *updateQuota) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
if len(context.Args()) != 1 {
cmd.ui.FailWithUsage(context, "update-quota")
}
return []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}, nil
}
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("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, "update-quota")
}
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("Updating quota %s as %s...",
terminal.EntityNameColor(oldQuotaName),
terminal.EntityNameColor(cmd.config.Username()))
err = cmd.quotaRepo.Update(quota)
if err != nil {
cmd.ui.Failed(err.Error())
}
cmd.ui.Ok()
}