forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quota.go
107 lines (89 loc) · 3.01 KB
/
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
package quota
import (
"fmt"
"strconv"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/api/quotas"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/formatters"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type showQuota struct {
ui terminal.UI
config coreconfig.Reader
quotaRepo quotas.QuotaRepository
}
func init() {
commandregistry.Register(&showQuota{})
}
func (cmd *showQuota) MetaData() commandregistry.CommandMetadata {
return commandregistry.CommandMetadata{
Name: "quota",
Usage: []string{
T("CF_NAME quota QUOTA"),
},
Description: T("Show quota info"),
}
}
func (cmd *showQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("quota"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
}
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return reqs, nil
}
func (cmd *showQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
return cmd
}
func (cmd *showQuota) Execute(c flags.FlagContext) error {
quotaName := c.Args()[0]
cmd.ui.Say(T("Getting quota {{.QuotaName}} info as {{.Username}}...", map[string]interface{}{"QuotaName": quotaName, "Username": cmd.config.Username()}))
quota, err := cmd.quotaRepo.FindByName(quotaName)
if err != nil {
return err
}
cmd.ui.Ok()
var megabytes string
if quota.InstanceMemoryLimit == -1 {
megabytes = T("unlimited")
} else {
megabytes = formatters.ByteSize(quota.InstanceMemoryLimit * formatters.MEGABYTE)
}
servicesLimit := strconv.Itoa(quota.ServicesLimit)
if servicesLimit == "-1" {
servicesLimit = T("unlimited")
}
appInstanceLimit := strconv.Itoa(quota.AppInstanceLimit)
if quota.AppInstanceLimit == resources.UnlimitedAppInstances {
appInstanceLimit = T("unlimited")
}
reservedRoutePorts := string(quota.ReservedRoutePorts)
if reservedRoutePorts == resources.UnlimitedReservedRoutePorts {
reservedRoutePorts = T("unlimited")
}
table := cmd.ui.Table([]string{"", ""})
table.Add(T("Total Memory"), formatters.ByteSize(quota.MemoryLimit*formatters.MEGABYTE))
table.Add(T("Instance Memory"), megabytes)
table.Add(T("Routes"), fmt.Sprint(quota.RoutesLimit))
table.Add(T("Services"), servicesLimit)
table.Add(T("Paid service plans"), formatters.Allowed(quota.NonBasicServicesAllowed))
table.Add(T("App instance limit"), appInstanceLimit)
if reservedRoutePorts != "" {
table.Add(T("Reserved Route Ports"), reservedRoutePorts)
}
err = table.Print()
if err != nil {
return err
}
return nil
}