Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kafka create): reduce number of calls made to AMS API #1596

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions pkg/cmd/kafka/create/api_validators.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package create

import (
"errors"
"strings"

"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
Expand Down Expand Up @@ -138,18 +137,3 @@ func ValidateBillingModel(billingModel string) error {

return flagutil.InvalidValueError("billing-model", billingModel, validBillingModels...)
}

// ValidateMarketplaceFlags validates if flag combination is valid for chosing quota
func ValidateMarketplaceFlags(billingModel string, accountID string, marketplace string) error {

if billingModel == StandardType && (accountID != "" || marketplace != "") {
return errors.New("accountID cant be provided with standard billing model")
}

if (accountID != "") != (marketplace != "") {
return errors.New("accountID and marketplace should be provided together")
}

return nil

}
5 changes: 0 additions & 5 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ func runCreate(opts *options) error {
return err
}

err = ValidateMarketplaceFlags(opts.billingModel, opts.marketplaceAcctId, opts.marketplace)
if err != nil {
return err
}

userInstanceType, err = accountmgmtutil.FetchQuotaCost(f, opts.billingModel, opts.marketplaceAcctId, opts.marketplace, &constants.Kafka.Ams)
if err != nil {
return err
Expand Down
57 changes: 44 additions & 13 deletions pkg/shared/accountmgmtutil/ams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package accountmgmtutil

import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
Expand Down Expand Up @@ -65,9 +67,18 @@ func FetchQuotaCost(f *factory.Factory, billingModel string, cloudAccountID stri
return nil, err
}

if billingModel == QuotaStandardType && (cloudAccountID != "" || marketplace != "") {
return nil, errors.New("accountID cant be provided with standard billing model")
}

if (cloudAccountID != "") != (marketplace != "") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever pattern!

return nil, errors.New("accountID and marketplace should be provided together")
}

if billingModel == "" && (cloudAccountID != "" || marketplace != "") {
billingModel = QuotaMarketplaceType
} else if billingModel == "" && cloudAccountID == "" && marketplace == "" {
f.Logger.Info("No billing model specified. Looking for prepaid instances")
billingModel = QuotaStandardType
}

Expand All @@ -76,36 +87,56 @@ func FetchQuotaCost(f *factory.Factory, billingModel string, cloudAccountID stri
return nil, err
}

var filteredQuotaCosts []amsclient.QuotaCost

quotaCostList := quotaCostGet.GetItems()

var userQuota amsclient.QuotaCost

for _, quota := range quotaCostGet.GetItems() {
for _, quota := range quotaCostList {
relatedResources := quota.GetRelatedResources()
for i := range relatedResources {
if relatedResources[i].GetResourceName() == spec.ResourceName && relatedResources[i].GetProduct() == spec.InstanceQuotaID && relatedResources[i].GetBillingModel() == billingModel {
userQuota = quota
filteredQuotaCosts = append(filteredQuotaCosts, quota)
}
}
}

if userQuota.GetQuotaId() == "" {
return nil, errors.New("quota could not be found")
if len(filteredQuotaCosts) == 0 {
return nil, errors.New("no quota object is available")
}

var cloudAccountExists bool
filteredQuotasJSON, _ := json.Marshal(filteredQuotaCosts)
f.Logger.Debug(fmt.Sprintf("Filtered Quotas : %#v", string(filteredQuotasJSON)))

if billingModel == QuotaMarketplaceType && marketplace != "" && cloudAccountID != "" {
for _, cloudAccount := range userQuota.GetCloudAccounts() {
if cloudAccount.GetCloudAccountId() == cloudAccountID && cloudAccount.GetCloudProviderId() == marketplace {
cloudAccountExists = true
break
}
if billingModel == QuotaMarketplaceType {

if len(filteredQuotaCosts) > 1 && marketplace == "" && cloudAccountID == "" {
return nil, errors.New("please specify marketplace provider and account id")
}

if !cloudAccountExists {
return nil, errors.New("cloud account doesnt exist for the given marketplace provider")
if len(filteredQuotaCosts) == 1 && marketplace == "" && cloudAccountID == "" {
userQuota = filteredQuotaCosts[0]
} else {
for _, filteredQuotaCost := range filteredQuotaCosts {
for _, cloudAccount := range filteredQuotaCost.GetCloudAccounts() {
if cloudAccount.GetCloudAccountId() == cloudAccountID && cloudAccount.GetCloudProviderId() == marketplace {
userQuota = filteredQuotaCost
}
}
}
}
} else {
userQuota = filteredQuotaCosts[0]
}

if userQuota.GetQuotaId() == "" {
return nil, errors.New("quota could not be found")
}

userQuotaJSON, _ := json.Marshal(userQuota)
f.Logger.Debug(fmt.Sprintf("Selected user quota : %#v", string(userQuotaJSON)))

userQuotaSpec = &QuotaSpec{billingModel, int(userQuota.GetAllowed() - userQuota.GetConsumed()), billingModel, userQuota.CloudAccounts}

return userQuotaSpec, nil
Expand Down