-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathams.go
More file actions
413 lines (332 loc) · 13.3 KB
/
ams.go
File metadata and controls
413 lines (332 loc) · 13.3 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package accountmgmtutil
import (
"context"
"errors"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
amsclient "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/accountmgmt/apiv1/client"
"github.com/redhat-developer/app-services-cli/pkg/shared/remote"
)
const (
RedHatMarketPlace = "rhm"
)
func CheckTermsAccepted(ctx context.Context, spec *remote.AmsConfig, conn connection.Connection) (accepted bool, redirectURI string, err error) {
termsReview, _, err := conn.API().AccountMgmt().
ApiAuthorizationsV1SelfTermsReviewPost(ctx).
SelfTermsReview(amsclient.SelfTermsReview{
EventCode: &spec.TermsAndConditionsEventCode,
SiteCode: &spec.TermsAndConditionsSiteCode,
}).Execute()
if err != nil {
return false, "", err
}
if !termsReview.GetTermsAvailable() && !termsReview.GetTermsRequired() {
return true, "", nil
}
if !termsReview.HasRedirectUrl() {
return false, "", errors.New("terms must be signed, but there is no terms URL")
}
return false, termsReview.GetRedirectUrl(), nil
}
// QuotaSpec - contains quota name and remaining quota count
type QuotaSpec struct {
Name string
Quota int
BillingModel string
CloudAccounts *[]amsclient.CloudAccount
}
type MarketplaceInfo struct {
BillingModel string
Provider string
CloudAccountID string
}
type OrgQuotas struct {
StandardQuotas []QuotaSpec
MarketplaceQuotas []QuotaSpec
TrialQuotas []QuotaSpec
EvalQuotas []QuotaSpec
EnterpriseQuotas []QuotaSpec
}
func fetchOrgQuotaCost(ctx context.Context, conn connection.Connection) (*amsclient.QuotaCostList, error) {
orgId, err := GetOrganizationID(ctx, conn)
if err != nil {
return nil, err
}
quotaCostGet, _, err := conn.API().AccountMgmt().
ApiAccountsMgmtV1OrganizationsOrgIdQuotaCostGet(ctx, orgId).
FetchRelatedResources(true).
FetchCloudAccounts(true).
Execute()
return "aCostGet, err
}
func GetOrgQuotas(f *factory.Factory, spec *remote.AmsConfig) (*OrgQuotas, error) {
conn, err := f.Connection()
if err != nil {
return nil, err
}
quotaCostGet, err := fetchOrgQuotaCost(f.Context, conn)
if err != nil {
return nil, err
}
// this should be refactored and base of the logic by the billing model information that's returned by KFM for each supported instance type
var standardQuotas, marketplaceQuotas, trialQuotas, evalQuotas, enterpriseQuotas []QuotaSpec
for _, quota := range quotaCostGet.GetItems() {
quotaResources := quota.GetRelatedResources()
for i := range quotaResources {
quotaResource := quotaResources[i]
if quotaResource.GetResourceName() == spec.ResourceName {
switch quotaResource.GetProduct() {
case spec.TrialProductQuotaID:
trialQuotas = append(trialQuotas, QuotaSpec{QuotaTrialType, 0, quotaResource.BillingModel, nil})
case spec.InstanceQuotaID:
remainingQuota := int(quota.GetAllowed() - quota.GetConsumed())
if quotaResource.BillingModel == QuotaStandardType {
standardQuotas = append(standardQuotas, QuotaSpec{QuotaStandardType, remainingQuota, quotaResource.BillingModel, nil})
} else if quotaResource.BillingModel == QuotaMarketplaceType {
marketplaceQuotas = append(marketplaceQuotas, QuotaSpec{QuotaMarketplaceType, remainingQuota, quotaResource.BillingModel, quota.CloudAccounts})
}
case "RHOSAKEval":
remainingQuota := int(quota.GetAllowed() - quota.GetConsumed())
evalQuotas = append(evalQuotas, QuotaSpec{QuotaEvalType, remainingQuota, quotaResource.BillingModel, quota.CloudAccounts})
// this needs to be reflected upstream to work
// case spec.EnterpriseProductQuotaId:
case "RHOSAKCC":
remainingQuota := int(quota.GetAllowed() - quota.GetConsumed())
enterpriseQuotas = append(enterpriseQuotas, QuotaSpec{QuotaEnterpriseType, remainingQuota, quotaResource.BillingModel, nil})
}
}
}
}
availableOrgQuotas := &OrgQuotas{standardQuotas, marketplaceQuotas, trialQuotas, evalQuotas, enterpriseQuotas}
return availableOrgQuotas, nil
}
// nolint:funlen
func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo MarketplaceInfo, provider string) (*QuotaSpec, error) {
if len(orgQuota.StandardQuotas) == 0 && len(orgQuota.MarketplaceQuotas) == 0 && len(orgQuota.EvalQuotas) == 0 && len(orgQuota.EnterpriseQuotas) == 0 {
if marketplaceInfo.BillingModel != "" || marketplaceInfo.Provider != "" {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.onlyTrialAvailable")
}
// select a trial quota as all other types are missing
return &orgQuota.TrialQuotas[0], nil
}
if len(orgQuota.MarketplaceQuotas) == 0 && len(orgQuota.StandardQuotas) > 0 && len(orgQuota.EvalQuotas) == 0 && len(orgQuota.EnterpriseQuotas) == 0 {
if marketplaceInfo.BillingModel == QuotaMarketplaceType || marketplaceInfo.Provider != "" || marketplaceInfo.CloudAccountID != "" {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noMarketplace")
}
if marketplaceInfo.BillingModel == QuotaEvalType {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noEval")
}
// select a standard quota
return &orgQuota.StandardQuotas[0], nil
}
if len(orgQuota.MarketplaceQuotas) == 0 && len(orgQuota.StandardQuotas) == 0 && len(orgQuota.EvalQuotas) > 0 && len(orgQuota.EnterpriseQuotas) == 0 {
if marketplaceInfo.BillingModel == QuotaMarketplaceType || marketplaceInfo.Provider != "" || marketplaceInfo.CloudAccountID != "" {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noMarketplace")
}
if marketplaceInfo.BillingModel == QuotaStandardType {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noStandard")
}
return &orgQuota.EvalQuotas[0], nil
}
if len(orgQuota.StandardQuotas) == 0 && len(orgQuota.MarketplaceQuotas) > 0 && len(orgQuota.EvalQuotas) == 0 && len(orgQuota.EnterpriseQuotas) == 0 {
if marketplaceInfo.BillingModel == QuotaStandardType {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noStandard")
}
if marketplaceInfo.BillingModel == QuotaEvalType {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noEval")
}
var filteredMarketPlaceQuotas []QuotaSpec
if provider != "" {
for _, quota := range orgQuota.MarketplaceQuotas {
for _, cloudAccount := range *quota.CloudAccounts {
if cloudAccount.GetCloudProviderId() == provider || cloudAccount.GetCloudProviderId() == RedHatMarketPlace {
filteredMarketPlaceQuotas = append(filteredMarketPlaceQuotas, quota)
break
}
}
}
orgQuota.MarketplaceQuotas = uniqueQuotaSpec(filteredMarketPlaceQuotas)
}
if len(orgQuota.MarketplaceQuotas) == 0 {
return nil, f.Localizer.MustLocalizeError("kafka.create.provider.error.noMarketplaceQuota")
}
marketplaceQuota, err := getMarketplaceQuota(f, orgQuota.MarketplaceQuotas, marketplaceInfo)
if err != nil {
return nil, err
}
marketplaceQuota.CloudAccounts, err = pickCloudAccount(f, marketplaceQuota.CloudAccounts, marketplaceInfo, provider)
if err != nil {
return nil, err
}
return marketplaceQuota, nil
}
if len(orgQuota.StandardQuotas) == 0 && len(orgQuota.MarketplaceQuotas) == 0 && len(orgQuota.EvalQuotas) == 0 && len(orgQuota.EnterpriseQuotas) > 0 {
return nil, f.Localizer.MustLocalizeError("kafka.create.provider.error.onlyEnterpriseQuota")
}
quotaTypeCount := 0
if len(orgQuota.StandardQuotas) > 0 {
quotaTypeCount++
}
if len(orgQuota.MarketplaceQuotas) > 0 {
quotaTypeCount++
}
if len(orgQuota.EvalQuotas) > 0 {
quotaTypeCount++
}
if quotaTypeCount > 1 {
switch marketplaceInfo.BillingModel {
case QuotaStandardType:
return &orgQuota.StandardQuotas[0], nil
case QuotaEvalType:
return &orgQuota.EvalQuotas[0], nil
}
if marketplaceInfo.BillingModel == QuotaMarketplaceType || marketplaceInfo.Provider != "" || marketplaceInfo.CloudAccountID != "" {
var filteredMarketPlaceQuotas []QuotaSpec
if provider != "" {
for _, quota := range orgQuota.MarketplaceQuotas {
for _, cloudAccount := range *quota.CloudAccounts {
if cloudAccount.GetCloudProviderId() == provider || cloudAccount.GetCloudProviderId() == RedHatMarketPlace {
filteredMarketPlaceQuotas = append(filteredMarketPlaceQuotas, quota)
break
}
}
}
orgQuota.MarketplaceQuotas = uniqueQuotaSpec(filteredMarketPlaceQuotas)
}
if len(orgQuota.MarketplaceQuotas) == 0 {
return nil, f.Localizer.MustLocalizeError("kafka.create.provider.error.noMarketplaceQuota")
}
marketplaceQuota, err := getMarketplaceQuota(f, orgQuota.MarketplaceQuotas, marketplaceInfo)
if err != nil {
return nil, err
}
marketplaceQuota.CloudAccounts, err = pickCloudAccount(f, marketplaceQuota.CloudAccounts, marketplaceInfo, provider)
if err != nil {
return nil, err
}
return marketplaceQuota, nil
}
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noBillingModel")
}
return &orgQuota.TrialQuotas[0], nil
}
func getMarketplaceQuota(f *factory.Factory, marketplaceQuotas []QuotaSpec, marketplace MarketplaceInfo) (*QuotaSpec, error) {
if len(marketplaceQuotas) == 1 {
if marketplace.Provider != "" && marketplace.CloudAccountID != "" {
marketplaceQuota, err := pickMarketplaceQuota(f, marketplaceQuotas, marketplace)
if err != nil {
return nil, err
}
return marketplaceQuota, nil
}
return &marketplaceQuotas[0], nil
} else if len(marketplaceQuotas) > 1 && marketplace.Provider == "" && marketplace.CloudAccountID == "" {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.multipleMarketplaceQuotas")
}
marketplaceQuota, err := pickMarketplaceQuota(f, marketplaceQuotas, marketplace)
if err != nil {
return nil, err
}
return marketplaceQuota, nil
}
func pickMarketplaceQuota(f *factory.Factory, marketplaceQuotas []QuotaSpec, marketplace MarketplaceInfo) (*QuotaSpec, error) {
matchedQuotas := []QuotaSpec{}
for _, quota := range marketplaceQuotas {
cloudAccounts := *quota.CloudAccounts
for _, cloudAccount := range cloudAccounts {
if *cloudAccount.CloudProviderId == marketplace.Provider && *cloudAccount.CloudAccountId == marketplace.CloudAccountID {
matchedQuotas = append(matchedQuotas, quota)
}
}
}
if len(matchedQuotas) == 0 {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.cloudAccountNotFound")
}
return &matchedQuotas[0], nil
}
func pickCloudAccount(f *factory.Factory, cloudAccounts *[]amsclient.CloudAccount, market MarketplaceInfo, provider string) (*[]amsclient.CloudAccount, error) {
// filter cloud accounts according to provider
var filteredCloudAccounts []amsclient.CloudAccount
if provider != "" {
for _, cloudAccount := range *cloudAccounts {
if *cloudAccount.CloudProviderId == provider || *cloudAccount.CloudProviderId == RedHatMarketPlace {
filteredCloudAccounts = append(filteredCloudAccounts, cloudAccount)
}
}
*cloudAccounts = filteredCloudAccounts
}
if len(*cloudAccounts) == 1 {
return cloudAccounts, nil
}
if len(*cloudAccounts) > 1 && market.Provider == "" && market.CloudAccountID == "" {
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.multipleCloudAccounts")
}
var matchedAccounts []amsclient.CloudAccount
for _, cloudAccount := range *cloudAccounts {
if *cloudAccount.CloudProviderId == market.Provider || *cloudAccount.CloudAccountId == market.CloudAccountID {
matchedAccounts = append(matchedAccounts, cloudAccount)
}
}
return &matchedAccounts, nil
}
// FetchValidMarketplaces returns the marketplaces available to the user to create Kafka Instance
func FetchValidMarketplaces(amsTypes []QuotaSpec) []string {
validMarketplaces := []string{}
for _, quota := range amsTypes {
if quota.CloudAccounts != nil {
for _, cloudAccount := range *quota.CloudAccounts {
validMarketplaces = append(validMarketplaces, *cloudAccount.CloudProviderId)
}
}
}
return unique(validMarketplaces)
}
// FetchValidMarketplaceAccounts returns the cloud accounts available for the specified marketplace
func FetchValidMarketplaceAccounts(amsTypes []QuotaSpec, marketplace string) []string {
validAccounts := []string{}
for _, quota := range amsTypes {
if quota.CloudAccounts != nil {
for _, cloudAccount := range *quota.CloudAccounts {
if marketplace != "" {
if cloudAccount.GetCloudProviderId() == marketplace {
validAccounts = append(validAccounts, cloudAccount.GetCloudAccountId())
}
} else {
validAccounts = append(validAccounts, cloudAccount.GetCloudAccountId())
}
}
}
}
return unique(validAccounts)
}
func GetOrganizationID(ctx context.Context, conn connection.Connection) (accountID string, err error) {
account, _, err := conn.API().AccountMgmt().ApiAccountsMgmtV1CurrentAccountGet(ctx).
Execute()
if err != nil {
return "", err
}
return account.Organization.GetId(), nil
}
func unique(s []string) []string {
inResult := make(map[string]bool)
var result []string
for _, str := range s {
if _, ok := inResult[str]; !ok {
inResult[str] = true
result = append(result, str)
}
}
return result
}
// uniqueQuotaSpec accepts a list of QuotaSpec objects and returns the unique QuotaSpecs
func uniqueQuotaSpec(s []QuotaSpec) []QuotaSpec {
inResult := make(map[QuotaSpec]bool)
var result []QuotaSpec
for _, quota := range s {
if _, ok := inResult[quota]; !ok {
inResult[quota] = true
result = append(result, quota)
}
}
return result
}