forked from cloudfoundry/cli
/
create_quota.go
125 lines (105 loc) · 4.09 KB
/
create_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package spacequota
import (
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/api/space_quotas"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/flag_helpers"
"github.com/cloudfoundry/cli/cf/formatters"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type CreateSpaceQuota struct {
ui terminal.UI
config core_config.Reader
quotaRepo space_quotas.SpaceQuotaRepository
orgRepo organizations.OrganizationRepository
}
func NewCreateSpaceQuota(ui terminal.UI, config core_config.Reader, quotaRepo space_quotas.SpaceQuotaRepository, orgRepo organizations.OrganizationRepository) CreateSpaceQuota {
return CreateSpaceQuota{
ui: ui,
config: config,
quotaRepo: quotaRepo,
orgRepo: orgRepo,
}
}
func (cmd CreateSpaceQuota) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "create-space-quota",
Description: T("Define a new space resource quota"),
Usage: T("CF_NAME create-space-quota QUOTA [-i INSTANCE_MEMORY] [-m MEMORY] [-r ROUTES] [-s SERVICE_INSTANCES] [--allow-paid-service-plans]"),
Flags: []cli.Flag{
flag_helpers.NewStringFlag("i", T("Maximum amount of memory an application instance can have (e.g. 1024M, 1G, 10G). -1 represents an unlimited amount. (Default: unlimited)")),
flag_helpers.NewStringFlag("m", T("Total amount of memory a space can have (e.g. 1024M, 1G, 10G)")),
flag_helpers.NewIntFlag("r", T("Total number of routes")),
flag_helpers.NewIntFlag("s", T("Total number of service instances")),
cli.BoolFlag{Name: "allow-paid-service-plans", Usage: T("Can provision instances of paid service plans (Default: disallowed)")},
},
}
}
func (cmd CreateSpaceQuota) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
if len(context.Args()) != 1 {
cmd.ui.FailWithUsage(context)
}
return []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedOrgRequirement(),
}, nil
}
func (cmd CreateSpaceQuota) Run(context *cli.Context) {
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
}
err = cmd.quotaRepo.Create(quota)
httpErr, ok := err.(errors.HttpError)
if ok && httpErr.ErrorCode() == errors.QUOTA_EXISTS {
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()
}