Skip to content

Commit 152c36e

Browse files
fix(kafka create): sync marketplace provider with cloud provider (#1792)
1 parent 08a333d commit 152c36e

File tree

5 files changed

+122
-16
lines changed

5 files changed

+122
-16
lines changed

pkg/cmd/kafka/create/completions.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func GetKafkaSizeCompletionValues(f *factory.Factory, providerID string, regionI
4545
return nil, directive
4646
}
4747

48-
userInstanceType, _ := accountmgmtutil.SelectQuotaForUser(f, orgQuota, accountmgmtutil.MarketplaceInfo{})
48+
userInstanceType, _ := accountmgmtutil.SelectQuotaForUser(f, orgQuota, accountmgmtutil.MarketplaceInfo{}, "")
4949

5050
// Not including quota in this request as it takes very long time to list quota for all regions in suggestion mode
5151
validSizes, _ = FetchValidKafkaSizesLabels(f, providerID, regionId, *userInstanceType)
@@ -67,7 +67,7 @@ func GetMarketplaceCompletionValues(f *factory.Factory) (validSizes []string, di
6767
return nil, directive
6868
}
6969

70-
validMarketPlaces := FetchValidMarketplaces(orgQuota.MarketplaceQuotas)
70+
validMarketPlaces := FetchValidMarketplaces(orgQuota.MarketplaceQuotas, "")
7171

7272
return validMarketPlaces, cobra.ShellCompDirectiveNoSpace
7373
}
@@ -109,7 +109,7 @@ func GetBillingModelCompletionValues(f *factory.Factory) (availableBillingModels
109109
return nil, directive
110110
}
111111

112-
availableBillingModels = FetchSupportedBillingModels(orgQuota)
112+
availableBillingModels = FetchSupportedBillingModels(orgQuota, "")
113113

114114
return availableBillingModels, directive
115115
}

pkg/cmd/kafka/create/create.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,13 @@ func runCreate(opts *options) error {
257257
CloudAccountID: opts.marketplaceAcctId,
258258
}
259259

260-
userQuota, err = accountmgmtutil.SelectQuotaForUser(f, orgQuotas, marketplaceInfo)
260+
if opts.marketplace != "" && opts.marketplace != accountmgmtutil.RedHatMarketPlace {
261+
if opts.marketplace != opts.provider {
262+
return opts.f.Localizer.MustLocalizeError("kafka.create.provider.error.unsupportedMarketplace")
263+
}
264+
}
265+
266+
userQuota, err = accountmgmtutil.SelectQuotaForUser(f, orgQuotas, marketplaceInfo, opts.provider)
261267
if err != nil {
262268
return err
263269
}
@@ -477,7 +483,11 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
477483
return nil, err
478484
}
479485

480-
availableBillingModels := FetchSupportedBillingModels(orgQuota)
486+
availableBillingModels := FetchSupportedBillingModels(orgQuota, answers.CloudProvider)
487+
488+
if len(availableBillingModels) == 0 && len(orgQuota.MarketplaceQuotas) > 0 {
489+
return nil, opts.f.Localizer.MustLocalizeError("kafka.create.provider.error.noStandardInstancesAvailable")
490+
}
481491

482492
if len(availableBillingModels) > 0 {
483493
if len(availableBillingModels) == 1 {
@@ -495,7 +505,7 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
495505
}
496506

497507
if answers.BillingModel == accountmgmtutil.QuotaMarketplaceType {
498-
validMarketPlaces := FetchValidMarketplaces(orgQuota.MarketplaceQuotas)
508+
validMarketPlaces := FetchValidMarketplaces(orgQuota.MarketplaceQuotas, answers.CloudProvider)
499509
if len(validMarketPlaces) == 1 {
500510
answers.Marketplace = validMarketPlaces[0]
501511
} else {
@@ -534,7 +544,7 @@ func promptKafkaPayload(opts *options, constants *remote.DynamicServiceConstants
534544
CloudAccountID: answers.MarketplaceAcctID,
535545
}
536546

537-
userQuota, err := accountmgmtutil.SelectQuotaForUser(f, orgQuota, marketplaceInfo)
547+
userQuota, err := accountmgmtutil.SelectQuotaForUser(f, orgQuota, marketplaceInfo, answers.CloudProvider)
538548
if err != nil {
539549
return nil, err
540550
}

pkg/cmd/kafka/create/data.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func FetchValidKafkaSizesLabels(f *factory.Factory,
5555

5656
}
5757

58-
func FetchSupportedBillingModels(userQuotas *accountmgmtutil.OrgQuotas) []string {
58+
func FetchSupportedBillingModels(userQuotas *accountmgmtutil.OrgQuotas, provider string) []string {
5959

6060
billingModels := []string{}
6161

@@ -64,20 +64,37 @@ func FetchSupportedBillingModels(userQuotas *accountmgmtutil.OrgQuotas) []string
6464
}
6565

6666
if len(userQuotas.MarketplaceQuotas) > 0 {
67-
billingModels = append(billingModels, accountmgmtutil.QuotaMarketplaceType)
67+
if provider != "" {
68+
for _, quota := range userQuotas.MarketplaceQuotas {
69+
for _, cloudAccount := range *quota.CloudAccounts {
70+
if cloudAccount.GetCloudProviderId() == provider || cloudAccount.GetCloudProviderId() == accountmgmtutil.RedHatMarketPlace {
71+
billingModels = append(billingModels, accountmgmtutil.QuotaMarketplaceType)
72+
return billingModels
73+
}
74+
}
75+
}
76+
} else {
77+
billingModels = append(billingModels, accountmgmtutil.QuotaMarketplaceType)
78+
}
6879
}
6980

7081
return billingModels
7182
}
7283

73-
func FetchValidMarketplaces(amsTypes []accountmgmtutil.QuotaSpec) []string {
84+
func FetchValidMarketplaces(amsTypes []accountmgmtutil.QuotaSpec, provider string) []string {
7485

7586
validMarketplaces := []string{}
7687

7788
for _, quota := range amsTypes {
7889
if quota.CloudAccounts != nil {
7990
for _, cloudAccount := range *quota.CloudAccounts {
80-
validMarketplaces = append(validMarketplaces, *cloudAccount.CloudProviderId)
91+
if provider != "" {
92+
if *cloudAccount.CloudProviderId == provider || *cloudAccount.CloudProviderId == accountmgmtutil.RedHatMarketPlace {
93+
validMarketplaces = append(validMarketplaces, *cloudAccount.CloudProviderId)
94+
}
95+
} else {
96+
validMarketplaces = append(validMarketplaces, *cloudAccount.CloudProviderId)
97+
}
8198
}
8299
}
83100
}

pkg/core/localize/locales/en/cmd/kafka.en.toml

+9
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,15 @@ provided instance size is not valid. Valid sizes: {{.ValidSizes}}
417417
[kafka.create.error.noRegionSupported]
418418
one = 'all regions in the selected cloud provider are temporarily unavailable'
419419

420+
[kafka.create.provider.error.noStandardInstancesAvailable]
421+
one = 'standard instances are unavailable for the cloud provider, try another provider'
422+
423+
[kafka.create.provider.error.noMarketplaceQuota]
424+
one = 'no marketplace quota available for given provider'
425+
426+
[kafka.create.provider.error.unsupportedMarketplace]
427+
one = 'selected marketplace is not supported for the cloud provider'
428+
420429
[kafka.create.error.billing.invalid]
421430
one = '''
422431
provided billing account id and provider are invalid {{.Billing}}

pkg/shared/accountmgmtutil/ams.go

+75-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"github.com/redhat-developer/app-services-cli/pkg/shared/remote"
1313
)
1414

15+
const (
16+
RedHatMarketPlace = "rhm"
17+
)
18+
1519
func CheckTermsAccepted(ctx context.Context, spec *remote.AmsConfig, conn connection.Connection) (accepted bool, redirectURI string, err error) {
1620
termsReview, _, err := conn.API().AccountMgmt().
1721
ApiAuthorizationsV1SelfTermsReviewPost(ctx).
@@ -107,7 +111,8 @@ func GetOrgQuotas(f *factory.Factory, spec *remote.AmsConfig) (*OrgQuotas, error
107111
return availableOrgQuotas, nil
108112
}
109113

110-
func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo MarketplaceInfo) (*QuotaSpec, error) {
114+
// nolint:funlen
115+
func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo MarketplaceInfo, provider string) (*QuotaSpec, error) {
111116

112117
if len(orgQuota.StandardQuotas) == 0 && len(orgQuota.MarketplaceQuotas) == 0 {
113118
if marketplaceInfo.BillingModel != "" || marketplaceInfo.Provider != "" {
@@ -131,12 +136,31 @@ func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo
131136
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.noStandard")
132137
}
133138

139+
var filteredMarketPlaceQuotas []QuotaSpec
140+
141+
if provider != "" {
142+
for _, quota := range orgQuota.MarketplaceQuotas {
143+
for _, cloudAccount := range *quota.CloudAccounts {
144+
if cloudAccount.GetCloudProviderId() == provider || cloudAccount.GetCloudProviderId() == RedHatMarketPlace {
145+
filteredMarketPlaceQuotas = append(filteredMarketPlaceQuotas, quota)
146+
break
147+
}
148+
}
149+
}
150+
151+
orgQuota.MarketplaceQuotas = uniqueQuotaSpec(filteredMarketPlaceQuotas)
152+
}
153+
154+
if len(orgQuota.MarketplaceQuotas) == 0 {
155+
return nil, f.Localizer.MustLocalizeError("kafka.create.provider.error.noMarketplaceQuota")
156+
}
157+
134158
marketplaceQuota, err := getMarketplaceQuota(f, orgQuota.MarketplaceQuotas, marketplaceInfo)
135159
if err != nil {
136160
return nil, err
137161
}
138162

139-
marketplaceQuota.CloudAccounts, err = pickCloudAccount(f, marketplaceQuota.CloudAccounts, marketplaceInfo)
163+
marketplaceQuota.CloudAccounts, err = pickCloudAccount(f, marketplaceQuota.CloudAccounts, marketplaceInfo, provider)
140164
if err != nil {
141165
return nil, err
142166
}
@@ -149,12 +173,32 @@ func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo
149173
if marketplaceInfo.BillingModel == QuotaStandardType {
150174
return &orgQuota.StandardQuotas[0], nil
151175
} else if marketplaceInfo.BillingModel == QuotaMarketplaceType || marketplaceInfo.Provider != "" || marketplaceInfo.CloudAccountID != "" {
176+
177+
var filteredMarketPlaceQuotas []QuotaSpec
178+
179+
if provider != "" {
180+
for _, quota := range orgQuota.MarketplaceQuotas {
181+
for _, cloudAccount := range *quota.CloudAccounts {
182+
if cloudAccount.GetCloudProviderId() == provider || cloudAccount.GetCloudProviderId() == RedHatMarketPlace {
183+
filteredMarketPlaceQuotas = append(filteredMarketPlaceQuotas, quota)
184+
break
185+
}
186+
}
187+
}
188+
189+
orgQuota.MarketplaceQuotas = uniqueQuotaSpec(filteredMarketPlaceQuotas)
190+
}
191+
192+
if len(orgQuota.MarketplaceQuotas) == 0 {
193+
return nil, f.Localizer.MustLocalizeError("kafka.create.provider.error.noMarketplaceQuota")
194+
}
195+
152196
marketplaceQuota, err := getMarketplaceQuota(f, orgQuota.MarketplaceQuotas, marketplaceInfo)
153197
if err != nil {
154198
return nil, err
155199
}
156200

157-
marketplaceQuota.CloudAccounts, err = pickCloudAccount(f, marketplaceQuota.CloudAccounts, marketplaceInfo)
201+
marketplaceQuota.CloudAccounts, err = pickCloudAccount(f, marketplaceQuota.CloudAccounts, marketplaceInfo, provider)
158202
if err != nil {
159203
return nil, err
160204
}
@@ -209,13 +253,26 @@ func pickMarketplaceQuota(f *factory.Factory, marketplaceQuotas []QuotaSpec, mar
209253
return &matchedQuotas[0], nil
210254
}
211255

212-
func pickCloudAccount(f *factory.Factory, cloudAccounts *[]amsclient.CloudAccount, market MarketplaceInfo) (*[]amsclient.CloudAccount, error) {
256+
func pickCloudAccount(f *factory.Factory, cloudAccounts *[]amsclient.CloudAccount, market MarketplaceInfo, provider string) (*[]amsclient.CloudAccount, error) {
257+
258+
// filter cloud accounts according to provider
259+
var filteredCloudAccounts []amsclient.CloudAccount
260+
261+
if provider != "" {
262+
for _, cloudAccount := range *cloudAccounts {
263+
if *cloudAccount.CloudProviderId == provider || *cloudAccount.CloudProviderId == RedHatMarketPlace {
264+
filteredCloudAccounts = append(filteredCloudAccounts, cloudAccount)
265+
}
266+
}
267+
268+
*cloudAccounts = filteredCloudAccounts
269+
}
213270

214271
if len(*cloudAccounts) == 1 {
215272
return cloudAccounts, nil
216273
}
217274

218-
if len(*cloudAccounts) > 2 && market.Provider == "" && market.CloudAccountID == "" {
275+
if len(*cloudAccounts) > 1 && market.Provider == "" && market.CloudAccountID == "" {
219276
return nil, f.Localizer.MustLocalizeError("kafka.create.quota.error.multipleCloudAccounts")
220277
}
221278

@@ -289,3 +346,16 @@ func unique(s []string) []string {
289346
}
290347
return result
291348
}
349+
350+
// uniqueQuotaSpec accepts a list of QuotaSpec objects and returns the unique QuotaSpecs
351+
func uniqueQuotaSpec(s []QuotaSpec) []QuotaSpec {
352+
inResult := make(map[QuotaSpec]bool)
353+
var result []QuotaSpec
354+
for _, quota := range s {
355+
if _, ok := inResult[quota]; !ok {
356+
inResult[quota] = true
357+
result = append(result, quota)
358+
}
359+
}
360+
return result
361+
}

0 commit comments

Comments
 (0)