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

feat(kafka): add promote command #1805

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
6 changes: 3 additions & 3 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,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
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
}
41 changes: 32 additions & 9 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'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest: "Account ID for cloud 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'
Copy link
Contributor

@jbyrne-redhat jbyrne-redhat Feb 14, 2023

Choose a reason for hiding this comment

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

Suggest: 'Name of cloud marketplace where Kafka instance was purchased'


[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 @@ -396,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'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest: 'the maximum number of Kafka instances for your organization has been reached. Before you can create a new instance, you must delete one or more of the existing instances'


[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 @@ -591,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

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

Loading