Skip to content

Commit fb51888

Browse files
feat(kafka create): add billing model logic (#1636)
Co-authored-by: Wojciech Trocki <wtrocki@redhat.com>
1 parent 27804be commit fb51888

10 files changed

Lines changed: 502 additions & 197 deletions

File tree

docs/commands/rhoas_kafka_create.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/redhat-developer/app-services-sdk-go/accountmgmt v0.2.0
2727
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0
2828
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0
29-
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0
29+
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.1
3030
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1
3131
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.6.1
3232
github.com/redhat-developer/service-binding-operator v0.9.0

go.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0 h1:GcbNg/Ad
640640
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0/go.mod h1:0WB4LlMmesjBlGKvnMXQ7twPxeSr27f5a+w4QnMoSdQ=
641641
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0 h1:ExEHQaihnPNxN2nKXB0q5nrmSv4p8b3Idzt7TChxv+Q=
642642
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0/go.mod h1:hMpejngP3BFnifCDH1gKRG9cU9Q4lr0WiQaW7A1LYo4=
643-
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0 h1:63UhOYB8TozKdnkkws2pXc0D1lEB+K3qX63/OxkjDas=
644-
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0/go.mod h1:m+m7d6xkC9WbSxemslyhjv0jVhquWLysRfdh+RQ5hH0=
643+
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.1 h1:Gcyn2kLlslsVT6T8qoiCJpJFPrnD2i2KIFeKQJrXkTY=
644+
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.1/go.mod h1:RoPo3tyHjv8apStFNVjChwWYdlWhg6hMzi1IrH3yQX8=
645645
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1 h1:xRq5XJzRDs/Z7e/9SDt6zbNRIyesC4LTqN9ajHKwjHo=
646646
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1/go.mod h1:Z/gr/snlpsqYg4vftmcx97vCR3qMQJhALGelDHx4pMA=
647647
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.6.1 h1:3sUmQ3nAawsYWg7ZCO2Q8HF2J7MW6YA38h/YFL3ao6o=
@@ -933,6 +933,7 @@ golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ
933933
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
934934
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
935935
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
936+
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb/go.mod h1:jaDAt6Dkxork7LmZnYtzbRWj0W47D86a3TGe0YHBvmE=
936937
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2 h1:+jnHzr9VPj32ykQVai5DNahi9+NSp7yYuCsl5eAQtL0=
937938
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2/go.mod h1:jaDAt6Dkxork7LmZnYtzbRWj0W47D86a3TGe0YHBvmE=
938939
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

pkg/cmd/kafka/create/api_validators.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package create
33
import (
44
"strings"
55

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

28+
var validBillingModels []string = []string{accountmgmtutil.QuotaMarketplaceType, accountmgmtutil.QuotaStandardType}
29+
2730
func (input *ValidatorInput) ValidateProviderAndRegion() error {
2831
f := input.f
2932
f.Logger.Debug("Validating provider and region")
@@ -119,3 +122,19 @@ func (input *ValidatorInput) ValidateSize() error {
119122

120123
return nil
121124
}
125+
126+
// ValidateBillingModel validates if user provided a supported billing model
127+
func ValidateBillingModel(billingModel string) error {
128+
129+
if billingModel == "" {
130+
return nil
131+
}
132+
133+
isValid := flagutil.IsValidInput(billingModel, validBillingModels...)
134+
135+
if isValid {
136+
return nil
137+
}
138+
139+
return flagutil.InvalidValueError("billing-model", billingModel, validBillingModels...)
140+
}

pkg/cmd/kafka/create/completions.go

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package create
22

33
import (
44
"github.com/redhat-developer/app-services-cli/pkg/shared/accountmgmtutil"
5-
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
65
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
76
"github.com/redhat-developer/app-services-cli/pkg/shared/remote"
87
"github.com/spf13/cobra"
@@ -28,7 +27,7 @@ func GetCloudProviderRegionCompletionValues(f *factory.Factory, providerID strin
2827
}
2928

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

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

44-
conn, err := f.Connection(connection.DefaultConfigSkipMasAuth)
43+
orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
4544
if err != nil {
4645
return nil, directive
4746
}
4847

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

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

54-
return validRegions, cobra.ShellCompDirectiveNoSpace
53+
return validSizes, cobra.ShellCompDirectiveNoSpace
5554
}
5655

57-
// GetMarketplaceAcctIdCompletionValues returns a list of valid marketplace account IDs for the organization
58-
func GetMarketplaceAcctIdCompletionValues(f *factory.Factory) (validMarketplaceAcctIDs []string, directive cobra.ShellCompDirective) {
56+
func GetMarketplaceCompletionValues(f *factory.Factory) (validSizes []string, directive cobra.ShellCompDirective) {
57+
5958
directive = cobra.ShellCompDirectiveNoSpace
6059

61-
validMarketplaceAcctIDs, _ = accountmgmtutil.GetValidMarketplaceAcctIDs(f.Context, f.Connection, "")
60+
err, constants := remote.GetRemoteServiceConstants(f.Context, f.Logger)
61+
if err != nil {
62+
return nil, directive
63+
}
64+
65+
orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
66+
if err != nil {
67+
return nil, directive
68+
}
69+
70+
validMarketPlaces := FetchValidMarketplaces(orgQuota.MarketplaceQuotas)
71+
72+
return validMarketPlaces, cobra.ShellCompDirectiveNoSpace
73+
}
74+
75+
func GetMarketplaceAccountCompletionValues(f *factory.Factory, marketplace string) (validMarketplaceAcctIDs []string, directive cobra.ShellCompDirective) {
76+
77+
directive = cobra.ShellCompDirectiveNoSpace
78+
79+
if marketplace == "" {
80+
return validMarketplaceAcctIDs, directive
81+
}
82+
83+
err, constants := remote.GetRemoteServiceConstants(f.Context, f.Logger)
84+
if err != nil {
85+
return nil, directive
86+
}
87+
88+
orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
89+
if err != nil {
90+
return nil, directive
91+
}
6292

63-
return validMarketplaceAcctIDs, directive
93+
validMarketplaceAcctIDs = FetchValidMarketplaceAccounts(orgQuota.MarketplaceQuotas, marketplace)
94+
95+
return validMarketplaceAcctIDs, cobra.ShellCompDirectiveNoSpace
6496
}
6597

66-
// GetMarketplaceCompletionValues returns a list of valid marketplaces for the organization
67-
func GetMarketplaceCompletionValues(f *factory.Factory) (validMarketplaces []string, directive cobra.ShellCompDirective) {
98+
func GetBillingModelCompletionValues(f *factory.Factory) (availableBillingModels []string, directive cobra.ShellCompDirective) {
99+
68100
directive = cobra.ShellCompDirectiveNoSpace
69101

70-
validMarketplaces, _ = accountmgmtutil.GetValidMarketplaces(f.Context, f.Connection)
102+
err, constants := remote.GetRemoteServiceConstants(f.Context, f.Logger)
103+
if err != nil {
104+
return nil, directive
105+
}
106+
107+
orgQuota, err := accountmgmtutil.GetOrgQuotas(f, &constants.Kafka.Ams)
108+
if err != nil {
109+
return nil, directive
110+
}
111+
112+
availableBillingModels = FetchSupportedBillingModels(orgQuota)
71113

72-
return validMarketplaces, directive
114+
return availableBillingModels, directive
73115
}

0 commit comments

Comments
 (0)