forked from cloudfoundry/cli
/
organization_quota.go
180 lines (146 loc) · 5.24 KB
/
organization_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package v7action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/types"
)
type QuotaLimits struct {
TotalMemoryInMB *types.NullInt
PerProcessMemoryInMB *types.NullInt
TotalInstances *types.NullInt
PaidServicesAllowed *bool
TotalServiceInstances *types.NullInt
TotalRoutes *types.NullInt
TotalReservedPorts *types.NullInt
}
func (actor Actor) ApplyOrganizationQuotaByName(quotaName string, orgGUID string) (Warnings, error) {
var allWarnings Warnings
quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
_, ccWarnings, err := actor.CloudControllerClient.ApplyOrganizationQuota(quota.GUID, orgGUID)
allWarnings = append(allWarnings, ccWarnings...)
return allWarnings, err
}
// CreateOrganization creates a new organization with the given name
func (actor Actor) CreateOrganizationQuota(name string, limits QuotaLimits) (Warnings, error) {
orgQuota := createQuotaStruct(name, limits)
setZeroDefaultsForQuotaCreation(&orgQuota.Apps, &orgQuota.Routes, &orgQuota.Services)
convertUnlimitedToNil(&orgQuota.Apps, &orgQuota.Routes, &orgQuota.Services)
_, apiWarnings, err := actor.CloudControllerClient.CreateOrganizationQuota(orgQuota)
return Warnings(apiWarnings), err
}
func (actor Actor) DeleteOrganizationQuota(quotaName string) (Warnings, error) {
var allWarnings Warnings
quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
jobURL, ccWarnings, err := actor.CloudControllerClient.DeleteOrganizationQuota(quota.GUID)
allWarnings = append(allWarnings, ccWarnings...)
if err != nil {
return allWarnings, err
}
ccWarnings, err = actor.CloudControllerClient.PollJob(jobURL)
allWarnings = append(allWarnings, ccWarnings...)
return allWarnings, err
}
func (actor Actor) GetOrganizationQuotas() ([]resources.OrganizationQuota, Warnings, error) {
orgQuotas, warnings, err := actor.CloudControllerClient.GetOrganizationQuotas()
if err != nil {
return []resources.OrganizationQuota{}, Warnings(warnings), err
}
return orgQuotas, Warnings(warnings), nil
}
func (actor Actor) GetOrganizationQuotaByName(orgQuotaName string) (resources.OrganizationQuota, Warnings, error) {
ccv3OrgQuotas, warnings, err := actor.CloudControllerClient.GetOrganizationQuotas(
ccv3.Query{
Key: ccv3.NameFilter,
Values: []string{orgQuotaName},
},
)
if err != nil {
return resources.OrganizationQuota{}, Warnings(warnings), err
}
if len(ccv3OrgQuotas) == 0 {
return resources.OrganizationQuota{}, Warnings(warnings), actionerror.OrganizationQuotaNotFoundForNameError{Name: orgQuotaName}
}
return ccv3OrgQuotas[0], Warnings(warnings), nil
}
func (actor Actor) UpdateOrganizationQuota(quotaName string, newName string, limits QuotaLimits) (Warnings, error) {
var allWarnings Warnings
quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
if newName == "" {
newName = quotaName
}
newQuota := createQuotaStruct(newName, limits)
newQuota.GUID = quota.GUID
convertUnlimitedToNil(&newQuota.Apps, &newQuota.Routes, &newQuota.Services)
_, ccWarnings, err := actor.CloudControllerClient.UpdateOrganizationQuota(newQuota)
allWarnings = append(allWarnings, ccWarnings...)
return allWarnings, err
}
func createQuotaStruct(name string, limits QuotaLimits) resources.OrganizationQuota {
AppLimit := resources.AppLimit{
TotalMemory: limits.TotalMemoryInMB,
InstanceMemory: limits.PerProcessMemoryInMB,
TotalAppInstances: limits.TotalInstances,
}
ServiceLimit := resources.ServiceLimit{
TotalServiceInstances: limits.TotalServiceInstances,
PaidServicePlans: limits.PaidServicesAllowed,
}
RouteLimit := resources.RouteLimit{
TotalRoutes: limits.TotalRoutes,
TotalReservedPorts: limits.TotalReservedPorts,
}
quota := resources.OrganizationQuota{
Quota: resources.Quota{
Name: name,
Apps: AppLimit,
Services: ServiceLimit,
Routes: RouteLimit,
},
}
return quota
}
func setZeroDefaultsForQuotaCreation(apps *resources.AppLimit, routes *resources.RouteLimit, services *resources.ServiceLimit) {
if apps.TotalMemory == nil {
apps.TotalMemory = &types.NullInt{IsSet: true, Value: 0}
}
if routes.TotalRoutes == nil {
routes.TotalRoutes = &types.NullInt{IsSet: true, Value: 0}
}
if routes.TotalReservedPorts == nil {
routes.TotalReservedPorts = &types.NullInt{IsSet: true, Value: 0}
}
if services.TotalServiceInstances == nil {
services.TotalServiceInstances = &types.NullInt{IsSet: true, Value: 0}
}
}
func convertUnlimitedToNil(apps *resources.AppLimit, routes *resources.RouteLimit, services *resources.ServiceLimit) {
flags := []*types.NullInt{
apps.TotalMemory,
apps.InstanceMemory,
apps.TotalAppInstances,
services.TotalServiceInstances,
routes.TotalRoutes,
routes.TotalReservedPorts,
}
for i := 0; i < len(flags); i++ {
if flags[i] != nil {
if flags[i].Value == -1 {
flags[i].IsSet = false
flags[i].Value = 0
}
}
}
}