@@ -12,6 +12,10 @@ import (
12
12
"github.com/redhat-developer/app-services-cli/pkg/shared/remote"
13
13
)
14
14
15
+ const (
16
+ RedHatMarketPlace = "rhm"
17
+ )
18
+
15
19
func CheckTermsAccepted (ctx context.Context , spec * remote.AmsConfig , conn connection.Connection ) (accepted bool , redirectURI string , err error ) {
16
20
termsReview , _ , err := conn .API ().AccountMgmt ().
17
21
ApiAuthorizationsV1SelfTermsReviewPost (ctx ).
@@ -107,7 +111,8 @@ func GetOrgQuotas(f *factory.Factory, spec *remote.AmsConfig) (*OrgQuotas, error
107
111
return availableOrgQuotas , nil
108
112
}
109
113
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 ) {
111
116
112
117
if len (orgQuota .StandardQuotas ) == 0 && len (orgQuota .MarketplaceQuotas ) == 0 {
113
118
if marketplaceInfo .BillingModel != "" || marketplaceInfo .Provider != "" {
@@ -131,12 +136,31 @@ func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo
131
136
return nil , f .Localizer .MustLocalizeError ("kafka.create.quota.error.noStandard" )
132
137
}
133
138
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
+
134
158
marketplaceQuota , err := getMarketplaceQuota (f , orgQuota .MarketplaceQuotas , marketplaceInfo )
135
159
if err != nil {
136
160
return nil , err
137
161
}
138
162
139
- marketplaceQuota .CloudAccounts , err = pickCloudAccount (f , marketplaceQuota .CloudAccounts , marketplaceInfo )
163
+ marketplaceQuota .CloudAccounts , err = pickCloudAccount (f , marketplaceQuota .CloudAccounts , marketplaceInfo , provider )
140
164
if err != nil {
141
165
return nil , err
142
166
}
@@ -149,12 +173,32 @@ func SelectQuotaForUser(f *factory.Factory, orgQuota *OrgQuotas, marketplaceInfo
149
173
if marketplaceInfo .BillingModel == QuotaStandardType {
150
174
return & orgQuota .StandardQuotas [0 ], nil
151
175
} 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
+
152
196
marketplaceQuota , err := getMarketplaceQuota (f , orgQuota .MarketplaceQuotas , marketplaceInfo )
153
197
if err != nil {
154
198
return nil , err
155
199
}
156
200
157
- marketplaceQuota .CloudAccounts , err = pickCloudAccount (f , marketplaceQuota .CloudAccounts , marketplaceInfo )
201
+ marketplaceQuota .CloudAccounts , err = pickCloudAccount (f , marketplaceQuota .CloudAccounts , marketplaceInfo , provider )
158
202
if err != nil {
159
203
return nil , err
160
204
}
@@ -209,13 +253,26 @@ func pickMarketplaceQuota(f *factory.Factory, marketplaceQuotas []QuotaSpec, mar
209
253
return & matchedQuotas [0 ], nil
210
254
}
211
255
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
+ }
213
270
214
271
if len (* cloudAccounts ) == 1 {
215
272
return cloudAccounts , nil
216
273
}
217
274
218
- if len (* cloudAccounts ) > 2 && market .Provider == "" && market .CloudAccountID == "" {
275
+ if len (* cloudAccounts ) > 1 && market .Provider == "" && market .CloudAccountID == "" {
219
276
return nil , f .Localizer .MustLocalizeError ("kafka.create.quota.error.multipleCloudAccounts" )
220
277
}
221
278
@@ -289,3 +346,16 @@ func unique(s []string) []string {
289
346
}
290
347
return result
291
348
}
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