@@ -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+
1519func 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