Skip to content

Commit

Permalink
feat(kafka create): enable creating long-lived trial instances (#1809)
Browse files Browse the repository at this point in the history
* feat(kafka create): enable creating long-lived trial instances (#1800)

* feat(kafka): add promote command (#1805)
  • Loading branch information
rkpattnaik780 authored Feb 13, 2023
1 parent 09f89da commit a2e1b6a
Show file tree
Hide file tree
Showing 74 changed files with 3,604 additions and 192 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/redhat-developer/app-services-sdk-go/accountmgmt v0.3.0
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.10.0
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.11.0
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.15.0
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.19.0
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.11.1
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.10.0 h1:CURbTHI
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.10.0/go.mod h1:t3IV0eKUPgCQjoInv2l8B/NMm2OVemCxGFO/z91wsCU=
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.11.0 h1:WdwVjneugUC898RSHuc2vLwlcNgPh3oF7/fuxEEGGPg=
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.11.0/go.mod h1:yazwUm4IHuIWrQ0CCsqN0h7rHZx51nlFbYWKnUn7B84=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.15.0 h1:0kNYXkZHEtoAfXJuydT5LgDjulP/3ePWI626lPfDSm0=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.15.0/go.mod h1:ILvcakLEXMLZyRdO//WJZNk9fdFbnU+cM3XrBvubE64=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.19.0 h1:RVDEeUfBgMzAK+BCnlhfHGHp2YYW6GH6jgYOv2jwYVY=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.19.0/go.mod h1:ILvcakLEXMLZyRdO//WJZNk9fdFbnU+cM3XrBvubE64=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2 h1:U2je87d/DIeOaQIycg2Y7TLiESmGu0/0rQC5n64Od0Y=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2/go.mod h1:HkNzOWHTW/SomobQ4343+yR4oTmiyvm85BIWlsh0qbA=
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.11.1 h1:VOv3wcodQ6EpKp2RRntMMTMuQSnNv1sqLezdbv18mjs=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/kafka/create/api_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ValidatorInput struct {
conn connection.Connection
}

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

func (input *ValidatorInput) ValidateProviderAndRegion() error {
f := input.f
Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command {
return f.Localizer.MustLocalizeError("kafka.create.error.standard.invalidFlags")
}

if opts.billingModel == accountmgmtutil.QuotaEvalType && (opts.marketplaceAcctId != "" || opts.marketplace != "") {
return f.Localizer.MustLocalizeError("kafka.create.error.eval.invalidFlags")
}

if !f.IOStreams.CanPrompt() && opts.name == "" {
return f.Localizer.MustLocalizeError("kafka.create.argument.name.error.requiredWhenNonInteractive")
} else if opts.name == "" {
Expand All @@ -135,13 +139,13 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command {
flags.StringVar(&opts.provider, FlagProvider, "", f.Localizer.MustLocalize("kafka.create.flag.cloudProvider.description"))
flags.StringVar(&opts.region, FlagRegion, "", f.Localizer.MustLocalize("kafka.create.flag.cloudRegion.description"))
flags.StringVar(&opts.size, FlagSize, "", f.Localizer.MustLocalize("kafka.create.flag.size.description"))
flags.StringVar(&opts.marketplaceAcctId, FlagMarketPlaceAcctID, "", f.Localizer.MustLocalize("kafka.create.flag.marketplaceId.description"))
flags.StringVar(&opts.marketplace, FlagMarketPlace, "", f.Localizer.MustLocalize("kafka.create.flag.marketplaceType.description"))
flags.StringVar(&opts.marketplaceAcctId, FlagMarketPlaceAcctID, "", f.Localizer.MustLocalize("kafka.common.flag.marketplaceId.description"))
flags.StringVar(&opts.marketplace, FlagMarketPlace, "", f.Localizer.MustLocalize("kafka.common.flag.marketplaceType.description"))
flags.AddOutput(&opts.outputFormat)
flags.BoolVar(&opts.autoUse, "use", true, f.Localizer.MustLocalize("kafka.create.flag.autoUse.description"))
flags.BoolVarP(&opts.wait, "wait", "w", false, f.Localizer.MustLocalize("kafka.create.flag.wait.description"))
flags.BoolVarP(&opts.dryRun, "dry-run", "", false, f.Localizer.MustLocalize("kafka.create.flag.dryrun.description"))
flags.StringVar(&opts.billingModel, FlagBillingModel, "", f.Localizer.MustLocalize("kafka.create.flag.billingModel.description"))
flags.StringVar(&opts.billingModel, FlagBillingModel, "", f.Localizer.MustLocalize("kafka.common.flag.billingModel.description"))
flags.AddBypassTermsCheck(&opts.bypassChecks)

_ = cmd.RegisterFlagCompletionFunc(FlagProvider, func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/cmd/kafka/create/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func mapAmsTypeToBackendType(amsType *accountmgmtutil.QuotaSpec) CloudProviderId
switch amsType.Name {
case accountmgmtutil.QuotaStandardType:
return StandardType
case accountmgmtutil.QuotaEvalType:
return StandardType
case accountmgmtutil.QuotaMarketplaceType:
return StandardType
case accountmgmtutil.QuotaTrialType:
Expand Down Expand Up @@ -63,6 +65,10 @@ func FetchSupportedBillingModels(userQuotas *accountmgmtutil.OrgQuotas, provider
billingModels = append(billingModels, accountmgmtutil.QuotaStandardType)
}

if len(userQuotas.EvalQuotas) > 0 {
billingModels = append(billingModels, accountmgmtutil.QuotaEvalType)
}

if len(userQuotas.MarketplaceQuotas) > 0 {
if provider != "" {
for _, quota := range userQuotas.MarketplaceQuotas {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/delete"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/describe"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/list"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/promote"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/providers"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/topic"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/update"
Expand Down Expand Up @@ -42,6 +43,7 @@ func NewKafkaCommand(f *factory.Factory) *cobra.Command {
acl.NewAclCommand(f),
billing.NewBillingCommand(f),
providers.NewProviderCommand(f),
promote.NewPromoteCommand(f),
)

return cmd
Expand Down
136 changes: 136 additions & 0 deletions pkg/cmd/kafka/promote/promote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package promote

import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil"
"github.com/spf13/cobra"

kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
kafkamgmtv1errors "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/error"
)

type options struct {
id string
name string
marketplaceAcctId string
marketplace string
desiredBillingModel string

f *factory.Factory
}

func NewPromoteCommand(f *factory.Factory) *cobra.Command {

opts := &options{
f: f,
}

cmd := &cobra.Command{
Use: "promote",
Short: opts.f.Localizer.MustLocalize("kafka.promote.cmd.shortDescription"),
Long: opts.f.Localizer.MustLocalize("kafka.promote.cmd.longDescription"),
Example: opts.f.Localizer.MustLocalize("kafka.promote.cmd.example"),
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {

if opts.name != "" && opts.id != "" {
return opts.f.Localizer.MustLocalizeError("service.error.idAndNameCannotBeUsed")
}

if opts.id != "" || opts.name != "" {
return runPromote(opts)
}

kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f)
if err != nil {
return err
}

opts.id = kafkaInstance.GetId()

return runPromote(opts)
},
}

flags := flagutil.NewFlagSet(cmd, opts.f.Localizer)

flags.StringVar(&opts.id, "id", "", opts.f.Localizer.MustLocalize("kafka.promote.flag.id"))
flags.StringVar(&opts.name, "name", "", opts.f.Localizer.MustLocalize("kafka.promote.flag.name"))

flags.StringVar(&opts.marketplaceAcctId, "marketplace-account-id", "", f.Localizer.MustLocalize("kafka.common.flag.marketplaceId.description"))
flags.StringVar(&opts.marketplace, "marketplace", "", f.Localizer.MustLocalize("kafka.common.flag.marketplaceType.description"))
flags.StringVar(&opts.desiredBillingModel, "billing-model", "", f.Localizer.MustLocalize("kafka.common.flag.billingModel.description"))

_ = cmd.MarkFlagRequired("billing-model")

return cmd

}

func runPromote(opts *options) error {

conn, err := opts.f.Connection()
if err != nil {
return err
}

api := conn.API()

if opts.name != "" {
response, _, newErr := kafkautil.GetKafkaByName(opts.f.Context, api.KafkaMgmt(), opts.name)
if newErr != nil {
return newErr
}

opts.id = response.GetId()
}

a := api.KafkaMgmt().PromoteKafka(opts.f.Context, opts.id)

var promoteOptions kafkamgmtclient.KafkaPromoteRequest

promoteOptions.SetDesiredKafkaBillingModel(opts.desiredBillingModel)

if opts.marketplace != "" {
promoteOptions.SetDesiredMarketplace(opts.marketplace)
}

if opts.marketplaceAcctId != "" {
promoteOptions.SetDesiredKafkaBillingModel(opts.marketplaceAcctId)
}

a = a.KafkaPromoteRequest(promoteOptions)
a = a.Async(true)

httpRes, err := a.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}

if apiErr := kafkamgmtv1errors.GetAPIError(err); apiErr != nil {
switch apiErr.GetCode() {
case kafkamgmtv1errors.ERROR_120:
// For standard instances
return opts.f.Localizer.MustLocalizeError("kafka.create.error.quota.exceeded")
case kafkamgmtv1errors.ERROR_24:
// For dev instances
return opts.f.Localizer.MustLocalizeError("kafka.create.error.instance.limit")
case kafkamgmtv1errors.ERROR_9:
return opts.f.Localizer.MustLocalizeError("kafka.create.error.standard.promote")
case kafkamgmtv1errors.ERROR_43:
return opts.f.Localizer.MustLocalizeError("kafka.create.error.billing.invalid", localize.NewEntry("Billing", opts.marketplaceAcctId))
}
}

if err != nil {
return err
}

opts.f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.promote.info.successAsync"))

return nil
}
49 changes: 39 additions & 10 deletions pkg/core/localize/locales/en/cmd/kafka.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ one = 'Kafka instance ID. Uses the current instance if not set'
[kafkas.common.flag.output.description]
one = 'Format in which to display the Kafka instances (choose from: "json", "yml", "yaml")'

[kafka.common.flag.marketplaceId.description]
one = 'Cloud Account ID for the marketplace'

[kafka.common.flag.billingModel.description]
one = 'Billing model to be used'

[kafka.common.flag.marketplaceType.description]
one = 'Name of the marketplace where the instance is purchased on'

[kafka.common.input.instanceName.message]
one = 'Select Kafka instance:'

Expand Down Expand Up @@ -286,15 +295,6 @@ one = 'Wait until the Kafka instance is created'
[kafka.create.flag.dryrun.description]
one = 'Validate all user provided arguments without creating the Kafka instance'

[kafka.create.flag.marketplaceId.description]
one = 'Cloud Account ID for the marketplace'

[kafka.create.flag.billingModel.description]
one = 'Billing model to be used'

[kafka.create.flag.marketplaceType.description]
one = 'Name of the marketplace where the instance is purchased on'

[kafka.create.log.info.creatingKafka]
description = 'Message when Kafka instance is being created'
one = 'Creating Kafka instance "{{.Name}}"...'
Expand Down Expand Up @@ -370,7 +370,10 @@ one = '"--billing-model", "--marketplace", "--marketplace-account-id" flags are
one = '"--marketplace" and "--marketplace-account-id" flags should be supplied together'

[kafka.create.error.standard.invalidFlags]
one = 'billing model cannot be standard if "--marketplace-account-id" or "--marketplace" are set'
one = 'billing model can not be standard if "--marketplace-account-id" or "--marketplace" are set'

[kafka.create.error.eval.invalidFlags]
one = 'billing model can not be eval if "--marketplace-account-id" or "--marketplace" are set'

[kafka.create.error.conflictError]
one = 'Kafka instance "{{.Name}}" already exists'
Expand All @@ -393,6 +396,9 @@ one = 'unable to create new Kafka instance at this time in specified cloud provi
[kafka.create.error.instance.limit]
one = 'maximum number of allowed kafka instances has been reached. Please review all instances that your user has access to and delete one or more instances before creating a new one'

[kafka.create.error.standard.promote]
one = 'only Kafka instances with billing model "eval" can be promoted'

[kafka.create.region.error.invalidRegion]
one = '''
the region "{{.Region}}" is not available for the cloud provider "{{.Provider}}".
Expand Down Expand Up @@ -437,6 +443,9 @@ one = "only trial quotas are available, don't specify billing model details"
[kafka.create.quota.error.noMarketplace]
one = "no marketplace quotas are available"

[kafka.create.quota.error.noEval]
one = "no evaluation quotas are available"

[kafka.create.quota.error.noStandard]
one = "no standard quotas are available"

Expand Down Expand Up @@ -585,6 +594,26 @@ $ rhoas kafka billing
[kafka.billing.log.info.noStandardInstancesAvailable]
one = "Only developer instances are available"

[kafka.promote.cmd.shortDescription]
one = 'Promote eval Kafka instance'

[kafka.promote.cmd.longDescription]
one = 'Promote an evaluation Kafka instance to billing model "standard" or "marketplace"'

[kafka.promote.cmd.example]
one = '''
# Promote eval instance to standard
$ rhoas kafka promote --billing-model standard --id 1iSY6RQ3JKI8Q0OTmjQFd3ocFRg
'''

[kafka.promote.flag.id]
one = 'ID of the Kafka instance'

[kafka.promote.flag.name]
one = 'Name of the Kafka instance'

[kafka.promote.info.successAsync]
one = 'Kafka instance is being promoted. To monitor its status run "rhoas kafka describe".'

[kafka.list.cmd.shortDescription]
description = "Short description for command"
Expand Down
Loading

0 comments on commit a2e1b6a

Please sign in to comment.