Skip to content

Commit

Permalink
feat(kafka create): add billing model logic (#1636)
Browse files Browse the repository at this point in the history
Co-authored-by: Wojciech Trocki <[email protected]>
  • Loading branch information
rkpattnaik780 and wtrocki authored Jul 15, 2022
1 parent 27804be commit fb51888
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 197 deletions.
1 change: 1 addition & 0 deletions docs/commands/rhoas_kafka_create.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/redhat-developer/app-services-sdk-go/accountmgmt v0.2.0
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.1
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.6.1
github.com/redhat-developer/service-binding-operator v0.9.0
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0 h1:GcbNg/Ad
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0/go.mod h1:0WB4LlMmesjBlGKvnMXQ7twPxeSr27f5a+w4QnMoSdQ=
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0 h1:ExEHQaihnPNxN2nKXB0q5nrmSv4p8b3Idzt7TChxv+Q=
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0/go.mod h1:hMpejngP3BFnifCDH1gKRG9cU9Q4lr0WiQaW7A1LYo4=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0 h1:63UhOYB8TozKdnkkws2pXc0D1lEB+K3qX63/OxkjDas=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0/go.mod h1:m+m7d6xkC9WbSxemslyhjv0jVhquWLysRfdh+RQ5hH0=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.1 h1:Gcyn2kLlslsVT6T8qoiCJpJFPrnD2i2KIFeKQJrXkTY=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.1/go.mod h1:RoPo3tyHjv8apStFNVjChwWYdlWhg6hMzi1IrH3yQX8=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1 h1:xRq5XJzRDs/Z7e/9SDt6zbNRIyesC4LTqN9ajHKwjHo=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1/go.mod h1:Z/gr/snlpsqYg4vftmcx97vCR3qMQJhALGelDHx4pMA=
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.6.1 h1:3sUmQ3nAawsYWg7ZCO2Q8HF2J7MW6YA38h/YFL3ao6o=
Expand Down Expand Up @@ -933,6 +933,7 @@ golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb/go.mod h1:jaDAt6Dkxork7LmZnYtzbRWj0W47D86a3TGe0YHBvmE=
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2 h1:+jnHzr9VPj32ykQVai5DNahi9+NSp7yYuCsl5eAQtL0=
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2/go.mod h1:jaDAt6Dkxork7LmZnYtzbRWj0W47D86a3TGe0YHBvmE=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
19 changes: 19 additions & 0 deletions pkg/cmd/kafka/create/api_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package create
import (
"strings"

"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/accountmgmtutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
Expand All @@ -24,6 +25,8 @@ type ValidatorInput struct {
conn connection.Connection
}

var validBillingModels []string = []string{accountmgmtutil.QuotaMarketplaceType, accountmgmtutil.QuotaStandardType}

func (input *ValidatorInput) ValidateProviderAndRegion() error {
f := input.f
f.Logger.Debug("Validating provider and region")
Expand Down Expand Up @@ -119,3 +122,19 @@ func (input *ValidatorInput) ValidateSize() error {

return nil
}

// ValidateBillingModel validates if user provided a supported billing model
func ValidateBillingModel(billingModel string) error {

if billingModel == "" {
return nil
}

isValid := flagutil.IsValidInput(billingModel, validBillingModels...)

if isValid {
return nil
}

return flagutil.InvalidValueError("billing-model", billingModel, validBillingModels...)
}
70 changes: 56 additions & 14 deletions pkg/cmd/kafka/create/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package create

import (
"github.com/redhat-developer/app-services-cli/pkg/shared/accountmgmtutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/remote"
"github.com/spf13/cobra"
Expand All @@ -28,7 +27,7 @@ func GetCloudProviderRegionCompletionValues(f *factory.Factory, providerID strin
}

// GetKafkaSizeCompletionValues returns a list of valid kafka sizes for the specified region and ams instance types
func GetKafkaSizeCompletionValues(f *factory.Factory, providerID string, regionId string) (validRegions []string, directive cobra.ShellCompDirective) {
func GetKafkaSizeCompletionValues(f *factory.Factory, providerID string, regionId string) (validSizes []string, directive cobra.ShellCompDirective) {
directive = cobra.ShellCompDirectiveNoSpace

// We need both values to provide a valid list of sizes
Expand All @@ -41,33 +40,76 @@ func GetKafkaSizeCompletionValues(f *factory.Factory, providerID string, regionI
return nil, directive
}

conn, err := f.Connection(connection.DefaultConfigSkipMasAuth)
orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
if err != nil {
return nil, directive
}

userInstanceType, _ := accountmgmtutil.GetUserSupportedInstanceType(f.Context, &constants.Kafka.Ams, conn)
userInstanceType, _ := accountmgmtutil.SelectQuotaForUser(f, orgQuota, accountmgmtutil.MarketplaceInfo{})

// Not including quota in this request as it takes very long time to list quota for all regions in suggestion mode
validRegions, _ = FetchValidKafkaSizesLabels(f, providerID, regionId, *userInstanceType)
validSizes, _ = FetchValidKafkaSizesLabels(f, providerID, regionId, *userInstanceType)

return validRegions, cobra.ShellCompDirectiveNoSpace
return validSizes, cobra.ShellCompDirectiveNoSpace
}

// GetMarketplaceAcctIdCompletionValues returns a list of valid marketplace account IDs for the organization
func GetMarketplaceAcctIdCompletionValues(f *factory.Factory) (validMarketplaceAcctIDs []string, directive cobra.ShellCompDirective) {
func GetMarketplaceCompletionValues(f *factory.Factory) (validSizes []string, directive cobra.ShellCompDirective) {

directive = cobra.ShellCompDirectiveNoSpace

validMarketplaceAcctIDs, _ = accountmgmtutil.GetValidMarketplaceAcctIDs(f.Context, f.Connection, "")
err, constants := remote.GetRemoteServiceConstants(f.Context, f.Logger)
if err != nil {
return nil, directive
}

orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
if err != nil {
return nil, directive
}

validMarketPlaces := FetchValidMarketplaces(orgQuota.MarketplaceQuotas)

return validMarketPlaces, cobra.ShellCompDirectiveNoSpace
}

func GetMarketplaceAccountCompletionValues(f *factory.Factory, marketplace string) (validMarketplaceAcctIDs []string, directive cobra.ShellCompDirective) {

directive = cobra.ShellCompDirectiveNoSpace

if marketplace == "" {
return validMarketplaceAcctIDs, directive
}

err, constants := remote.GetRemoteServiceConstants(f.Context, f.Logger)
if err != nil {
return nil, directive
}

orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
if err != nil {
return nil, directive
}

return validMarketplaceAcctIDs, directive
validMarketplaceAcctIDs = FetchValidMarketplaceAccounts(orgQuota.MarketplaceQuotas, marketplace)

return validMarketplaceAcctIDs, cobra.ShellCompDirectiveNoSpace
}

// GetMarketplaceCompletionValues returns a list of valid marketplaces for the organization
func GetMarketplaceCompletionValues(f *factory.Factory) (validMarketplaces []string, directive cobra.ShellCompDirective) {
func GetBillingModelCompletionValues(f *factory.Factory) (availableBillingModels []string, directive cobra.ShellCompDirective) {

directive = cobra.ShellCompDirectiveNoSpace

validMarketplaces, _ = accountmgmtutil.GetValidMarketplaces(f.Context, f.Connection)
err, constants := remote.GetRemoteServiceConstants(f.Context, f.Logger)
if err != nil {
return nil, directive
}

orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
if err != nil {
return nil, directive
}

availableBillingModels = FetchSupportedBillingModels(orgQuota)

return validMarketplaces, directive
return availableBillingModels, directive
}
Loading

0 comments on commit fb51888

Please sign in to comment.